Documentation
¶
Overview ¶
Package testutil provides testing utilities for the httpx package.
Index ¶
- func AssertBody(t *testing.T, resp *http.Response, expected string)
- func AssertBodyContains(t *testing.T, resp *http.Response, expected string)
- func AssertEqual(t *testing.T, expected, actual any)
- func AssertError(t *testing.T, err error)
- func AssertErrorContains(t *testing.T, err error, expected string)
- func AssertFalse(t *testing.T, value bool, msg string)
- func AssertHeader(t *testing.T, resp *http.Response, key, expected string)
- func AssertHeaderContains(t *testing.T, resp *http.Response, key, expected string)
- func AssertNil(t *testing.T, v any)
- func AssertNoError(t *testing.T, err error)
- func AssertNotNil(t *testing.T, v any)
- func AssertRequestHeader(t *testing.T, req *http.Request, key, expected string)
- func AssertRequestHeaderContains(t *testing.T, req *http.Request, key, expected string)
- func AssertStatus(t *testing.T, resp *http.Response, expected int)
- func AssertTrue(t *testing.T, value bool, msg string)
- func NewJSONResponse(statusCode int, v any) *http.Response
- func NewResponse(statusCode int, body string) *http.Response
- type MockTransport
- type ServerOption
- func WithBody(body []byte) ServerOption
- func WithCacheControl(value string) ServerOption
- func WithCompression(encoding string) ServerOption
- func WithContentType(contentType string) ServerOption
- func WithETag(etag string) ServerOption
- func WithFailRate(rate float64) ServerOption
- func WithHeader(key, value string) ServerOption
- func WithJSONBody(v any) ServerOption
- func WithLastModified(t time.Time) ServerOption
- func WithLatency(d time.Duration) ServerOption
- func WithStatusCode(code int) ServerOption
- type TestServer
- func (s *TestServer) LastRequest() *http.Request
- func (s *TestServer) RequestCount() int64
- func (s *TestServer) ResetCount()
- func (s *TestServer) SetBody(body []byte) *TestServer
- func (s *TestServer) SetCacheControl(value string) *TestServer
- func (s *TestServer) SetETag(etag string) *TestServer
- func (s *TestServer) SetFailRate(rate float64) *TestServer
- func (s *TestServer) SetLatency(d time.Duration) *TestServer
- func (s *TestServer) SetStatusCode(code int) *TestServer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertBody ¶
AssertBody checks that the response body matches the expected value.
func AssertBodyContains ¶
AssertBodyContains checks that the response body contains the expected value.
func AssertEqual ¶
AssertEqual checks that two values are equal.
func AssertError ¶
AssertError checks that an error occurred.
func AssertErrorContains ¶
AssertErrorContains checks that an error occurred with a message containing the expected string.
func AssertFalse ¶
AssertFalse checks that a value is false.
func AssertHeader ¶
AssertHeader checks that the response has the expected header value.
func AssertHeaderContains ¶
AssertHeaderContains checks that the response header contains the expected value.
func AssertNoError ¶
AssertNoError checks that no error occurred.
func AssertNotNil ¶
AssertNotNil checks that a value is not nil.
func AssertRequestHeader ¶
AssertRequestHeader checks that a request has the expected header value.
func AssertRequestHeaderContains ¶
AssertRequestHeaderContains checks that a request header contains the expected value.
func AssertStatus ¶
AssertStatus checks that the response has the expected status code.
func AssertTrue ¶
AssertTrue checks that a value is true.
func NewJSONResponse ¶
NewJSONResponse creates a new JSON 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.
type ServerOption ¶
type ServerOption func(*TestServer)
ServerOption configures a TestServer.
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 WithFailRate ¶
func WithFailRate(rate float64) ServerOption
WithFailRate sets the failure rate (0.0-1.0).
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 ¶
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.