mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-27 10:20:54 +08:00
POST /api/assets decoded with json.NewDecoder directly. A multipart Content-Type skips the JSON body middleware, so that request was uncapped. Decode through the shared limit first and answer 413 before touching the database.
91 lines
3.0 KiB
Go
91 lines
3.0 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 TestInsertAssetsCapsMultipartBody(t *testing.T) {
|
|
s := &Server{m: &Manager{}}
|
|
h := limitRequestBody(http.HandlerFunc(s.insertAssets))
|
|
|
|
large := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/assets", strings.NewReader(`{"assets":[{"type":"`+strings.Repeat("a", decodeMaxBytes)+`"}]}`))
|
|
req.Header.Set("Content-Type", "multipart/form-data; boundary=abc")
|
|
h.ServeHTTP(large, req)
|
|
if large.Code != http.StatusRequestEntityTooLarge {
|
|
t.Fatalf("status=%d body=%s, want 413", large.Code, large.Body.String())
|
|
}
|
|
|
|
small := httptest.NewRecorder()
|
|
ok := httptest.NewRequest(http.MethodPost, "/api/assets", strings.NewReader(`{"assets":[]}`))
|
|
ok.Header.Set("Content-Type", "multipart/form-data; boundary=abc")
|
|
h.ServeHTTP(small, ok)
|
|
if small.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status=%d body=%s, want 503 without a store", small.Code, small.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)
|
|
}
|
|
}
|