Documentation ¶
Index ¶
- type Match
- type MatchRule
- func MatchHeader(name, value string) MatchRule
- func MatchJSONCompatible(expected string) MatchRule
- func MatchJSONFull(expected string) MatchRule
- func MatchMethod(method string) MatchRule
- func MatchRequest(method, url string) MatchRule
- func MatchURLPath(expected string) MatchRule
- func MatchURLQuery(name, value string) MatchRule
- type MatchRuleFunc
- type MatchRules
- type MockServer
- type Response
- type ResponseBuilder
- type ResponseBuilderFunc
- type ResponseBuilders
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Match ¶
type Match struct {
// contains filtered or unexported fields
}
Match represents a matching in the mock server to potentially handle incoming requests.
func (*Match) Count ¶ added in v0.1.1
Count will return the number of times this match has been used to respond to a request.
func (*Match) Matches ¶
Matches will check if every rule in this `Match` passes for the incoming request.
func (*Match) RespondsWith ¶
func (m *Match) RespondsWith(builders ...ResponseBuilder) *Match
RespondsWith registers new response builders to use to build the response to an incoming request.
type MatchRule ¶
type MatchRule interface { // Matches will check to see if the provided HTTP request matches this rule. Matches(r *http.Request) bool }
MatchRule represents a rule to match against to see if a request should be processed.
func MatchHeader ¶
MatchHeader builds a `MatchRule` to check if the given header is present and has the given value. If the header is repeated then only one of the repeated values needs to have the provided value.
func MatchJSONCompatible ¶
MatchJSONCompatible will compare the request body to the provided JSON string and ensure that the two are compatible. Every value in the request body must be present in the expected body, but the request body may contain keys that are not present in the expected body as well.
For example, the following is a valid match: * Request Body = {"a": 1, "b": {"c": 2, "d": 3}, "e": 4} * Expected = {"a": 1, "b": {"c": 2}}
As with MatchJSONFull, the order of keys is not important.
func MatchJSONFull ¶
MatchJSONFull will compare the request body to the provided JSON string and ensure that the two are semantically identical. The order of keys in JSON objects is not important, but every value must be present.
func MatchMethod ¶
MatchMethod builds a `MatchRule` to check if the HTTP Method of the request matches the one provided.
func MatchRequest ¶
MatchRequest is a helper that matches both the HTTP Method and URL of the request.
func MatchURLPath ¶ added in v0.1.1
MatchURLPath builds a `MatchRule` to check if the URL Path of the request matches the one provided. Note that this does a complete match, not a partial one.
func MatchURLQuery ¶ added in v0.1.1
MatchURLQuery builds a `MatchRule` to check if a query parameter is present with the given name and value. If the query parameter is repeated then only one of the repeated values needs to have the provided value.
type MatchRuleFunc ¶
MatchRuleFunc is a function type that implements the `MatchRule` interface. This allows for simple functions to be used in place of the interface.
type MatchRules ¶
type MatchRules []MatchRule
MatchRules is a type representing a slice of `MatchRule`. This allows for multiple rules to be treated as a single rule.
type MockServer ¶
type MockServer interface { // Close will shut the mock server down. This must always be called, preferably via `defer`. Close() // URL will generate a URL representing the mock server. This includes the scheme, host and post of the server. URL() string // Matches will record a new match against the server that will potentially process any incoming requests. Matches(...MatchRule) *Match // UnmatchedCount will return the number of times a request has been handmed and not matched. UnmatchedCount() int }
MockServer represents the actual server that will be used in the tests.
func New ¶
func New(t *testing.T) MockServer
New will create a new mock server ready for use in tests.
type Response ¶
Response is a representation of the response to send to the client.
func (Response) Write ¶
func (r Response) Write(w http.ResponseWriter)
Write will write the response details to the provided response writer.
type ResponseBuilder ¶
type ResponseBuilder interface { // PopulateResponse is called to allow the builder to update any parts of the response struct. PopulateResponse(r *Response, req *http.Request) }
ResponseBuilder is a means to contribute to the response to send to the client.
func ResponseAppendHeader ¶
func ResponseAppendHeader(name, value string) ResponseBuilder
ResponseAppendHeader will append a new value for the header name provided.
func ResponseBody ¶
func ResponseBody(data []byte) ResponseBuilder
ResponseBody will indicate the data to return as the response body.
func ResponseJSON ¶
func ResponseJSON(data interface{}) ResponseBuilder
ResponseJSON will encode the provided value as JSON and use it as the response, also setting the content-type header.
func ResponseSetHeader ¶
func ResponseSetHeader(name, value string) ResponseBuilder
ResponseSetHeader will set a header value to exactly the value provided.
func ResponseStatus ¶
func ResponseStatus(status int) ResponseBuilder
ResponseStatus returns a `ResponseBuilder` to specify the status code of the response.
type ResponseBuilderFunc ¶
ResponseBuilderFunc is function type that implements the `ResponseBuilder` interface.
func (ResponseBuilderFunc) PopulateResponse ¶
func (f ResponseBuilderFunc) PopulateResponse(r *Response, req *http.Request)
type ResponseBuilders ¶
type ResponseBuilders []ResponseBuilder
ResponseBuilders represents a set of builders that are all called in sequence to populate a response.
func (ResponseBuilders) PopulateResponse ¶
func (b ResponseBuilders) PopulateResponse(r *Response, req *http.Request)