mockclient

package
v0.0.0-...-80d5528 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2019 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionResponse

type ActionResponse struct {
	Headers    map[string][]string `json:"headers,omitempty"`
	StatusCode int                 `json:"statusCode,omitempty"`
	Body       *ResponseBody       `json:"body,omitempty"`
	Delay      *Delay              `json:"delay,omitempty"`
}

ActionResponse defines what actions to take when a request is matched e.g. response, delay, forward etc.

type Client

type Client struct {
	// testing type T
	T *testing.T
	// mock server base address
	BaseURL string
}

Client wraps the testing context used for all interactions with MockServer

func (*Client) AddExpectation

func (c *Client) AddExpectation(exp *Expectation)

AddExpectation adds an expectation based on a request matcher to MockServer

func (*Client) AddVerification

func (c *Client) AddVerification(exp *Expectation)

AddVerification adds a verification of requests to MockServer

func (*Client) AddVerificationSequence

func (c *Client) AddVerificationSequence(exps ...*Expectation)

AddVerificationSequence adds a verification of a specific sequence of requests to MockServer

func (*Client) Clear

func (c *Client) Clear(path string)

Clear everything that matches a given path in MockServer

func (*Client) Reset

func (c *Client) Reset()

Reset the entire MockServer, clearing all state

type Delay

type Delay struct {
	TimeUnit string `json:"timeUnit"`
	Value    int    `json:"value"`
}

Delay sets how long the MockServer will wait before serving a matched response

type Expectation

type Expectation struct {
	Request  *RequestMatcher `json:"httpRequest"`
	Response *ActionResponse `json:"httpResponse,omitempty"`
	Times    *Times          `json:"times,omitempty"`
}

Expectation defines the complete request/response interaction for a given scenario

func CreateExpectation

func CreateExpectation(opts ...ExpectationOption) *Expectation

CreateExpectation converts a number of expectation parts (options) into a single Expectation

func CreateVerification

func CreateVerification(opts ...ExpectationOption) *Expectation

CreateVerification converts a number of expectation parts (options) into a single Expectation

type ExpectationOption

type ExpectationOption func(e *Expectation) *Expectation

ExpectationOption enables building expectations in many parts

func ThenAtLeastCalls

func ThenAtLeastCalls(times int) ExpectationOption

ThenAtLeastCalls creates a verification that a matching call was received at least x times by MockServer

func ThenAtMostCalls

func ThenAtMostCalls(times int) ExpectationOption

ThenAtMostCalls creates a verification that a matching call was received at most x times by MockServer

func ThenResponseDelay

func ThenResponseDelay(delay time.Duration) ExpectationOption

ThenResponseDelay creates an action that delays returning an HTTP response when a request is matched

func ThenResponseJSON

func ThenResponseJSON(body string) ExpectationOption

ThenResponseJSON creates an action that returns an HTTP body as JSON when a request is matched

func ThenResponseStatus

func ThenResponseStatus(statusCode int) ExpectationOption

ThenResponseStatus creates an action that returns an HTTP status code when a request is matched

func ThenResponseText

func ThenResponseText(body string) ExpectationOption

ThenResponseText creates an action that returns an HTTP body as text when a request is matched

func WhenRequestAuth

func WhenRequestAuth(authToken string) ExpectationOption

WhenRequestAuth creates an expectation based on a required Authorization request header

func WhenRequestHeaders

func WhenRequestHeaders(headers map[string][]string) ExpectationOption

WhenRequestHeaders creates an expectation based on required request headers

func WhenRequestMethod

func WhenRequestMethod(method string) ExpectationOption

WhenRequestMethod creates an expectation based on an HTTP method

func WhenRequestPath

func WhenRequestPath(path string) ExpectationOption

WhenRequestPath creates an expectation based on a path

func WhenRequestQueryStringParameters

func WhenRequestQueryStringParameters(qsp map[string][]string) ExpectationOption

WhenRequestQueryStringParameters creates an expectation based on required query string parameters

func WhenTimes

func WhenTimes(times int) ExpectationOption

WhenTimes creates an expectation bounded by a limited number of calls

type RequestMatcher

type RequestMatcher struct {
	Path                  string              `json:"path,omitempty"`
	Method                string              `json:"method,omitempty"`
	Headers               map[string][]string `json:"headers,omitempty"`
	QueryStringParameters map[string][]string `json:"queryStringParameters,omitempty"`
}

RequestMatcher is used to match which requests the expectation will be applied to

type ResponseBody

type ResponseBody struct {
	Type   string `json:"type"`
	String string `json:"string"`
}

ResponseBody defines the request body the MockServer will return when serving a matched response

type Times

type Times struct {
	AtLeast        *int  `json:"atLeast,omitempty"`        // valid for verifications only
	AtMost         *int  `json:"atMost,omitempty"`         // valid for verifications only
	RemainingTimes int   `json:"remainingTimes,omitempty"` // valid for expectations only
	Unlimited      *bool `json:"unlimited,omitempty"`      // valid for expectations only
}

Times defines how many times the MockServer will serve a given request in expectation mode whilst in verification mode defines the expected number of calls

type VerificationSequence

type VerificationSequence struct {
	Requests []*RequestMatcher `json:"httpRequests"`
}

VerificationSequence defines a specific ordered sequence of requests to MockServer

Jump to

Keyboard shortcuts

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