Documentation ¶
Overview ¶
Package mock contains mock versions of certain systems that are useful for testing.
Index ¶
- func HTTPResponse(statusCode int, content string) *http.Response
- type ACArgs
- type ACRep
- type APICall
- type APICaller
- func (m *APICaller) AddExpectedCall(fn string, c APICall) int
- func (m *APICaller) AllCallsMade() bool
- func (m *APICaller) CreateAccessKey(ctx context.Context, emailAddress string, password string, pwPrehashed bool, ...) (*els.AccessKey, int, error)
- func (m *APICaller) Do(ctx context.Context, req *http.Request, s els.Signer, isELSAPI bool) (*http.Response, error)
- func (m *APICaller) Get(ctx context.Context, URL string, s els.Signer, isELSAPI bool) (*http.Response, error)
- func (m *APICaller) GetCall(i int) *APICall
- func (m *APICaller) LastTimeout() time.Time
- func (m *APICaller) NumCallsMade() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
AddExpectedCall adds an expectation of a call. The total number of expected calls is returned.
func (*APICaller) AllCallsMade ¶
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 ¶
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 ¶
LastTimeout implements interface core.APICaller
func (*APICaller) NumCallsMade ¶
NumCallsMade returns the number of calls that have been executed so far by the SUT.