From 2ae7865cf3550b6dba3dc04c4f89b13e34d7a60a Mon Sep 17 00:00:00 2001 From: Will McCutchen <will@mccutch.org> Date: Mon, 10 May 2021 19:01:17 -0400 Subject: [PATCH] Bump go module version to v2 (#58) --- README.md | 82 +++++++++++++++++++++++------------------- cmd/go-httpbin/main.go | 2 +- cmd/go_httpbin/main.go | 2 +- cmd/maincmd/main.go | 2 +- example_test.go | 26 ++++++++++++++ go.mod | 4 +-- httpbin/handlers.go | 4 +-- 7 files changed, 79 insertions(+), 43 deletions(-) create mode 100644 example_test.go diff --git a/README.md b/README.md index b849b1b..f12573c 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 71c0bb9..830cd9e 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 71c0bb9..830cd9e 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 ebe53a0..0778d5c 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 0000000..2e7bdf0 --- /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 6ca3782..91b9af2 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 9f26a43..8dbf11e 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{ -- GitLab