(t *testing.T)
| 18 | ) |
| 19 | |
| 20 | func TestRequestInvalidChunkedEncoding(t *testing.T) { |
| 21 | testLog := proxy.NewTestLog() |
| 22 | defer testLog.Close() |
| 23 | |
| 24 | backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) |
| 25 | defer backend.Close() |
| 26 | |
| 27 | p := proxytest.Config{ |
| 28 | Routes: eskip.MustParse(fmt.Sprintf(`* -> "%s"`, backend.URL)), |
| 29 | }.Create() |
| 30 | defer p.Close() |
| 31 | |
| 32 | doChunkedRequest := func(t *testing.T, body string) *http.Response { |
| 33 | t.Helper() |
| 34 | |
| 35 | conn, err := net.Dial("tcp", strings.TrimPrefix(p.URL, "http://")) |
| 36 | require.NoError(t, err) |
| 37 | defer conn.Close() |
| 38 | |
| 39 | _, err = conn.Write([]byte("POST / HTTP/1.1\r\nHost: skipper.test\r\nTransfer-Encoding: chunked\r\n\r\n" + body)) |
| 40 | require.NoError(t, err) |
| 41 | |
| 42 | resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "POST"}) |
| 43 | require.NoError(t, err) |
| 44 | resp.Body.Close() |
| 45 | |
| 46 | return resp |
| 47 | } |
| 48 | |
| 49 | for _, tc := range []struct { |
| 50 | body string |
| 51 | expectErrMessage string |
| 52 | }{ |
| 53 | { |
| 54 | // "!" is an invalid byte in chunk length |
| 55 | body: "!" + "\r\nabcd\r\n" + "0\r\n\r\n", |
| 56 | expectErrMessage: "invalid byte in chunk length", |
| 57 | }, |
| 58 | { |
| 59 | // empty chunk length |
| 60 | body: "" + "\r\nabcd\r\n" + "0\r\n\r\n", |
| 61 | expectErrMessage: "empty hex number for chunk length", |
| 62 | }, |
| 63 | { |
| 64 | // missing \r\n after first chunk |
| 65 | body: "4\r\nabcd" + "0\r\n\r\n", |
| 66 | expectErrMessage: "malformed chunked encoding", |
| 67 | }, |
| 68 | } { |
| 69 | t.Run(tc.expectErrMessage, func(t *testing.T) { |
| 70 | testLog.Reset() |
| 71 | |
| 72 | resp := doChunkedRequest(t, tc.body) |
| 73 | assert.Equal(t, http.StatusBadRequest, resp.StatusCode) |
| 74 | |
| 75 | err := testLog.WaitFor("failed to do backend roundtrip due to invalid request: "+tc.expectErrMessage, 100*time.Millisecond) |
| 76 | if !assert.NoError(t, err) { |
| 77 | t.Logf("proxy log: %s", testLog.String()) |
nothing calls this directly
no test coverage detected
searching dependent graphs…