Documentation ¶
Overview ¶
Package httptest provides facilities for handling HTTP tests for servers.
Index ¶
- func ChiContextFromRequest(r *http.Request) *chi.Context
- func CloneRequest(r *http.Request) *http.Request
- func EmptyRequest() *http.Request
- func EmptyRequestHandler() http.Handler
- func ExecuteLogHandler(r *http.Request, handler func(http.ResponseWriter, *http.Request, *zap.Logger)) *http.Response
- func ExecuteLogHandlerWithMiddlewares(r *http.Request, handler func(http.ResponseWriter, *http.Request, *zap.Logger), ...) *http.Response
- func ExecuteMiddlewareWithRequest(r *http.Request, baseHandler http.Handler, ...) *http.Response
- func ExecuteRequest(req *http.Request, handler func(w http.ResponseWriter, r *http.Request)) *http.Response
- func JSONFromResponse(r *http.Response, into interface{})
- func PassThroughRequestHandler() (func(*testing.T, *http.Response), http.Handler)
- func PrepareRequest(req *http.Request, mutators ...RequestMutator) *http.Request
- func RequestInterceptorHandler() (handler http.Handler, capturedRequest *http.Request)
- type RequestMutator
- func WithBody(reader io.Reader) RequestMutator
- func WithBodyJSON(data interface{}) RequestMutator
- func WithBodyString(data string) RequestMutator
- func WithContentLength(len int64) RequestMutator
- func WithForm(form url.Values) RequestMutator
- func WithHeader(key, value string) RequestMutator
- func WithMethod(method string) RequestMutator
- func WithQueryString(query url.Values) RequestMutator
- func WithURLParam(k, v string) RequestMutator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ChiContextFromRequest ¶
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 ¶
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 ¶
EmptyRequest returns a dummy GET request to a "foo" endpoint, without a body. Useful for testing middlewares and handlers.
func EmptyRequestHandler ¶
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 ¶
JSONFromResponse attempts to unmarshal a given http.Response body into the provided interface. Panics if reading the body or unmarshalling fails.
func PassThroughRequestHandler ¶
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.
Types ¶
type RequestMutator ¶
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.