Skip to content
Snippets Groups Projects
Unverified Commit 499044e1 authored by Will McCutchen's avatar Will McCutchen Committed by GitHub
Browse files

fix: include Transfer-Encoding when echoing request headers (#130)

Fixes #128
parent 69e0f18f
No related branches found
No related tags found
No related merge requests found
...@@ -553,6 +553,7 @@ func testRequestWithBody(t *testing.T, verb, path string) { ...@@ -553,6 +553,7 @@ func testRequestWithBody(t *testing.T, verb, path string) {
testRequestWithBodyQueryParams, testRequestWithBodyQueryParams,
testRequestWithBodyQueryParamsAndBody, testRequestWithBodyQueryParamsAndBody,
testRequestWithBodyBinaryBody, testRequestWithBodyBinaryBody,
testRequestWithBodyTransferEncoding,
} }
for _, testFunc := range testFuncs { for _, testFunc := range testFuncs {
testFunc := testFunc testFunc := testFunc
...@@ -1030,6 +1031,45 @@ func testRequestWithBodyQueryParamsAndBody(t *testing.T, verb, path string) { ...@@ -1030,6 +1031,45 @@ func testRequestWithBodyQueryParamsAndBody(t *testing.T, verb, path string) {
} }
} }
func testRequestWithBodyTransferEncoding(t *testing.T, verb string, path string) {
testCases := []struct {
given string
want string
}{
{"", ""},
{"identity", ""},
{"chunked", "chunked"},
}
for _, tc := range testCases {
tc := tc
t.Run("transfer-encoding/"+tc.given, func(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(app)
defer srv.Close()
r, _ := http.NewRequest(verb, srv.URL+path, bytes.NewReader([]byte("{}")))
if tc.given != "" {
r.TransferEncoding = []string{tc.given}
}
httpResp, err := srv.Client().Do(r)
assertNilError(t, err)
assertIntEqual(t, httpResp.StatusCode, http.StatusOK)
var resp *bodyResponse
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
t.Fatalf("failed to unmarshal body from JSON: %s", err)
}
got := resp.Headers.Get("Transfer-Encoding")
if got != tc.want {
t.Errorf("expected Transfer-Encoding %#v, got %#v", tc.want, got)
}
})
}
}
// TODO: implement and test more complex /status endpoint // TODO: implement and test more complex /status endpoint
func TestStatus(t *testing.T) { func TestStatus(t *testing.T) {
t.Parallel() t.Parallel()
......
...@@ -25,11 +25,15 @@ const Base64MaxLen = 2000 ...@@ -25,11 +25,15 @@ const Base64MaxLen = 2000
// requestHeaders takes in incoming request and returns an http.Header map // requestHeaders takes in incoming request and returns an http.Header map
// suitable for inclusion in our response data structures. // suitable for inclusion in our response data structures.
// //
// This is necessary to ensure that the incoming Host header is included, // This is necessary to ensure that the incoming Host and Transfer-Encoding
// because golang only exposes that header on the http.Request struct itself. // headers are included, because golang only exposes those values on the
// http.Request struct itself.
func getRequestHeaders(r *http.Request) http.Header { func getRequestHeaders(r *http.Request) http.Header {
h := r.Header h := r.Header
h.Set("Host", r.Host) h.Set("Host", r.Host)
if len(r.TransferEncoding) > 0 {
h.Set("Transfer-Encoding", strings.Join(r.TransferEncoding, ","))
}
return h return h
} }
......
...@@ -14,8 +14,16 @@ import ( ...@@ -14,8 +14,16 @@ import (
) )
func assertNil(t *testing.T, v interface{}) { func assertNil(t *testing.T, v interface{}) {
t.Helper()
if v != nil { if v != nil {
t.Errorf("expected nil, got %#v", v) t.Fatalf("expected nil, got %#v", v)
}
}
func assertNilError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("expected nil error, got %s (%T)", err, err)
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment