mocks

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2016 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMockTransport = &MockTransport{}

The global default RoundTripper for all http requests.

Functions

func Activate

func Activate(failNoResponder bool)

Activate replaces the `Transport` on the `http.DefaultClient` with our `DefaultMockTransport`.

func Deactivate

func Deactivate()

Deactivate replaces our `DefaultMockTransport` with the `http.DefaultTransport`.

func NewTestServer

func NewTestServer() (server *httptest.Server)

Start a new Server with status OK, caller should call Close when finished

func NewTestServerWithBody

func NewTestServerWithBody(body string) (server *httptest.Server)

func RegisterResponder

func RegisterResponder(method, url string, responder Responder)

Add a responder to `DefaultMockTransport`.

Types

type MockTransport

type MockTransport struct {
	FailNoResponder bool
	// contains filtered or unexported fields
}

MockTransport implements http.RoundTripper. The implementation doesn't make the call, instead defering to the registered list of responders. Return Error if no responder is found when FailNoResponder = true

func (*MockTransport) RegisterResponder

func (m *MockTransport) RegisterResponder(method, url string, responder Responder)

Add a new responder, associated with a given HTTP method and URL. When a request matches, the responder will be called and the response returned.

func (*MockTransport) RoundTrip

func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip is required to implement http.MockTransport.

type Responder

type Responder func(*http.Request) (*http.Response, error)

Responders are callbacks that receive http request and return a mocked response.

func CreateResponder

func CreateResponder(status int, response string) Responder

Create a responder with protocol version "HTTP/1.0" and a mocked response in JSON

Jump to

Keyboard shortcuts

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