mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-18 22:20:56 +08:00
A handler that installs its own MaxBytesReader and then calls decode lost the 413: decode converted the error to a plain one and the middleware's limitedBody had been replaced. Return the original *http.MaxBytesError and update the cap test to errors.As.
70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDecodeAcceptsSmallBody(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"x":"ok"}`))
|
|
var v map[string]string
|
|
if err := decode(w, r, &v); err != nil || v["x"] != "ok" {
|
|
t.Fatalf("decode: %v %+v", err, v)
|
|
}
|
|
}
|
|
|
|
func TestDecodeCapsBody(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
body := strings.NewReader(`{"x":"` + strings.Repeat("a", decodeMaxBytes) + `"}`)
|
|
r := httptest.NewRequest(http.MethodPost, "/", body)
|
|
var v map[string]string
|
|
err := decode(w, r, &v)
|
|
var tooLarge *http.MaxBytesError
|
|
if !errors.As(err, &tooLarge) {
|
|
t.Fatalf("err=%v, want the body cap", err)
|
|
}
|
|
}
|
|
|
|
func TestRawJSONBodyOverTheCapIs413(t *testing.T) {
|
|
h := limitRequestBody(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var v map[string]string
|
|
if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
|
|
writeErr(w, 400, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, 200, v)
|
|
}))
|
|
small := httptest.NewRecorder()
|
|
h.ServeHTTP(small, httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"x":"ok"}`)))
|
|
if small.Code != 200 {
|
|
t.Fatalf("small body status=%d", small.Code)
|
|
}
|
|
large := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"x":"`+strings.Repeat("a", decodeMaxBytes)+`"}`))
|
|
h.ServeHTTP(large, req)
|
|
if large.Code != http.StatusRequestEntityTooLarge {
|
|
t.Fatalf("status=%d body=%s, want 413", large.Code, large.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestMultipartBodyIsNotGivenTheJSONCap(t *testing.T) {
|
|
h := limitRequestBody(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if _, ok := r.Body.(*limitedBody); ok {
|
|
t.Errorf("multipart body was given the JSON cap")
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("not-read"))
|
|
r.Header.Set("Content-Type", "multipart/form-data; boundary=abc")
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
if w.Code != http.StatusNoContent {
|
|
t.Fatalf("status=%d", w.Code)
|
|
}
|
|
}
|