github.com/webdestroya/awsmocker@v0.2.6/mocks_common.go (about)

     1  package awsmocker
     2  
     3  import "net/http"
     4  
     5  // Returns an error response for a given service/action call
     6  func Mock_Failure(service, action string) *MockedEndpoint {
     7  	return Mock_Failure_WithCode(0, service, action, "AccessDenied", "This mock was requested to fail")
     8  }
     9  
    10  func Mock_Failure_WithCode(statusCode int, service, action, errorCode, errorMessage string) *MockedEndpoint {
    11  	return &MockedEndpoint{
    12  		Request: &MockedRequest{
    13  			Service: service,
    14  			Action:  action,
    15  		},
    16  		Response: MockResponse_Error(statusCode, errorCode, errorMessage),
    17  	}
    18  }
    19  
    20  // Returns an error response with a custom code and message
    21  func MockResponse_Error(statusCode int, errorCode, errorMessage string) *MockedResponse {
    22  	if statusCode == 0 {
    23  		statusCode = 400
    24  	}
    25  
    26  	errObj := generateErrorStruct(statusCode, errorCode, errorMessage)
    27  
    28  	return &MockedResponse{
    29  		Handler: func(rr *ReceivedRequest) *http.Response {
    30  			resp := errObj.getResponse(rr).toHttpResponse(rr.HttpRequest)
    31  			resp.StatusCode = statusCode
    32  			return resp
    33  		},
    34  	}
    35  }