jsonhttptest

package
v2.3.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 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

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

type Option interface {
	// contains filtered or unexported methods
}

func WithContext

func WithContext(ctx context.Context) Option

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

func WithExpectedContentLength

func WithExpectedContentLength(value int) Option

WithExpectedContentLength is shorthand for creating "Content-Length" header check.

func WithExpectedJSONResponse

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

func WithExpectedResponse(response []byte) Option

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

func WithExpectedResponseHeader

func WithExpectedResponseHeader(key, value string) Option

WithExpectedResponseHeader validates that the response from the request has header with specified value

func WithJSONRequestBody

func WithJSONRequestBody(r interface{}) Option

WithJSONRequestBody writes a request JSON-encoded body to the request made by the Request function.

func WithMultipartRequest

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

func WithNoResponseBody() Option

WithNoResponseBody ensures that there is no data sent by the response of the request in the Request function.

func WithNonEmptyResponseHeader

func WithNonEmptyResponseHeader(key string) Option

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

func WithPutResponseBody

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

func WithRequestBody(body io.Reader) Option

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

func WithRequestHeader

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

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