httpdriver

package
v3.3.6 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: ISC Imports: 9 Imported by: 5

Documentation

Overview

Package httpdriver provides interfaces and implementations of a simple HTTP client.

Index

Constants

View Source
const NoContent = 204

NoContent is the status code for HTTP 204, or http.StatusNoContent.

Variables

This section is empty.

Functions

func ExpectMockRequest added in v3.2.0

func ExpectMockRequest(expected *MockRequest, gotAny Request) error

ExpectMockRequest asserts that the given request is a mock request that matches what is expected. The given request for got must be of type *MockRequest.

func OptHeader

func OptHeader(from Response) http.Header

OptHeader returns the response header, or nil if from is nil.

Types

type Client

type Client interface {
	NewRequest(ctx context.Context, method, url string) (Request, error)
	Do(req Request) (Response, error)
}

Client is a generic interface used as an adapter allowing for custom HTTP client implementations, such as fasthttp.

func NewClient

func NewClient() Client

NewClient creates a new client around the standard library's http.Client. The client will have a timeout of 10 seconds.

func WrapClient

func WrapClient(client http.Client) Client

WrapClient wraps around the standard library's http.Client and returns an implementation that's compatible with the Client driver interface.

type DefaultClient

type DefaultClient http.Client

DefaultClient implements Client and wraps around the stdlib Client.

func (DefaultClient) Do

func (d DefaultClient) Do(req Request) (Response, error)

func (DefaultClient) NewRequest

func (d DefaultClient) NewRequest(ctx context.Context, method, url string) (Request, error)

type DefaultRequest

type DefaultRequest http.Request

DefaultRequest wraps around the stdlib Request and satisfies the Request interface.

func (*DefaultRequest) AddHeader

func (r *DefaultRequest) AddHeader(header http.Header)

func (*DefaultRequest) AddQuery

func (r *DefaultRequest) AddQuery(values url.Values)

func (*DefaultRequest) GetContext

func (r *DefaultRequest) GetContext() context.Context

func (*DefaultRequest) GetPath

func (r *DefaultRequest) GetPath() string

func (*DefaultRequest) WithBody

func (r *DefaultRequest) WithBody(body io.ReadCloser)

type DefaultResponse

type DefaultResponse http.Response

DefaultResponse wraps around the stdlib Response and satisfies the Response interface.

func (*DefaultResponse) GetBody

func (r *DefaultResponse) GetBody() io.ReadCloser

func (*DefaultResponse) GetHeader

func (r *DefaultResponse) GetHeader() http.Header

func (*DefaultResponse) GetStatus

func (r *DefaultResponse) GetStatus() int

type MockRequest added in v3.2.0

type MockRequest struct {
	Method string
	URL    url.URL
	Header http.Header
	Body   []byte
	// contains filtered or unexported fields
}

MockRequest is a mock request. It implements the Request interface.

func NewMockRequest added in v3.2.0

func NewMockRequest(method, urlstr string, header http.Header, jsonBody interface{}) *MockRequest

NewMockRequest creates a new mock request. If any of the given parameters cause an error, the function will panic.

func NewMockRequestWithContext added in v3.2.0

func NewMockRequestWithContext(ctx context.Context, method, urlstr string, header http.Header, jsonBody interface{}) *MockRequest

NewMockRequestWithContext creates a new mock request with context. If any of the given parameters cause an error, the function will panic.

func (*MockRequest) AddHeader added in v3.2.0

func (r *MockRequest) AddHeader(h http.Header)

func (*MockRequest) AddQuery added in v3.2.0

func (r *MockRequest) AddQuery(v url.Values)

func (*MockRequest) GetContext added in v3.2.0

func (r *MockRequest) GetContext() context.Context

func (*MockRequest) GetPath added in v3.2.0

func (r *MockRequest) GetPath() string

func (*MockRequest) ToHTTPRequest added in v3.2.0

func (r *MockRequest) ToHTTPRequest() *http.Request

ToHTTPRequest converts a mock request to a net/http request. If an error occurs, the function will panic.

func (*MockRequest) WithBody added in v3.2.0

func (r *MockRequest) WithBody(body io.ReadCloser)

type MockResponse added in v3.2.0

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

MockResponse is a mock response. It implements the Response interface.

func NewMockResponse added in v3.2.0

func NewMockResponse(code int, h http.Header, jsonBody interface{}) *MockResponse

NewMockResponse creates a new mock response.

func (*MockResponse) GetBody added in v3.2.0

func (r *MockResponse) GetBody() io.ReadCloser

func (*MockResponse) GetHeader added in v3.2.0

func (r *MockResponse) GetHeader() http.Header

func (*MockResponse) GetStatus added in v3.2.0

func (r *MockResponse) GetStatus() int

type Request

type Request interface {
	// GetPath should return the URL path, for example "/endpoint/thing".
	GetPath() string
	// GetContext should return the same context that's passed into NewRequest.
	// For implementations that don't support this, it can remove a
	// context.Background().
	GetContext() context.Context
	// AddHeader appends headers.
	AddHeader(http.Header)
	// AddQuery appends URL query values.
	AddQuery(url.Values)
	// WithBody should automatically close the ReadCloser on finish. This is
	// similar to the stdlib's Request behavior.
	WithBody(io.ReadCloser)
}

Request is a generic interface for a normal HTTP request. It should be constructed using (Requester).NewRequest().

type Response

type Response interface {
	GetStatus() int
	GetHeader() http.Header
	// Body's ReadCloser will always be closed when done, unless DoContext()
	// returns an error.
	GetBody() io.ReadCloser
}

Response is returned from (Requester).DoContext().

Jump to

Keyboard shortcuts

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