diff --git a/README.md b/README.md index b849b1b1fbfb5f0efacfc358625835785d4fdd8e..f12573c629dcbb1f3e434fcbc82f52893a3d2542 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ A reasonably complete and well-tested golang port of [Kenneth Reitz][kr]'s [httpbin][httpbin-org] service, with zero dependencies outside the go stdlib. -[](https://godoc.org/github.com/mccutchen/go-httpbin) +[](https://pkg.go.dev/github.com/mccutchen/go-httpbin) [](http://travis-ci.org/mccutchen/go-httpbin) [](https://codecov.io/gh/mccutchen/go-httpbin) @@ -14,38 +14,38 @@ Run as a standalone binary, configured by command line flags or environment variables: ``` -$ go-httpbin -help +$ go-httpbin --help Usage of go-httpbin: -host string Host to listen on (default "0.0.0.0") - -port int - Port to listen on (default 8080) -https-cert-file string - HTTPS certificate file + HTTPS Server certificate file -https-key-file string - HTTPS private key file + HTTPS Server private key file -max-body-size int - Maximum size of request or response, in bytes (default 1048576) + Maximum size of request or response, in bytes (default 1048576) -max-duration duration - Maximum duration a response may take (default 10s) + Maximum duration a response may take (default 10s) + -port int + Port to listen on (default 8080) +``` Examples: - # Run http server - $ go-httpbin -host 127.0.0.1 -port 8081 - - # Run https server - # Generate .crt and .key files - $ 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 +```bash +# Run http server +$ go-httpbin -host 127.0.0.1 -port 8081 - $ go-httpbin -host 127.0.0.1 -port 8081 -https-cert-file ./server.crt -https-key-file ./server.key +# 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 ``` Docker images are published to [Docker Hub][docker-hub]: -``` +```bash # Run http server $ docker run -P mccutchen/go-httpbin @@ -53,7 +53,7 @@ $ docker run -P mccutchen/go-httpbin $ 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 ``` -The `github.com/mccutchen/go-httpbin/httpbin` package can also be used as a +The `github.com/mccutchen/go-httpbin/httpbin/v2` package can also be used as a library for testing an applications interactions with an upstream HTTP service, like so: @@ -61,34 +61,44 @@ like so: package httpbin_test import ( - "net/http" - "net/http/httptest" - "testing" - "time" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" - "github.com/mccutchen/go-httpbin/httpbin" + "github.com/mccutchen/go-httpbin/v2/httpbin" ) func TestSlowResponse(t *testing.T) { - svc := httpbin.New() - srv := httptest.NewServer(svc.Handler()) - defer srv.Close() - - client := http.Client{ - Timeout: time.Duration(1 * time.Second), - } - _, err := client.Get(srv.URL + "/delay/10") - if err == nil { - t.Fatal("expected timeout error") - } + app := httpbin.New() + testServer := httptest.NewServer(app.Handler()) + 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) + } } ``` ## 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 get github.com/mccutchen/go-httpbin/cmd/go-httpbin +go install github.com/mccutchen/go-httpbin/v2/cmd/go-httpbin ``` diff --git a/cmd/go-httpbin/main.go b/cmd/go-httpbin/main.go index 71c0bb95b3a974ebb91918896ade6222bfa309dd..830cd9e1ee16a8da0f76f85cbcc1c07c77d54aa1 100644 --- a/cmd/go-httpbin/main.go +++ b/cmd/go-httpbin/main.go @@ -1,6 +1,6 @@ package main -import "github.com/mccutchen/go-httpbin/cmd/maincmd" +import "github.com/mccutchen/go-httpbin/v2/cmd/maincmd" func main() { maincmd.Main() diff --git a/cmd/go_httpbin/main.go b/cmd/go_httpbin/main.go index 71c0bb95b3a974ebb91918896ade6222bfa309dd..830cd9e1ee16a8da0f76f85cbcc1c07c77d54aa1 100644 --- a/cmd/go_httpbin/main.go +++ b/cmd/go_httpbin/main.go @@ -1,6 +1,6 @@ package main -import "github.com/mccutchen/go-httpbin/cmd/maincmd" +import "github.com/mccutchen/go-httpbin/v2/cmd/maincmd" func main() { maincmd.Main() diff --git a/cmd/maincmd/main.go b/cmd/maincmd/main.go index ebe53a010030684380e3c93da66254c791923dac..0778d5c5feba5d30e2ff01c0359a080475c6b2d1 100644 --- a/cmd/maincmd/main.go +++ b/cmd/maincmd/main.go @@ -13,7 +13,7 @@ import ( "syscall" "time" - "github.com/mccutchen/go-httpbin/httpbin" + "github.com/mccutchen/go-httpbin/v2/httpbin" ) const defaultHost = "0.0.0.0" diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000000000000000000000000000000000000..2e7bdf0ccd396a103affe1e9c481d88488280740 --- /dev/null +++ b/example_test.go @@ -0,0 +1,26 @@ +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.Handler()) + 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) + } +} diff --git a/go.mod b/go.mod index 6ca378279492952d6e702e0ef5830433a4d6d263..91b9af29f6e1bfb7674972084e618c574f5eee08 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/mccutchen/go-httpbin +module github.com/mccutchen/go-httpbin/v2 -go 1.12 +go 1.16 diff --git a/httpbin/handlers.go b/httpbin/handlers.go index 9f26a43970f741ca65c2dcf97d25d1e62e685b5b..8dbf11e3e1a2a29ab6dbb696f5910bda43904796 100644 --- a/httpbin/handlers.go +++ b/httpbin/handlers.go @@ -11,8 +11,8 @@ import ( "strings" "time" - "github.com/mccutchen/go-httpbin/httpbin/assets" - "github.com/mccutchen/go-httpbin/httpbin/digest" + "github.com/mccutchen/go-httpbin/v2/httpbin/assets" + "github.com/mccutchen/go-httpbin/v2/httpbin/digest" ) var acceptedMediaTypes = []string{