Skip to content

Commit 6353cee

Browse files
$gitUsername$gitUsername
authored andcommitted
Cap oversized page size values
1 parent bcce214 commit 6353cee

5 files changed

Lines changed: 31 additions & 16 deletions

File tree

internal/handlers/general/pagination.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ func pageSizeFromQuery(ctx *fiber.Ctx) (int, error) {
7878
return 0, errInvalidPageSize
7979
}
8080

81-
if size < 1 || size > MaxLimitCount {
81+
if size < 1 {
8282
return 0, errPageSizeOutOfRange
8383
}
8484

85+
if size > MaxLimitCount {
86+
return MaxLimitCount, nil
87+
}
88+
8589
return size, nil
8690
}
8791

internal/handlers/general/pagination_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,14 @@ func TestNewPaginatorWithConfig_DoesNotMutateDefaultConfig(t *testing.T) {
2828
assert.Equal(t, paginator.ASC, DefaultConfig.Order,
2929
"DefaultConfig.Order must not be mutated by NewPaginatorWithConfig")
3030
}
31+
32+
func TestPageSizeFromQueryCapsValuesAboveMax(t *testing.T) {
33+
app, ctx := newTestCtx()
34+
defer app.ReleaseCtx(ctx)
35+
36+
ctx.Request().URI().SetQueryString("page[size]=200")
37+
38+
size, err := pageSizeFromQuery(ctx)
39+
assert.NoError(t, err)
40+
assert.Equal(t, MaxLimitCount, size)
41+
}

logs_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ func TestLogsEndpoints(t *testing.T) {
8686
},
8787
},
8888
{
89-
description: "GET with page[size] bigger than the max of 100",
89+
description: "GET with page[size] bigger than the max of 100 caps the size",
9090
query: "GET /v1/logs?page[size]=200",
91-
expectedCode: 422,
92-
expectedContentType: "application/problem+json",
91+
expectedCode: 200,
92+
expectedContentType: "application/json",
9393
validateFunc: func(t *testing.T, response map[string]interface{}) {
94-
assert.Equal(t, `can't get Logs`, response["title"])
95-
assert.Equal(t, "page[size] must be between 1 and 100", response["detail"])
94+
items := assertListResponse(t, response)
95+
assert.LessOrEqual(t, len(items), 100)
9696
},
9797
},
9898
{

publishers_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ func TestPublishersEndpoints(t *testing.T) {
109109
},
110110
},
111111
{
112-
description: "GET with page[size] bigger than the max of 100",
112+
description: "GET with page[size] bigger than the max of 100 caps the size",
113113
query: "GET /v1/publishers?page[size]=200",
114114

115-
expectedCode: 422,
116-
expectedContentType: "application/problem+json",
115+
expectedCode: 200,
116+
expectedContentType: "application/json",
117117
validateFunc: func(t *testing.T, response map[string]interface{}) {
118-
assert.Equal(t, `can't get Publishers`, response["title"])
119-
assert.Equal(t, "page[size] must be between 1 and 100", response["detail"])
118+
items := assertListResponse(t, response)
119+
assert.LessOrEqual(t, len(items), 100)
120120
},
121121
},
122122
{

software_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ func TestSoftwareEndpoints(t *testing.T) {
195195
},
196196
},
197197
{
198-
description: "GET with page[size] bigger than the max of 100",
198+
description: "GET with page[size] bigger than the max of 100 caps the size",
199199
query: "GET /v1/software?page[size]=200",
200200

201-
expectedCode: 422,
202-
expectedContentType: "application/problem+json",
201+
expectedCode: 200,
202+
expectedContentType: "application/json",
203203
validateFunc: func(t *testing.T, response map[string]interface{}) {
204-
assert.Equal(t, `can't get Software`, response["title"])
205-
assert.Equal(t, "page[size] must be between 1 and 100", response["detail"])
204+
items := assertListResponse(t, response)
205+
assert.LessOrEqual(t, len(items), 100)
206206
},
207207
},
208208
{

0 commit comments

Comments
 (0)