77
}
78
`, "caddyfile")
79
80
- req, err := http.NewRequest(http.MethodPost, "http://localhost:9080/", bytes.NewBufferString("abcdefghijklm"))
81
- if err != nil {
82
- t.Fatalf("creating request: %v", err)
83
- }
84
- res := tester.AssertResponseCode(req, http.StatusRequestEntityTooLarge)
80
+ res := tester.AssertResponseCode(newUnknownLengthPost(t, "abcdefghijklm"), http.StatusRequestEntityTooLarge)
81
res.Body.Close()
82
}
83
105
}
106
`, "caddyfile")
107
112
- req, err := http.NewRequest(http.MethodPost, "http://localhost:9080/", bytes.NewBufferString("abcdefghijklmnopqrstuvwxyz"))
113
- if err != nil {
114
- t.Fatalf("creating request: %v", err)
115
- }
116
- res := tester.AssertResponseCode(req, http.StatusRequestEntityTooLarge)
108
+ res := tester.AssertResponseCode(newUnknownLengthPost(t, "abcdefghijklmnopqrstuvwxyz"), http.StatusRequestEntityTooLarge)
109
res.Body.Close()
110
}
111
130
// direct placeholder consumers expand the sanitized request-body limit
131
// marker to its text instead of failing the request with 413; only the
132
// templates and vars matchers propagate the 413 status
141
- tester.AssertPostResponseBody("http://localhost:9080/", nil, bytes.NewBufferString("abcdefghijklm"), http.StatusOK, "request body: http: request body too large")
133
+ tester.AssertResponse(newUnknownLengthPost(t, "abcdefghijklm"), http.StatusOK, "request body: http: request body too large")
134
+}
135
+
136
+// newUnknownLengthPost sends no Content-Length, so the body gets past
137
+// request_body's early check and max_size trips while handlers read it.
138
+func newUnknownLengthPost(t *testing.T, body string) *http.Request {
139
+ t.Helper()
140
+ req, err := http.NewRequest(http.MethodPost, "http://localhost:9080/", bytes.NewBufferString(body))
141
+ if err != nil {
142
+ t.Fatalf("creating request: %v", err)
143
+ }
144
+ req.ContentLength = -1
145
+ return req
146
+}
147
+
148
+func TestRequestBodyMaxSizeLimitsErrorRoute(t *testing.T) {
149
+ tester := caddytest.NewTester(t)
150
+ tester.InitServer(`
151
+ {
152
+ skip_install_trust
153
+ admin localhost:2999
154
+ http_port 9080
155
+ https_port 9443
156
+ grace_period 1ns
157
+ }
158
+ http://localhost:9080 {
159
+ request_body {
160
+ max_size 10
161
+ }
162
+ respond "next handler reached"
163
+ handle_errors {
164
+ respond "{http.request.body}" {err.status_code}
165
+ }
166
+ }
167
+ `, "caddyfile")
168
+
169
+ // the declared length is rejected before respond runs, and the error
170
+ // route reading the body must still be held to max_size
171
+ tester.AssertPostResponseBody("http://localhost:9080/", nil, bytes.NewBufferString("abcdefghijklm"), http.StatusRequestEntityTooLarge, "request body: http: request body too large")
172
}