Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Error messages now respect custom option names and values. #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pkg/pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package pagination

import (
"errors"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -79,15 +78,15 @@ func (p *paginator) getIntValueWithDefault(key string, defaultValue string) (int

func (p *paginator) validatePage(page int) error {
if page < 0 {
return errors.New("page number must be positive")
return fmt.Errorf("%s number must be positive", p.opts.PageText)
}

return nil
}

func (p *paginator) validatePageSize(size int) error {
if size < p.opts.MinPageSize || size > p.opts.MaxPageSize {
return errors.New("page size must be between %d and %d")
return fmt.Errorf("%s must be between %d and %d", p.opts.SizeText, p.opts.MinPageSize, p.opts.MaxPageSize)
}

return nil
Expand Down
80 changes: 72 additions & 8 deletions pkg/pagination/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/gin-gonic/gin"
Expand All @@ -19,6 +20,8 @@ func TestPaginationMiddleware(t *testing.T) {
expectedSize int
customPageText string
customSizeText string
expectError bool
errorContains string
}{
{
"Non int Page Param - Bad Request",
Expand All @@ -30,6 +33,8 @@ func TestPaginationMiddleware(t *testing.T) {
0,
"",
"",
true,
"page parameter must be an integer",
},
{
"Non int Size Param - Bad Request",
Expand All @@ -42,6 +47,8 @@ func TestPaginationMiddleware(t *testing.T) {
0,
"",
"",
true,
"size parameter must be an integer",
},
{
"Negative Page Param - Bad Request",
Expand All @@ -53,6 +60,8 @@ func TestPaginationMiddleware(t *testing.T) {
0,
"",
"",
true,
"page number must be positive",
},
{
"Size below min - Bad Request",
Expand All @@ -65,6 +74,8 @@ func TestPaginationMiddleware(t *testing.T) {
0,
"",
"",
true,
"size must be between",
},
{
"Size above max - Bad Request",
Expand All @@ -77,6 +88,8 @@ func TestPaginationMiddleware(t *testing.T) {
0,
"",
"",
true,
"size must be between",
},
{
"Default Handling",
Expand All @@ -86,6 +99,8 @@ func TestPaginationMiddleware(t *testing.T) {
10,
"",
"",
false,
"",
},
{
"The first 100 results",
Expand All @@ -98,6 +113,8 @@ func TestPaginationMiddleware(t *testing.T) {
100,
"",
"",
false,
"",
},
{
"The second 20 results",
Expand All @@ -110,6 +127,8 @@ func TestPaginationMiddleware(t *testing.T) {
20,
"",
"",
false,
"",
},
{
"Custom Handling",
Expand All @@ -126,14 +145,50 @@ func TestPaginationMiddleware(t *testing.T) {
5,
"pages",
"items",
false,
"",
},
{
"Invalid Custom Page Param - Bad Request",
pagination.New(
pagination.WithPageText("offset"),
),
url.Values{
"offset": {"-1"},
},
0,
0,
"offset",
"",
true,
"offset number must be positive",
},
{
"Invalid Custom Size Param - Bad Request",
pagination.New(
pagination.WithSizeText("limit"),
pagination.WithMinPageSize(1),
pagination.WithMaxPageSize(25),
),
url.Values{
"limit": {"-1"},
},
0,
0,
"",
"limit",
true,
"limit must be between 1 and 25",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gin.SetMode(gin.TestMode)

// Create a test context
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)

// Add the query parameters to the Request of the test context
ctx.Request = &http.Request{
Expand All @@ -155,14 +210,23 @@ func TestPaginationMiddleware(t *testing.T) {
}

gotPage := ctx.GetInt(tt.customPageText)
// Check if the page and pageSize are set correctly
if gotPage != tt.expectedPage {
t.Errorf("Expected page %d, got %d", tt.expectedPage, gotPage)
}

gotSize := ctx.GetInt(tt.customSizeText)
if gotSize != tt.expectedSize {
t.Errorf("Expected size %d, got %d", tt.expectedSize, gotSize)

// Check if the page and pageSize are set correctly or if an error is expected
if tt.expectError {
if gotPage != 0 || gotSize != 0 {
t.Errorf("Expected error, but got page %d and size %d", gotPage, gotSize)
}
if !strings.Contains(recorder.Body.String(), tt.errorContains) {
t.Errorf("Expected error message to contain '%s', but got '%s'", tt.errorContains, recorder.Body.String())
}
} else {
if gotPage != tt.expectedPage {
t.Errorf("Expected page %d, got %d", tt.expectedPage, gotPage)
}
if gotSize != tt.expectedSize {
t.Errorf("Expected size %d, got %d", tt.expectedSize, gotSize)
}
}
})
}
Expand Down
Loading