amock

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2020 License: MIT Imports: 11 Imported by: 0

README

amock lets you easily mock any HTTP dependency you may have.
It respects the http.RoundTripper interface to replace an http client's transport.

Responses are stacked, and indexed by code path: you specify responses for a certain Go function,
and when it's invoked the mock object will go up the stack until it reaches max depth,
or finds a function for which you specified a response.

The response will be pop'ed, so the next identical call will get the next expected response.

You can specify conditional filters on responses:

    - OnFunc(foo.GetFoo):
        Filter on calls that went through a given go function


    - OnIdentifier("foo"):
        Shortcut to filter on requests following a path pattern of /.../foo(/...)
        It is a reasonable assumption that REST implementations follow that pattern,
        which makes writing conditions for these simple cases very easy.

    - On(func(c *amock.Context) bool { return c.Request.Method == "GET" } ):
        More verbose but possible to express anything.

For a working example, see amock_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrUnexpectedCall

func ErrUnexpectedCall(reason string) error

ErrUnexpectedCall crafts an error including a stack trace, to pinpoint a call that did not match any of the configured responses

Types

type Context

type Context struct {
	Request *http.Request
	// contains filtered or unexported fields
}

Context describes the context of the current call to conditional filter functions

func (*Context) Callers

func (c *Context) Callers() map[string]struct{}

Callers returns the functions in the current stack that may be of interest to the conditional filter funcs

type JSON

type JSON struct {
	Obj interface{}
}

JSON respects the ResponsePayload interface. It encloses another object and marshals it into json. This is used if your body object does not respect ResponsePayload.

func (JSON) Payload

func (j JSON) Payload() ([]byte, error)

type MockRoundTripper

type MockRoundTripper struct {
	sync.Mutex
	Responses []*Response
	// contains filtered or unexported fields
}

MockRoundTripper implements http.RoundTripper for mocking/testing purposes

func NewMock

func NewMock() *MockRoundTripper

NewMock creates a MockRoundTripper object

func (*MockRoundTripper) AssertEmpty

func (mc *MockRoundTripper) AssertEmpty(t *testing.T)

AssertEmpty ensures all expected responses have been consumed. It will call t.Error() detailing the remaining unconsumed responses.

func (*MockRoundTripper) Expect

func (mc *MockRoundTripper) Expect(status int, body interface{}) *Response

Expect adds a new expected response, specifying status and body. The other components (headers, conditional filters) can be further specified by chaining setter calls on the response object.

func (*MockRoundTripper) RoundTrip

func (mc *MockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip respects http.RoundTripper. It finds the code path taken to get to here, and returns the first matching expected response.

type Raw

type Raw []byte

Raw respects the ResponsePayload interface. It lets you define a Body object with raw bytes.

func (Raw) Payload

func (r Raw) Payload() ([]byte, error)

type Response

type Response struct {
	Status int

	Body ResponsePayload
	Cond func(*Context) bool

	Mock *MockRoundTripper
	// contains filtered or unexported fields
}

An expected mocked response. Defining traits are status and body. Optionally includes conditional filter function defined by one or several On(...) or OnIdentifier(...) calls.

func (*Response) Headers

func (r *Response) Headers(h http.Header) *Response

Headers adds http headers to the response

func (*Response) On

func (r *Response) On(f func(*Context) bool) *Response

On adds a conditional filter to the response.

func (*Response) OnFunc

func (r *Response) OnFunc(callerFunc interface{}) *Response

OnFunc matches calls that went throug a given go function. It accepts a reference to a function as input, and panics otherwise.

func (*Response) OnIdentifier

func (r *Response) OnIdentifier(ident string) *Response

OnIdentifier adds a conditional filter to the response. The response will be selected only if the HTTP path of the request contains "/.../IDENT(/...)": the identifier enclosed in a distinct path segment

func (*Response) Sticky

func (r *Response) Sticky() *Response

Sticky marks the response as reusable. It will not get consumed whenever it is returned.

type ResponsePayload

type ResponsePayload interface {
	Payload() ([]byte, error)
}

ResponsePayload is an interface that the Body object you pass in your expected responses can respect. It lets you customize the way your body is handled. If you pass an object that does NOT respect ResponsePayload, JSON is the default.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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