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
- func (c *Client) Do(ctx context.Context, req Request, maxRetries int, retryDelay time.Duration) (*Response, error)
- func (c *Client) Handshake(ctx context.Context, address string, opts *ClientTLS) (*TLSState, error)
- func (c *Client) Stream(ctx context.Context, req Request, visit StreamVisitor) (*Response, error)
- type ClientTLS
- 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 TLSState
- 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) Handshake ¶ added in v0.2.0
Handshake completes one TLS handshake with address, a host:port, and closes the connection without sending a request. It reports what the handshake negotiated, including whether the server asked for a client certificate.
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 ClientTLS ¶ added in v0.2.0
type ClientTLS struct {
// Certificate is presented whenever the server asks for one, whether or not the server
// names its issuer, so an untrusted certificate reaches the server. Nil presents none.
Certificate *tls.Certificate
// ServerName is the SNI value sent. Empty sends the URL host, which Go omits for an IP.
ServerName string
// OmitServerName sends no SNI whatever the URL host is. It ignores ServerName and needs
// InsecureSkipVerify, since a certificate cannot be verified against no name.
OmitServerName bool
// Sessions is shared by requests that may resume one another's TLS session. Nil
// disables resumption.
Sessions tls.ClientSessionCache
// InsecureSkipVerify disables server certificate and hostname verification, for a local
// listener that serves a self-signed certificate.
InsecureSkipVerify bool
}
ClientTLS describes the TLS one HTTPS request negotiates. A request carrying it runs on a connection of its own, because a client certificate is negotiated per connection.
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
// TLS, when set, runs the request on its own connection with this client TLS.
TLS *ClientTLS
}
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
// TLS is the negotiated handshake of a request that carried ClientTLS, else nil.
TLS *TLSState
}
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 TLSState ¶ added in v0.2.0
type TLSState struct {
// Version is the negotiated TLS version.
Version uint16
// ServerName is the SNI value the client sent.
ServerName string
// DidResume reports that the handshake resumed a cached session.
DidResume bool
// ClientCertificateRequested reports that the server sent a CertificateRequest.
ClientCertificateRequested bool
// PeerCertificates is the chain the server presented, leaf first.
PeerCertificates []*x509.Certificate
}
TLSState is what the handshake of a ClientTLS request negotiated.
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.