3
3
A reasonably complete and well-tested golang port of [ Kenneth Reitz] [ kr ] 's
4
4
[ httpbin] [ httpbin-org ] service, with zero dependencies outside the go stdlib.
5
5
6
- [ ![ GoDoc] ( https://godoc.org/github.com/mccutchen/go-httpbin?status.svg )] ( https://godoc.org /github.com/mccutchen/go-httpbin )
6
+ [ ![ GoDoc] ( https://godoc.org/github.com/mccutchen/go-httpbin?status.svg )] ( https://pkg.go.dev /github.com/mccutchen/go-httpbin )
7
7
[ ![ Build Status] ( https://travis-ci.org/mccutchen/go-httpbin.svg?branch=master )] ( http://travis-ci.org/mccutchen/go-httpbin )
8
8
[ ![ Coverage] ( https://codecov.io/gh/mccutchen/go-httpbin/branch/master/graph/badge.svg )] ( https://codecov.io/gh/mccutchen/go-httpbin )
9
9
@@ -14,81 +14,91 @@ Run as a standalone binary, configured by command line flags or environment
14
14
variables:
15
15
16
16
```
17
- $ go-httpbin -help
17
+ $ go-httpbin -- help
18
18
Usage of go-httpbin:
19
19
-host string
20
20
Host to listen on (default "0.0.0.0")
21
- -port int
22
- Port to listen on (default 8080)
23
21
-https-cert-file string
24
- HTTPS certificate file
22
+ HTTPS Server certificate file
25
23
-https-key-file string
26
- HTTPS private key file
24
+ HTTPS Server private key file
27
25
-max-body-size int
28
- Maximum size of request or response, in bytes (default 1048576)
26
+ Maximum size of request or response, in bytes (default 1048576)
29
27
-max-duration duration
30
- Maximum duration a response may take (default 10s)
28
+ Maximum duration a response may take (default 10s)
29
+ -port int
30
+ Port to listen on (default 8080)
31
+ ```
31
32
32
33
Examples:
33
- # Run http server
34
- $ go-httpbin -host 127.0.0.1 -port 8081
35
-
36
- # Run https server
37
34
38
- # Generate .crt and .key files
39
- $ openssl genrsa -out server.key 2048
40
- $ openssl ecparam -genkey -name secp384r1 -out server.key
41
- $ openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
35
+ ``` bash
36
+ # Run http server
37
+ $ go-httpbin -host 127.0.0.1 -port 8081
42
38
43
- $ go-httpbin -host 127.0.0.1 -port 8081 -https-cert-file ./server.crt -https-key-file ./server.key
39
+ # Run https server
40
+ $ openssl genrsa -out server.key 2048
41
+ $ openssl ecparam -genkey -name secp384r1 -out server.key
42
+ $ openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
43
+ $ go-httpbin -host 127.0.0.1 -port 8081 -https-cert-file ./server.crt -https-key-file ./server.key
44
44
```
45
45
46
46
Docker images are published to [ Docker Hub] [ docker-hub ] :
47
47
48
- ```
48
+ ``` bash
49
49
# Run http server
50
50
$ docker run -P mccutchen/go-httpbin
51
51
52
52
# Run https server
53
53
$ 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
54
54
```
55
55
56
- The ` github.com/mccutchen/go-httpbin/httpbin ` package can also be used as a
56
+ The ` github.com/mccutchen/go-httpbin/httpbin/v2 ` package can also be used as a
57
57
library for testing an applications interactions with an upstream HTTP service,
58
58
like so:
59
59
60
60
``` go
61
61
package httpbin_test
62
62
63
63
import (
64
- " net/http"
65
- " net/http/httptest"
66
- " testing"
67
- " time"
64
+ " net/http"
65
+ " net/http/httptest"
66
+ " os"
67
+ " testing"
68
+ " time"
68
69
69
- " github.com/mccutchen/go-httpbin/httpbin"
70
+ " github.com/mccutchen/go-httpbin/v2 /httpbin"
70
71
)
71
72
72
73
func TestSlowResponse (t *testing .T ) {
73
- svc := httpbin.New ()
74
- srv := httptest.NewServer (svc.Handler ())
75
- defer srv.Close ()
76
-
77
- client := http.Client {
78
- Timeout: time.Duration (1 * time.Second ),
79
- }
80
- _ , err := client.Get (srv.URL + " /delay/10" )
81
- if err == nil {
82
- t.Fatal (" expected timeout error" )
83
- }
74
+ app := httpbin.New ()
75
+ testServer := httptest.NewServer (app.Handler ())
76
+ defer testServer.Close ()
77
+
78
+ client := http.Client {
79
+ Timeout: time.Duration (1 * time.Second ),
80
+ }
81
+
82
+ _ , err := client.Get (testServer.URL + " /delay/10" )
83
+ if !os.IsTimeout (err) {
84
+ t.Fatalf (" expected timeout error, got %s " , err)
85
+ }
84
86
}
85
87
```
86
88
87
89
88
90
## Installation
89
91
92
+ To add go-httpbin to an existing golang project:
93
+
94
+ ```
95
+ go get -u github.com/mccutchen/go-httpbin/v2
96
+ ```
97
+
98
+ To install the ` go-httpbin ` binary:
99
+
90
100
```
91
- go get github.com/mccutchen/go-httpbin/cmd/go-httpbin
101
+ go install github.com/mccutchen/go-httpbin/v2 /cmd/go-httpbin
92
102
```
93
103
94
104
0 commit comments