testutil

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testutil provides testing utilities for the httpx package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertBody

func AssertBody(t *testing.T, resp *http.Response, expected string)

AssertBody checks that the response body matches the expected value.

func AssertBodyContains

func AssertBodyContains(t *testing.T, resp *http.Response, expected string)

AssertBodyContains checks that the response body contains the expected value.

func AssertEqual

func AssertEqual(t *testing.T, expected, actual any)

AssertEqual checks that two values are equal.

func AssertError

func AssertError(t *testing.T, err error)

AssertError checks that an error occurred.

func AssertErrorContains

func AssertErrorContains(t *testing.T, err error, expected string)

AssertErrorContains checks that an error occurred with a message containing the expected string.

func AssertFalse

func AssertFalse(t *testing.T, value bool, msg string)

AssertFalse checks that a value is false.

func AssertHeader

func AssertHeader(t *testing.T, resp *http.Response, key, expected string)

AssertHeader checks that the response has the expected header value.

func AssertHeaderContains

func AssertHeaderContains(t *testing.T, resp *http.Response, key, expected string)

AssertHeaderContains checks that the response header contains the expected value.

func AssertNil

func AssertNil(t *testing.T, v any)

AssertNil checks that a value is nil.

func AssertNoError

func AssertNoError(t *testing.T, err error)

AssertNoError checks that no error occurred.

func AssertNotNil

func AssertNotNil(t *testing.T, v any)

AssertNotNil checks that a value is not nil.

func AssertRequestHeader

func AssertRequestHeader(t *testing.T, req *http.Request, key, expected string)

AssertRequestHeader checks that a request has the expected header value.

func AssertRequestHeaderContains

func AssertRequestHeaderContains(t *testing.T, req *http.Request, key, expected string)

AssertRequestHeaderContains checks that a request header contains the expected value.

func AssertStatus

func AssertStatus(t *testing.T, resp *http.Response, expected int)

AssertStatus checks that the response has the expected status code.

func AssertTrue

func AssertTrue(t *testing.T, value bool, msg string)

AssertTrue checks that a value is true.

func NewJSONResponse

func NewJSONResponse(statusCode int, v any) *http.Response

NewJSONResponse creates a new JSON http.Response for testing.

func NewResponse

func NewResponse(statusCode int, body string) *http.Response

NewResponse creates a new http.Response for testing.

Types

type MockTransport

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

MockTransport is a mock http.RoundTripper for testing.

func NewMockTransport

func NewMockTransport() *MockTransport

NewMockTransport creates a new mock transport.

func (*MockTransport) AddError

func (m *MockTransport) AddError(err error) *MockTransport

AddError adds an error to be returned.

func (*MockTransport) AddResponse

func (m *MockTransport) AddResponse(resp *http.Response) *MockTransport

AddResponse adds a response to be returned.

func (*MockTransport) Requests

func (m *MockTransport) Requests() []*http.Request

Requests returns all requests made through this transport.

func (*MockTransport) Reset

func (m *MockTransport) Reset()

Reset resets the mock transport state.

func (*MockTransport) RoundTrip

func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

type ServerOption

type ServerOption func(*TestServer)

ServerOption configures a TestServer.

func WithBody

func WithBody(body []byte) ServerOption

WithBody sets the response body.

func WithCacheControl

func WithCacheControl(value string) ServerOption

WithCacheControl sets the Cache-Control header.

func WithCompression

func WithCompression(encoding string) ServerOption

WithCompression sets the compression encoding to use.

func WithContentType

func WithContentType(contentType string) ServerOption

WithContentType sets the Content-Type header.

func WithETag

func WithETag(etag string) ServerOption

WithETag sets the ETag header value.

func WithFailRate

func WithFailRate(rate float64) ServerOption

WithFailRate sets the failure rate (0.0-1.0).

func WithHeader

func WithHeader(key, value string) ServerOption

WithHeader adds a custom header.

func WithJSONBody

func WithJSONBody(v any) ServerOption

WithJSONBody sets the response body as JSON.

func WithLastModified

func WithLastModified(t time.Time) ServerOption

WithLastModified sets the Last-Modified header.

func WithLatency

func WithLatency(d time.Duration) ServerOption

WithLatency sets the response latency.

func WithStatusCode

func WithStatusCode(code int) ServerOption

WithStatusCode sets the response status code.

type TestServer

type TestServer struct {
	*httptest.Server
	// contains filtered or unexported fields
}

TestServer is a configurable test server for testing HTTP transports.

func NewTLSTestServer

func NewTLSTestServer(opts ...ServerOption) *TestServer

NewTLSTestServer creates a new TLS test server.

func NewTestServer

func NewTestServer(opts ...ServerOption) *TestServer

NewTestServer creates a new configurable test server.

func (*TestServer) LastRequest

func (s *TestServer) LastRequest() *http.Request

LastRequest returns the last request received.

func (*TestServer) RequestCount

func (s *TestServer) RequestCount() int64

RequestCount returns the number of requests received.

func (*TestServer) ResetCount

func (s *TestServer) ResetCount()

ResetCount resets the request counter.

func (*TestServer) SetBody

func (s *TestServer) SetBody(body []byte) *TestServer

SetBody updates the response body at runtime.

func (*TestServer) SetCacheControl

func (s *TestServer) SetCacheControl(value string) *TestServer

SetCacheControl updates the Cache-Control header at runtime.

func (*TestServer) SetETag

func (s *TestServer) SetETag(etag string) *TestServer

SetETag updates the ETag at runtime.

func (*TestServer) SetFailRate

func (s *TestServer) SetFailRate(rate float64) *TestServer

SetFailRate updates the fail rate at runtime.

func (*TestServer) SetLatency

func (s *TestServer) SetLatency(d time.Duration) *TestServer

SetLatency updates the latency at runtime.

func (*TestServer) SetStatusCode

func (s *TestServer) SetStatusCode(code int) *TestServer

SetStatusCode updates the status code at runtime.

Jump to

Keyboard shortcuts

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