Documentation ¶
Overview ¶
Package httpapitest helps HTTP response testing.
To test specific endpoint, Request function should be called with options that should validate response from the server:
httpapitest.Request(t, http.MethodGet, "/", http.StatusOk, httpapitest.WithRequestHeader("Content-Type", "application/json")) // ...
The HTTP request will be executed using the supplied client, and response checked in expected status code is returned, as well as with each configured option function.
Index ¶
- func Request(t testing.TB, client *http.Client, method, url string, opts ...Option)
- type Option
- func ExpectNoResponseBody() Option
- func ExpectResponseHeader(key, value string) Option
- func ExpectStatus(code int) Option
- func ExpectedJSONResponse(response interface{}) Option
- func ExpectedResponse(r io.Reader) Option
- func PutResponseBody(b *[]byte) Option
- func UnmarshalJSONResponse(response interface{}) Option
- func WithContext(ctx context.Context) Option
- func WithJSONRequestBody(r interface{}) Option
- func WithMultipartRequest(body io.Reader, length int, filename, contentType string) Option
- func WithRequestBody(body io.Reader) Option
- func WithRequestHeader(key, value string) Option
- func WithRequestHeaders(h http.Header) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Request ¶
Request is a testing helper function that makes an HTTP request using provided client with provided method and url. It performs a validation on expected response code and additional options. It returns response headers if the request and all validation are successful. In case of any error, testing Errorf or Fatal functions will be called.
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func ExpectNoResponseBody ¶
func ExpectNoResponseBody() Option
ExpectNoResponseBody ensures that there is no data sent by the response of the request in the Request function.
func ExpectResponseHeader ¶
ExpectResponseHeader validates a response header value.
func ExpectStatus ¶
ExpectStatus validates that the response from the request has the specific HTTP response status code.
func ExpectedJSONResponse ¶
func ExpectedJSONResponse(response interface{}) Option
ExpectedJSONResponse validates that the response from the request in the Request function matches JSON-encoded body provided here.
func ExpectedResponse ¶
ExpectedResponse validates that the response from the request in the Request function matches the date rad from the reader.
func PutResponseBody ¶
PutResponseBody replaces the data in the provided byte slice with the data from the response body of the request in the Request function.
Example:
var respBytes []byte options := []httpapitest.Option{ httpapitest.PutResponseBody(&respBytes), }
func UnmarshalJSONResponse ¶
func UnmarshalJSONResponse(response interface{}) Option
UnmarshalJSONResponse unmarshals response body from the request in the Request function to the provided response. Response must be a pointer.
func WithContext ¶
WithContext sets a context to the request made by the Request function.
func WithJSONRequestBody ¶
func WithJSONRequestBody(r interface{}) Option
WithJSONRequestBody writes a request JSON-encoded body to the request made by the Request function.
func WithMultipartRequest ¶
WithMultipartRequest writes a multipart request with a single file in it to the request made by the Request function.
func WithRequestBody ¶
WithRequestBody writes a request body to the request made by the Request function.
func WithRequestHeader ¶
WithRequestHeader adds a single header to the request made by the Request function. To add multiple headers call multiple times this option when as arguments to the Request function.
func WithRequestHeaders ¶
WithRequestHeaders sets headers to be sent with the request. It will override already set headers, so any possible WithRequestHeader options must be specified as later arguments in the Request function call.