jsonhttptest

package
v1.17.2-rc1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2023 License: BSD-3-Clause Imports: 13 Imported by: 0

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

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

func WithContext(ctx context.Context) Option

WithContext sets a context to the request made by the Request function.

func WithExpectedContentLength added in v1.14.0

func WithExpectedContentLength(value int) Option

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

func WithExpectedResponse(response []byte) Option

WithExpectedResponse validates that the response from the request in the Request function matches completely bytes provided here.

func WithExpectedResponseHeader added in v1.14.0

func WithExpectedResponseHeader(key, value string) Option

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

func WithMultipartRequest(body io.Reader, length int, filename, contentType string) Option

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 WithNonEmptyResponseHeader added in v1.17.0

func WithNonEmptyResponseHeader(key string) Option

WithNonEmptyResponseHeader validates that the response from the request has header with non empty value.

func WithPutResponseBody added in v0.2.0

func WithPutResponseBody(b *[]byte) Option

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

func WithRequestBody(body io.Reader) Option

WithRequestBody writes a request body to the request made by the Request function.

func WithRequestHeader added in v0.2.0

func WithRequestHeader(key, value string) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL