Documentation
¶
Index ¶
- func NewData(d HTTPData, key string, value any)
- func SetBasicAuth(req *http.Request, auth *BasicAuth)
- func SetCookies(req *http.Request, cookies []*http.Cookie)
- func SetHeaders(req *http.Request, headers []HTTPData)
- func SetQueryParams(url string, queryParams ...HTTPData) string
- type BasicAuth
- type Call
- type Client
- func (c *Client) Connect(url string, options HTTPOptions) (*http.Response, error)
- func (c *Client) Context() context.Context
- func (c *Client) Delete(url string, options HTTPOptions) (*http.Response, error)
- func (c *Client) Get(url string, options HTTPOptions) (*http.Response, error)
- func (c *Client) Head(url string, options HTTPOptions) (*http.Response, error)
- func (c *Client) Options(url string, options HTTPOptions) (*http.Response, error)
- func (c *Client) Patch(url string, options HTTPOptions) (*http.Response, error)
- func (c *Client) Post(url string, options HTTPOptions) (*http.Response, error)
- func (c *Client) Put(url string, options HTTPOptions) (*http.Response, error)
- func (c *Client) Trace(url string, options HTTPOptions) (*http.Response, error)
- type Data
- type HTTPClient
- type HTTPData
- type HTTPOptions
- type Header
- type MockClient
- func (m *MockClient) Clear()
- func (m *MockClient) Connect(url string, options HTTPOptions) (*http.Response, error)
- func (m *MockClient) Delete(url string, options HTTPOptions) (*http.Response, error)
- func (m *MockClient) Get(url string, options HTTPOptions) (*http.Response, error)
- func (m *MockClient) Head(url string, options HTTPOptions) (*http.Response, error)
- func (m *MockClient) MapResponse(method, url string, res *http.Response, err error)
- func (m *MockClient) Options(url string, options HTTPOptions) (*http.Response, error)
- func (m *MockClient) Patch(url string, options HTTPOptions) (*http.Response, error)
- func (m *MockClient) Post(url string, options HTTPOptions) (*http.Response, error)
- func (m *MockClient) Put(url string, options HTTPOptions) (*http.Response, error)
- func (m *MockClient) Trace(url string, options HTTPOptions) (*http.Response, error)
- type Options
- type QueryParam
- type RequestError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetBasicAuth ¶
SetBasicAuth applies Basic Authentication credentials to the provided http.Request if auth is not nil.
func SetCookies ¶
SetCookies applies a list of http.Cookie pointers to the provided http.Request.
func SetHeaders ¶
SetHeaders applies a list of HTTPData headers to the provided http.Request.
Types ¶
type Call ¶
type Call struct {
Method string
URL string
Options HTTPOptions
}
Call represents the arguments of an HTTP call recorded by the MockClient.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents the HTTP client implementation. It holds a default context.Context and a pointer to an http.Client.
func (*Client) Context ¶
Context returns the default context.Context associated with the Client.
type HTTPClient ¶
type HTTPClient interface {
// Get executes an HTTP GET request to the given URL with the provided [HTTPOptions].
Get(url string, options HTTPOptions) (*http.Response, error)
// Post executes an HTTP POST request to the given URL with the provided [HTTPOptions].
Post(url string, options HTTPOptions) (*http.Response, error)
// Put executes an HTTP PUT request to the given URL with the provided [HTTPOptions].
Put(url string, options HTTPOptions) (*http.Response, error)
// Patch executes an HTTP PATCH request to the given URL with the provided [HTTPOptions].
Patch(url string, options HTTPOptions) (*http.Response, error)
// Delete executes an HTTP DELETE request to the given URL with the provided [HTTPOptions].
Delete(url string, options HTTPOptions) (*http.Response, error)
// Options executes an HTTP OPTIONS request to the given URL with the provided [HTTPOptions].
Options(url string, options HTTPOptions) (*http.Response, error)
// Head executes an HTTP HEAD request to the given URL with the provided [HTTPOptions].
Head(url string, options HTTPOptions) (*http.Response, error)
// Connect executes an HTTP CONNECT request to the given URL with the provided [HTTPOptions].
Connect(url string, options HTTPOptions) (*http.Response, error)
// Trace executes an HTTP TRACE request to the given URL with the provided [HTTPOptions].
Trace(url string, options HTTPOptions) (*http.Response, error)
}
HTTPClient defines the interface for an HTTP client.
func New ¶
func New( ctx context.Context, config ...http.Client, ) HTTPClient
New initializes and returns a new HTTPClient. It accepts a context.Context and an optional variadic http.Client config.
type HTTPData ¶
type HTTPData interface {
// Set assigns a key and a value to the underlying data structure.
Set(key string, value any)
// Data retrieves the underlying [Data] representation.
Data() Data
}
HTTPData defines an interface for key-value pair data used in HTTP requests, such as headers or query parameters.
type HTTPOptions ¶
type HTTPOptions interface {
// H returns a slice of HTTPData representing the headers.
H() []HTTPData
// B returns an [io.Reader] representing the request body.
B() io.Reader
// Q returns a slice of HTTPData representing the query parameters.
Q() []HTTPData
// C returns a slice of [http.Cookie] pointers representing the cookies.
C() []*http.Cookie
// Ctx returns the [context.Context] for managing request lifecycles.
Ctx() context.Context
// Auth returns the [BasicAuth] credentials for the request.
Auth() *BasicAuth
}
HTTPOptions defines the interface for HTTP request options, providing methods to retrieve headers, the request body, query parameters, cookies, context, and auth.
type MockClient ¶
type MockClient struct {
Calls []Call
DefaultResponse *http.Response
DefaultErr error
// contains filtered or unexported fields
}
MockClient is a mock implementation of the HTTPClient interface. It records all calls in a history slice and allows O(1) response mapping.
func NewMockClient ¶
func NewMockClient(defaultRes *http.Response, defaultErr error) *MockClient
NewMockClient returns a new instance of MockClient. If a default response or error is provided, it will be used when no specific mapping exists.
func (*MockClient) Clear ¶
func (m *MockClient) Clear()
Clear resets the history of calls recorded by the MockClient.
func (*MockClient) Connect ¶
func (m *MockClient) Connect(url string, options HTTPOptions) (*http.Response, error)
func (*MockClient) Delete ¶
func (m *MockClient) Delete(url string, options HTTPOptions) (*http.Response, error)
func (*MockClient) Get ¶
func (m *MockClient) Get(url string, options HTTPOptions) (*http.Response, error)
func (*MockClient) Head ¶
func (m *MockClient) Head(url string, options HTTPOptions) (*http.Response, error)
func (*MockClient) MapResponse ¶
func (m *MockClient) MapResponse(method, url string, res *http.Response, err error)
MapResponse sets a specific response for a method and URL combination (O(1) lookup).
func (*MockClient) Options ¶
func (m *MockClient) Options(url string, options HTTPOptions) (*http.Response, error)
func (*MockClient) Patch ¶
func (m *MockClient) Patch(url string, options HTTPOptions) (*http.Response, error)
func (*MockClient) Post ¶
func (m *MockClient) Post(url string, options HTTPOptions) (*http.Response, error)
func (*MockClient) Put ¶
func (m *MockClient) Put(url string, options HTTPOptions) (*http.Response, error)
func (*MockClient) Trace ¶
func (m *MockClient) Trace(url string, options HTTPOptions) (*http.Response, error)
type Options ¶
type Options struct {
Headers []HTTPData
Body io.Reader
QueryParams []HTTPData
Cookies []*http.Cookie
Context context.Context
BasicAuth *BasicAuth
}
Options implements the HTTPOptions interface and holds headers, the request body, query parameters, cookies, context, and authentication.
func (*Options) Ctx ¶
Ctx returns the context defined in the Options struct. If none is set, it returns nil.
type QueryParam ¶
type QueryParam = Data
QueryParam is an alias for Data representing an HTTP query parameter.
type RequestError ¶
RequestError represents an error that occurred during an HTTP request. It contains the HTTP status code and a payload holding the error details.
func NewRequestError ¶
func NewRequestError(err error) *RequestError
NewRequestError creates a new RequestError from a standard Go error. The provided error's message is converted into the RequestError payload.
func (*RequestError) Error ¶
func (err *RequestError) Error() string
Error implements the standard error interface. If the payload holds a valid JSON object, it returns its formatted representation. Otherwise (e.g. a transport-level error message), it returns the raw payload content so the original message is preserved.
func (*RequestError) JSON ¶
func (err *RequestError) JSON() (payload xjson.JSON)
JSON reads the error Payload and decodes it into an xjson.JSON object.