mock

package
v0.0.0-...-dc172c4 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2018 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package mock contains mock versions of certain systems that are useful for testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPResponse

func HTTPResponse(statusCode int, content string) *http.Response

HTTPResponse creates a mock http.Response with the given statusCode and content body. Use with AddExpectedCall to simulate a response from the API.

Types

type ACArgs

type ACArgs struct {
	// Context is the context passed to use in the call.
	Context context.Context

	// EmailAddress is the email address presented to CreateAccessKey.
	EmailAddress string

	// Password is the email user password presented to CreateAccessKey.
	Password string

	// PwPrehashed is the pwPrehashed arg presented to CreateAccessKey.
	PwPrehashed bool

	// ExpiryDays is the expiryDays arg presented to CreateAccessKey.
	ExpiryDays uint

	// Req is the request presented to Do.
	Req *http.Request

	// URL is the URL passed to Get.
	URL string

	// Signer is the signer presented to Do or Get.
	Signer els.Signer

	// IsELSAPI stores the flag used to determine if calling the ELS API or a
	// third party API.
	IsELSAPI bool
}

ACArgs represents the arguments an API call was invoked with.

type ACRep

type ACRep struct {
	// Delay is how long to wait until returning the simulated response when
	// a call is made.
	Delay time.Duration

	// Err is the error that should be returned from a call (E.g. to simulate
	// a call timing out, return context.DeadlineExceeded).
	Err error

	// Rep is the simulated http response that should be returned. Leave nil if
	// simulating a timeout error, or if simulating a response to
	// CreateAccessKey.
	Rep *http.Response

	// StatusCode is the statusCode arg returned from CreateAccessKey. Only set
	// this if simulating a response to a call to CreateAccessKey.
	StatusCode int

	// AccessKey is the AccessKey arg returns from CreateAccessKey.Only set
	// this if simulating a response to a call to CreateAccessKey.
	AccessKey *els.AccessKey
}

ACRep represents the simulated response to be returned when an API call is made.

type APICall

type APICall struct {
	ACArgs
	ACRep

	// CallMade records whether the call was made
	CallMade bool
	// contains filtered or unexported fields
}

APICall represents a single expected call in an APICaller. It allows you to check the value of the arguments passed on the invocation this covers and you can stipulate the simulated response to be passed back. If you wish to simulate a timeout, then set error to context.DeadlineExceeded

type APICaller

type APICaller struct {
	sync.RWMutex

	// Calls stores the calls expected to be made - if more are encountered
	// than configured in this list, then a panic is thrown.
	Calls []*APICall

	// CallsMade records how many calls have been made by the SUT.
	CallsMade int

	// LastTo simulates the time the lastTimeout was encountered.
	LastTo time.Time
}

APICaller is a mock implementation of els.APICaller that allows us to record multiple requests and simulate a response for each in turn. It is useful in situations where there are expected to be a chain of API calls for which you want to check the arguments passed and simulate responses.

To use, set up the test with one or more calls to AddExpectedCall() as many times as you expect an APICaller method to be invoked by the SUT. Specify the simulated response.

Then after running the test, use GetCall to check the arguments sent to each invocation.

If the expected method wasn't called by the SUT at any stage, or if the SUT makes more API calls than have been configured with AddExpectedCall() then APICaller will panic, and the test will fail.

func NewAPICaller

func NewAPICaller() *APICaller

NewAPICaller returns a new APICaller which implements interface core.APICaller.

func (*APICaller) AddExpectedCall

func (m *APICaller) AddExpectedCall(fn string, c APICall) int

AddExpectedCall adds an expectation of a call. The total number of expected calls is returned.

func (*APICaller) AllCallsMade

func (m *APICaller) AllCallsMade() bool

AllCallsMade returns true if all the configured calls have been made.

func (*APICaller) CreateAccessKey

func (m *APICaller) CreateAccessKey(ctx context.Context, emailAddress string, password string, pwPrehashed bool, expiryDays uint) (*els.AccessKey, int, error)

CreateAccessKey implements interface core.APICaller

func (*APICaller) Do

func (m *APICaller) Do(ctx context.Context, req *http.Request, s els.Signer, isELSAPI bool) (*http.Response, error)

Do implements interface core.APICaller

func (*APICaller) Get

func (m *APICaller) Get(ctx context.Context, URL string, s els.Signer, isELSAPI bool) (*http.Response, error)

Get implements interface core.APICaller

func (*APICaller) GetCall

func (m *APICaller) GetCall(i int) *APICall

GetCall returns a copy of the ith Call if it exists, or an error if it doesn't. exist. Use this after running the SUT to see what arguments each call was invoked with.

func (*APICaller) LastTimeout

func (m *APICaller) LastTimeout() time.Time

LastTimeout implements interface core.APICaller

func (*APICaller) NumCallsMade

func (m *APICaller) NumCallsMade() int

NumCallsMade returns the number of calls that have been executed so far by the SUT.

Jump to

Keyboard shortcuts

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