Documentation ¶
Overview ¶
Package httpmock defines a DSL that can be used to create a set of mock endpoints. The root of the mock service is the Mockery object and is an net/http.Handler
Index ¶
- Constants
- Variables
- func BodyXPathEquals(xpath, value string) predicate.Predicate
- func BodyXPathEqualsIgnoreCase(xpath, value string) predicate.Predicate
- func BodyXPathMatches(xpath string, pattern *regexp.Regexp) predicate.Predicate
- func Case(predicate predicate.Predicate, responseBuilder func())
- func Created()
- func CurrentHandler() http.Handler
- func DecorateHandler(preHandler, postHandler http.Handler)
- func DecorateHandlerAfter(postHandler http.Handler)
- func DecorateHandlerBefore(preHandler http.Handler)
- func Default(responseBuilder func())
- func Endpoint(url string, configureFunc func())
- func EndpointForCondition(predicate predicate.Predicate, configFunc func())
- func EndpointForConditionWithPriority(priority int, predicate predicate.Predicate, configFunc func())
- func EndpointPattern(urlPattern string, configFunc func())
- func FixedDelay(d string)
- func Header(name, value string)
- func LogLocation(comment string)
- func LogRequest()
- func Method(method string, configFunc func())
- func Mockery(configFunc func()) http.Handler
- func NormalDelay(mean, stdDev, max string)
- func NotFound()
- func Respond(status int)
- func RespondWithBadRequest(body interface{})
- func RespondWithFile(status int, fileName string)
- func RespondWithInternalServerError(body interface{})
- func RespondWithJson(status int, jsonBody interface{})
- func RespondWithReader(status int, bodyProducer func() io.Reader)
- func RespondWithString(status int, body string)
- func SmoothedNormalDelay(mean, stdDev, max string)
- func Switch(keySupplier extractor.Extractor, cases func())
- func Trailer(name, value string)
- func UniformDelay(min, max string)
- func Waiter(waitTime func() time.Duration) http.Handler
- func When(predicate predicate.Predicate, trueResponseBuilder func(), ...)
- func WriteStatusAndBody(status int, body interface{})
Examples ¶
Constants ¶
const DefaultPriority = 100
DefaultPriority is the default priority for endpoint consideration.
Variables ¶
var NoopHandler http.HandlerFunc = func(w http.ResponseWriter, request *http.Request) {
}
NoopHandler is a handler that does nothing.
Functions ¶
func BodyXPathEquals ¶
BodyXPathEquals checks to see if the result of the xpath expression, matches the string supplied in the 'value' parameter.
func BodyXPathEqualsIgnoreCase ¶
BodyXPathEqualsIgnoreCase similar to BodyXPathEquals but ignores case when comparing the strings.
func BodyXPathMatches ¶
BodyXPathMatches checks to see if the result of the xpath expression, matches the regular expression given in the 'pattern' parameter.
func Case ¶
Case used within a Switch to define a Response that will be returned if the case's predicate is true. The order of the case calls matter as the first to match will be used.
func Created ¶
func Created()
Created is a shortcut for returning Created (201) http response with no body.
func CurrentHandler ¶
CurrentHandler returns the current handler that should be decorated with any additional behaviors.
func DecorateHandler ¶
DecorateHandler is used by DSL methods to inject pre & post actions to the current handler. For instance, the Header(string,string) function adds a preHandler that adds a Header to the ResponseWriter. To use this function to create new DSL Methods, follow this pattern:
func Header(name, value string) { return DecorateHandler(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { w.Header.Add(name,value) }), NoopHandler) }
func DecorateHandlerAfter ¶
DecorateHandlerAfter is like DecorateHandler but only applies a Decoration after the handler is called.
func DecorateHandlerBefore ¶
DecorateHandlerBefore is like DecorateHandler but only applies a Decoration before the handler is called.
func Default ¶
func Default(responseBuilder func())
Default used to define the Response that will be returned when no other case is triggered. The default can be placed anywhere but there can only be one.
func Endpoint ¶
func Endpoint(url string, configureFunc func())
Endpoint defines an endpoint that uses the http.ServeMux to dispatch requests. The content of the configureFunc should be Method elements which may contain
func EndpointForCondition ¶
EndpointForCondition creates an endpoint that is selected by the predicate passed.
func EndpointForConditionWithPriority ¶
func EndpointForConditionWithPriority(priority int, predicate predicate.Predicate, configFunc func())
EndpointForConditionWithPriority defines an endpoint that is selected by the predicate given with the priority provided.
func EndpointPattern ¶
func EndpointPattern(urlPattern string, configFunc func())
EndpointPattern creates an endpoint that is selected by comparing the URL path with the pattern provided.
func FixedDelay ¶
func FixedDelay(d string)
FixedDelay defines a fixed delay for the response. The duration string should be formatted as expected by time.ParseDuration
func Header ¶
func Header(name, value string)
Header adds a header to the response, may be called at any time.
func LogLocation ¶
func LogLocation(comment string)
LogLocation will log the given comment along with the file and line number.
func LogRequest ¶
func LogRequest()
LogRequest will cause the request information to be logged to the console.
func Method ¶
func Method(method string, configFunc func())
Method is a DSL element that is used within an Endpoint element to define a method handler.
func Mockery ¶
Mockery contains the top level dispatcher. This method establishes the root handler and the configFunc is called to create handlers for the various mocks. Once the config method returns some clean up actions will occur and the mock handler will be returned.
Example ¶
mockery := Mockery(func() { Endpoint("/foo/bar", func() { Method("GET", func() { Header("Content-Type", "application/json") Header("FOO", "BAR") RespondWithFile(500, "./error.json") FixedDelay("10ms") }) }) Endpoint("/foo/bar/", func() { Method("GET", func() { Header("Content-Type", "application/json") Header("FOO", "BAR") RespondWithFile(200, "./ok.json") NormalDelay("10s", "5s", "20s") }) }) Endpoint("/snafu/", func() { Method("GET", func() { Header("Content-Type", "application/xml") Header("Cache-Control", "no-cache") Header("Access-Control-Allow-Origin", "*") Switch(ExtractQueryParameter("foo"), func() { Case(StringEquals("bar"), func() { RespondWithFile(http.StatusOK, "response.xml") }) Default(func() { RespondWithFile(http.StatusBadRequest, "error.xml") }) }) }) NormalDelay("300ms", "120ms", "5s") }) }) log.Fatal(http.ListenAndServe(":8080", mockery))
Output:
func NormalDelay ¶
func NormalDelay(mean, stdDev, max string)
NormalDelay defines a delay whose distribution conforms to a Normal Distribution with the given mean, standard deviation, and maximum. All the durations are expressed in a format compatible with time.ParseDuration
func Respond ¶
func Respond(status int)
Respond responds with an empty body and the given status code.
func RespondWithBadRequest ¶
func RespondWithBadRequest(body interface{})
RespondWithBadRequest is a shortcut for returning Bad Request (400) with the given body.
func RespondWithFile ¶
RespondWithFile responds with the status code given and the content of the file specified.
func RespondWithInternalServerError ¶
func RespondWithInternalServerError(body interface{})
RespondWithInternalServerError is a shortcut for returning Internal Server Error (500) with the given body.
func RespondWithJson ¶
func RespondWithJson(status int, jsonBody interface{})
RespondWithJson adds a response code and body to the response. The jsonBody parameter is JSON encoded using the json encoder in the encoding/json package.
func RespondWithReader ¶
RespondWithReader responds with the status code given and the body read from the io.Reader
func RespondWithString ¶
RespondWithString responds with the status code given and the body
func SmoothedNormalDelay ¶
func SmoothedNormalDelay(mean, stdDev, max string)
SmoothedNormalDelay WIP, This is an incomplete API and should not be used.
func Switch ¶
Switch can be used within a Method's config function to conditionally choose one of many possible responses. The first Case whose predicate returns true will be selected. Otherwise the Response defined in the Default is used. If there is no Default, then 404 is returned with an empty Body.
func Trailer ¶
func Trailer(name, value string)
Trailer adds a trailer to the response, must be called after the response body has been specified.
func UniformDelay ¶
func UniformDelay(min, max string)
UniformDelay defines a delay that is uniformly distributed between a minimum and a maximum. The min and max parameters are expected to conform the the format expected by time.ParseDuration
func Waiter ¶
Waiter defines a generic waiter that will use the provided waitTime function to acquire the duration to wait.
func When ¶
When can be used within a Method's config function to conditionally choose one Response or another.
func WriteStatusAndBody ¶
func WriteStatusAndBody(status int, body interface{})
WriteStatusAndBody writes the given status with the given body. It is expected that any headers needed by the response have been added as this will being the sending of the response.
Types ¶
This section is empty.