expect

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package expect implements several Matcher functions to facilitate matching HTTP request parameters.

Index

Constants

View Source
const (
	ScenarioStateStarted      = "STARTED"
	ScenarioBuiltInParamStore = "@@mocha:scenarios"
)

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(p string, v 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

func AllOf(matchers ...Matcher) Matcher

AllOf matches when all the given matchers returns true. Example:

AllOf(EqualTo("test"),ToEqualFold("test"),ToContains("tes"))

func AnyOf

func AnyOf(matchers ...Matcher) Matcher

AnyOf matches when any of the given matchers returns true. Example:

AnyOf(EqualTo("test"),ToEqualFold("TEST"),ToContains("tes"))

func Func

func Func(fn func(v any, a Args) (bool, error)) Matcher

Func creates an anonymous Matcher using the given function.

func JSONPath

func JSONPath(p string, matcher Matcher) Matcher

JSONPath applies the provided matcher to the JSON field value in the given path. Example:

JSONPath("address.city", EqualTo("Santiago"))

func LowerCase

func LowerCase(matcher Matcher) Matcher

LowerCase lower case matcher string argument before submitting it to provided matcher.

func Not

func Not(matcher Matcher) Matcher

Not negates the provided matcher.

func Peek

func Peek(matcher Matcher, action func(v any) error) Matcher

Peek will return the result of the given matcher, after executing the provided function. Peek can be used to check the matcher argument.

func Repeat

func Repeat(times int) Matcher

Repeat returns true if total request hits for current mock is equal or lower total the provided max call times. If Repeat is used direct, it must be set using Mock After Expectations.

func Scenario

func Scenario(name, requiredState, newState string) Matcher

func ToBe

func ToBe(matcher Matcher) Matcher

ToBe returns the result of the provided matcher.

func ToBeEmpty

func ToBeEmpty() Matcher

ToBeEmpty returns true if matcher value has zero length.

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

func ToContain(expectation any) Matcher

ToContain returns true when the expected value is contained in the matcher argument.

func ToEqual

func ToEqual(expected any) Matcher

ToEqual returns true if matcher value is equal to the given parameter value.

func ToEqualFold

func ToEqualFold(expected string) Matcher

ToEqualFold returns true if expected value is equal to matcher value, ignoring case. ToEqualFold uses strings.EqualFold function.

func ToEqualJSON

func ToEqualJSON(expected any) Matcher

ToEqualJSON returns true if matcher value is equal to the given parameter value.

func ToHaveKey

func ToHaveKey(path string) Matcher

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

func ToHaveLen(length int) Matcher

ToHaveLen returns true when matcher argument length is equal to the expected value.

func ToHavePrefix

func ToHavePrefix(prefix string) Matcher

ToHavePrefix returns true if the matcher argument starts with the given prefix.

func ToHaveSuffix

func ToHaveSuffix(suffix string) Matcher

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 Trim

func Trim(matcher Matcher) Matcher

Trim trims' spaces of matcher argument before submitting it to the given matcher.

func URLPath

func URLPath(expected string) Matcher

URLPath returns true if request URL path is equal to the expected path, ignoring case.

func UpperCase

func UpperCase(matcher Matcher) Matcher

UpperCase upper case matcher string argument before submitting it to provided matcher.

func XOR

func XOR(first Matcher, second Matcher) Matcher

XOR is an exclusive or matcher

func (Matcher) And

func (m Matcher) And(and Matcher) Matcher

And compose the current Matcher with another one using the "and" operator.

func (Matcher) Or

func (m Matcher) Or(or Matcher) Matcher

Or compose the current Matcher with another one using the "or" operator.

func (Matcher) Xor

func (m Matcher) Xor(and Matcher) Matcher

Xor compose the current Matcher with another one using the "xor" operator.

type Params

type Params interface {
	Get(key string) (any, bool)
}

Params define a custom parameter holder for matchers.

type RegExpMatcherTypes

type RegExpMatcherTypes interface {
	string | regexp.Regexp | *regexp.Regexp
}

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.

type Store

type Store interface {
	FetchByName(name string) (scenario, bool)
	CreateNewIfNeeded(name string) scenario
	Save(scenario scenario)
}

func NewScenarioStore

func NewScenarioStore() Store

type ValueSelector

type ValueSelector func(r *RequestInfo) any

ValueSelector defines a function that will be used to extract RequestInfo value and provide it to Matcher instances.

Jump to

Keyboard shortcuts

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