Help us shape the future of Go! Take the 2026 Go Developer Survey

httpx

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

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

View Source
const ResponseKey = "httpResponse"

ResponseKey is the context key holding the response assertion steps read back.

Variables

This section is empty.

Functions

func ClearPublished

func ClearPublished(ctx context.Context)

ClearPublished removes the stored response.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the shared HTTP transport layer.

func NewClient

func NewClient(opts Options) *Client

NewClient returns a client suitable for talking to components under test.

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

func (c *Client) Handshake(ctx context.Context, address string, opts *ClientTLS) (*TLSState, error)

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

func (c *Client) Stream(ctx context.Context, req Request, visit StreamVisitor) (*Response, error)

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 NewFunnel

func NewFunnel(client *Client, maxRetries int, retryDelay time.Duration) *Funnel

NewFunnel wraps a client.

func (*Funnel) Client

func (f *Funnel) Client() *Client

Client exposes the underlying client for responses consumed locally.

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) Get

func (f *Funnel) Get(ctx context.Context, url string, headers map[string]string) (*Response, error)

Get issues a GET 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

func (f *Funnel) Publish(ctx context.Context, response *Response) error

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.

func (*Funnel) Send

func (f *Funnel) Send(ctx context.Context, req Request) (*Response, error)

Send issues an arbitrary request and publishes the response.

func (*Funnel) Stream

func (f *Funnel) Stream(ctx context.Context, req Request, visit StreamVisitor) (*Response, error)

Stream clears the published response, streams the request and publishes the result.

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 Published

func Published(ctx context.Context) (*Response, error)

Published returns the response stored by a previous step.

func (*Response) Describe

func (r *Response) Describe() string

Describe renders the exchange for an assertion message.

func (*Response) HasBody

func (r *Response) HasBody() bool

HasBody reports a non-empty body.

func (*Response) RequireSuccessWithBody

func (r *Response) RequireSuccessWithBody(what string) error

RequireSuccessWithBody checks that a response can be parsed as a successful non-empty response.

func (*Response) Succeeded

func (r *Response) Succeeded() bool

Succeeded reports a 2xx.

func (*Response) Text

func (r *Response) Text() string

Text returns the body as a string.

type StreamVisitor

type StreamVisitor func(line string, sinceHeaders time.Duration) bool

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

type TransientMatcher func(*Response) bool

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL