http

package
v0.2.16 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

This package provides an interface to functions and structs in the standard net/http package when using it as an http client to facilitate mocking.

Index

Constants

View Source
const (
	MethodGet     = http.MethodGet
	MethodHead    = http.MethodHead
	MethodPost    = http.MethodPost
	MethodPut     = http.MethodPut
	MethodPatch   = http.MethodPatch
	MethodDelete  = http.MethodDelete
	MethodConnect = http.MethodConnect
	MethodOptions = http.MethodOptions
	MethodTrace   = http.MethodTrace
)
View Source
const (
	StatusContinue           = http.StatusContinue
	StatusSwitchingProtocols = http.StatusSwitchingProtocols
	StatusProcessing         = http.StatusProcessing
	StatusEarlyHints         = http.StatusEarlyHints

	StatusOK                   = http.StatusOK
	StatusCreated              = http.StatusCreated
	StatusAccepted             = http.StatusAccepted
	StatusNonAuthoritativeInfo = http.StatusNonAuthoritativeInfo
	StatusNoContent            = http.StatusNoContent
	StatusResetContent         = http.StatusResetContent
	StatusPartialContent       = http.StatusPartialContent
	StatusMultiStatus          = http.StatusMultiStatus
	StatusAlreadyReported      = http.StatusAlreadyReported
	StatusIMUsed               = http.StatusIMUsed

	StatusMultipleChoices  = http.StatusMultipleChoices
	StatusMovedPermanently = http.StatusMovedPermanently
	StatusFound            = http.StatusFound
	StatusSeeOther         = http.StatusSeeOther
	StatusNotModified      = http.StatusNotModified
	StatusUseProxy         = http.StatusUseProxy

	StatusTemporaryRedirect = http.StatusTemporaryRedirect
	StatusPermanentRedirect = http.StatusPermanentRedirect

	StatusBadRequest                   = http.StatusBadRequest
	StatusUnauthorized                 = http.StatusUnauthorized
	StatusPaymentRequired              = http.StatusPaymentRequired
	StatusForbidden                    = http.StatusForbidden
	StatusNotFound                     = http.StatusNotFound
	StatusMethodNotAllowed             = http.StatusMethodNotAllowed
	StatusNotAcceptable                = http.StatusNotAcceptable
	StatusProxyAuthRequired            = http.StatusProxyAuthRequired
	StatusRequestTimeout               = http.StatusRequestTimeout
	StatusConflict                     = http.StatusConflict
	StatusGone                         = http.StatusGone
	StatusLengthRequired               = http.StatusLengthRequired
	StatusPreconditionFailed           = http.StatusPreconditionFailed
	StatusRequestEntityTooLarge        = http.StatusRequestEntityTooLarge
	StatusRequestURITooLong            = http.StatusRequestURITooLong
	StatusUnsupportedMediaType         = http.StatusUnsupportedMediaType
	StatusRequestedRangeNotSatisfiable = http.StatusRequestedRangeNotSatisfiable
	StatusExpectationFailed            = http.StatusExpectationFailed
	StatusTeapot                       = http.StatusTeapot
	StatusMisdirectedRequest           = http.StatusMisdirectedRequest
	StatusUnprocessableEntity          = http.StatusUnprocessableEntity
	StatusLocked                       = http.StatusLocked
	StatusFailedDependency             = http.StatusFailedDependency
	StatusTooEarly                     = http.StatusTooEarly
	StatusUpgradeRequired              = http.StatusUpgradeRequired
	StatusPreconditionRequired         = http.StatusPreconditionRequired
	StatusTooManyRequests              = http.StatusTooManyRequests
	StatusRequestHeaderFieldsTooLarge  = http.StatusRequestHeaderFieldsTooLarge
	StatusUnavailableForLegalReasons   = http.StatusUnavailableForLegalReasons

	StatusInternalServerError           = http.StatusInternalServerError
	StatusNotImplemented                = http.StatusNotImplemented
	StatusBadGateway                    = http.StatusBadGateway
	StatusServiceUnavailable            = http.StatusServiceUnavailable
	StatusGatewayTimeout                = http.StatusGatewayTimeout
	StatusHTTPVersionNotSupported       = http.StatusHTTPVersionNotSupported
	StatusVariantAlsoNegotiates         = http.StatusVariantAlsoNegotiates
	StatusInsufficientStorage           = http.StatusInsufficientStorage
	StatusLoopDetected                  = http.StatusLoopDetected
	StatusNotExtended                   = http.StatusNotExtended
	StatusNetworkAuthenticationRequired = http.StatusNetworkAuthenticationRequired
)
View Source
const (
	DefaultMaxHeaderBytes      = http.DefaultMaxHeaderBytes
	DefaultMaxIdleConnsPerHost = http.DefaultMaxIdleConnsPerHost
	TimeFormat                 = http.TimeFormat
	TrailerPrefix              = http.TrailerPrefix
)

Variables

View Source
var (
	ErrNotSupported         = http.ErrNotSupported
	ErrUnexpectedTrailer    = http.ErrUnexpectedTrailer // Deprecated
	ErrMissingBoundary      = http.ErrMissingBoundary
	ErrNotMultipart         = http.ErrNotMultipart
	ErrHeaderTooLong        = http.ErrHeaderTooLong        // Deprecated
	ErrShortBody            = http.ErrShortBody            // Deprecated
	ErrMissingContentLength = http.ErrMissingContentLength // Deprecated

	ErrBodyNotAllowed  = http.ErrBodyNotAllowed
	ErrHijacked        = http.ErrHijacked
	ErrContentLength   = http.ErrContentLength
	ErrWriteAfterFlush = http.ErrWriteAfterFlush // Deprecated

	ServerContextKey    = http.ServerContextKey
	LocalAddrContextKey = http.LocalAddrContextKey

	DefaultClient         = http.DefaultClient
	DefaultServeMux       = http.DefaultServeMux
	ErrAbortHandler       = http.ErrAbortHandler
	ErrBodyReadAfterClose = http.ErrBodyReadAfterClose
	ErrHandlerTimeout     = http.ErrHandlerTimeout
	ErrLineTooLong        = http.ErrLineTooLong
	ErrMissingFile        = http.ErrMissingFile
	ErrNoCookie           = http.ErrNoCookie
	ErrNoLocation         = http.ErrNoLocation
	ErrSchemeMismatch     = http.ErrSchemeMismatch
	ErrServerClosed       = http.ErrServerClosed
	ErrSkipAltProtocol    = http.ErrSkipAltProtocol
	ErrUseLastResponse    = http.ErrUseLastResponse
	NoBody                = http.NoBody
)
View Source
var WithJar = WithCookieJar

WithJar is just an alias to WithCookieJar

Functions

This section is empty.

Types

type CheckRedirectFn

type CheckRedirectFn = func(*http.Request, []*http.Request) error

type Client

type Client interface {
	CloseIdleConnections()
	Do(Request) (Response, error)
	Get(string) (Response, error)
	Head(string) (Response, error)
	Post(string, string, io.Reader) (Response, error)
	PostForm(string, url.Values) (Response, error)
	GetTransport() RoundTripper
}

Client is an interface for the net/http.Client struct

func WrapClient added in v0.2.10

func WrapClient(cl *http.Client) Client

type ClientOption

type ClientOption func(cl *http.Client)

ClientOption allows you to set options on a client in the NewClient constructor

func WithCheckRedirect

func WithCheckRedirect(f func(*http.Request, []*http.Request) error) ClientOption

Set the net/http.Client CheckRedirect

func WithCookieJar

func WithCookieJar(j CookieJar) ClientOption

Set the net/http.Client CookieJar

func WithTimeout

func WithTimeout(t time.Duration) ClientOption

Set the net/http.Client Timeout

func WithTransport

func WithTransport(t RoundTripper) ClientOption

Set the net/http.Client Transport

type ConnState

type ConnState = http.ConnState
type Cookie = http.Cookie

type CookieJar

type CookieJar = http.CookieJar

type HTTP

type HTTP interface {
	// Client constructors:
	NewClient(options ...ClientOption) Client

	// Request constructors:
	NewRequest(method, url string, body io.Reader, options ...RequestOption) (Request, error)
	NewRequestWithContext(ctx context.Context, method, url string, body io.Reader, options ...RequestOption) (Request, error)

	// Response constructors:
	Get(string) (Response, error)
	Head(string) (Response, error)
	Post(string, string, io.Reader) (Response, error)
	PostForm(string, url.Values) (Response, error)
	ReadResponse(*bufio.Reader, *http.Request) (Response, error)

	CanonicalHeaderKey(string) string
	StatusText(int) string
}

HTTP is an interface for the functions in the net/http package

func NewHTTP

func NewHTTP() HTTP

NewHTTP creates a new HTTP instance.

type HTTP2Config added in v0.0.6

type HTTP2Config = http.HTTP2Config
type Header = http.Header

type Protocols added in v0.0.6

type Protocols = http.Protocols

type Request

type Request interface {
	Write(io.Writer) error
	WriteProxy(io.Writer) error

	RealRequest() *http.Request
}

Request provides an interface to a net/http.Request for use on a client making an http request.

func ReadRequest

func ReadRequest(r *bufio.Reader) (Request, error)

ReadRequest is a "constructor" that constructs a Request from a Reader

type RequestOption

type RequestOption func(req *http.Request)

RequestOption allows you to set options on a request in the NewRequest constructor

func WithBody

func WithBody(r io.ReadCloser) RequestOption

Set the net/http.Request Body

func WithClose

func WithClose(v bool) RequestOption

Set the net/http.Request Close

func WithContentLength

func WithContentLength(l int64) RequestOption

Set the net/http.Request ContentLength

func WithForm

func WithForm(form url.Values) RequestOption

Set the net/http.Request Form

func WithGetBody

func WithGetBody(f func() (io.ReadCloser, error)) RequestOption

Set the net/http.Request GetBody

func WithHeader

func WithHeader(name string, values ...string) RequestOption

Set a an individual net/http.Request Header

func WithHeaders

func WithHeaders(headers Header) RequestOption

Set the net/http.Request Header map

func WithHost

func WithHost(host string) RequestOption

Set the net/http.Request Host

func WithMultipartForm

func WithMultipartForm(form *multipart.Form) RequestOption

Set the net/http.Request MultipartForm

func WithPostForm

func WithPostForm(form url.Values) RequestOption

Set the net/http.Request PostForm

func WithRemoteAddr

func WithRemoteAddr(addr string) RequestOption

Set the net/http.RequestForm RemoteAddr

func WithRequestURI

func WithRequestURI(uri string) RequestOption

Set the net/http.RequestForm RequestURI

func WithTrailer

func WithTrailer(t Header) RequestOption

Set the net/http.RequestForm Trailer

func WithTransferEncoding

func WithTransferEncoding(encodings []string) RequestOption

Set the net/http.Request TransferEncoding

type Response

type Response interface {
	// Simple pass-through methods:
	Cookies() []*Cookie
	Location() (*url.URL, error)
	ProtoAtLeast(int, int) bool
	Write(io.Writer) error

	// Methods allowing access to member variables:
	Status() string
	StatusCode() int
	Proto() string
	ProtoMajor() int
	ProtoMinor() int
	Header() Header
	Body() io.ReadCloser
	ContentLength() int64
	TransferEncoding() []string
	Close() bool
	Uncompressed() bool
	Trailer() Header
	Request() Request
	TLS() *tls.ConnectionState
}

Response is an interface to a Response received by the client.

type RoundTripper

type RoundTripper = http.RoundTripper

type SameSite

type SameSite = http.SameSite

type Transport added in v0.2.12

type Transport = http.Transport

Jump to

Keyboard shortcuts

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