fix(api): 限制公开接口分页结果窗口 (#45)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -46,3 +47,39 @@ func TestPageParams(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageTooDeep(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
page, size int
|
||||
want bool
|
||||
}{
|
||||
{name: "boundary", page: 100, size: 100, want: false},
|
||||
{name: "over boundary", page: 101, size: 100, want: true},
|
||||
{name: "small page", page: 1, size: 20, want: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := pageTooDeep(tt.page, tt.size); got != tt.want {
|
||||
t.Fatalf("pageTooDeep(%d, %d) = %v, want %v",
|
||||
tt.page, tt.size, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeepPaginationReturns400BeforeStoreAccess(t *testing.T) {
|
||||
h := New(nil, nil)
|
||||
for _, path := range []string{
|
||||
"/api/" + APIVersion + "/products/search?page=101&size=100",
|
||||
"/api/" + APIVersion + "/brands?page=101&size=100",
|
||||
} {
|
||||
rec := doGET(t, h, path)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("%s: status = %d, want %d", path, rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "page_too_deep") {
|
||||
t.Fatalf("%s: response missing page_too_deep: %s", path, rec.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user