Documentation
¶
Index ¶
- Variables
- type APITest
- func (a *APITest) BuildRequest() *http.Request
- func (a *APITest) Handler(handler http.Handler) *Request
- func (a *APITest) HttpClient(cli *http.Client) *APITest
- func (a *APITest) Mocks(mocks ...*Mock) *APITest
- func (a *APITest) Observe(observer Observe) *APITest
- func (a *APITest) Request() *Request
- func (a *APITest) Response() *Response
- type Assert
- type Cookie
- func (cookie *Cookie) Domain(domain string) *Cookie
- func (cookie *Cookie) Expires(expires time.Time) *Cookie
- func (cookie *Cookie) HttpOnly(httpOnly bool) *Cookie
- func (cookie *Cookie) MaxAge(maxAge int) *Cookie
- func (cookie *Cookie) Path(path string) *Cookie
- func (cookie *Cookie) Secure(secure bool) *Cookie
- func (cookie *Cookie) ToHttpCookie() *http.Cookie
- func (cookie *Cookie) Value(value string) *Cookie
- type Intercept
- type Matcher
- type Mock
- type MockRequest
- func (r *MockRequest) Body(b string) *MockRequest
- func (r *MockRequest) Header(key, value string) *MockRequest
- func (r *MockRequest) Headers(headers map[string]string) *MockRequest
- func (r *MockRequest) Query(key, value string) *MockRequest
- func (r *MockRequest) QueryParams(queryParams map[string]string) *MockRequest
- func (r *MockRequest) QueryPresent(key string) *MockRequest
- func (r *MockRequest) RespondWith() *MockResponse
- type MockResponse
- func (r *MockResponse) Body(body string) *MockResponse
- func (r *MockResponse) Cookie(name, value string) *MockResponse
- func (r *MockResponse) Cookies(cookie ...*Cookie) *MockResponse
- func (r *MockResponse) End() *Mock
- func (r *MockResponse) Header(name string, value string) *MockResponse
- func (r *MockResponse) Headers(headers map[string]string) *MockResponse
- func (r *MockResponse) Status(statusCode int) *MockResponse
- func (r *MockResponse) Times(times int) *MockResponse
- type Observe
- type Request
- func (r *Request) BasicAuth(auth string) *Request
- func (r *Request) Body(b string) *Request
- func (r *Request) Cookies(c ...*Cookie) *Request
- func (r *Request) Delete(url string) *Request
- func (r *Request) Expect(t *testing.T) *Response
- func (r *Request) Get(url string) *Request
- func (r *Request) Headers(h map[string]string) *Request
- func (r *Request) Intercept(interceptor Intercept) *Request
- func (r *Request) Method(method string) *Request
- func (r *Request) Patch(url string) *Request
- func (r *Request) Post(url string) *Request
- func (r *Request) Put(url string) *Request
- func (r *Request) Query(q map[string]string) *Request
- func (r *Request) QueryCollection(q map[string][]string) *Request
- func (r *Request) URL(url string) *Request
- type Response
- func (r *Response) Assert(fn func(*http.Response, *http.Request) error) *Response
- func (r *Response) Body(b string) *Response
- func (r *Response) CookieNotPresent(cookieName string) *Response
- func (r *Response) CookiePresent(cookieName string) *Response
- func (r *Response) Cookies(cookies ...*Cookie) *Response
- func (r *Response) End()
- func (r *Response) Headers(headers map[string]string) *Response
- func (r *Response) Status(s int) *Response
- type Transport
Constants ¶
This section is empty.
Variables ¶
var (
ErrFailedToMatch = "failed to match any of the defined mocks"
)
Functions ¶
This section is empty.
Types ¶
type APITest ¶
type APITest struct {
// contains filtered or unexported fields
}
APITest is the top level struct holding the test spec
func (*APITest) BuildRequest ¶
func (*APITest) HttpClient ¶
HttpClient allows the developer to provide a custom http client when using mocks
type Assert ¶
Assert is a user defined custom assertion function
var IsClientError Assert = func(response *http.Response, request *http.Request) error { if response.StatusCode >= 400 && response.StatusCode < 500 { return nil } return errors.New("not a client error. Status code=" + strconv.Itoa(response.StatusCode)) }
IsClientError is a convenience function to assert on a range of client error status codes
var IsServerError Assert = func(response *http.Response, request *http.Request) error { if response.StatusCode >= 500 { return nil } return errors.New("not a server error. Status code=" + strconv.Itoa(response.StatusCode)) }
IsServerError is a convenience function to assert on a range of server error status codes
var IsSuccess Assert = func(response *http.Response, request *http.Request) error { if response.StatusCode >= 200 && response.StatusCode < 400 { return nil } return errors.New("not a client error. Status code=" + strconv.Itoa(response.StatusCode)) }
IsSuccess is a convenience function to assert on a range of happy path status codes
type Cookie ¶
type Cookie struct {
// contains filtered or unexported fields
}
func (*Cookie) ToHttpCookie ¶
type Intercept ¶
Intercept will be called before the request is made. Updates to the request will be reflected in the test
type Mock ¶
type Mock struct {
// contains filtered or unexported fields
}
func (*Mock) Delete ¶
func (m *Mock) Delete(u string) *MockRequest
func (*Mock) Get ¶
func (m *Mock) Get(u string) *MockRequest
func (*Mock) Method ¶
func (m *Mock) Method(method string) *MockRequest
func (*Mock) Patch ¶
func (m *Mock) Patch(u string) *MockRequest
func (*Mock) Post ¶
func (m *Mock) Post(u string) *MockRequest
func (*Mock) Put ¶
func (m *Mock) Put(u string) *MockRequest
type MockRequest ¶
type MockRequest struct {
// contains filtered or unexported fields
}
func (*MockRequest) Body ¶
func (r *MockRequest) Body(b string) *MockRequest
func (*MockRequest) Header ¶
func (r *MockRequest) Header(key, value string) *MockRequest
func (*MockRequest) Headers ¶
func (r *MockRequest) Headers(headers map[string]string) *MockRequest
func (*MockRequest) Query ¶
func (r *MockRequest) Query(key, value string) *MockRequest
func (*MockRequest) QueryParams ¶
func (r *MockRequest) QueryParams(queryParams map[string]string) *MockRequest
func (*MockRequest) QueryPresent ¶
func (r *MockRequest) QueryPresent(key string) *MockRequest
func (*MockRequest) RespondWith ¶
func (r *MockRequest) RespondWith() *MockResponse
type MockResponse ¶
type MockResponse struct {
// contains filtered or unexported fields
}
func (*MockResponse) Body ¶
func (r *MockResponse) Body(body string) *MockResponse
func (*MockResponse) Cookie ¶
func (r *MockResponse) Cookie(name, value string) *MockResponse
func (*MockResponse) Cookies ¶
func (r *MockResponse) Cookies(cookie ...*Cookie) *MockResponse
func (*MockResponse) End ¶
func (r *MockResponse) End() *Mock
func (*MockResponse) Header ¶
func (r *MockResponse) Header(name string, value string) *MockResponse
func (*MockResponse) Headers ¶
func (r *MockResponse) Headers(headers map[string]string) *MockResponse
func (*MockResponse) Status ¶
func (r *MockResponse) Status(statusCode int) *MockResponse
func (*MockResponse) Times ¶
func (r *MockResponse) Times(times int) *MockResponse
type Observe ¶
Observe will be called by with the request and response on completion
var DumpHttp Observe = func(res *http.Response, req *http.Request) { requestDump, err := httputil.DumpRequest(req, true) if err == nil { fmt.Println("--> http request dump\n\n" + string(requestDump)) } responseDump, err := httputil.DumpResponse(res, true) if err == nil { fmt.Println("<-- http response dump:\n\n" + string(responseDump)) } }
DumpHttp logs the http wire representation of the request and response
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request is the user defined request that will be invoked on the handler under test
func (*Request) BasicAuth ¶
BasicAuth is a builder method to sets basic auth on the request. The credentials should be provided delimited by a colon, e.g. "username:password"
func (*Request) Delete ¶
Delete is a convenience method for setting the request as http.MethodDelete
func (*Request) Expect ¶
Expect marks the request spec as complete and following code will define the expected response
func (*Request) Query ¶
Query is a builder method to set the request query parameters. This can be used in combination with request.QueryCollection
func (*Request) QueryCollection ¶
QueryCollection is a builder method to set the request query parameters This can be used in combination with request.Query
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response is the user defined expected response from the application under test
func (*Response) Assert ¶
Assert allows the consumer to provide a user defined function containing their own custom assertions
func (*Response) CookieNotPresent ¶
CookieNotPresent is used to assert that a cookie is not present in the response
func (*Response) CookiePresent ¶
CookiePresent is used to assert that a cookie is present in the response, regardless of its value