Documentation ¶
Overview ¶
Package gock provides a small controller to isolate testing of services (gateways) by mocking the network communication using.
Index ¶
- Variables
- type Controller
- type MockStore
- func (s *MockStore) All() []gock.Mock
- func (s *MockStore) Clean() *MockStore
- func (s *MockStore) Exists(m gock.Mock) bool
- func (s *MockStore) Flush() *MockStore
- func (s *MockStore) IsDone() bool
- func (s *MockStore) IsPending() bool
- func (s *MockStore) Match(req *http.Request) (gock.Mock, error)
- func (s *MockStore) NewMock(uri string) gock.Mock
- func (s *MockStore) Pending() []gock.Mock
- func (s *MockStore) Register(mock gock.Mock) *MockStore
- func (s *MockStore) Remove(m gock.Mock) *MockStore
- type Transport
Constants ¶
This section is empty.
Variables ¶
var ErrCannotMatch = gock.ErrCannotMatch
ErrCannotMatch is the re-exported error for a failed match.
Functions ¶
This section is empty.
Types ¶
type Controller ¶
type Controller struct { // MockStore the attached HTTP request/response mock storage. MockStore *MockStore // contains filtered or unexported fields }
Controller is a Gock based HTTP request/response mock controller.
func NewController ¶
func NewController(t test.Test) *Controller
NewController creates a new HTTP request/response mock controller.
func NewGock ¶
func NewGock(ctrl *gomock.Controller) *Controller
NewGock creates a new HTTP request/response mock controller from the given go mock controller.
func (*Controller) Cleanup ¶
func (ctrl *Controller) Cleanup()
Cleanup checks if all the HTTP request/response mocks that were expected to be called have been called. This function is automatically registered with the test controller and will be called when the test is finished.
func (*Controller) InterceptClient ¶
func (ctrl *Controller) InterceptClient(client *http.Client)
InterceptClient allows to intercept HTTP traffic of a custom http.Client that uses a non default http.Transport/http.RoundTripper implementation.
func (*Controller) New ¶
func (ctrl *Controller) New(uri string) *gock.Request
New creates and registers a new HTTP request/response mock with given full qualified URI and default settings. It returns the request builder for setup of HTTP request and response mock details.
func (*Controller) RestoreClient ¶
func (*Controller) RestoreClient(client *http.Client)
RestoreClient allows to disable and restore the original transport in the given http.Client.
func (*Controller) RoundTrip ¶
RoundTrip receives HTTP requests and matches them against the registered HTTP request/response mocks. If a match is found it is used to construct the response, else the request is tracked as unmatched. If networing is enabled, the original transport is used to handle the request to .
This method implements the `http.RoundTripper` interface and is used by attaching the controller to a `http.client` via `SetTransport`.
type MockStore ¶
type MockStore struct { // Matcher template used when creating new HTTP request/response mocks. Matcher *gock.MockMatcher // contains filtered or unexported fields }
MockStore store for HTTP request/response mocks.
func NewStore ¶
func NewStore(matcher *gock.MockMatcher) *MockStore
NewStore creates a new mock registry for HTTP request/response mocks. If nil is provided as matcher the default matcher is used.
func (*MockStore) Clean ¶
Clean cleans the store removing disabled or obsolete HTTP request/response mocks.
func (*MockStore) Exists ¶
Exists checks if the given HTTP request/response mocks is already registered.
func (*MockStore) Flush ¶
Flush flushes the current stack of registered HTTP request/response mocks.
func (*MockStore) IsDone ¶
IsDone returns true if all the registered HTTP request/response mocks have been triggered successfully.
func (*MockStore) IsPending ¶
IsPending returns true if there are pending HTTP request/response mocks.
func (*MockStore) Match ¶
Match matches the given `http.Request` with the registered HTTP request mocks. It is returning the mock that matches or an error, if a matching function fails. If no HTTP mock request matches nothing is returned.
func (*MockStore) NewMock ¶
NewMock creates and registers a new HTTP request/response mock with default settings and returns the mock.
func (*MockStore) Pending ¶
Pending returns a slice of the pending HTTP request/response mocks. As a side effect the mock store is cleaned up similar as calling `Clean` on it.
type Transport ¶
type Transport struct {
// contains filtered or unexported fields
}
Transport is a small transport implementation delegating requests to the owning HTTP request/response mock controller.
func NewTransport ¶
func NewTransport( controller *Controller, transport http.RoundTripper, ) *Transport
NewTransport creates a new *Transport with no responders.