xhttp

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewData

func NewData(d HTTPData, key string, value any)

NewData initializes or updates an HTTPData instance with the provided key and value.

func SetBasicAuth

func SetBasicAuth(req *http.Request, auth *BasicAuth)

SetBasicAuth applies Basic Authentication credentials to the provided http.Request if auth is not nil.

func SetCookies

func SetCookies(req *http.Request, cookies []*http.Cookie)

SetCookies applies a list of http.Cookie pointers to the provided http.Request.

func SetHeaders

func SetHeaders(req *http.Request, headers []HTTPData)

SetHeaders applies a list of HTTPData headers to the provided http.Request.

func SetQueryParams

func SetQueryParams(
	url string,
	queryParams ...HTTPData,
) string

SetQueryParams appends a list of HTTPData query parameters to the given URL string. It properly escapes the parameter keys and values and returns the complete URL string.

Types

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

BasicAuth holds the credentials for HTTP Basic Authentication.

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

func (c *Client) Connect(url string, options HTTPOptions) (*http.Response, error)

Connect executes an HTTP CONNECT request using the underlying Client.

func (*Client) Context

func (c *Client) Context() context.Context

Context returns the default context.Context associated with the Client.

func (*Client) Delete

func (c *Client) Delete(url string, options HTTPOptions) (*http.Response, error)

Delete executes an HTTP DELETE request using the underlying Client.

func (*Client) Get

func (c *Client) Get(url string, options HTTPOptions) (*http.Response, error)

Get executes an HTTP GET request using the underlying Client.

func (*Client) Head

func (c *Client) Head(url string, options HTTPOptions) (*http.Response, error)

Head executes an HTTP HEAD request using the underlying Client.

func (*Client) Options

func (c *Client) Options(url string, options HTTPOptions) (*http.Response, error)

Options executes an HTTP OPTIONS request using the underlying Client.

func (*Client) Patch

func (c *Client) Patch(url string, options HTTPOptions) (*http.Response, error)

Patch executes an HTTP PATCH request using the underlying Client.

func (*Client) Post

func (c *Client) Post(url string, options HTTPOptions) (*http.Response, error)

Post executes an HTTP POST request using the underlying Client.

func (*Client) Put

func (c *Client) Put(url string, options HTTPOptions) (*http.Response, error)

Put executes an HTTP PUT request using the underlying Client.

func (*Client) Trace

func (c *Client) Trace(url string, options HTTPOptions) (*http.Response, error)

Trace executes an HTTP TRACE request using the underlying Client.

type Data

type Data struct {
	Key   string
	Value any
}

Data represents a basic key-value pair used for headers and query parameters.

func (*Data) Data

func (d *Data) Data() Data

Data returns a copy of the Data struct.

func (*Data) Set

func (d *Data) Set(key string, value any)

Set assigns the given key and value to the Data instance.

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 Header = Data

Header is an alias for Data representing an HTTP header.

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

func (o *Options) Auth() *BasicAuth

Auth returns the BasicAuth credentials defined in the Options struct.

func (*Options) B

func (o *Options) B() io.Reader

B returns the request body defined in the Options struct.

func (*Options) C

func (o *Options) C() []*http.Cookie

C returns the cookies defined in the Options struct.

func (*Options) Ctx

func (o *Options) Ctx() context.Context

Ctx returns the context defined in the Options struct. If none is set, it returns nil.

func (*Options) H

func (o *Options) H() []HTTPData

H returns the headers defined in the Options struct.

func (*Options) Q

func (o *Options) Q() []HTTPData

Q returns the query parameters defined in the Options struct.

type QueryParam

type QueryParam = Data

QueryParam is an alias for Data representing an HTTP query parameter.

type RequestError

type RequestError struct {
	StatusCode int
	Payload    io.Reader
}

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.

Directories

Path Synopsis
Package xjson provides utility functions and types for handling JSON data.
Package xjson provides utility functions and types for handling JSON data.

Jump to

Keyboard shortcuts

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