web

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2022 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package web helps testing web handlers. A simulator can be started with the handler to test. Then standard http requests can be sent and the returned http responses can be analyzed.

h := NewMyHandler()
s := web.NewSimulator(h)

req := s.CreateRequest(http.MethodGet, "http://localhost:8080/", nil)

resp, err := s.Do(req)
assert.NoError(err)
assert.Equal(resp.StatusCode, http.StatusOK)
body, err := web.BodyToString(resp)
assert.NoError(err)
assert.Equal(body, test.expected)

Some smaller functions help working with the requests and responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BodyToJSON added in v0.6.1

func BodyToJSON(r *http.Response, obj any) error

BodyToJSON reads the whole body and decodes the JSON content into the given object.

func BodyToString added in v0.6.1

func BodyToString(r *http.Response) (string, error)

BodyToString reads the whole body and simply interprets it as string.

func JSONToBody added in v0.6.1

func JSONToBody(obj any, r *http.Request) error

JSONToBody sets the request body to the JSON representation of the given object.

func StringToBody added in v0.6.1

func StringToBody(s string, r *http.Request)

StringToBody sets the request body to a given string.

Types

type Preprocessor

type Preprocessor func(r *http.Request) error

Preprocessor will be executed before a request is passed to the handler.

type Simulator

type Simulator struct {
	// contains filtered or unexported fields
}

Simulator locally simulates HTTP requests to handler.

func NewFuncSimulator

func NewFuncSimulator(f http.HandlerFunc, pps ...Preprocessor) *Simulator

NewFuncSimulator is a convenient variant of NewSimulator just for a http.HandlerFunc.

func NewSimulator

func NewSimulator(h http.Handler, pps ...Preprocessor) *Simulator

NewSimulator creates a new local HTTP request simulator.

func (*Simulator) CreateRequest added in v0.6.3

func (s *Simulator) CreateRequest(method, target string, body io.Reader) *http.Request

CreateRequest creates a request for the simulator.

func (*Simulator) Do

func (s *Simulator) Do(r *http.Request) (*http.Response, error)

Do executes first all registered preprocessors and then lets the handler executes it. The build response is returned.

func (*Simulator) Get added in v0.6.4

func (s *Simulator) Get(target string) (*http.Response, error)

Get conveniently executes a simple GET request.

func (*Simulator) Post added in v0.6.4

func (s *Simulator) Post(target, contentType string, body io.Reader) (*http.Response, error)

Post conveniently executes a simple POST request.

func (*Simulator) PostJSON added in v0.6.4

func (s *Simulator) PostJSON(target string, body any) (*http.Response, error)

PostJSON conveniently executes a simple POST request with the given interface body.

func (*Simulator) PostText added in v0.6.4

func (s *Simulator) PostText(target, body string) (*http.Response, error)

PostText conveniently executes a simple POST request with the given string body.

Jump to

Keyboard shortcuts

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