Skip to content

Commit 1223a73

Browse files
authored
feat: add method property to responses (#123)
```console # before: httpbingo lacks the "method" field but httpbin.org has it $ curl -s https://httpbingo.org/anything | jq .method  9:31PM null $ curl -s https://httpbin.org/anything | jq .method "GET" # after: httpbingo has the method field! $ curl -s http://0.0.0.0:8080/anything | jq .method "GET" ``` This was mentioned in #6, but then not listed in #91 - I took the highly technical testing approach of adding a test for `Method` anywhere in `handlers_test.go` that mentioned `Args`; do you desire more tests? fewer tests? - Anywhere else I should add this field that I didn't already? For what it's worth, I discovered this when working on upgrading the testing for our [httpsnippet library](https://github.com/readmeio/httpsnippet); I wanted to use `go-httpbin` but our tests expect the method field to be present in the responses
1 parent 7e81a93 commit 1223a73

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

httpbin/handlers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (h *HTTPBin) Get(w http.ResponseWriter, r *http.Request) {
4747
writeJSON(http.StatusOK, w, &noBodyResponse{
4848
Args: r.URL.Query(),
4949
Headers: getRequestHeaders(r),
50+
Method: r.Method,
5051
Origin: getClientIP(r),
5152
URL: getURL(r).String(),
5253
})
@@ -71,6 +72,7 @@ func (h *HTTPBin) RequestWithBody(w http.ResponseWriter, r *http.Request) {
7172
resp := &bodyResponse{
7273
Args: r.URL.Query(),
7374
Headers: getRequestHeaders(r),
75+
Method: r.Method,
7476
Origin: getClientIP(r),
7577
URL: getURL(r).String(),
7678
}
@@ -93,6 +95,7 @@ func (h *HTTPBin) Gzip(w http.ResponseWriter, r *http.Request) {
9395
mustMarshalJSON(gzw, &noBodyResponse{
9496
Args: r.URL.Query(),
9597
Headers: getRequestHeaders(r),
98+
Method: r.Method,
9699
Origin: getClientIP(r),
97100
Gzipped: true,
98101
})
@@ -115,6 +118,7 @@ func (h *HTTPBin) Deflate(w http.ResponseWriter, r *http.Request) {
115118
mustMarshalJSON(zw, &noBodyResponse{
116119
Args: r.URL.Query(),
117120
Headers: getRequestHeaders(r),
121+
Method: r.Method,
118122
Origin: getClientIP(r),
119123
Deflated: true,
120124
})
@@ -749,6 +753,7 @@ func (h *HTTPBin) ETag(w http.ResponseWriter, r *http.Request) {
749753
mustMarshalJSON(&buf, noBodyResponse{
750754
Args: r.URL.Query(),
751755
Headers: getRequestHeaders(r),
756+
Method: r.Method,
752757
Origin: getClientIP(r),
753758
URL: getURL(r).String(),
754759
})

httpbin/handlers_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ func TestGet(t *testing.T) {
150150
if resp.Args.Encode() != "" {
151151
t.Fatalf("expected empty args, got %s", resp.Args.Encode())
152152
}
153+
if resp.Method != "GET" {
154+
t.Fatalf("expected method to be GET, got %s", resp.Method)
155+
}
153156
if resp.Origin != "" {
154157
t.Fatalf("expected empty origin, got %#v", resp.Origin)
155158
}
@@ -179,6 +182,9 @@ func TestGet(t *testing.T) {
179182
if resp.Args.Encode() != params.Encode() {
180183
t.Fatalf("args mismatch: %s != %s", resp.Args.Encode(), params.Encode())
181184
}
185+
if resp.Method != "GET" {
186+
t.Fatalf("expected method to be GET, got %s", resp.Method)
187+
}
182188
})
183189

184190
t.Run("only_allows_gets", func(t *testing.T) {
@@ -601,6 +607,9 @@ func testRequestWithBodyBinaryBody(t *testing.T, verb string, path string) {
601607
if len(resp.Args) > 0 {
602608
t.Fatalf("expected no query params, got %#v", resp.Args)
603609
}
610+
if resp.Method != verb {
611+
t.Fatalf("expected method to be %s, got %s", verb, resp.Method)
612+
}
604613
if len(resp.Form) > 0 {
605614
t.Fatalf("expected no form data, got %#v", resp.Form)
606615
}
@@ -645,6 +654,9 @@ func testRequestWithBodyEmptyBody(t *testing.T, verb string, path string) {
645654
if len(resp.Args) > 0 {
646655
t.Fatalf("expected no query params, got %#v", resp.Args)
647656
}
657+
if resp.Method != verb {
658+
t.Fatalf("expected method to be %s, got %s", verb, resp.Method)
659+
}
648660
if len(resp.Form) > 0 {
649661
t.Fatalf("expected no form data, got %#v", resp.Form)
650662
}
@@ -675,6 +687,9 @@ func testRequestWithBodyFormEncodedBody(t *testing.T, verb, path string) {
675687
if len(resp.Args) > 0 {
676688
t.Fatalf("expected no query params, got %#v", resp.Args)
677689
}
690+
if resp.Method != verb {
691+
t.Fatalf("expected method to be %s, got %s", verb, resp.Method)
692+
}
678693
if len(resp.Form) != len(params) {
679694
t.Fatalf("expected %d form values, got %d", len(params), len(resp.Form))
680695
}
@@ -731,6 +746,9 @@ func testRequestWithBodyFormEncodedBodyNoContentType(t *testing.T, verb, path st
731746
if len(resp.Args) > 0 {
732747
t.Fatalf("expected no query params, got %#v", resp.Args)
733748
}
749+
if resp.Method != verb {
750+
t.Fatalf("expected method to be %s, got %s", verb, resp.Method)
751+
}
734752
if len(resp.Form) != 0 {
735753
t.Fatalf("expected no form values, got %d", len(resp.Form))
736754
}
@@ -781,6 +799,9 @@ func testRequestWithBodyMultiPartBody(t *testing.T, verb, path string) {
781799
if len(resp.Args) > 0 {
782800
t.Fatalf("expected no query params, got %#v", resp.Args)
783801
}
802+
if resp.Method != verb {
803+
t.Fatalf("expected method to be %s, got %s", verb, resp.Method)
804+
}
784805
if len(resp.Form) != len(params) {
785806
t.Fatalf("expected %d form values, got %d", len(params), len(resp.Form))
786807
}
@@ -846,6 +867,9 @@ func testRequestWithBodyJSON(t *testing.T, verb, path string) {
846867
if len(resp.Args) > 0 {
847868
t.Fatalf("expected no query params, got %#v", resp.Args)
848869
}
870+
if resp.Method != verb {
871+
t.Fatalf("expected method to be %s, got %s", verb, resp.Method)
872+
}
849873
if len(resp.Form) != 0 {
850874
t.Fatalf("expected no form values, got %d", len(resp.Form))
851875
}
@@ -905,6 +929,10 @@ func testRequestWithBodyQueryParams(t *testing.T, verb, path string) {
905929
t.Fatalf("expected args = %#v in response, got %#v", params.Encode(), resp.Args.Encode())
906930
}
907931

932+
if resp.Method != "POST" {
933+
t.Fatalf("expected method to be POST, got %s", resp.Method)
934+
}
935+
908936
if len(resp.Form) > 0 {
909937
t.Fatalf("expected form data, got %#v", resp.Form)
910938
}
@@ -942,6 +970,10 @@ func testRequestWithBodyQueryParamsAndBody(t *testing.T, verb, path string) {
942970
t.Fatalf("expected args = %#v in response, got %#v", args.Encode(), resp.Args.Encode())
943971
}
944972

973+
if resp.Method != "POST" {
974+
t.Fatalf("expected method to be POST, got %s", resp.Method)
975+
}
976+
945977
if len(resp.Form) != len(form) {
946978
t.Fatalf("expected %d form values, got %d", len(form), len(resp.Form))
947979
}

httpbin/responses.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type userAgentResponse struct {
2727
type noBodyResponse struct {
2828
Args url.Values `json:"args"`
2929
Headers http.Header `json:"headers"`
30+
Method string `json:"method"`
3031
Origin string `json:"origin"`
3132
URL string `json:"url"`
3233

@@ -39,6 +40,7 @@ type noBodyResponse struct {
3940
type bodyResponse struct {
4041
Args url.Values `json:"args"`
4142
Headers http.Header `json:"headers"`
43+
Method string `json:"method"`
4244
Origin string `json:"origin"`
4345
URL string `json:"url"`
4446

0 commit comments

Comments
 (0)