Documentation
¶
Overview ¶
Package client provides an HTTP client with support for various authentication schemes, cookies, proxies, TLS certificates, and response timing.
Package client provides HTTP client implementations.
Index ¶
- func GetResponseCookies(resp *http.Response) []*http.Cookie
- type Certificate
- type ClientConfig
- type HTTPDoer
- type HttpClient
- func (c *HttpClient) ClearCookies()
- func (c *HttpClient) GetCookies(urlStr string) []*http.Cookie
- func (c *HttpClient) Send(request *models.HttpRequest) (*models.HttpResponse, error)
- func (c *HttpClient) SendWithContext(ctx context.Context, request *models.HttpRequest) (*models.HttpResponse, error)
- func (c *HttpClient) SetCookies(urlStr string, cookies []*http.Cookie)
- type MockHTTPClient
- func (m *MockHTTPClient) ClearCookies()
- func (m *MockHTTPClient) GetCookies(urlStr string) []*http.Cookie
- func (m *MockHTTPClient) LastRequest() *models.HttpRequest
- func (m *MockHTTPClient) RequestCount() int
- func (m *MockHTTPClient) Reset()
- func (m *MockHTTPClient) Send(request *models.HttpRequest) (*models.HttpResponse, error)
- func (m *MockHTTPClient) SendWithContext(ctx context.Context, request *models.HttpRequest) (*models.HttpResponse, error)
- func (m *MockHTTPClient) SetCookies(urlStr string, cookies []*http.Cookie)
- func (m *MockHTTPClient) WithError(err error) *MockHTTPClient
- func (m *MockHTTPClient) WithResponse(resp *models.HttpResponse) *MockHTTPClient
- func (m *MockHTTPClient) WithResponseFunc(fn func(req *models.HttpRequest) (*models.HttpResponse, error)) *MockHTTPClient
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Certificate ¶
Certificate holds TLS certificate configuration
type ClientConfig ¶
type ClientConfig struct {
Timeout time.Duration
FollowRedirects bool
InsecureSSL bool
Proxy string
ExcludeProxy []string
RememberCookies bool
DefaultHeaders map[string]string
Certificates map[string]Certificate
}
ClientConfig holds client configuration
func DefaultConfig ¶
func DefaultConfig() *ClientConfig
DefaultConfig returns a default client configuration
type HTTPDoer ¶ added in v0.2.10
type HTTPDoer interface {
Send(request *models.HttpRequest) (*models.HttpResponse, error)
SendWithContext(ctx context.Context, request *models.HttpRequest) (*models.HttpResponse, error)
GetCookies(urlStr string) []*http.Cookie
SetCookies(urlStr string, cookies []*http.Cookie)
ClearCookies()
}
HTTPDoer defines the interface for sending HTTP requests. This abstraction enables dependency injection and easier testing.
type HttpClient ¶
type HttpClient struct {
// contains filtered or unexported fields
}
HttpClient is an HTTP client with auth support
func NewHttpClient ¶
func NewHttpClient(config *ClientConfig) (*HttpClient, error)
NewHttpClient creates a new HTTP client
func (*HttpClient) ClearCookies ¶
func (c *HttpClient) ClearCookies()
ClearCookies clears all stored cookies
func (*HttpClient) GetCookies ¶ added in v0.2.5
func (c *HttpClient) GetCookies(urlStr string) []*http.Cookie
GetCookies returns all cookies for a given URL
func (*HttpClient) Send ¶
func (c *HttpClient) Send(request *models.HttpRequest) (*models.HttpResponse, error)
Send sends an HTTP request and returns the response
func (*HttpClient) SendWithContext ¶
func (c *HttpClient) SendWithContext(ctx context.Context, request *models.HttpRequest) (*models.HttpResponse, error)
SendWithContext sends an HTTP request with context
func (*HttpClient) SetCookies ¶ added in v0.2.5
func (c *HttpClient) SetCookies(urlStr string, cookies []*http.Cookie)
SetCookies sets cookies for a given URL
type MockHTTPClient ¶ added in v0.2.10
type MockHTTPClient struct {
// Response is the response to return from Send/SendWithContext.
Response *models.HttpResponse
// Error is the error to return from Send/SendWithContext.
Error error
// Requests records all requests made to this mock.
Requests []*models.HttpRequest
// ResponseFunc allows dynamic response generation based on the request.
// If set, it takes precedence over Response/Error fields.
ResponseFunc func(req *models.HttpRequest) (*models.HttpResponse, error)
// Cookies stores cookies by URL for GetCookies/SetCookies.
Cookies map[string][]*http.Cookie
// contains filtered or unexported fields
}
MockHTTPClient is a mock implementation of HTTPDoer for testing. It allows setting up expected responses and recording request history.
func NewMockHTTPClient ¶ added in v0.2.10
func NewMockHTTPClient() *MockHTTPClient
NewMockHTTPClient creates a new MockHTTPClient.
func (*MockHTTPClient) ClearCookies ¶ added in v0.2.10
func (m *MockHTTPClient) ClearCookies()
ClearCookies removes all stored cookies.
func (*MockHTTPClient) GetCookies ¶ added in v0.2.10
func (m *MockHTTPClient) GetCookies(urlStr string) []*http.Cookie
GetCookies returns cookies for the given URL.
func (*MockHTTPClient) LastRequest ¶ added in v0.2.10
func (m *MockHTTPClient) LastRequest() *models.HttpRequest
LastRequest returns the most recent request, or nil if none recorded.
func (*MockHTTPClient) RequestCount ¶ added in v0.2.10
func (m *MockHTTPClient) RequestCount() int
RequestCount returns the number of requests recorded.
func (*MockHTTPClient) Reset ¶ added in v0.2.10
func (m *MockHTTPClient) Reset()
Reset clears all recorded requests and stored cookies.
func (*MockHTTPClient) Send ¶ added in v0.2.10
func (m *MockHTTPClient) Send(request *models.HttpRequest) (*models.HttpResponse, error)
Send sends an HTTP request and returns the mock response.
func (*MockHTTPClient) SendWithContext ¶ added in v0.2.10
func (m *MockHTTPClient) SendWithContext(ctx context.Context, request *models.HttpRequest) (*models.HttpResponse, error)
SendWithContext sends an HTTP request with context and returns the mock response.
func (*MockHTTPClient) SetCookies ¶ added in v0.2.10
func (m *MockHTTPClient) SetCookies(urlStr string, cookies []*http.Cookie)
SetCookies sets cookies for the given URL.
func (*MockHTTPClient) WithError ¶ added in v0.2.10
func (m *MockHTTPClient) WithError(err error) *MockHTTPClient
WithError sets the error to return and returns the mock for chaining.
func (*MockHTTPClient) WithResponse ¶ added in v0.2.10
func (m *MockHTTPClient) WithResponse(resp *models.HttpResponse) *MockHTTPClient
WithResponse sets the response to return and returns the mock for chaining.
func (*MockHTTPClient) WithResponseFunc ¶ added in v0.2.10
func (m *MockHTTPClient) WithResponseFunc(fn func(req *models.HttpRequest) (*models.HttpResponse, error)) *MockHTTPClient
WithResponseFunc sets a dynamic response function and returns the mock for chaining.