httpclienttest

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DoerSpy

type DoerSpy struct {
	// contains filtered or unexported fields
}

DoerSpy implements Doer and exposes calls made on the underlying Doer. It is safe to call it concurrently.

func NewDoerSpy

func NewDoerSpy(doer httpclient.Doer) *DoerSpy

NewDoerSpy creates a new spy on provided Doer. Spied doer keep track of input and output made on the underlying doer.

func (*DoerSpy) Calls

func (d *DoerSpy) Calls() []DoerSpyRecord

Calls returns the list of calls made on the Doer and clears the list.

func (*DoerSpy) Do

func (d *DoerSpy) Do(req *http.Request) (*http.Response, error)

Do wraps the underlying doer call and keep track of input and outputs.

type DoerSpyRecord

type DoerSpyRecord struct {
	InputRequest   *http.Request
	OutputResponse *http.Response
	OutputError    error
}

DoerSpyRecord stores input and outputs of one Doer call.

type DoerStub

type DoerStub struct {
	// contains filtered or unexported fields
}

DoerStub implements Doer and returns pre-configured calls. It is safe to call it concurrently.

func NewDoerStub

func NewDoerStub(calls []DoerStubCall, strictOrder bool) *DoerStub

NewDoerStub returns a new stubbed Doer. See DoerStubCall for how to configure calls.

func (*DoerStub) Do

func (d *DoerStub) Do(req *http.Request) (*http.Response, error)

Do wraps the underlying doer call and returns pre-configured responses. If strictOrder is true, Doer will go through each configured calls in order and if the request matcher is set and don't match the request, an error will be returned. Otherwise, if strictOrder is false, Doer will go through each configured calls in order but if the request matcher is set and don't match the request, the call will be skipped and the next will be tried. If no calls are remaining, or if structOrder is false and no call match, an error will be returned.

func (*DoerStub) RemainingCalls

func (d *DoerStub) RemainingCalls() []DoerStubCall

RemainingCalls returns calls that were not made but configured.

type DoerStubCall

type DoerStubCall struct {
	Matcher RequestMatcher

	Response *http.Response
	Error    error
}

DoerStubCall define the configuration of a call. If a matcher is not set, the duo 'response,error' will be returned regardless of the request. Otherwise, the request will be checked against matcher and duo 'response,error' will be returned only if the request match.

type RequestMatcher

type RequestMatcher interface {
	MatchRequest(req *http.Request) error
}

RequestMatcher defines a way to check whenever a request match against some pre-defined rules.

type RequestMatcherBuilder

type RequestMatcherBuilder struct {
	// contains filtered or unexported fields
}

RequestMatcherBuilder stores assertions and implements RequestMatcher.

func NewRequestMatcherBuilder

func NewRequestMatcherBuilder() *RequestMatcherBuilder

NewRequestMatcherBuilder creates a new empty RequestMatcherBuilder.

func (*RequestMatcherBuilder) BodyForm

func (b *RequestMatcherBuilder) BodyForm(compareWith url.Values, strict bool) *RequestMatcherBuilder

BodyForm asserts that the provided url values are contained in request.PostForm. Strict parameters define whenever the request.PostForm should be exactly the provided url values or more values can exists.

func (*RequestMatcherBuilder) BodyJSON

func (b *RequestMatcherBuilder) BodyJSON(compareWith any, getDest func() any, strict bool) *RequestMatcherBuilder

BodyJSON asserts that request's body is a JSON can be bound to getDest()'s output and is the same as compareWith. Strict parameters define whenever the body can contain unknown fields.

func (*RequestMatcherBuilder) HeadersContains

func (b *RequestMatcherBuilder) HeadersContains(headers http.Header) *RequestMatcherBuilder

HeadersContains asserts that the provided headers are contained in request.Header.

func (*RequestMatcherBuilder) MatchRequest

func (b *RequestMatcherBuilder) MatchRequest(req *http.Request) error

MatchRequest implements RequestMatcher and asserts all built assertions.

func (*RequestMatcherBuilder) Method

Method asserts that the provided method matches request.Method.

func (*RequestMatcherBuilder) URLHost

URLHost asserts that the provided host matches request.URL.Host.

func (*RequestMatcherBuilder) URLPath

URLPath asserts that the provided path matches request.URL.Path.

func (*RequestMatcherBuilder) URLQueryParamsContains

func (b *RequestMatcherBuilder) URLQueryParamsContains(params url.Values) *RequestMatcherBuilder

URLQueryParamsContains asserts that the provided url values are contained in request.URL.Query().

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server runs a server on which one can assert requests.

func NewServer

func NewServer(do func(serverAddress url.URL, serverDoer httpclient.Doer, checkResponseFunc any) error) *Server

NewServer creates a server. Provided do argument is a fonction that should perform a request.

func (*Server) AssertRequest

func (srv *Server) AssertRequest(requestExpectations RequestMatcher, writeResponse func(http.ResponseWriter) error, checkResponseFunc any) error

AssertRequest performs the request and asserts.

Jump to

Keyboard shortcuts

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