httputil

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package httputil provides HTTP client abstractions for testability.

Index

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) Do

func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error)

Do records the request and returns the next queued response.

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) Post

func (m *MockHTTPClient) Post(url, contentType string, body io.Reader) (*http.Response, error)

Post issues a POST 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

type MockResponse struct {
	StatusCode int
	Body       string
	Headers    http.Header
	Error      error
}

MockResponse defines a canned HTTP response for testing.

type StandardClient

type StandardClient struct {
	*http.Client
}

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.

func (*StandardClient) Do

func (c *StandardClient) Do(req *http.Request) (*http.Response, error)

Do sends an HTTP request.

func (*StandardClient) Get

func (c *StandardClient) Get(url string) (*http.Response, error)

Get issues a GET request.

func (*StandardClient) Post

func (c *StandardClient) Post(url, contentType string, body io.Reader) (*http.Response, error)

Post issues a POST request.

Jump to

Keyboard shortcuts

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