Documentation ¶
Overview ¶
Package jsonhttptest helps with end-to-end testing of JSON-based HTTP APIs.
To test specific endpoint, Request function should be called with options that should validate response from the server:
options := []jsonhttptest.Option{ jsonhttptest.WithRequestHeader("Content-Type", "text/html"), } jsonhttptest.Request(t, client, http.MethodGet, "/", http.StatusOk, options...) // ...
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(tb testing.TB, client *http.Client, method, url string, responseCode int, ...) http.Header
- type Option
- func WithContext(ctx context.Context) Option
- func WithExpectedContentLength(value int) Option
- func WithExpectedJSONResponse(response interface{}) Option
- func WithExpectedResponse(response []byte) Option
- func WithExpectedResponseHeader(key, value string) Option
- func WithJSONRequestBody(r interface{}) Option
- func WithMultipartRequest(body io.Reader, length int, filename, contentType string) Option
- func WithNoResponseBody() Option
- func WithPutResponseBody(b *[]byte) Option
- func WithRequestBody(body io.Reader) Option
- func WithRequestHeader(key, value string) Option
- func WithUnmarshalJSONResponse(response interface{}) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Request ¶ added in v0.2.0
func Request(tb testing.TB, client *http.Client, method, url string, responseCode int, opts ...Option) http.Header
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 ¶ added in v0.2.0
type Option interface {
// contains filtered or unexported methods
}
func WithContext ¶ added in v0.2.0
WithContext sets a context to the request made by the Request function.
func WithExpectedContentLength ¶ added in v1.14.0
WithExpectedContentLength is shorthand for creating "Content-Length" header check.
func WithExpectedJSONResponse ¶ added in v0.2.0
func WithExpectedJSONResponse(response interface{}) Option
WithExpectedJSONResponse validates that the response from the request in the Request function matches JSON-encoded body provided here.
func WithExpectedResponse ¶ added in v0.2.0
WithExpectedResponse validates that the response from the request in the Request function matches completely bytes provided here.
func WithExpectedResponseHeader ¶ added in v1.14.0
WithExpectedResponseHeader validates that the response from the request has header with specified value
func WithJSONRequestBody ¶ added in v0.2.0
func WithJSONRequestBody(r interface{}) Option
WithJSONRequestBody writes a request JSON-encoded body to the request made by the Request function.
func WithMultipartRequest ¶ added in v0.2.0
WithMultipartRequest writes a multipart request with a single file in it to the request made by the Request function.
func WithNoResponseBody ¶ added in v0.2.0
func WithNoResponseBody() Option
WithNoResponseBody ensures that there is no data sent by the response of the request in the Request function.
func WithPutResponseBody ¶ added in v0.2.0
WithPutResponseBody 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 := []jsonhttptest.Option{ jsonhttptest.WithPutResponseBody(&respBytes), }
func WithRequestBody ¶ added in v0.2.0
WithRequestBody writes a request body to the request made by the Request function.
func WithRequestHeader ¶ added in v0.2.0
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 WithUnmarshalJSONResponse ¶ added in v0.2.0
func WithUnmarshalJSONResponse(response interface{}) Option
WithUnmarshalJSONResponse unmarshals response body from the request in the Request function to the provided response. Response must be a pointer.