mock

package
v1.0.0-bc7 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2019 License: Apache-2.0 Imports: 9 Imported by: 4

Documentation

Overview

Package mock provides functions and types to help test and stub external calls that the API structures would otherwise perform causing external calls through the network.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewByteBody

func NewByteBody(b []byte) io.ReadCloser

NewByteBody creates an io.ReadCloser from a slice of bytes.

func NewClient

func NewClient(r ...Response) *http.Client

NewClient returns a pointer to http.Client with the mocked Transport.

func NewStringBody

func NewStringBody(b string) io.ReadCloser

NewStringBody creates an io.ReadCloser from a string.

func NewStructBody

func NewStructBody(i interface{}) io.ReadCloser

NewStructBody creates an io.ReadCloser from a structure that is attempted to be encoded into JSON. In case of failure, it panics.

Types

type Response

type Response struct {
	Response http.Response
	Error    error
}

Response Wraps the response of the RoundTrip.

func New200Response

func New200Response(body io.ReadCloser) Response

New200Response creates a new response with a statuscode 200

func New201Response

func New201Response(body io.ReadCloser) Response

New201Response creates a new response with a statuscode 201

func New202Response

func New202Response(body io.ReadCloser) Response

New202Response creates a new response with a statuscode 202

func New404Response

func New404Response(body io.ReadCloser) Response

New404Response creates a new response with a statuscode 404

func New500Response

func New500Response(body io.ReadCloser) Response

New500Response creates a new response with a statuscode 500

type RoundTripper

type RoundTripper struct {
	Responses []Response
	// contains filtered or unexported fields
}

RoundTripper is aimed to be used as the Transport property in an http.Client in order to mock the responses that it would return in the normal execution. If the number of responses that are mocked are not enough, an error with the request iteration ID, method and full URL is returned.

func NewRoundTripper

func NewRoundTripper(r ...Response) *RoundTripper

NewRoundTripper initializes a new roundtripper and accepts multiple Response structures as variadric arguments.

func (*RoundTripper) Add

func (rt *RoundTripper) Add(res ...Response) *RoundTripper

Add accepts multiple Response structures as variadric arguments and appends those to the current list of Responses.

func (*RoundTripper) RoundTrip

func (rt *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes a single HTTP transaction, returning a Response for the provided Request.

RoundTrip should not attempt to interpret the response. In particular, RoundTrip must return err == nil if it obtained a response, regardless of the response's HTTP status code. A non-nil err should be reserved for failure to obtain a response. Similarly, RoundTrip should not attempt to handle higher-level protocol details such as redirects, authentication, or cookies.

RoundTrip should not modify the request, except for consuming and closing the Request's Body. RoundTrip may read fields of the request in a separate goroutine. Callers should not mutate or reuse the request until the Response's Body has been closed.

RoundTrip must always close the body, including on errors, but depending on the implementation may do so in a separate goroutine even after RoundTrip returns. This means that callers wanting to reuse the body for subsequent requests must arrange to wait for the Close call before doing so.

The Request's URL and Header fields must be initialized.

Jump to

Keyboard shortcuts

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