httptest

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package httptest provides facilities for handling HTTP tests for servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChiContextFromRequest

func ChiContextFromRequest(r *http.Request) *chi.Context

ChiContextFromRequest returns a chi.Context associated with a given http.Request, creating a new one in case the request does not have one yet.

func CloneRequest

func CloneRequest(r *http.Request) *http.Request

CloneRequest takes a given http.Request, and clones it into a new http.Request, preserving its body, if any. Use this instead of http.Request#Clone

func EmptyRequest

func EmptyRequest() *http.Request

EmptyRequest returns a dummy GET request to a "foo" endpoint, without a body. Useful for testing middlewares and handlers.

func EmptyRequestHandler

func EmptyRequestHandler() http.Handler

EmptyRequestHandler returns an http.Handler that simply returns 200, without a body

func ExecuteLogHandler

func ExecuteLogHandler(r *http.Request, handler func(http.ResponseWriter, *http.Request, *zap.Logger)) *http.Response

ExecuteLogHandler takes a request and applies it to a handler that uses middleware.LoggerHandler

func ExecuteLogHandlerWithMiddlewares

func ExecuteLogHandlerWithMiddlewares(r *http.Request, handler func(http.ResponseWriter, *http.Request, *zap.Logger), middlewares ...func(http.Handler) http.Handler) *http.Response

ExecuteLogHandlerWithMiddlewares takes a handler that uses middleware.LoggerHandler and an arbitrary number of middlewares and executes all of them.

func ExecuteMiddlewareWithRequest

func ExecuteMiddlewareWithRequest(r *http.Request, baseHandler http.Handler, middlewares ...func(http.Handler) http.Handler) *http.Response

ExecuteMiddlewareWithRequest takes an HTTP request, a base handler (your controller's action handler, for instance), and a set of middlewares to process the request, returning the response generated by applying the provided request through all middlewares and the provided base handler.

func ExecuteRequest

func ExecuteRequest(req *http.Request, handler func(w http.ResponseWriter, r *http.Request)) *http.Response

ExecuteRequest simply executes the handler with a given http.Request and returns its response.

func JSONFromResponse

func JSONFromResponse(r *http.Response, into interface{})

JSONFromResponse attempts to unmarshal a given http.Response body into the provided interface. Panics if reading the body or unmarshalling fails.

func PassThroughRequestHandler

func PassThroughRequestHandler() (func(*testing.T, *http.Response), http.Handler)

PassThroughRequestHandler returns an assertion function and an HTTP handler. After applying a request to the handler, the response can be tested by the returned assertion function, as to ensure that a response contains the body and header generated randomly by the handler.

For instance:

assertion, handler := test.PassThroughRequestHandler()
resp := test.ExecuteMiddlewareWithRequest(
	test.EmptyRequest(),
	handler,
	ARandomMiddleware{})
assertion(t, resp)

func PrepareRequest

func PrepareRequest(req *http.Request, mutators ...RequestMutator) *http.Request

PrepareRequest takes an HTTP request instance and a set of mutators, applying them to it and returning a new request with all mutations applied.

func RequestInterceptorHandler

func RequestInterceptorHandler() (handler http.Handler, capturedRequest *http.Request)

RequestInterceptorHandler provides a handler that returns an HTTP 200 with no body, and intercepts the request, storing a reference to it for later inspection.

Types

type RequestMutator

type RequestMutator func(r *http.Request) *http.Request

RequestMutator represents a mutation to be applied against an HTTP request. It takes a request, and returns a mutated copy of it.

func WithBody

func WithBody(reader io.Reader) RequestMutator

WithBody associates a body to the http.Request. See also: WithBodyString, WithBodyJSON, WithForm

func WithBodyJSON

func WithBodyJSON(data interface{}) RequestMutator

WithBodyJSON accepts an arbitrary data, marshals it into a JSON structure using json.Marshal, and sets the request body to it. Panics in case marshalling fails. See also: WithBody

func WithBodyString

func WithBodyString(data string) RequestMutator

WithBodyString adds a given data string into the http.Request body. See also: WithBody

func WithContentLength

func WithContentLength(len int64) RequestMutator

WithContentLength sets the Content-Length header of the current request and its internal ContentLength field as the value provided.

func WithForm

func WithForm(form url.Values) RequestMutator

WithForm takes a form represented by url.Values, encodes it, and sets it to the http.Request body. Changes the request method to POST in case it is not set or set to GET, and defines Content-Type to application/x-www-form-urlencoded. See also: WithBody

func WithHeader

func WithHeader(key, value string) RequestMutator

WithHeader sets a given HTTP header of the request to the provided key and value pair.

func WithMethod

func WithMethod(method string) RequestMutator

WithMethod sets a given method as the HTTP method of the request.

func WithQueryString

func WithQueryString(query url.Values) RequestMutator

WithQueryString sets the provided url.Values as the request RawQuery field after encoding it.

func WithURLParam

func WithURLParam(k, v string) RequestMutator

WithURLParam adds a given chi URLParam with key k and value v into a provided http.Request. Useful for handlers using chi.URLParam to obtain URL parameters.

Jump to

Keyboard shortcuts

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