Documentation
¶
Overview ¶
Package expect implements several Matcher functions to facilitate matching HTTP request parameters.
Index ¶
- type Args
- type BothMatcherBuilder
- type EitherMatcherBuilder
- type Matcher
- func AllOf(matchers ...Matcher) Matcher
- func AnyOf(matchers ...Matcher) Matcher
- func Func(fn func(v any, a Args) (bool, error)) Matcher
- func JSONPath(p string, matcher Matcher) Matcher
- func LowerCase(matcher Matcher) Matcher
- func Not(matcher Matcher) Matcher
- func Peek(matcher Matcher, action func(v any) error) Matcher
- func ToBe(matcher Matcher) Matcher
- func ToBeEmpty() Matcher
- func ToBePresent() Matcher
- func ToContain(expectation any) Matcher
- func ToEqual(expected any) Matcher
- func ToEqualFold(expected string) Matcher
- func ToEqualJSON(expected any) Matcher
- func ToHaveKey(path string) Matcher
- func ToHaveLen(length int) Matcher
- func ToHavePrefix(prefix string) Matcher
- func ToHaveSuffix(suffix string) Matcher
- func ToMatchExpr[T RegExpMatcherTypes](re T) Matcher
- func Trim(matcher Matcher) Matcher
- func URLPath(expected string) Matcher
- func UpperCase(matcher Matcher) Matcher
- func XOR(first Matcher, second Matcher) Matcher
- type Params
- type RegExpMatcherTypes
- type RequestInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Args ¶
type Args struct { RequestInfo *RequestInfo Params Params }
Args groups contextual information available for each Matcher.
type BothMatcherBuilder ¶
type BothMatcherBuilder struct {
// contains filtered or unexported fields
}
BothMatcherBuilder is a builder for Both matcher. Use .Both() function to create a new Both matcher.
func Both ¶
func Both(first Matcher) *BothMatcherBuilder
Both matches true when both given matchers evaluates to true.
func (*BothMatcherBuilder) And ¶
func (ba *BothMatcherBuilder) And(second Matcher) Matcher
And sets the second matcher.
type EitherMatcherBuilder ¶
type EitherMatcherBuilder struct {
// contains filtered or unexported fields
}
EitherMatcherBuilder is a builder for Either matcher. Prefer to use the Either() function.
func Either ¶
func Either(first Matcher) *EitherMatcherBuilder
Either matches true when any of the two given matchers returns true.
func (*EitherMatcherBuilder) Or ¶
func (e *EitherMatcherBuilder) Or(second Matcher) Matcher
Or sets the second matcher
type Matcher ¶
type Matcher struct { // Name is a metadata that defines the matcher name. Name string // DescribeMismatch gives more context of why the Matcher failed to match a given value. DescribeMismatch func(target string, value any) string // Matches is the function that does the actual matching logic. Matches func(v any, args Args) (bool, error) }
Matcher defines request matchers. Request matchers are used to match requests in order to find a mock to serve a stub response.
func AllOf ¶
AllOf matches when all the given matchers returns true. Example:
AllOf(EqualTo("test"),ToEqualFold("test"),ToContains("tes"))
func AnyOf ¶
AnyOf matches when any of the given matchers returns true. Example:
AnyOf(EqualTo("test"),ToEqualFold("TEST"),ToContains("tes"))
func JSONPath ¶
JSONPath applies the provided matcher to the JSON field value in the given path. Example:
JSONPath("address.city", EqualTo("Santiago"))
func LowerCase ¶
LowerCase lower case matcher string argument before submitting it to provided matcher.
func Peek ¶
Peek will return the result of the given matcher, after executing the provided function. Peek can be used to check the matcher argument.
func ToBePresent ¶
func ToBePresent() Matcher
ToBePresent checks if matcher argument contains a value that is not nil or the zero value for the argument type.
func ToContain ¶
ToContain returns true when the expected value is contained in the matcher argument.
func ToEqualFold ¶
ToEqualFold returns true if expected value is equal to matcher value, ignoring case. ToEqualFold uses strings.EqualFold function.
func ToEqualJSON ¶
ToEqualJSON returns true if matcher value is equal to the given parameter value.
func ToHaveKey ¶
ToHaveKey returns true if the JSON key in the given path is present. Example:
JSON: { "name": "test" } ToHaveKey("name") will return true ToHaveKey("address.street") will return false.
func ToHaveLen ¶
ToHaveLen returns true when matcher argument length is equal to the expected value.
func ToHavePrefix ¶
ToHavePrefix returns true if the matcher argument starts with the given prefix.
func ToHaveSuffix ¶
ToHaveSuffix returns true when matcher argument ends with the given suffix.
func ToMatchExpr ¶
func ToMatchExpr[T RegExpMatcherTypes](re T) Matcher
ToMatchExpr returns true then the given regular expression matches matcher argument. ToMatchExpr accepts a string or a regexp.Regexp.
func URLPath ¶
URLPath returns true if request URL path is equal to the expected path, ignoring case.
func UpperCase ¶
UpperCase upper case matcher string argument before submitting it to provided matcher.
type RegExpMatcherTypes ¶
RegExpMatcherTypes defines the acceptable generic types of RegExpMatches.
type RequestInfo ¶
type RequestInfo struct { // Request is the actual http.Request. Request *http.Request // ParsedBody is http.Request parsed body. // Value of parsed body can vary depending on the mocha.RequestBodyParser that parsed the request. ParsedBody any }
RequestInfo implements HTTP request information to be passed to each Matcher.