Documentation
¶
Overview ¶
Package httpt provides test helpers for HTTP server and client operations. It creates mock HTTP servers on random local ports for testing.
Index ¶
- func TestServer(t *test.Test, handler http.Handler, opts ...httpcli.Option) (*httptest.Server, *httpcli.Client)
- type MockServer
- func (m *MockServer) BaseURL() string
- func (m *MockServer) Client(opts ...httpcli.Option) *httpcli.Client
- func (m *MockServer) GET(path string, handler any) *MockServer
- func (m *MockServer) Mock(method string, path string, handler any) *MockServer
- func (m *MockServer) POST(path string, handler any) *MockServer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TestServer ¶
func TestServer(t *test.Test, handler http.Handler, opts ...httpcli.Option) (*httptest.Server, *httpcli.Client)
TestServer creates and starts an HTTP test server with the provided handler, returning both the server and a configured client. The server listens on a random local port and the client is pre-configured with the server's URL.
Types ¶
type MockServer ¶
MockServer provides a mock HTTP server for testing. It uses httptest.Server and allows registering handlers for specific HTTP methods and paths.
func NewMock ¶
func NewMock(t *test.Test) *MockServer
NewMock creates a new mock HTTP server. The server listens on a random local port and is automatically shut down when the test completes.
func (*MockServer) BaseURL ¶
func (m *MockServer) BaseURL() string
BaseURL returns the base URL of the mock server.
func (*MockServer) Client ¶
func (m *MockServer) Client(opts ...httpcli.Option) *httpcli.Client
Client creates a new HTTP client configured to send requests to the mock server's base URL. Optional client configuration can be provided.
func (*MockServer) GET ¶
func (m *MockServer) GET(path string, handler any) *MockServer
GET registers a handler for GET requests at the specified path. Returns the MockServer for method chaining.
func (*MockServer) Mock ¶
func (m *MockServer) Mock(method string, path string, handler any) *MockServer
Mock registers a handler for the specified HTTP method and path. Returns the MockServer for method chaining.
func (*MockServer) POST ¶
func (m *MockServer) POST(path string, handler any) *MockServer
POST registers a handler for POST requests at the specified path. Returns the MockServer for method chaining.