• Stars
    star
    459
  • Rank 94,744 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A reasonably complete and well-tested golang port of httpbin, with zero dependencies outside the go stdlib.

go-httpbin

A reasonably complete and well-tested golang port of Kenneth Reitz's httpbin service, with zero dependencies outside the go stdlib.

GoDoc Build status Coverage Docker Pulls

Usage

Docker

Docker images are published to Docker Hub:

# Run http server
$ docker run -P mccutchen/go-httpbin

# Run https server
$ docker run -e HTTPS_CERT_FILE='/tmp/server.crt' -e HTTPS_KEY_FILE='/tmp/server.key' -p 8080:8080 -v /tmp:/tmp mccutchen/go-httpbin

Standalone binary

Follow the Installation instructions to install go-httpbin as a standalone binary. (This currently requires a working Go runtime.)

Examples:

# Run http server
$ go-httpbin -host 127.0.0.1 -port 8081

# Run https server
$ openssl genrsa -out server.key 2048
$ openssl ecparam -genkey -name secp384r1 -out server.key
$ openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
$ go-httpbin -host 127.0.0.1 -port 8081 -https-cert-file ./server.crt -https-key-file ./server.key

Unit testing helper library

The github.com/mccutchen/go-httpbin/httpbin/v2 package can also be used as a library for testing an application's interactions with an upstream HTTP service, like so:

package httpbin_test

import (
	"net/http"
	"net/http/httptest"
	"os"
	"testing"
	"time"

	"github.com/mccutchen/go-httpbin/v2/httpbin"
)

func TestSlowResponse(t *testing.T) {
	app := httpbin.New()
	testServer := httptest.NewServer(app)
	defer testServer.Close()

	client := http.Client{
		Timeout: time.Duration(1 * time.Second),
	}

	_, err := client.Get(testServer.URL + "/delay/10")
	if !os.IsTimeout(err) {
		t.Fatalf("expected timeout error, got %s", err)
	}
}

Configuration

go-httpbin can be configured via either command line arguments or environment variables (or a combination of the two):

Argument Env var Documentation Default
-allowed-redirect-domains ALLOWED_REDIRECT_DOMAINS Comma-separated list of domains the /redirect-to endpoint will allow
-host HOST Host to listen on "0.0.0.0"
-https-cert-file HTTPS_CERT_FILE HTTPS Server certificate file
-https-key-file HTTPS_KEY_FILE HTTPS Server private key file
-max-body-size MAX_BODY_SIZE Maximum size of request or response, in bytes 1048576
-max-duration MAX_DURATION Maximum duration a response may take 10s
-port PORT Port to listen on 8080
-use-real-hostname USE_REAL_HOSTNAME Expose real hostname as reported by os.Hostname() in the /hostname endpoint false

Notes:

  • Command line arguments take precedence over environment variables.
  • See Production considerations for recommendations around safe configuration of public instances of go-httpbin

Installation

To add go-httpbin to an existing golang project:

go get -u github.com/mccutchen/go-httpbin/v2

To install the go-httpbin binary:

go install github.com/mccutchen/go-httpbin/v2/cmd/go-httpbin

Production considerations

Before deploying an instance of go-httpbin on your own infrastructure on the public internet, consider tuning it appropriately:

  1. Restrict the domains to which the /redirect-to endpoint will send traffic to avoid the security issues of an open redirect

    Use the -allowed-redirect-domains CLI argument or the ALLOWED_REDIRECT_DOMAINS env var to configure an appropriate allowlist.

  2. Tune per-request limits

    Because go-httpbin allows clients send arbitrary data in request bodies and control the duration some requests (e.g. /delay/60s), it's important to properly tune limits to prevent misbehaving or malicious clients from taking too many resources.

    Use the -max-body-size/MAX_BODY_SIZE and -max-duration/MAX_DURATION CLI arguments or env vars to enforce appropriate limits on each request.

  3. Decide whether to expose real hostnames in the /hostname endpoint

    By default, the /hostname endpoint serves a dummy hostname value, but it can be configured to serve the real underlying hostname (according to os.Hostname()) using the -use-real-hostname CLI argument or the USE_REAL_HOSTNAME env var to enable this functionality.

    Before enabling this, ensure that your hostnames do not reveal too much about your underlying infrastructure.

  4. Add custom instrumentation

    By default, go-httpbin logs basic information about each request. To add more detailed instrumentation (metrics, structured logging, request tracing), you'll need to wrap this package in your own code, which you can then instrument as you would any net/http server. Some examples:

Development

# local development
make
make test
make testcover
make run

# building & pushing docker images
make image
make imagepush

Motivation & prior art

I've been a longtime user of Kenneith Reitz's original httpbin.org, and wanted to write a golang port for fun and to see how far I could get using only the stdlib.

When I started this project, there were a handful of existing and incomplete golang ports, with the most promising being ahmetb/go-httpbin. This project showed me how useful it might be to have an httpbin library available for testing golang applications.

Known differences from other httpbin versions

Compared to the original:

  • No /brotli endpoint (due to lack of support in Go's stdlib)
  • The ?show_env=1 query param is ignored (i.e. no special handling of runtime environment headers)
  • Response values which may be encoded as either a string or a list of strings will always be encoded as a list of strings (e.g. request headers, query params, form values)

Compared to ahmetb/go-httpbin:

  • No dependencies on 3rd party packages
  • More complete implementation of endpoints

More Repositories

1

strftime.org

A single-serving-site that provides a reference for Python's strftime formatting options
Mustache
575
star
2

triangulizor

A scalable, fault-tolerant way to triangulize your images!
Python
51
star
3

palettor

Yet another way to extract the color palette from an image using k-means clustering
Go
16
star
4

speculatively

Package speculatively provides a simple mechanism to re-execute a task in parallel only after some initial timeout has elapsed.
Go
10
star
5

twitter-url-regexen

The regular expression(s) Twitter uses to match URLs.
Python
7
star
6

slabtype

Fancy text layout in a box
JavaScript
5
star
7

thresholderbot-standalone

An older, standalone version of Thresholderbot that you can run on your own servers.
Python
5
star
8

dycco

Another Python port of Docco, the literate-style documentation generator
Python
4
star
9

sketches

A little collection of visual experiments.
JavaScript
4
star
10

gaefab

A set of fabric tasks for Google App Engine apps
Python
3
star
11

playcanvas2obj

A quick and dirty script that attempts to convert a PlayCanvas JSON model into a (partial) Wavefront OBJ file
Python
3
star
12

funcgeo

Python and Haskell ports of Frank Buß's Lisp implementation of Peter Henderson's original idea.
Python
3
star
13

urlresolver

A golang package that resolves URLs and fetches their titles.
Go
2
star
14

appengine-simpleadmin

A simple, drop in admin backend for Google App Engine
Python
2
star
15

pickem

An NFL pick 'em pool, running on App Engine
Python
2
star
16

humortree.org

Early web experiments in programming and design (mostly 1999-2004)
HTML
2
star
17

mom-christmas-gift

A Christmas gift for my mom
Python
1
star
18

docker-python-black

Source for mccutchen/python-black docker image
Shell
1
star
19

terraform-example

An example approach for using Terraform to manage AWS resources
HCL
1
star
20

moonlight-girl-1950

A Christmas gift for my mom and my grandfather
1
star
21

reckoning

A small program designed to help a friend get appraisal jobs from a particular bank
Python
1
star
22

httpbingo.org

A public instance of https://github.com/mccutchen/go-httpbin hosted on fly.io
Go
1
star
23

wongthesis

A little app to (hopefully) help Liz with her thesis.
Python
1
star
24

watercooler

Web-based realtime multiuser chat (final project for CS 329E)
Python
1
star
25

WoodersonBot

Alright alright alright
Python
1
star
26

muybridge

A simple animation loop
JavaScript
1
star
27

fillcache

A simple in-process cache with single-flight filling semantics
Go
1
star