feat: исправила строгую валидацию JSON и пагинации

This commit is contained in:
2026-10-29 12:41:45 +03:00
parent 9ac1762101
commit 10ca948a53
7 changed files with 316 additions and 6 deletions
+70
View File
@@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
@@ -27,6 +28,12 @@ func TestCursorScopeAndValidation(t *testing.T) {
query string
valid bool
}{
{"", true},
{"limit=", false}, {"cursor=", false}, {"limit", false}, {"cursor", false},
{"limit=&cursor=" + encodeCursor(p), false},
{"limit=1&cursor=", false},
{"limit=1&limit=2", false},
{"cursor=" + encodeCursor(p) + "&cursor=", false},
{"limit=1&cursor=" + encodeCursor(p), true},
{"limit=100&cursor=" + encodeCursor(p), true},
{"limit=0", false}, {"limit=101", false}, {"limit=1.2", false}, {"cursor=garbage", false},
@@ -41,6 +48,69 @@ func TestCursorScopeAndValidation(t *testing.T) {
if ok != c.valid {
t.Errorf("cursor query %s: valid=%v", c.query, ok)
}
if !c.valid {
assertInvalidRequest(t, w)
} else if c.query == "" {
_, limit, _ := a.position(httptest.NewRecorder(), r, p.Resource)
if limit != 50 {
t.Fatal("absent limit must default to 50")
}
}
}
}
func assertInvalidRequest(t *testing.T, w *httptest.ResponseRecorder) {
t.Helper()
var body struct {
Error struct {
Code string `json:"code"`
} `json:"error"`
}
if w.Code != 400 || json.Unmarshal(w.Body.Bytes(), &body) != nil || body.Error.Code != "invalid_request" {
t.Fatalf("expected 400 invalid_request, received %d %s", w.Code, w.Body.String())
}
}
func TestExactJSONFieldNames(t *testing.T) {
a := &app{limits: limits{maxBodyBytes: 1024}}
cases := []struct {
name, data string
target func() any
valid bool
}{
{"message", `{"text":"valid"}`, func() any { return &createMessage{} }, true},
{"escaped exact key", `{"te\u0078t":"valid"}`, func() any { return &createMessage{} }, true},
{"case overwrite", `{"text":"valid","TEXT":"unexpected-property"}`, func() any { return &createMessage{} }, false},
{"case first", `{"TEXT":"unexpected-property","text":"valid"}`, func() any { return &createMessage{} }, false},
{"mixed case", `{"Text":"unexpected-property"}`, func() any { return &createMessage{} }, false},
{"escaped uppercase", `{"\u0054EXT":"unexpected-property"}`, func() any { return &createMessage{} }, false},
{"duplicate", `{"text":"valid","text":"overwrite"}`, func() any { return &createMessage{} }, false},
{"escaped duplicate", `{"text":"valid","te\u0078t":"overwrite"}`, func() any { return &createMessage{} }, false},
{"null object", `null`, func() any { return &createMessage{} }, false},
{"array", `[]`, func() any { return &createMessage{} }, false},
{"user", `{"username":"alice","display_name":"Alice","password":"long-password"}`, func() any { return &createUser{} }, true},
{"user case", `{"username":"alice","display_name":"Alice","PASSWORD":"long-password"}`, func() any { return &createUser{} }, false},
{"Go field name", `{"Username":"alice"}`, func() any { return &createUser{} }, false},
{"chat", `{"title":"Chat","member_ids":[]}`, func() any { return &createChat{} }, true},
{"chat case", `{"TITLE":"Chat","member_ids":[]}`, func() any { return &createChat{} }, false},
{"trailing object", `{"text":"valid"} {}`, func() any { return &createMessage{} }, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
w := httptest.NewRecorder()
target := c.target()
if got := a.decode(w, httptest.NewRequest("POST", "/", strings.NewReader(c.data)), target); got != c.valid {
t.Fatalf("valid=%v, expected %v", got, c.valid)
}
if !c.valid {
assertInvalidRequest(t, w)
}
if c.name == "message" || c.name == "escaped exact key" {
if target.(*createMessage).Text != "valid" {
t.Fatal("valid text changed")
}
}
})
}
}