Documentation
¶
Overview ¶
Package request provides all the expectations for a HTTP request.
Index ¶
- func BodyMatcher(req *Request) *matcher.BodyMatcher
- func CountCall(r *Request)
- func FailResponse(w http.ResponseWriter, format string, args ...interface{}) error
- func Handle(r *Request, w http.ResponseWriter, req *http.Request, ...) error
- func HeaderMatcher(req *Request) matcher.HeaderMatcher
- func Method(req *Request) string
- func NumCalls(r *Request) int
- func Repeatability(r *Request) int
- func SetRepeatability(r *Request, i int)
- func URIMatcher(req *Request) matcher.Matcher
- type Header
- type Request
- func (r *Request) After(d time.Duration) *Request
- func (r *Request) Once() *Request
- func (r *Request) Return(v interface{}) *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 interface{}) *Request
- func (r *Request) Returnf(format string, args ...interface{}) *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 interface{}) *Request
- func (r *Request) WithBodyJSON(v interface{}) *Request
- func (r *Request) WithBodyf(format string, args ...interface{}) *Request
- func (r *Request) WithHeader(header string, value interface{}) *Request
- func (r *Request) WithHeaders(headers map[string]interface{}) *Request
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BodyMatcher ¶
func BodyMatcher(req *Request) *matcher.BodyMatcher
BodyMatcher returns the body matcher of the expectation.
func FailResponse ¶
func FailResponse(w http.ResponseWriter, format string, args ...interface{}) error
FailResponse responds a failure to client.
func Handle ¶
func Handle(r *Request, w http.ResponseWriter, req *http.Request, defaultHeaders map[string]string) error
Handle handles the incoming request using the expectation.
func HeaderMatcher ¶
func HeaderMatcher(req *Request) matcher.HeaderMatcher
HeaderMatcher returns the header matcher of the expectation.
func Repeatability ¶
Repeatability gets the repeatability of the expectation.
func SetRepeatability ¶
SetRepeatability sets the repeatability of the expectation.
func URIMatcher ¶
URIMatcher returns the uri matcher of the expectation.
Types ¶
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request is an expectation.
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")
func (*Request) WithHeaders ¶
WithHeaders sets a list of expected headers of the given request.
Server.Expect(httpmock.MethodGet, "/path"). WithHeaders(map[string]interface{}{"foo": "bar"})