testhttpclient

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package testhttpclient provides a tool for testing code that uses HTTP client(s).

Index

Constants

View Source
const (
	ContentTypeApplicationJSON = "application/json; charset=UTF-8"
	ContentTypeApplicationXML  = "application/xml; charset=UTF-8"
)

Variables

This section is empty.

Functions

func MockJSONResponse added in v0.14.0

func MockJSONResponse(code int, body interface{}) *http.Response

MockJSONResponse builds a JSON http.Response using a literal JSON string or a data struct.

func MockResponse

func MockResponse(code int, body []byte, contentType string) *http.Response

MockResponse builds a http.Response. If contentType blank it is ignored.

func MockXMLResponse added in v0.14.0

func MockXMLResponse(code int, body interface{}) *http.Response

MockXMLResponse builds an XML http.Response using a literal XML string or a data struct.

Types

type MockHttpClient

type MockHttpClient struct {
	CapturedRequests []*http.Request
	// contains filtered or unexported fields
}

MockHttpClient is a HttpClient that holds some stubbed outcomes.

func New

func New(t TestingT) *MockHttpClient

func (*MockHttpClient) AddError

func (m *MockHttpClient) AddError(method, url string, err error) *MockHttpClient

AddError adds an expected outcome that returns an error instead of a response.

func (*MockHttpClient) AddLiteralByteResponse

func (m *MockHttpClient) AddLiteralByteResponse(method, url string, wholeResponse []byte) *MockHttpClient

func (*MockHttpClient) AddLiteralResponse

func (m *MockHttpClient) AddLiteralResponse(method, url string, wholeResponse string) *MockHttpClient

AddLiteralResponse adds an expected outcome that has a literal HTTP response as provided. An example might be

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 18

{"A":"foo","B":7}

func (*MockHttpClient) AddOutcome

func (m *MockHttpClient) AddOutcome(method, url string, outcome Outcome) *MockHttpClient

AddOutcome adds an outcome directly.

func (*MockHttpClient) AddResponse

func (m *MockHttpClient) AddResponse(method, url string, response *http.Response) *MockHttpClient

AddResponse adds an expected outcome that returns a response.

func (*MockHttpClient) CapturedBody

func (m *MockHttpClient) CapturedBody(i int) *bodypkg.Body

CapturedBody gets the request body from the i'th request.

func (*MockHttpClient) Do

func (m *MockHttpClient) Do(req *http.Request) (*http.Response, error)

Do is a pluggable method that implements standard library behaviour using stubbed behaviours. See httpclient.HttpClient. This uses RoundTrip.

func (*MockHttpClient) RemainingOutcomes

func (m *MockHttpClient) RemainingOutcomes() []string

RemainingOutcomes describes the remaining outcomes. Typically, this should be empty at the end of a test (otherwise there might be a setup error).

func (*MockHttpClient) Reset

func (m *MockHttpClient) Reset()

Reset deletes all outcomes and captured responses.

func (*MockHttpClient) RoundTrip added in v0.0.5

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

RoundTrip is a pluggable method that implements standard library http.RoundTripper behaviour using stubbed behaviours.

type Outcome

type Outcome struct {
	Response *http.Response
	Err      error
}

Outcome defines a matching rule for an expected HTTP request outcome.

type TestingT

type TestingT interface {
	Fatalf(format string, args ...interface{})
	Helper()
}

TestingT is a simple *testing.T interface wrapper

Jump to

Keyboard shortcuts

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