Documentation
¶
Overview ¶
Package request provides all the expectations for a HTTP request.
Deprecated: the package will be removed in the future.
Index ¶
- func BodyMatcher(req *Request) *matcher.BodyMatcherdeprecated
- func CountCall(r *Request)deprecated
- func FailResponse(w http.ResponseWriter, format string, args ...any) errordeprecated
- func Handle(r *Request, w http.ResponseWriter, req *http.Request, ...) errordeprecated
- func HeaderMatcher(req *Request) matcher.HeaderMatcherdeprecated
- func Method(req *Request) stringdeprecated
- func NumCalls(r *Request) intdeprecated
- func Repeatability(r *Request) intdeprecated
- func SetRepeatability(r *Request, i int)deprecated
- func URIMatcher(req *Request) matcher.Matcherdeprecated
- type Headerdeprecated
- type Requestdeprecated
- func (r *Request) After(d time.Duration) *Request
- func (r *Request) Once() *Request
- func (r *Request) Return(v any) *Request
- func (r *Request) ReturnCode(code int) *Request
- func (r *Request) ReturnFile(filePath string) *Request
- func (r *Request) ReturnHeader(header, value string) *Request
- func (r *Request) ReturnHeaders(headers Header) *Request
- func (r *Request) ReturnJSON(body any) *Request
- func (r *Request) Returnf(format string, args ...any) *Request
- func (r *Request) Run(handle func(r *http.Request) ([]byte, error)) *Request
- func (r *Request) Times(i int) *Request
- func (r *Request) Twice() *Request
- func (r *Request) UnlimitedTimes() *Request
- func (r *Request) WaitUntil(w <-chan time.Time) *Request
- func (r *Request) WithBody(body any) *Request
- func (r *Request) WithBodyJSON(v any) *Request
- func (r *Request) WithBodyf(format string, args ...any) *Request
- func (r *Request) WithHeader(header string, value any) *Request
- func (r *Request) WithHeaders(headers map[string]any) *Request
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 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 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 Repeatability
deprecated
func SetRepeatability
deprecated
func URIMatcher
deprecated
Types ¶
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 ¶
NewRequest creates a new request expectation.
func (*Request) After ¶
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 ¶
Once indicates that the mock should only return the value once.
Server.Expect(http.MethodGet, "/path"). Return("hello world!"). Once()
func (*Request) Return ¶
Return sets the result to return to client.
Server.Expect(httpmock.MethodGet, "/path"). Return("hello world!")
func (*Request) ReturnCode ¶
ReturnCode sets the response code.
Server.Expect(httpmock.MethodGet, "/path"). ReturnCode(httpmock.StatusBadRequest)
func (*Request) ReturnFile ¶
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 ¶
ReturnHeader sets a response header.
Server.Expect(httpmock.MethodGet, "/path"). ReturnHeader("foo", "bar")
func (*Request) ReturnHeaders ¶
ReturnHeaders sets a list of response headers.
Server.Expect(httpmock.MethodGet, "/path"). ReturnHeaders(httpmock.Header{"foo": "bar"})
func (*Request) ReturnJSON ¶
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 ¶
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 ¶
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 ¶
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 ¶
Twice indicates that the mock should only return the value twice.
Server.Expect(http.MethodGet, "/path"). Return("hello world!"). Twice()
func (*Request) UnlimitedTimes ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
WithHeader sets an expected header of the given request.
Server.Expect(httpmock.MethodGet, "/path"). WithHeader("foo", "bar")