httpbin

package
v2.3.0-fork Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2022 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxBodySize int64 = 1024 * 1024
	DefaultMaxDuration       = 10 * time.Second
)

Default configuration values

View Source
const Base64MaxLen = 2000

Base64MaxLen - Maximum input length for Base64 functions

Variables

View Source
var DefaultDefaultParams = DefaultParams{
	DripDuration: 2 * time.Second,
	DripDelay:    2 * time.Second,
	DripNumBytes: 10,
}

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 {
	DripDuration time.Duration
	DripDelay    time.Duration
	DripNumBytes int64
}

DefaultParams defines default parameter values

type HTTPBin

type HTTPBin struct {
	// Max size of an incoming request 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
}

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) 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, r *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) ETag

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

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

func (*HTTPBin) FormsPost

func (h *HTTPBin) FormsPost(w http.ResponseWriter, r *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, r *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) 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, r *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, r *http.Request)

Robots renders a basic robots.txt file

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, r *http.Request)

UTF8 renders an HTML encoding stress test

func (*HTTPBin) UUID

func (h *HTTPBin) UUID(w http.ResponseWriter, r *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) XML

func (h *HTTPBin) XML(w http.ResponseWriter, r *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 WithDefaultParams

func WithDefaultParams(defaultParams DefaultParams) OptionFunc

WithDefaultParams sets the default params handlers will use

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

type Result

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

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

Directories

Path Synopsis
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.

Jump to

Keyboard shortcuts

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