request

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package request provides all the expectations for a HTTP request.

Deprecated: the package will be removed in the future.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BodyMatcher deprecated

func BodyMatcher(req *Request) *matcher.BodyMatcher

BodyMatcher returns the body matcher of the expectation.

Deprecated: the package will be removed in the future.

func CountCall deprecated

func CountCall(r *Request)

CountCall records a call to the expectation.

Deprecated: the package will be removed in the future.

func FailResponse deprecated

func FailResponse(w http.ResponseWriter, format string, args ...any) error

FailResponse responds a failure to client.

Deprecated: the package will be removed in the future.

func Handle deprecated

func Handle(r *Request, w http.ResponseWriter, req *http.Request, defaultHeaders map[string]string) error

Handle handles the incoming request using the expectation.

Deprecated: the package will be removed in the future.

func HeaderMatcher deprecated

func HeaderMatcher(req *Request) matcher.HeaderMatcher

HeaderMatcher returns the header matcher of the expectation.

Deprecated: the package will be removed in the future.

func Method deprecated

func Method(req *Request) string

Method returns the method of the expectation.

Deprecated: the package will be removed in the future.

func NumCalls deprecated

func NumCalls(r *Request) int

NumCalls returns the number of times the expectation was called.

Deprecated: the package will be removed in the future.

func Repeatability deprecated

func Repeatability(r *Request) int

Repeatability gets the repeatability of the expectation.

Deprecated: the package will be removed in the future.

func SetRepeatability deprecated

func SetRepeatability(r *Request, i int)

SetRepeatability sets the repeatability of the expectation.

Deprecated: the package will be removed in the future.

func URIMatcher deprecated

func URIMatcher(req *Request) matcher.Matcher

URIMatcher returns the uri matcher of the expectation.

Deprecated: the package will be removed in the future.

Types

type Header = map[string]string

Header is an alias of a string map.

Deprecated: the package will be removed in the future.

type Request deprecated

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

Request is an expectation.

Deprecated: the package will be removed in the future.

func NewRequest

func NewRequest(locker sync.Locker, method string, requestURI any) *Request

NewRequest creates a new request expectation.

func (*Request) After

func (r *Request) After(d time.Duration) *Request

After sets how long to block until the call returns.

Server.Expect(http.MethodGet, "/path").
	After(time.Second).
	Return("hello world!")

nolint: unparam

func (*Request) Once

func (r *Request) Once() *Request

Once indicates that the mock should only return the value once.

Server.Expect(http.MethodGet, "/path").
	Return("hello world!").
	Once()

func (*Request) Return

func (r *Request) Return(v any) *Request

Return sets the result to return to client.

Server.Expect(httpmock.MethodGet, "/path").
	Return("hello world!")

func (*Request) ReturnCode

func (r *Request) ReturnCode(code int) *Request

ReturnCode sets the response code.

Server.Expect(httpmock.MethodGet, "/path").
	ReturnCode(httpmock.StatusBadRequest)

func (*Request) ReturnFile

func (r *Request) ReturnFile(filePath string) *Request

ReturnFile reads the file using ioutil.ReadFile and uses it as the result to return to client.

Server.Expect(httpmock.MethodGet, "/path").
	ReturnFile("resources/fixtures/response.txt")

nolint:unparam

func (*Request) ReturnHeader

func (r *Request) ReturnHeader(header, value string) *Request

ReturnHeader sets a response header.

Server.Expect(httpmock.MethodGet, "/path").
	ReturnHeader("foo", "bar")

func (*Request) ReturnHeaders

func (r *Request) ReturnHeaders(headers Header) *Request

ReturnHeaders sets a list of response headers.

Server.Expect(httpmock.MethodGet, "/path").
	ReturnHeaders(httpmock.Header{"foo": "bar"})

func (*Request) ReturnJSON

func (r *Request) ReturnJSON(body any) *Request

ReturnJSON marshals the object using json.Marshal and uses it as the result to return to client.

Server.Expect(httpmock.MethodGet, "/path").
	ReturnJSON(map[string]string{"foo": "bar"})

func (*Request) Returnf

func (r *Request) Returnf(format string, args ...any) *Request

Returnf formats according to a format specifier and use it as the result to return to client.

Server.Expect(httpmock.MethodGet, "/path").
	Returnf("hello %s", "john")

func (*Request) Run

func (r *Request) Run(handle func(r *http.Request) ([]byte, error)) *Request

Run sets the handler to handle a given request.

   Server.Expect(httpmock.MethodGet, "/path").
		Run(func(*http.Request) ([]byte, error) {
			return []byte("hello world!"), nil
		})

func (*Request) Times

func (r *Request) Times(i int) *Request

Times indicates that the mock should only return the indicated number of times.

Server.Expect(http.MethodGet, "/path").
	Return("hello world!").
	Times(5)

func (*Request) Twice

func (r *Request) Twice() *Request

Twice indicates that the mock should only return the value twice.

Server.Expect(http.MethodGet, "/path").
	Return("hello world!").
	Twice()

func (*Request) UnlimitedTimes

func (r *Request) UnlimitedTimes() *Request

UnlimitedTimes indicates that the mock should return the value at least once and there is no max limit in the number of return.

Server.Expect(http.MethodGet, "/path").
	Return("hello world!").
	UnlimitedTimes()

func (*Request) WaitUntil

func (r *Request) WaitUntil(w <-chan time.Time) *Request

WaitUntil sets the channel that will block the mocked return until its closed or a message is received.

Server.Expect(http.MethodGet, "/path").
	WaitUntil(time.After(time.Second)).
	Return("hello world!")

nolint: unparam

func (*Request) WithBody

func (r *Request) WithBody(body any) *Request

WithBody sets the expected body of the given request. It could be []byte, string, fmt.Stringer, or a Matcher.

Server.Expect(httpmock.MethodGet, "/path").
	WithBody("hello world!")

func (*Request) WithBodyJSON

func (r *Request) WithBodyJSON(v any) *Request

WithBodyJSON marshals the object and use it as the expected body of the given request.

Server.Expect(httpmock.MethodGet, "/path").
	WithBodyJSON(map[string]string{"foo": "bar"})

nolint:unparam

func (*Request) WithBodyf

func (r *Request) WithBodyf(format string, args ...any) *Request

WithBodyf formats according to a format specifier and use it as the expected body of the given request.

Server.Expect(httpmock.MethodGet, "/path").
	WithBodyf("hello %s", "john)

func (*Request) WithHeader

func (r *Request) WithHeader(header string, value any) *Request

WithHeader sets an expected header of the given request.

Server.Expect(httpmock.MethodGet, "/path").
	WithHeader("foo", "bar")

func (*Request) WithHeaders

func (r *Request) WithHeaders(headers map[string]any) *Request

WithHeaders sets a list of expected headers of the given request.

Server.Expect(httpmock.MethodGet, "/path").
	WithHeaders(map[string]any{"foo": "bar"})

Jump to

Keyboard shortcuts

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