Documentation ¶
Overview ¶
package match contains matchers for HTTP and JSON data.
Matchers are composable functions which check for the data specified, returning a golang error if a matcher fails. They are typically used with the 'must' package in the following way:
res := user.Do(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "m.room.server_acl"}) must.MatchResponse(t, res, match.HTTPResponse{ StatusCode: 200, JSON: []match.JSON{ match.JSONKeyEqual("allow", []string{"*"}), match.JSONKeyEqual("deny", []string{"hs2"}), match.JSONKeyEqual("allow_ip_literals", true), }, })
Matchers have no concept of tests, and do not automatically fail tests if the match fails. This can be useful when you want to repeatedly perform a check until it succeeds (e.g from /sync). If you want matches to fail a test, you can use the 'must' package.
Index ¶
- func CheckOffAllowUnwanted() func(*checkOffOpts)
- func CheckOffForEach(forEach func(interface{}, gjson.Result) error) func(*checkOffOpts)
- func CheckOffMapper(mapper func(gjson.Result) interface{}) func(*checkOffOpts)
- type HTTPRequest
- type HTTPResponse
- type JSON
- func AnyOf(checkers ...JSON) JSON
- func JSONArrayEach(wantKey string, fn func(gjson.Result) error) JSON
- func JSONCheckOff(wantKey string, wantItems []interface{}, opts ...func(*checkOffOpts)) JSON
- func JSONCheckOffDeprecated(wantKey string, wantItems []interface{}, mapper func(gjson.Result) interface{}, ...) JSON
- func JSONKeyArrayOfSize(wantKey string, wantSize int) JSON
- func JSONKeyEqual(wantKey string, wantValue interface{}) JSON
- func JSONKeyMissing(forbiddenKey string) JSON
- func JSONKeyPresent(wantKey string) JSON
- func JSONKeyTypeEqual(wantKey string, wantType gjson.Type) JSON
- func JSONMapEach(wantKey string, fn func(k, v gjson.Result) error) JSON
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckOffAllowUnwanted ¶
func CheckOffAllowUnwanted() func(*checkOffOpts)
CheckOffAllowUnwanted allows unwanted items, that is items not in `wantItems`, to not fail the check.
func CheckOffForEach ¶
CheckOffForEach does not change the check off logic, but instead passes each item to the provided function. If the function returns an error, the check fails. It is called with 2 args: the item being checked and the element itself (or value if it's an object).
func CheckOffMapper ¶
CheckOffMapper maps each item /before/ continuing the check off process. This is useful to convert a gjson.Result to something more domain specific such as an event ID. For example, if `r` is a Matrix event, this allows `wantItems` to be a slice of event IDs:
CheckOffMapper(func(r gjson.Result) interface{} { return r.Get("event_id").Str })
The `mapper` function should map the item to an interface which will be comparable via JSONDeepEqual with items in `wantItems`.
Types ¶
type HTTPRequest ¶
HTTPRequest is the desired shape of the HTTP request. Can include any number of JSON matchers.
type HTTPResponse ¶
HTTPResponse is the desired shape of the HTTP response. Can include any number of JSON matchers.
type JSON ¶
JSON will perform some matches on the given JSON body, returning an error on a mis-match. It can be assumed that the bytes are valid JSON.
func AnyOf ¶
EXPERIMENTAL AnyOf takes 1 or more `checkers`, and builds a new checker which accepts a given json body iff it's accepted by at least one of the original `checkers`.
func JSONArrayEach ¶
JSONArrayEach returns a matcher which will check that `wantKey` is an array then loops over each item calling `fn`. If `fn` returns an error, iterating stops and an error is returned.
func JSONCheckOff ¶
EXPERIMENTAL JSONCheckOff returns a matcher which will loop over `wantKey` and ensure that the items (which can be array elements or object keys) are present exactly once in `wantItems`. This matcher can be used to check off items in an array/object.
This function supports functional options which change the behaviour of the check off logic, see match.CheckOff... functions for more information.
Usage: (ensures `events` has these events in any order, with the right event type)
JSONCheckOff("events", []interface{}{"$foo:bar", "$baz:quuz"}, CheckOffMapper(func(r gjson.Result) interface{} { return r.Get("event_id").Str }), CheckOffForEach(func(eventID interface{}, eventBody gjson.Result) error { if eventBody.Get("type").Str != "m.room.message" { return fmt.Errorf("expected event to be 'm.room.message'") } }))
func JSONCheckOffDeprecated ¶
func JSONCheckOffDeprecated(wantKey string, wantItems []interface{}, mapper func(gjson.Result) interface{}, fn func(interface{}, gjson.Result) error) JSON
DEPRECATED: Prefer JSONCheckOff as this uses functional options which makes params easier to understand.
JSONCheckOff returns a matcher which will loop over `wantKey` and ensure that the items (which can be array elements or object keys) are present exactly once in any order in `wantItems`. If there are unexpected items or items appear more than once then the match fails. This matcher can be used to check off items in an array/object. The `mapper` function should map the item to an interface which will be comparable via JSONDeepEqual with items in `wantItems`. The optional `fn` callback allows more checks to be performed other than checking off the item from the list. It is called with 2 args: the result of the `mapper` function and the element itself (or value if it's an object).
Usage: (ensures `events` has these events in any order, with the right event type)
JSONCheckOff("events", []interface{}{"$foo:bar", "$baz:quuz"}, func(r gjson.Result) interface{} { return r.Get("event_id").Str }, func(eventID interface{}, eventBody gjson.Result) error { if eventBody.Get("type").Str != "m.room.message" { return fmt.Errorf("expected event to be 'm.room.message'") } })
func JSONKeyArrayOfSize ¶
JSONKeyArrayOfSize returns a matcher which will check that `wantKey` is present and its value is an array with the given size. `wantKey` can be nested, see https://godoc.org/github.com/tidwall/gjson#Get for details.
func JSONKeyEqual ¶
JSONKeyEqual returns a matcher which will check that `wantKey` is present and its value matches `wantValue`. `wantKey` can be nested, see https://godoc.org/github.com/tidwall/gjson#Get for details. `wantValue` is matched via JSONDeepEqual and the JSON takes the forms according to https://godoc.org/github.com/tidwall/gjson#Result.Value
func JSONKeyMissing ¶
JSONKeyMissing returns a matcher which will check that `forbiddenKey` is not present in the JSON object. `forbiddenKey` can be nested, see https://godoc.org/github.com/tidwall/gjson#Get for details.
func JSONKeyPresent ¶
JSONKeyPresent returns a matcher which will check that `wantKey` is present in the JSON object. `wantKey` can be nested, see https://godoc.org/github.com/tidwall/gjson#Get for details.
func JSONKeyTypeEqual ¶
JSONKeyTypeEqual returns a matcher which will check that `wantKey` is present and its value is of the type `wantType`. `wantKey` can be nested, see https://godoc.org/github.com/tidwall/gjson#Get for details.