Documentation
¶
Overview ¶
Package httpx provides shared HTTP clients and response publication for test steps. Client handles transport concerns and Requests publishes the response used by later assertions. Publishing requests clear the previous response before execution.
Index ¶
- Constants
- func ClearPublished(ctx context.Context)
- type Client
- type Funnel
- func (f *Funnel) Client() *Client
- func (f *Funnel) Delete(ctx context.Context, url string, headers map[string]string) (*Response, error)
- func (f *Funnel) Get(ctx context.Context, url string, headers map[string]string) (*Response, error)
- func (f *Funnel) Patch(ctx context.Context, url string, headers map[string]string, body []byte) (*Response, error)
- func (f *Funnel) Post(ctx context.Context, url string, headers map[string]string, body []byte) (*Response, error)
- func (f *Funnel) Publish(ctx context.Context, response *Response) error
- func (f *Funnel) Put(ctx context.Context, url string, headers map[string]string, body []byte) (*Response, error)
- func (f *Funnel) Send(ctx context.Context, req Request) (*Response, error)
- func (f *Funnel) Stream(ctx context.Context, req Request, visit StreamVisitor) (*Response, error)
- type Options
- type Request
- type Response
- type StreamVisitor
- type TransientMatcher
Constants ¶
const ResponseKey = "httpResponse"
ResponseKey is the context key holding the response assertion steps read back.
Variables ¶
This section is empty.
Functions ¶
func ClearPublished ¶
ClearPublished removes the stored response.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the shared HTTP transport layer.
func (*Client) Do ¶
func (c *Client) Do(ctx context.Context, req Request, maxRetries int, retryDelay time.Duration) (*Response, error)
Do issues a request and retries only responses recognized as transient.
func (*Client) Stream ¶
Stream issues one request and reads its body incrementally, handing every line to visit as it arrives.
A fully buffered read collapses every arrival time onto the moment the body closed, which hides whether a server streamed at all. The returned Response carries the lines read as its body, so ordinary status, header and body assertions still apply to it. Transfer-Encoding and Content-Length are restored into the header map from the typed fields net/http moves them into, so a header assertion sees what the server actually sent.
The client's whole-request timeout is not applied: a long-lived stream is bounded by ctx instead. Transient-response retries are not attempted because a stream is not replayable.
type Funnel ¶
type Funnel struct {
// contains filtered or unexported fields
}
Funnel issues a request and publishes its response under ResponseKey.
func (*Funnel) Delete ¶
func (f *Funnel) Delete(ctx context.Context, url string, headers map[string]string) (*Response, error)
Delete issues a DELETE and publishes the response.
func (*Funnel) Patch ¶
func (f *Funnel) Patch(ctx context.Context, url string, headers map[string]string, body []byte) (*Response, error)
Patch issues a PATCH and publishes the response.
func (*Funnel) Post ¶
func (f *Funnel) Post(ctx context.Context, url string, headers map[string]string, body []byte) (*Response, error)
Post issues a POST and publishes the response.
func (*Funnel) Publish ¶
Publish records a response produced by a protocol-specific funnel operation.
func (*Funnel) Put ¶
func (f *Funnel) Put(ctx context.Context, url string, headers map[string]string, body []byte) (*Response, error)
Put issues a PUT and publishes the response.
type Options ¶
type Options struct {
// Timeout bounds one request.
Timeout time.Duration
// FollowRedirects enables normal HTTP redirect handling.
FollowRedirects bool
// MaxRetries bounds transient-error retries.
MaxRetries int
// RetryDelay is the pause between transient retries.
RetryDelay time.Duration
// RetryOn recognises transient responses.
RetryOn []TransientMatcher
// TLSClientConfig configures certificate verification for trusted test services.
// A nil value uses the system verification roots and hostname from the URL, unless
// InsecureSkipVerify is explicitly enabled.
TLSClientConfig *tls.Config
// InsecureSkipVerify disables TLS certificate and hostname verification. Use only for
// local test targets that intentionally use self-signed certificates.
InsecureSkipVerify bool
}
Options configure a client.
type Request ¶
type Request struct {
Method string
URL string
Headers map[string]string
Body []byte
// ContentType is a convenience; an explicit Content-Type header wins.
ContentType string
// Host overrides the HTTP Host header. Empty uses the URL host.
Host string
}
Request describes one call.
type Response ¶
type Response struct {
StatusCode int
Body []byte
Headers http.Header
// Request describes the call that produced the response.
Method string
URL string
// Elapsed is the duration of the request and body read.
Elapsed time.Duration
}
Response is a fully read HTTP exchange.
func (*Response) RequireSuccessWithBody ¶
RequireSuccessWithBody checks that a response can be parsed as a successful non-empty response.
type StreamVisitor ¶
StreamVisitor receives one line of a streamed response body, without its line terminator, and the time that elapsed between the response headers arriving and the line becoming readable. Returning false stops the read and closes the connection.
type TransientMatcher ¶
TransientMatcher identifies responses that may be retried by Client.
func TransientByCodeInBody ¶
func TransientByCodeInBody(marker string) TransientMatcher
TransientByCodeInBody recognizes a transient response by status and body marker.