httptest

package
v0.0.0-...-519d24f Latest Latest
Warning

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

Go to latest
Published: May 5, 2020 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRequestFactory = RequestFactoryFunc(http.NewRequest)

DefaultRequestFactory is a RequestFactory on top of http.NewRequest

Functions

This section is empty.

Types

type Assert

type Assert struct {
	*assert.Assert
}

Assert is a wrapper for github.com/yssk22/go/x/xtesting/assert.Assert and provides http specific assertions

func NewAssert

func NewAssert(t *testing.T) *Assert

NewAssert returns *Assert

func (*Assert) Body

func (a *Assert) Body(expected string, res *ResponseRecorder, msgContext ...interface{})

Body asserts the body string

func (*Assert) Cookie

func (a *Assert) Cookie(res *ResponseRecorder, name string, msgContext ...interface{}) *http.Cookie

Cookie asserts the cookie name and extract it as *http.Cookie

func (*Assert) Header

func (a *Assert) Header(expected string, res *ResponseRecorder, fieldName string, msgContext ...interface{})

Header asserts the header value

func (*Assert) JSON

func (a *Assert) JSON(v interface{}, res *ResponseRecorder, msgContext ...interface{})

JSON asserts the body string as as json and returns the result as interface{}

func (*Assert) Status

func (a *Assert) Status(expected response.HTTPStatus, res *ResponseRecorder, msgContext ...interface{})

Status asserts the http status code

type Recorder

type Recorder struct {
	Cookies []*http.Cookie
	// contains filtered or unexported fields
}

Recorder is a test recorder.

func NewRecorder

func NewRecorder(handler http.Handler) *Recorder

NewRecorder returns a new *Recorder

func NewRecorderWithFactory

func NewRecorderWithFactory(handler http.Handler, factory RequestFactory) *Recorder

NewRecorderWithFactory returns a new *Recorder with the custom factory configuration

func (*Recorder) NewRequest

func (r *Recorder) NewRequest(method, path string, v interface{}, options ...RequestOption) *http.Request

NewRequest returns http.Request with request body given by `v`

func (*Recorder) TestDelete

func (r *Recorder) TestDelete(path string, options ...RequestOption) *ResponseRecorder

TestDelete make a test DELETE request to the router and returns the response as *http.ResponseRecorder

func (*Recorder) TestGet

func (r *Recorder) TestGet(path string, options ...RequestOption) *ResponseRecorder

TestGet make a test GET request to the router and returns the response as *http.ResponseRecorder

func (*Recorder) TestPost

func (r *Recorder) TestPost(path string, v interface{}, options ...RequestOption) *ResponseRecorder

TestPost make a test POST request to the router and returns the response as *http.ResponseRecorder

func (*Recorder) TestPut

func (r *Recorder) TestPut(path string, v interface{}, options ...RequestOption) *ResponseRecorder

TestPut make a test PUT request to the router and returns the response as *http.ResponseRecorder

func (*Recorder) TestRequest

func (r *Recorder) TestRequest(req *http.Request) *ResponseRecorder

TestRequest make a test request to the router and returns the response as *http.ResponseRecorder

type RequestFactory

type RequestFactory interface {
	NewRequest(string, string, io.Reader) (*http.Request, error)
}

RequestFactory is an interface to create a new request.

type RequestFactoryFunc

type RequestFactoryFunc func(string, string, io.Reader) (*http.Request, error)

RequestFactoryFunc is a wrapper to NewRequest function to be RequestFactory

func (RequestFactoryFunc) NewRequest

func (f RequestFactoryFunc) NewRequest(method string, path string, body io.Reader) (*http.Request, error)

NewRequest implements RequestFactory#NewRequest

type RequestOption

type RequestOption func(req *http.Request) *http.Request

RequestOption is a option function to configure a request

func Header(key string, value string) RequestOption

Header set the request header for Test functions

type ResponseRecorder

type ResponseRecorder struct {
	*httptest.ResponseRecorder
	Request *http.Request
}

func (*ResponseRecorder) Dump

func (r *ResponseRecorder) Dump() string

Dump returns the http trace dump

type Runner

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

Runner is a struct to run a test

func NewRunner

func NewRunner(t *testing.T) *Runner

NewRunner returns a *Runner

func (*Runner) Run

func (r *Runner) Run(name string, f func(a *Assert))

Run wraps xtesting.Runner#Run

func (*Runner) Setup

func (r *Runner) Setup(f func(a *Assert))

Setup wraps xtesting.Runner#Setup

func (*Runner) Teardown

func (r *Runner) Teardown(f func(a *Assert))

Teardown wraps xtesting.Runner#Teardown

Jump to

Keyboard shortcuts

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