Documentation
¶
Overview ¶
Package httputil provides HTTP client abstractions for testability.
Index ¶
- func BadRequest(w http.ResponseWriter, msg string)
- func InternalServerError(w http.ResponseWriter, msg string)
- func MethodNotAllowed(w http.ResponseWriter)
- func NotFound(w http.ResponseWriter, msg string)
- func WriteJSON(w http.ResponseWriter, status int, data interface{})
- func WriteJSONError(w http.ResponseWriter, status int, msg string)
- func WriteJSONOK(w http.ResponseWriter, data interface{})
- type HTTPClient
- type MockHTTPClient
- func (m *MockHTTPClient) AddErrorResponse(err error) *MockHTTPClient
- func (m *MockHTTPClient) AddResponse(statusCode int, body string) *MockHTTPClient
- func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error)
- func (m *MockHTTPClient) Get(url string) (*http.Response, error)
- func (m *MockHTTPClient) GetRequest(n int) *http.Request
- func (m *MockHTTPClient) Post(url, contentType string, body io.Reader) (*http.Response, error)
- func (m *MockHTTPClient) RequestCount() int
- func (m *MockHTTPClient) Reset()
- type MockResponse
- type StandardClient
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BadRequest ¶
func BadRequest(w http.ResponseWriter, msg string)
BadRequest writes a 400 Bad Request response with the given message.
func InternalServerError ¶
func InternalServerError(w http.ResponseWriter, msg string)
InternalServerError writes a 500 Internal Server Error response.
func MethodNotAllowed ¶
func MethodNotAllowed(w http.ResponseWriter)
MethodNotAllowed writes a 405 Method Not Allowed response.
func NotFound ¶
func NotFound(w http.ResponseWriter, msg string)
NotFound writes a 404 Not Found response.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, status int, data interface{})
WriteJSON writes a JSON response with the given status code and data.
func WriteJSONError ¶
func WriteJSONError(w http.ResponseWriter, status int, msg string)
WriteJSONError writes a JSON error response with the given status code and message. This helper reduces duplication across API handlers.
func WriteJSONOK ¶
func WriteJSONOK(w http.ResponseWriter, data interface{})
WriteJSONOK writes a successful JSON response (200 OK).
Types ¶
type HTTPClient ¶
type HTTPClient interface {
// Do sends an HTTP request and returns an HTTP response.
Do(req *http.Request) (*http.Response, error)
// Get issues a GET to the specified URL.
Get(url string) (*http.Response, error)
// Post issues a POST to the specified URL.
Post(url, contentType string, body io.Reader) (*http.Response, error)
}
HTTPClient abstracts HTTP operations for testability. Use http.DefaultClient or http.Client for production; MockHTTPClient for testing.
type MockHTTPClient ¶
type MockHTTPClient struct {
DoFunc func(req *http.Request) (*http.Response, error)
Requests []*http.Request
Responses []*MockResponse
DefaultError error
// contains filtered or unexported fields
}
MockHTTPClient provides a testable HTTP client implementation.
func NewMockHTTPClient ¶
func NewMockHTTPClient() *MockHTTPClient
NewMockHTTPClient creates a new mock HTTP client.
func (*MockHTTPClient) AddErrorResponse ¶
func (m *MockHTTPClient) AddErrorResponse(err error) *MockHTTPClient
AddErrorResponse queues an error response.
func (*MockHTTPClient) AddResponse ¶
func (m *MockHTTPClient) AddResponse(statusCode int, body string) *MockHTTPClient
AddResponse queues a response to be returned by subsequent requests.
func (*MockHTTPClient) Get ¶
func (m *MockHTTPClient) Get(url string) (*http.Response, error)
Get issues a GET request.
func (*MockHTTPClient) GetRequest ¶
func (m *MockHTTPClient) GetRequest(n int) *http.Request
GetRequest returns the nth recorded request.
func (*MockHTTPClient) RequestCount ¶
func (m *MockHTTPClient) RequestCount() int
RequestCount returns the number of recorded requests.
func (*MockHTTPClient) Reset ¶
func (m *MockHTTPClient) Reset()
Reset clears all recorded requests and responses.
type MockResponse ¶
MockResponse defines a canned HTTP response for testing.
type StandardClient ¶
StandardClient wraps *http.Client to implement HTTPClient.
func NewStandardClient ¶
func NewStandardClient(c *http.Client) *StandardClient
NewStandardClient creates a new StandardClient wrapping the given http.Client.