httptest

package
v0.0.0-...-e0434a4 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2019 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusNext    = 555
	StatusNoMatch = 556
	FailedBody    = "testing mock failed to find matching request handler"
)

Variables

View Source
var (
	NoBody     Matcher = &noBodyMatcher{}
	AnyRequest Matcher = &anyRequestMatcher{}

	Get    = Method(http.MethodGet)
	Patch  = Method(http.MethodPatch)
	Post   = Method(http.MethodPost)
	Put    = Method(http.MethodPut)
	Delete = Method(http.MethodDelete)
)

Functions

This section is empty.

Types

type HTTPMock

type HTTPMock struct {
	RequestSnapshots *RequestSnapshotter
	// contains filtered or unexported fields
}

func MockHandler

func MockHandler(h ...http.Handler) *HTTPMock

func (*HTTPMock) ServeHTTP

func (m *HTTPMock) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Matcher

type Matcher interface {
	Match(*http.Request) bool
}

func JSONof

func JSONof(t *testing.T, input interface{}) Matcher

match if the request body would deserialize from json into the input type and be equal input is used for comparison, it will not have unmarshalled into it

type MatcherResponder

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

func (*MatcherResponder) ServeHTTP

func (mr *MatcherResponder) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Matchers

type Matchers []Matcher

func Match

func Match(m ...Matcher) Matchers

func (Matchers) Respond

func (m Matchers) Respond(r ...Responder) *MatcherResponder

func (Matchers) RespondSeq

func (m Matchers) RespondSeq(r ...Responders) *SeqMatcherResponder

type MethodMatcher

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

func Method

func Method(method string) *MethodMatcher

func (*MethodMatcher) Match

func (m *MethodMatcher) Match(r *http.Request) bool

type PathMatcher

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

func Path

func Path(path string) *PathMatcher

func (*PathMatcher) Match

func (p *PathMatcher) Match(r *http.Request) bool

type QueryMatcher

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

func Query

func Query(key, value string) *QueryMatcher

func QueryValues

func QueryValues(key string, values ...string) *QueryMatcher

func (*QueryMatcher) Add

func (q *QueryMatcher) Add(key, value string) *QueryMatcher

func (*QueryMatcher) AddValues

func (q *QueryMatcher) AddValues(key string, values ...string) *QueryMatcher

func (*QueryMatcher) ExactMatch

func (q *QueryMatcher) ExactMatch() *QueryMatcher

ExactMatch requires query values to match exactly (no extra keys/values accepted)

func (*QueryMatcher) Match

func (q *QueryMatcher) Match(r *http.Request) bool

Match ignores order for query parameters, exact only cares there are no extra params present

type RequestSnapshot

type RequestSnapshot struct {
	Method string
	Path   string
	Query  string
	Header map[string]string
}

func NewRequestSnapshot

func NewRequestSnapshot(req *http.Request) *RequestSnapshot

type RequestSnapshotter

type RequestSnapshotter struct {
	sync.Mutex
	Snapshots []*RequestSnapshot
}

func NewRequestSnapshotter

func NewRequestSnapshotter() *RequestSnapshotter

func (*RequestSnapshotter) Add

func (s *RequestSnapshotter) Add(req *http.Request)

func (*RequestSnapshotter) Calls

func (s *RequestSnapshotter) Calls() int

func (*RequestSnapshotter) Debug

func (s *RequestSnapshotter) Debug(t *testing.T)

func (*RequestSnapshotter) ServeHTTP

func (s *RequestSnapshotter) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Responder

type Responder func(w http.ResponseWriter, r *http.Request)

func Body

func Body(body string) Responder

func BytesBody

func BytesBody(body []byte) Responder

func Debug

func Debug(t *testing.T) Responder

Debug can be useful to match specific paths Match(<matchers>).Respond(Debug(t)))

func Header(key string, value ...string) Responder

func JSON

func JSON(t *testing.T, body interface{}) Responder

JSON serializes body and writes json content headers

func JSONContent

func JSONContent(body []byte) Responder

JSONContent should be used when you already have json as []byte

func JSONFromFile

func JSONFromFile(t *testing.T, filename string) Responder

JSONFromFile reads the contents of filename as []bytes writes that along with json content headers

func Status

func Status(status int) Responder

type Responders

type Responders []Responder

type ResponseRecorder

type ResponseRecorder struct {
	Code      int
	HeaderMap http.Header
	Body      *bytes.Buffer
}

func NewResponseRecorder

func NewResponseRecorder() *ResponseRecorder

func (*ResponseRecorder) Header

func (rr *ResponseRecorder) Header() http.Header

func (*ResponseRecorder) Write

func (rr *ResponseRecorder) Write(p []byte) (int, error)

func (*ResponseRecorder) WriteHeader

func (rr *ResponseRecorder) WriteHeader(code int)

type SeqMatcherResponder

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

func (*SeqMatcherResponder) ServeHTTP

func (mr *SeqMatcherResponder) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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