match

package
v0.0.0-...-0332eaf Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

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

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

func CheckOffForEach(forEach func(interface{}, gjson.Result) error) func(*checkOffOpts)

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

func CheckOffMapper(mapper func(gjson.Result) interface{}) func(*checkOffOpts)

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

type HTTPRequest struct {
	Headers map[string]string
	JSON    []JSON
}

HTTPRequest is the desired shape of the HTTP request. Can include any number of JSON matchers.

type HTTPResponse

type HTTPResponse struct {
	StatusCode int
	Headers    map[string]string
	JSON       []JSON
}

HTTPResponse is the desired shape of the HTTP response. Can include any number of JSON matchers.

type JSON

type JSON func(body gjson.Result) error

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

func AnyOf(checkers ...JSON) JSON

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

func JSONArrayEach(wantKey string, fn func(gjson.Result) error) JSON

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

func JSONCheckOff(wantKey string, wantItems []interface{}, opts ...func(*checkOffOpts)) JSON

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

func JSONKeyArrayOfSize(wantKey string, wantSize int) JSON

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

func JSONKeyEqual(wantKey string, wantValue interface{}) JSON

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

func JSONKeyMissing(forbiddenKey string) JSON

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

func JSONKeyPresent(wantKey string) JSON

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

func JSONKeyTypeEqual(wantKey string, wantType gjson.Type) JSON

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.

func JSONMapEach

func JSONMapEach(wantKey string, fn func(k, v gjson.Result) error) JSON

JSONMapEach returns a matcher which will check that `wantKey` is a map then loops over each item calling `fn`. If `fn` returns an error, iterating stops and an error is returned.

Jump to

Keyboard shortcuts

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