Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrNoTestProvided = errors.New("no test provided when creating a new client - did you forget to use WithTest?")
)
Functions ¶
func Embed ¶
func Embed(httpClient *http.Client, options ...ClientOption) *http.Client
Embed installs a Client as the transport of the provided http.Client. The Client will be created using the NewClient function with the provided options. This is a shorthand for creating a new client and setting it as the transport of a http.Client.
If an error occurs during the creation of the client, and options contain a testing.T instance, the error will be reported using the testing.T.Error method and nil is returned. If no testing.T instance is provided, the error will cause a panic.
The returned http.Client is the same as the one provided as an argument. Either can be used after the call.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a client that can make HTTP requests via its [Client.do] method. For every request processed, the client will fingerprint the request (see RequestFingerprint) and if the clients corpus contains a response for that fingerprint, it will return the cached response. If the response is not in the corpus, the client will make the request and install the response in the corpus before returning it.
The corpus directory defaults to `testdata/httptestcorpus` and can be changed with the WithCorpusDir option. If the corpus directory path is relative, it will be relative to the current working directory.
The client requires a testing.T instance to be provided via the WithTest option. This is used to name the corpus entries and to ensure that the client is used in the context of a test.
The client should not be created directly, but instead via the NewClient function.
func NewClient ¶
func NewClient(options ...ClientOption) (*Client, error)
NewClient creates a new client with the provided options. The client will return an error if WithTest is not provided.
Default options are: - WithCorpusDir with the default corpus directory path `testdata/httptestcorpus`
type ClientOption ¶
type ClientOption interface {
// contains filtered or unexported methods
}
ClientOption is an option for configuring a Client.
func WithCorpusDir ¶
func WithCorpusDir(dir string) ClientOption
WithCorpusDir returns a ClientOption that, when provided to a Client, instructs the client to use the provided directory as the corpus directory. If not provided, the default directory is "testdata/httptestcorpus".
func WithHTTPClient ¶
func WithHTTPClient(client *http.Client) ClientOption
WithHTTPClient returns a ClientOption that, when provided to a Client, instructs the client to use the provided http.Client for making requests. If not provided, the default client is http.DefaultClient.
func WithTest ¶
func WithTest(t *testing.T) ClientOption
WithTest returns a ClientOption that instructs the client to use the provided testing.T. This is required for the client to function correctly.
type RequestFingerprint ¶
func NewRequestFingerprint ¶
func NewRequestFingerprint(req *http.Request) (RequestFingerprint, error)
NewRequestFingerprint creates a new request fingerprint from the provided request.
If the request body is not nil, it will be read and stored in the fingerprint.
func (RequestFingerprint) Fingerprint ¶
func (rf RequestFingerprint) Fingerprint() string
Fingerprint returns the fingerprint of the request. The fingerprint is a SHA256 hash of the JSON representation of the RequestFingerprint struct.