httpClient

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMaxResponseSize is the maximum size of response body to read (10MB)
	DefaultMaxResponseSize = 10 * 1024 * 1024
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPResponse

type HTTPResponse struct {
	StatusCode int
	Body       []byte
	Headers    http.Header
}

HTTPResponse stores the response from a request, including the payload and original response.

type HTTPService

type HTTPService interface {
	Get(ctx context.Context, url string, headers map[string]string) (*HTTPResponse, error)
	Post(ctx context.Context, url string, headers map[string]string, body string) (*HTTPResponse, error)
	Put(ctx context.Context, url string, headers map[string]string, body string) (*HTTPResponse, error)
	Patch(ctx context.Context, url string, headers map[string]string, body string) (*HTTPResponse, error)
	Delete(ctx context.Context, url string, headers map[string]string) (*HTTPResponse, error)
}

HTTPService defines the interface for HTTP operations. This is the low-level HTTP layer that handles raw HTTP requests.

func NewHTTPService added in v1.2.0

func NewHTTPService(timeout time.Duration, maxRetry int, logger *slog.Logger) HTTPService

NewHTTPService creates a new HTTPService with the specified configuration.

type MockCall added in v1.2.0

type MockCall struct {
	Method  string
	URL     string
	Headers map[string]string
	Body    string
}

MockCall represents a single call to the mock service

type MockHTTPService added in v1.2.0

type MockHTTPService struct {
	// Response to return for all requests
	Response *HTTPResponse
	// Error to return for all requests
	Error error

	// Method-specific responses (takes precedence over Response)
	GetResponse    *HTTPResponse
	GetError       error
	PostResponse   *HTTPResponse
	PostError      error
	PutResponse    *HTTPResponse
	PutError       error
	PatchResponse  *HTTPResponse
	PatchError     error
	DeleteResponse *HTTPResponse
	DeleteError    error

	// Capture the last request for assertions
	LastMethod  string
	LastURL     string
	LastHeaders map[string]string
	LastBody    string
	CallCount   int
	CallHistory []MockCall
}

MockHTTPService is a mock implementation of HTTPService for testing

func NewMockHTTPService added in v1.2.0

func NewMockHTTPService() *MockHTTPService

NewMockHTTPService creates a new mock HTTP service

func (*MockHTTPService) Delete added in v1.2.0

func (m *MockHTTPService) Delete(ctx context.Context, url string, headers map[string]string) (*HTTPResponse, error)

Delete implements HTTPService.Delete

func (*MockHTTPService) Get added in v1.2.0

func (m *MockHTTPService) Get(ctx context.Context, url string, headers map[string]string) (*HTTPResponse, error)

Get implements HTTPService.Get

func (*MockHTTPService) Patch added in v1.2.0

func (m *MockHTTPService) Patch(ctx context.Context, url string, headers map[string]string, body string) (*HTTPResponse, error)

Patch implements HTTPService.Patch

func (*MockHTTPService) Post added in v1.2.0

func (m *MockHTTPService) Post(ctx context.Context, url string, headers map[string]string, body string) (*HTTPResponse, error)

Post implements HTTPService.Post

func (*MockHTTPService) Put added in v1.2.0

func (m *MockHTTPService) Put(ctx context.Context, url string, headers map[string]string, body string) (*HTTPResponse, error)

Put implements HTTPService.Put

func (*MockHTTPService) Reset added in v1.2.0

func (m *MockHTTPService) Reset()

Reset clears all recorded calls and responses

func (*MockHTTPService) WithError added in v1.2.0

func (m *MockHTTPService) WithError(err error) *MockHTTPService

WithError sets the default error for all methods

func (*MockHTTPService) WithGetResponse added in v1.2.0

func (m *MockHTTPService) WithGetResponse(statusCode int, body string) *MockHTTPService

WithGetResponse sets a specific response for GET requests

func (*MockHTTPService) WithPostResponse added in v1.2.0

func (m *MockHTTPService) WithPostResponse(statusCode int, body string) *MockHTTPService

WithPostResponse sets a specific response for POST requests

func (*MockHTTPService) WithResponse added in v1.2.0

func (m *MockHTTPService) WithResponse(statusCode int, body string) *MockHTTPService

WithResponse sets the default response for all methods

Jump to

Keyboard shortcuts

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