httpapitest

package module
v0.1.1 Latest Latest
Warning

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

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

README

Go HTTP response testing utility

Go PkgGoDev NewReleases

Installation

Run go get resenje.org/httpapitest from command line.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Request

func Request(t testing.TB, client *http.Client, method, url string, opts ...Option)

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

func ExpectResponseHeader(key, value string) Option

ExpectResponseHeader validates a response header value.

func ExpectStatus

func ExpectStatus(code int) Option

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

func ExpectedResponse(r io.Reader) Option

ExpectedResponse validates that the response from the request in the Request function matches the date rad from the reader.

func PutResponseBody

func PutResponseBody(b *[]byte) Option

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

func WithContext(ctx context.Context) Option

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

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 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 WithRequestHeaders

func WithRequestHeaders(h http.Header) Option

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.

Jump to

Keyboard shortcuts

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