Documentation
¶
Overview ¶
Package api provides wrappers around net/http types that support stubbing.
Index ¶
- func MatchAny(*http.Request) bool
- type Client
- type ClientOptions
- type Matcher
- func MatchDelete(path string) Matcher
- func MatchGet(path string) Matcher
- func MatchPatch(path string) Matcher
- func MatchPost(path string) Matcher
- func MatchPut(path string) Matcher
- func MatchRequest(method string, path string) Matcher
- func MatchRequestQuery(method string, path string, query url.Values) Matcher
- type RESTClient
- func (c *RESTClient) Delete(path string, resp any) error
- func (c *RESTClient) Do(method string, path string, body any, response any) error
- func (c *RESTClient) DoWithContext(ctx context.Context, method string, url string, payload any, response any) error
- func (c *RESTClient) Get(path string, resp any) error
- func (c *RESTClient) Patch(path string, body any, resp any) error
- func (c *RESTClient) Post(path string, body any, resp any) error
- func (c *RESTClient) Put(path string, body any, resp any) error
- func (c *RESTClient) RegisterStub(matcher Matcher, responder Responder) *RESTClient
- func (c *RESTClient) WithStubbing() *RESTClient
- type RESTClientError
- type Responder
- func ErrorResponse(err error) Responder
- func FileResponse(filename string) Responder
- func JSONResponse(data interface{}) Responder
- func StringResponse(body string) Responder
- func WithHeader(header string, value string, responder Responder) Responder
- func WithRequestHeaders(responder Responder) Responder
- func WithStatus(status int, responder Responder) Responder
- type Stub
- type StubbedTransport
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
Client is a wrapper around net/http.Client that supports stubbing.
func NewClient ¶
func NewClient() *Client
NewClient returns a new client configured with default options.
func NewClientWith ¶
func NewClientWith(opts *ClientOptions) *Client
NewClientWith returns a new Client configured with opts.
func (*Client) RegisterStub ¶
RegisterStub registers a new stub for the given matcher/responder pair.
func (*Client) VerifyStubs ¶
func (c *Client) VerifyStubs(t testable)
VerifyStubs fails the test if there are unmatched stubs.
func (*Client) WithStubbing ¶
WithStubbing configures stubbing and returns the receiver.
type ClientOptions ¶
type ClientOptions struct { // AuthToken is the authorization token included on requests made to BaseURL. AuthToken string // BaseURL is the base URL for relative API requests. BaseURL string // Headers are the HTTP headers that will be sent with every API request. Headers map[string]string // Timeout specifies a time limit for each API request. // Default is no timeout. Timeout time.Duration // Transport specifies the mechanism by which individual API requests are made. // Default is http.DefaultTransport. Transport http.RoundTripper }
ClientOptions allow for configuring new clients.
type Matcher ¶
func MatchDelete ¶
func MatchPatch ¶
func MatchRequest ¶
MatchRequest creates a matcher that matches on method and path.
type RESTClient ¶
RESTClient is a client wrapper for HTTP requests that return JSON.
func NewRESTClient ¶
func NewRESTClient(opts *ClientOptions) *RESTClient
func (*RESTClient) Delete ¶
func (c *RESTClient) Delete(path string, resp any) error
Delete performs a HTTP Delete and parses the response JSON into resp.
func (*RESTClient) Do ¶
Do performs a HTTP request from the given args and parses the response as JSON.
func (*RESTClient) DoWithContext ¶
func (c *RESTClient) DoWithContext(ctx context.Context, method string, url string, payload any, response any) error
DoWithContext performs a HTTP request from the given args and parses the returned text as JSON into the response struct.
- url may be a path (relative to BaseURL), or an absolute URL.
- body may be either a JSON encodable struct, or an io.Reader.
- response may be a JSON encodable struct (or nil if unused).
Note: Absolute URLs will only include the auth token if they share the same root domain.
func (*RESTClient) Get ¶
func (c *RESTClient) Get(path string, resp any) error
Get performs a HTTP Get and parses the response JSON into resp.
func (*RESTClient) Patch ¶
func (c *RESTClient) Patch(path string, body any, resp any) error
Patch performs a HTTP Patch and parses the response JSON into resp.
func (*RESTClient) Post ¶
func (c *RESTClient) Post(path string, body any, resp any) error
Post performs a HTTP Post and parses the response JSON into resp.
func (*RESTClient) Put ¶
func (c *RESTClient) Put(path string, body any, resp any) error
Put performs a HTTP Put and parses the response JSON into resp.
func (*RESTClient) RegisterStub ¶
func (c *RESTClient) RegisterStub(matcher Matcher, responder Responder) *RESTClient
RegisterStub registers a new stub for the given matcher/responder pair.
func (*RESTClient) WithStubbing ¶
func (c *RESTClient) WithStubbing() *RESTClient
WithStubbing configures stubbing and returns the receiver.
type RESTClientError ¶ added in v0.13.2
func (*RESTClientError) Error ¶ added in v0.13.2
func (e *RESTClientError) Error() string
type Responder ¶
func ErrorResponse ¶
ErrorResponse creates a responder that returns err.
func FileResponse ¶
FileResponse creates a responder that returns the content located at filename.
func JSONResponse ¶
func JSONResponse(data interface{}) Responder
JSONResponse creates a responder that serializes and returns data.
func StringResponse ¶
StringResponse creates a responder that returns body.
func WithHeader ¶
WithHeader wraps a responder so that it responds with the provided HTTP header.
func WithRequestHeaders ¶
WithRequestHeaders wraps a responder so that it responds with all HTTP headers from the request.
func WithStatus ¶
WithStatus wraps a responder so that it responds with the provided HTTP status code.
type StubbedTransport ¶
StubbedTransport is a net/http.RoundTripper that serves stubbed responses.
func NewStubbedTransport ¶
func NewStubbedTransport() *StubbedTransport
func (*StubbedTransport) RegisterStub ¶
func (t *StubbedTransport) RegisterStub(matcher Matcher, responder Responder) *StubbedTransport
RegisterStub registers a new stub for the given matcher/responder pair.
func (*StubbedTransport) RoundTrip ¶
RoundTrip implements the RoundTripper interface. Will attempt to match a registered stub or return an error if none found.
func (*StubbedTransport) VerifyStubs ¶
func (t *StubbedTransport) VerifyStubs(test testable)
VerifyStubs fails the test if there are unmatched stubs.