httpbin

package
v2.13.4 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: MIT Imports: 29 Imported by: 4

Documentation

Overview

Package httpbin provides a simple HTTP request and response testing server, modeled on the original httpbin.org Python project.

Index

Constants

View Source
const (
	DefaultMaxBodySize int64 = 1024 * 1024
	DefaultMaxDuration       = 10 * time.Second
	DefaultHostname          = "go-httpbin"
)

Default configuration values

Variables

View Source
var DefaultDefaultParams = DefaultParams{
	DripDuration: 2 * time.Second,
	DripDelay:    2 * time.Second,
	DripNumBytes: 10,
	SSECount:     10,
	SSEDuration:  5 * time.Second,
	SSEDelay:     0,
}

DefaultDefaultParams defines the DefaultParams that are used by default. In general, these should match the original httpbin.org's defaults.

Functions

This section is empty.

Types

type DefaultParams

type DefaultParams struct {
	// for the /drip endpoint
	DripDuration time.Duration
	DripDelay    time.Duration
	DripNumBytes int64

	// for the /sse endpoint
	SSECount    int
	SSEDuration time.Duration
	SSEDelay    time.Duration
}

DefaultParams defines default parameter values

type HTTPBin

type HTTPBin struct {
	// Max size of an incoming request or generated response body, in bytes
	MaxBodySize int64

	// Max duration of a request, for those requests that allow user control
	// over timing (e.g. /delay)
	MaxDuration time.Duration

	// Observer called with the result of each handled request
	Observer Observer

	// Default parameter values
	DefaultParams DefaultParams

	// Set of hosts to which the /redirect-to endpoint will allow redirects
	AllowedRedirectDomains map[string]struct{}
	// contains filtered or unexported fields
}

HTTPBin contains the business logic

func New

func New(opts ...OptionFunc) *HTTPBin

New creates a new HTTPBin instance

func (*HTTPBin) AbsoluteRedirect

func (h *HTTPBin) AbsoluteRedirect(w http.ResponseWriter, r *http.Request)

AbsoluteRedirect responds with an HTTP 302 redirect a given number of times

func (*HTTPBin) Anything added in v2.5.0

func (h *HTTPBin) Anything(w http.ResponseWriter, r *http.Request)

Anything returns anything that is passed to request.

func (*HTTPBin) Base64

func (h *HTTPBin) Base64(w http.ResponseWriter, r *http.Request)

Base64 - encodes/decodes input data

func (*HTTPBin) BasicAuth

func (h *HTTPBin) BasicAuth(w http.ResponseWriter, r *http.Request)

BasicAuth requires basic authentication

func (*HTTPBin) Bearer

func (h *HTTPBin) Bearer(w http.ResponseWriter, r *http.Request)

Bearer - Prompts the user for authorization using bearer authentication.

func (*HTTPBin) Bytes

func (h *HTTPBin) Bytes(w http.ResponseWriter, r *http.Request)

Bytes returns N random bytes generated with an optional seed

func (*HTTPBin) Cache

func (h *HTTPBin) Cache(w http.ResponseWriter, r *http.Request)

Cache returns a 304 if an If-Modified-Since or an If-None-Match header is present, otherwise returns the same response as Get.

func (*HTTPBin) CacheControl

func (h *HTTPBin) CacheControl(w http.ResponseWriter, r *http.Request)

CacheControl sets a Cache-Control header for N seconds for /cache/N requests

func (*HTTPBin) Cookies

func (h *HTTPBin) Cookies(w http.ResponseWriter, r *http.Request)

Cookies responds with the cookies in the incoming request

func (*HTTPBin) Deflate

func (h *HTTPBin) Deflate(w http.ResponseWriter, r *http.Request)

Deflate returns a gzipped response

func (*HTTPBin) Delay

func (h *HTTPBin) Delay(w http.ResponseWriter, r *http.Request)

Delay waits for a given amount of time before responding, where the time may be specified as a golang-style duration or seconds in floating point.

func (*HTTPBin) DeleteCookies

func (h *HTTPBin) DeleteCookies(w http.ResponseWriter, r *http.Request)

DeleteCookies deletes cookies specified in query params and redirects to Cookies endpoint

func (*HTTPBin) Deny

func (h *HTTPBin) Deny(w http.ResponseWriter, _ *http.Request)

Deny renders a basic page that robots should never access

func (*HTTPBin) DigestAuth

func (h *HTTPBin) DigestAuth(w http.ResponseWriter, r *http.Request)

DigestAuth handles a simple implementation of HTTP Digest Authentication, which supports the "auth" QOP and the MD5 and SHA-256 crypto algorithms.

/digest-auth/<qop>/<user>/<passwd> /digest-auth/<qop>/<user>/<passwd>/<algorithm>

func (*HTTPBin) Drip

func (h *HTTPBin) Drip(w http.ResponseWriter, r *http.Request)

Drip returns data over a duration after an optional initial delay, then (optionally) returns with the given status code.

func (*HTTPBin) DumpRequest added in v2.5.5

func (h *HTTPBin) DumpRequest(w http.ResponseWriter, r *http.Request)

DumpRequest - returns the given request in its HTTP/1.x wire representation. The returned representation is an approximation only; some details of the initial request are lost while parsing it into an http.Request. In particular, the order and case of header field names are lost.

func (*HTTPBin) ETag

func (h *HTTPBin) ETag(w http.ResponseWriter, r *http.Request)

ETag assumes the resource has the given etag and responds to If-None-Match and If-Match headers appropriately.

func (*HTTPBin) FormsPost

func (h *HTTPBin) FormsPost(w http.ResponseWriter, _ *http.Request)

FormsPost renders an HTML form that submits a request to the /post endpoint

func (*HTTPBin) Get

func (h *HTTPBin) Get(w http.ResponseWriter, r *http.Request)

Get handles HTTP GET requests

func (*HTTPBin) Gzip

func (h *HTTPBin) Gzip(w http.ResponseWriter, r *http.Request)

Gzip returns a gzipped response

func (*HTTPBin) HTML

func (h *HTTPBin) HTML(w http.ResponseWriter, _ *http.Request)

HTML renders a basic HTML page

func (*HTTPBin) Handler

func (h *HTTPBin) Handler() http.Handler

Handler returns an http.Handler that exposes all HTTPBin endpoints

func (*HTTPBin) Headers

func (h *HTTPBin) Headers(w http.ResponseWriter, r *http.Request)

Headers echoes the incoming request headers

func (*HTTPBin) HiddenBasicAuth

func (h *HTTPBin) HiddenBasicAuth(w http.ResponseWriter, r *http.Request)

HiddenBasicAuth requires HTTP Basic authentication but returns a status of 404 if the request is unauthorized

func (*HTTPBin) Hostname added in v2.4.1

func (h *HTTPBin) Hostname(w http.ResponseWriter, _ *http.Request)

Hostname - returns the hostname.

func (*HTTPBin) IP

func (h *HTTPBin) IP(w http.ResponseWriter, r *http.Request)

IP echoes the IP address of the incoming request

func (*HTTPBin) Image

func (h *HTTPBin) Image(w http.ResponseWriter, r *http.Request)

Image responds with an image of a specific kind, from /image/<kind>

func (*HTTPBin) ImageAccept

func (h *HTTPBin) ImageAccept(w http.ResponseWriter, r *http.Request)

ImageAccept responds with an appropriate image based on the Accept header

func (*HTTPBin) Index

func (h *HTTPBin) Index(w http.ResponseWriter, r *http.Request)

Index renders an HTML index page

func (*HTTPBin) JSON

func (h *HTTPBin) JSON(w http.ResponseWriter, _ *http.Request)

JSON - returns a sample json

func (h *HTTPBin) Links(w http.ResponseWriter, r *http.Request)

Links redirects to the first page in a series of N links

func (*HTTPBin) Range

func (h *HTTPBin) Range(w http.ResponseWriter, r *http.Request)

Range returns up to N bytes, with support for HTTP Range requests.

This departs from httpbin by not supporting the chunk_size or duration parameters.

func (*HTTPBin) Redirect

func (h *HTTPBin) Redirect(w http.ResponseWriter, r *http.Request)

Redirect responds with 302 redirect a given number of times. Defaults to a relative redirect, but an ?absolute=true query param will trigger an absolute redirect.

func (*HTTPBin) RedirectTo

func (h *HTTPBin) RedirectTo(w http.ResponseWriter, r *http.Request)

RedirectTo responds with a redirect to a specific URL with an optional status code, which defaults to 302

func (*HTTPBin) RelativeRedirect

func (h *HTTPBin) RelativeRedirect(w http.ResponseWriter, r *http.Request)

RelativeRedirect responds with an HTTP 302 redirect a given number of times

func (*HTTPBin) RequestWithBody

func (h *HTTPBin) RequestWithBody(w http.ResponseWriter, r *http.Request)

RequestWithBody handles POST, PUT, and PATCH requests

func (*HTTPBin) ResponseHeaders

func (h *HTTPBin) ResponseHeaders(w http.ResponseWriter, r *http.Request)

ResponseHeaders responds with a map of header values

func (*HTTPBin) Robots

func (h *HTTPBin) Robots(w http.ResponseWriter, _ *http.Request)

Robots renders a basic robots.txt file

func (*HTTPBin) SSE added in v2.13.0

func (h *HTTPBin) SSE(w http.ResponseWriter, r *http.Request)

SSE writes a stream of events over a duration after an optional initial delay.

func (*HTTPBin) ServeHTTP added in v2.5.2

func (h *HTTPBin) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implememnts the http.Handler interface.

func (*HTTPBin) SetCookies

func (h *HTTPBin) SetCookies(w http.ResponseWriter, r *http.Request)

SetCookies sets cookies as specified in query params and redirects to Cookies endpoint

func (*HTTPBin) Status

func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request)

Status responds with the specified status code. TODO: support random choice from multiple, optionally weighted status codes.

func (*HTTPBin) Stream

func (h *HTTPBin) Stream(w http.ResponseWriter, r *http.Request)

Stream responds with max(n, 100) lines of JSON-encoded request data.

func (*HTTPBin) StreamBytes

func (h *HTTPBin) StreamBytes(w http.ResponseWriter, r *http.Request)

StreamBytes streams N random bytes generated with an optional seed in chunks of a given size.

func (*HTTPBin) UTF8

func (h *HTTPBin) UTF8(w http.ResponseWriter, _ *http.Request)

UTF8 renders an HTML encoding stress test

func (*HTTPBin) UUID

func (h *HTTPBin) UUID(w http.ResponseWriter, _ *http.Request)

UUID - responds with a generated UUID

func (*HTTPBin) Unstable

func (h *HTTPBin) Unstable(w http.ResponseWriter, r *http.Request)

Unstable - returns 500, sometimes

func (*HTTPBin) UserAgent

func (h *HTTPBin) UserAgent(w http.ResponseWriter, r *http.Request)

UserAgent echoes the incoming User-Agent header

func (*HTTPBin) WebSocketEcho added in v2.13.0

func (h *HTTPBin) WebSocketEcho(w http.ResponseWriter, r *http.Request)

WebSocketEcho - simple websocket echo server, where the max fragment size and max message size can be controlled by clients.

func (*HTTPBin) XML

func (h *HTTPBin) XML(w http.ResponseWriter, _ *http.Request)

XML responds with an XML document

type Observer

type Observer func(result Result)

Observer is a function that will be called with the details of a handled request, which can be used for logging, instrumentation, etc

func StdLogObserver

func StdLogObserver(l *log.Logger) Observer

StdLogObserver creates an Observer that will log each request in structured format using the given stdlib logger

type OptionFunc

type OptionFunc func(*HTTPBin)

OptionFunc uses the "functional options" pattern to customize an HTTPBin instance

func WithAllowedRedirectDomains added in v2.5.1

func WithAllowedRedirectDomains(hosts []string) OptionFunc

WithAllowedRedirectDomains limits the domains to which the /redirect-to endpoint will redirect traffic.

func WithDefaultParams

func WithDefaultParams(defaultParams DefaultParams) OptionFunc

WithDefaultParams sets the default params handlers will use

func WithExcludeHeaders added in v2.11.0

func WithExcludeHeaders(excludeHeaders string) OptionFunc

WithExcludeHeaders sets the headers to exclude in outgoing responses, to prevent possible information leakage.

func WithHostname added in v2.4.1

func WithHostname(s string) OptionFunc

WithHostname sets the hostname to return via the /hostname endpoint.

func WithMaxBodySize

func WithMaxBodySize(m int64) OptionFunc

WithMaxBodySize sets the maximum amount of memory

func WithMaxDuration

func WithMaxDuration(d time.Duration) OptionFunc

WithMaxDuration sets the maximum amount of time httpbin may take to respond

func WithObserver

func WithObserver(o Observer) OptionFunc

WithObserver sets the request observer callback

func WithPrefix added in v2.13.0

func WithPrefix(p string) OptionFunc

WithPrefix sets the path prefix

type Result

type Result struct {
	Status    int
	Method    string
	URI       string
	Size      int64
	Duration  time.Duration
	UserAgent string
	ClientIP  string
}

Result is the result of handling a request, used for instrumentation

Directories

Path Synopsis
Package cmd implements the go-httpbin command line interface as a testable package.
Package cmd implements the go-httpbin command line interface as a testable package.
Package digest provides a limited implementation of HTTP Digest Authentication, as defined in RFC 2617.
Package digest provides a limited implementation of HTTP Digest Authentication, as defined in RFC 2617.
Package websocket implements a basic websocket server.
Package websocket implements a basic websocket server.

Jump to

Keyboard shortcuts

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