httpmock

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2019 License: Apache-2.0 Imports: 18 Imported by: 0

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

Examples

Constants

View Source
const DefaultPriority = 100

DefaultPriority is the default priority for endpoint consideration.

Variables

View Source
var NoopHandler http.HandlerFunc = func(w http.ResponseWriter, request *http.Request) {
}

NoopHandler is a handler that does nothing.

Functions

func BodyXPathEquals

func BodyXPathEquals(xpath, value string) predicate.Predicate

BodyXPathEquals checks to see if the result of the xpath expression, matches the string supplied in the 'value' parameter.

func BodyXPathEqualsIgnoreCase

func BodyXPathEqualsIgnoreCase(xpath, value string) predicate.Predicate

BodyXPathEqualsIgnoreCase similar to BodyXPathEquals but ignores case when comparing the strings.

func BodyXPathMatches

func BodyXPathMatches(xpath string, pattern *regexp.Regexp) predicate.Predicate

BodyXPathMatches checks to see if the result of the xpath expression, matches the regular expression given in the 'pattern' parameter.

func Case

func Case(predicate predicate.Predicate, responseBuilder func())

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

func CurrentHandler() http.Handler

CurrentHandler returns the current handler that should be decorated with any additional behaviors.

func DecorateHandler

func DecorateHandler(preHandler, postHandler http.Handler)

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

func DecorateHandlerAfter(postHandler http.Handler)

DecorateHandlerAfter is like DecorateHandler but only applies a Decoration after the handler is called.

func DecorateHandlerBefore

func DecorateHandlerBefore(preHandler http.Handler)

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

func EndpointForCondition(predicate predicate.Predicate, configFunc func())

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(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

func Mockery(configFunc func()) http.Handler

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 NotFound

func NotFound()

NotFound is a shortcut for returning Not Found (404) with no body.

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

func RespondWithFile(status int, fileName string)

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

func RespondWithReader(status int, bodyProducer func() io.Reader)

RespondWithReader responds with the status code given and the body read from the io.Reader

func RespondWithString

func RespondWithString(status int, body string)

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

func Switch(keySupplier extractor.Extractor, cases func())

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

func Waiter(waitTime func() time.Duration) http.Handler

Waiter defines a generic waiter that will use the provided waitTime function to acquire the duration to wait.

func When

func When(predicate predicate.Predicate, trueResponseBuilder func(), falseResponseBuilder func())

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.

Directories

Path Synopsis
Package wiremock provides a mechanism for importing wiremock Json API Mappings into mockery/httpmock.
Package wiremock provides a mechanism for importing wiremock Json API Mappings into mockery/httpmock.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL