httptest

package
v0.22.2 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	T        *testing.T
	Requests []Request
	// contains filtered or unexported fields
}

Client asserts that requests made match the expected ones.

func (*Client) Do

func (c *Client) Do(r *http.Request) (*http.Response, error)

Do implements httputil.Requester.

func (*Client) DoCached

func (c *Client) DoCached(r *http.Request) (*http.Response, error)

DoCached implements httputil.Requester.

func (*Client) Reset

func (c *Client) Reset()

Resets the calls made to the client.

type ErrorReader

type ErrorReader struct {
	Error error
}

ErrorReader is an io.Reader that always returns an error. Useful for testing network errors when reading HTTP response bodies.

func (ErrorReader) Read

func (e ErrorReader) Read(p []byte) (n int, err error)

Read implements io.Reader.

type Request

type Request struct {
	// Expectations set the expectations on the request, which are asserted when
	// the request is made.
	Expectations RequestExpectations
	// Response describes the response to the request.
	Response Response
}

Request describes a request made to and handled by Client.

type RequestExpectations

type RequestExpectations struct {
	URL    string
	Header http.Header
	// Body is the response's body. Defaults to being empty.
	// Available types, in order of precedence:
	//	- string: path to file
	//	- string: verbatim string
	//	- []byte
	//	- io.Reader
	Body any
}

RequestExpectations describes the expectations on the request.

func (RequestExpectations) Assert

func (e RequestExpectations) Assert(t *testing.T, r *http.Request)

Assert asserts the expectations on the request.

type Response

type Response struct {
	// Status defaults to the status text of StatusCode.
	Status string
	// StatusCode defaults to 200.
	StatusCode int
	// Header defaults to an empty header.
	Header http.Header
	// Body is the response's body. Defaults to being empty.
	// Available types, in order of precedence:
	//	- string: path to file
	//	- string: verbatim string
	//	- []byte
	// 	- io.ReadCloser
	//	- io.Reader
	// 	- error: alias for ErrorReader{Error: err}
	Body any
	// Error is the response error, if any.
	Error error
}

Response describes a response to a request made to Client.

func (Response) Response

func (r Response) Response(t *testing.T, req *http.Request) (*http.Response, error)

Response returns the HTTP response and error just like an HTTP client would, meaning the returned response and error can be returned from a "client.Do" function.

Jump to

Keyboard shortcuts

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