Skip to content

Commit 499044e

Browse files
authored
fix: include Transfer-Encoding when echoing request headers (#130)
Fixes #128
1 parent 69e0f18 commit 499044e

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

httpbin/handlers_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ func testRequestWithBody(t *testing.T, verb, path string) {
553553
testRequestWithBodyQueryParams,
554554
testRequestWithBodyQueryParamsAndBody,
555555
testRequestWithBodyBinaryBody,
556+
testRequestWithBodyTransferEncoding,
556557
}
557558
for _, testFunc := range testFuncs {
558559
testFunc := testFunc
@@ -1030,6 +1031,45 @@ func testRequestWithBodyQueryParamsAndBody(t *testing.T, verb, path string) {
10301031
}
10311032
}
10321033

1034+
func testRequestWithBodyTransferEncoding(t *testing.T, verb string, path string) {
1035+
testCases := []struct {
1036+
given string
1037+
want string
1038+
}{
1039+
{"", ""},
1040+
{"identity", ""},
1041+
{"chunked", "chunked"},
1042+
}
1043+
for _, tc := range testCases {
1044+
tc := tc
1045+
t.Run("transfer-encoding/"+tc.given, func(t *testing.T) {
1046+
t.Parallel()
1047+
1048+
srv := httptest.NewServer(app)
1049+
defer srv.Close()
1050+
1051+
r, _ := http.NewRequest(verb, srv.URL+path, bytes.NewReader([]byte("{}")))
1052+
if tc.given != "" {
1053+
r.TransferEncoding = []string{tc.given}
1054+
}
1055+
1056+
httpResp, err := srv.Client().Do(r)
1057+
assertNilError(t, err)
1058+
assertIntEqual(t, httpResp.StatusCode, http.StatusOK)
1059+
1060+
var resp *bodyResponse
1061+
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
1062+
t.Fatalf("failed to unmarshal body from JSON: %s", err)
1063+
}
1064+
1065+
got := resp.Headers.Get("Transfer-Encoding")
1066+
if got != tc.want {
1067+
t.Errorf("expected Transfer-Encoding %#v, got %#v", tc.want, got)
1068+
}
1069+
})
1070+
}
1071+
}
1072+
10331073
// TODO: implement and test more complex /status endpoint
10341074
func TestStatus(t *testing.T) {
10351075
t.Parallel()

httpbin/helpers.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ const Base64MaxLen = 2000
2525
// requestHeaders takes in incoming request and returns an http.Header map
2626
// suitable for inclusion in our response data structures.
2727
//
28-
// This is necessary to ensure that the incoming Host header is included,
29-
// because golang only exposes that header on the http.Request struct itself.
28+
// This is necessary to ensure that the incoming Host and Transfer-Encoding
29+
// headers are included, because golang only exposes those values on the
30+
// http.Request struct itself.
3031
func getRequestHeaders(r *http.Request) http.Header {
3132
h := r.Header
3233
h.Set("Host", r.Host)
34+
if len(r.TransferEncoding) > 0 {
35+
h.Set("Transfer-Encoding", strings.Join(r.TransferEncoding, ","))
36+
}
3337
return h
3438
}
3539

httpbin/helpers_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ import (
1414
)
1515

1616
func assertNil(t *testing.T, v interface{}) {
17+
t.Helper()
1718
if v != nil {
18-
t.Errorf("expected nil, got %#v", v)
19+
t.Fatalf("expected nil, got %#v", v)
20+
}
21+
}
22+
23+
func assertNilError(t *testing.T, err error) {
24+
t.Helper()
25+
if err != nil {
26+
t.Fatalf("expected nil error, got %s (%T)", err, err)
1927
}
2028
}
2129

0 commit comments

Comments
 (0)