mock

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 13 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

View Source
var (
	// MultierrorInternalError represents the multierror returned by apier.Unwrap().
	MultierrorInternalError = apierror.Wrap(multierror.NewPrefixed("api error",
		fmt.Errorf("%s: %s", code500, message500),
	))

	// MultierrorNotFound represents the multierror returned by apier.Unwrap().
	MultierrorNotFound = apierror.Wrap(multierror.NewPrefixed("api error",
		fmt.Errorf("%s: %s", code404, message404),
	))

	// MultierrorBadRequest represents the multierror returned by apier.Unwrap().
	MultierrorBadRequest = apierror.Wrap(multierror.NewPrefixed("api error",
		fmt.Errorf("%s: %s", code400, message400),
	))
)
View Source
var ValidClusterID = "320b7b540dfc967a7a649c18e2fce4ed"

ValidClusterID holds a valid cluster id value

Functions

func AssertRequest

func AssertRequest(want *RequestAssertion, req *http.Request) error

AssertRequest ensures that a RequestAssertion matches certain *http.Request fields. If they do not match, an error is return.

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 NewErrorBody

func NewErrorBody(errs ...APIError) io.ReadCloser

NewErrorBody creates a replica of a body representing an EC API error.

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 APIError

type APIError struct {
	Code, Message string
	Fields        []string
}

APIError can be used to create a multiple error response.

type RequestAssertion

type RequestAssertion struct {
	Body   io.ReadCloser
	Header http.Header
	Method string
	Path   string
	Host   string
	Query  url.Values
}

RequestAssertion is used to assert the contents of the request passed to an http.RoundTripper.

type Response

type Response struct {
	Response http.Response
	Error    error

	// If specified, it'll assert that the received request's fields match
	// the assertion.
	Assert *RequestAssertion
}

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 New200ResponseAssertion

func New200ResponseAssertion(assertion *RequestAssertion, body io.ReadCloser) Response

New200ResponseAssertion creates a new response with request assertion and a statuscode 200

func New200StructResponse

func New200StructResponse(i interface{}) Response

New200StructResponse takes in a structure and creates a 200 Response.

func New201Response

func New201Response(body io.ReadCloser) Response

New201Response creates a new response with a statuscode 201

func New201ResponseAssertion

func New201ResponseAssertion(assertion *RequestAssertion, body io.ReadCloser) Response

New201ResponseAssertion creates a new response with request assertion and a statuscode 201

func New202Response

func New202Response(body io.ReadCloser) Response

New202Response creates a new response with a statuscode 202

func New202ResponseAssertion

func New202ResponseAssertion(assertion *RequestAssertion, body io.ReadCloser) Response

New202ResponseAssertion creates a new response with request assertion and a statuscode 202

func New302Response added in v1.8.0

func New302Response(body io.ReadCloser) Response

New302Response creates a new response with a statuscode 302

func New302ResponseAssertion added in v1.8.0

func New302ResponseAssertion(assertion *RequestAssertion, body io.ReadCloser) Response

New302ResponseAssertion creates a new response with request assertion and a statuscode 302

func New400Response added in v1.8.0

func New400Response(body io.ReadCloser) Response

New400Response creates a new response with a statuscode 400

func New400ResponseAssertion added in v1.8.0

func New400ResponseAssertion(assertion *RequestAssertion, body io.ReadCloser) Response

New400ResponseAssertion creates a new response with request assertion and a statuscode 400

func New404Response

func New404Response(body io.ReadCloser) Response

New404Response creates a new response with a statuscode 404

func New404ResponseAssertion

func New404ResponseAssertion(assertion *RequestAssertion, body io.ReadCloser) Response

New404ResponseAssertion creates a new response with request assertion and a statuscode 404

func New409Response added in v1.8.0

func New409Response(body io.ReadCloser) Response

New409Response creates a new response with a statuscode 409

func New409ResponseAssertion added in v1.8.0

func New409ResponseAssertion(assertion *RequestAssertion, body io.ReadCloser) Response

New409ResponseAssertion creates a new response with request assertion and a statuscode 409

func New500Response

func New500Response(body io.ReadCloser) Response

New500Response creates a new response with a statuscode 500

func New500ResponseAssertion

func New500ResponseAssertion(assertion *RequestAssertion, body io.ReadCloser) Response

New500ResponseAssertion creates a new response with request assertion and a statuscode 500

func New501Response added in v1.8.0

func New501Response(body io.ReadCloser) Response

New501Response creates a new response with a statuscode 501

func New501ResponseAssertion added in v1.8.0

func New501ResponseAssertion(assertion *RequestAssertion, body io.ReadCloser) Response

New501ResponseAssertion creates a new response with request assertion and a statuscode 501

func New502Response added in v1.8.0

func New502Response(body io.ReadCloser) Response

New502Response creates a new response with a statuscode 502

func New502ResponseAssertion added in v1.8.0

func New502ResponseAssertion(assertion *RequestAssertion, body io.ReadCloser) Response

New502ResponseAssertion creates a new response with request assertion and a statuscode 502

func NewErrorResponse

func NewErrorResponse(code int, errs ...APIError) Response

NewErrorResponse creates a replica of a responnse representing an EC API error.

func NewStructResponse

func NewStructResponse(i interface{}, code int) Response

NewStructResponse takes in a structure and creates a Response with the specified StatusCode.

func SampleBadRequestError

func SampleBadRequestError() Response

SampleBadRequestError returns a response which encapsulates a 400 error.

func SampleInternalError

func SampleInternalError() Response

SampleInternalError returns a response which encapsulates a 500 error.

func SampleNotFoundError

func SampleNotFoundError() Response

SampleNotFoundError returns a response which encapsulates a 404 error.

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