fix: correct paging parameter logic and add paging options

- fix bug where `pageNumber` and `pageSize` were swapped during default
initialization in `apps.go`.
- fix bug in `pagingRequest.go` where `ToParams` mapped keys to the
incorrect struct fields.
- add `ForPageNumber` and `WithPageSize` functional options to allow
custom paging configuration.
- enhance `Apps` endpoint tests to verify that `pageNumber` and
`pageSize` query parameters are correctly passed to the API.
- add test case for verifying non-default paging options.
This commit is contained in:
Stevan Freeborn
2026-01-15 17:03:59 -06:00
parent bfa1b06a83
commit 126b027a81
3 changed files with 66 additions and 5 deletions
+2 -2
View File
@@ -21,8 +21,8 @@ type App struct {
func (p *AppsEndpoint) Get(ctx context.Context, pagingOpts ...PagingOption) (Page[App], error) { func (p *AppsEndpoint) Get(ctx context.Context, pagingOpts ...PagingOption) (Page[App], error) {
pagingRequest := &PagingRequest{ pagingRequest := &PagingRequest{
pageSize: 1, pageNumber: 1,
pageNumber: 50, pageSize: 50,
} }
for _, opt := range pagingOpts { for _, opt := range pagingOpts {
+49
View File
@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"reflect" "reflect"
"strconv"
"testing" "testing"
"github.com/StevanFreeborn/onspring-api-sdk-go" "github.com/StevanFreeborn/onspring-api-sdk-go"
@@ -75,6 +76,9 @@ func TestApps(t *testing.T) {
}) })
t.Run("it should perform a GET request to the /apps endpoint and return page of apps if receives 200 status code", func(t *testing.T) { t.Run("it should perform a GET request to the /apps endpoint and return page of apps if receives 200 status code", func(t *testing.T) {
expectedPageNumber := 1
expectedPageSize := 50
expectedPage := onspring.Page[onspring.App]{ expectedPage := onspring.Page[onspring.App]{
TotalPages: 1, TotalPages: 1,
TotalRecords: 1, TotalRecords: 1,
@@ -98,6 +102,17 @@ func TestApps(t *testing.T) {
t.Errorf("Expected /apps endpoint, got %s", r.URL.Path) t.Errorf("Expected /apps endpoint, got %s", r.URL.Path)
} }
pageNumber := r.URL.Query().Get("pageNumber")
pageSize := r.URL.Query().Get("pageSize")
if pageNumber != strconv.Itoa(expectedPageNumber) {
t.Errorf("Expected query param pageNumber to be %d but got %s", expectedPageNumber, pageNumber)
}
if pageSize != strconv.Itoa(expectedPageSize) {
t.Errorf("Expected query param pageSize to be %d but got %s", expectedPageSize, pageSize)
}
jsonData, _ := json.Marshal(expectedPage) jsonData, _ := json.Marshal(expectedPage)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
@@ -116,6 +131,40 @@ func TestApps(t *testing.T) {
} }
}) })
t.Run("it should perform a GET request to the /apps endpoint with non-default paging information when provided", func(t *testing.T) {
expectedPageNumber := 2
expectedPageSize := 1
_, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("Expected GET method, got %s", r.Method)
}
if r.URL.Path != "/apps" {
t.Errorf("Expected /apps endpoint, got %s", r.URL.Path)
}
pageNumber := r.URL.Query().Get("pageNumber")
pageSize := r.URL.Query().Get("pageSize")
if pageNumber != strconv.Itoa(expectedPageNumber) {
t.Errorf("Expected query param pageNumber to be %d but got %s", expectedPageNumber, pageNumber)
}
if pageSize != strconv.Itoa(expectedPageSize) {
t.Errorf("Expected query param pageSize to be %d but got %s", expectedPageSize, pageSize)
}
w.WriteHeader(http.StatusOK)
})
client.Apps.Get(
t.Context(),
onspring.ForPageNumber(expectedPageNumber),
onspring.WithPageSize(expectedPageSize),
)
})
t.Run("it should return an error if the /apps endpoint returns a non-200 status code", func(t *testing.T) { t.Run("it should return an error if the /apps endpoint returns a non-200 status code", func(t *testing.T) {
_, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) { _, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
+15 -3
View File
@@ -3,15 +3,27 @@ package onspring
import "strconv" import "strconv"
type PagingRequest struct { type PagingRequest struct {
pageSize int
pageNumber int pageNumber int
pageSize int
} }
func (pr *PagingRequest) ToParams() map[string]string { func (pr *PagingRequest) ToParams() map[string]string {
return map[string]string{ return map[string]string{
"pageSize": strconv.Itoa(pr.pageNumber), "pageNumber": strconv.Itoa(pr.pageNumber),
"pageNumber": strconv.Itoa(pr.pageSize), "pageSize": strconv.Itoa(pr.pageSize),
} }
} }
type PagingOption func(*PagingRequest) type PagingOption func(*PagingRequest)
func ForPageNumber(pageNumber int) PagingOption {
return func(pr *PagingRequest) {
pr.pageNumber = pageNumber
}
}
func WithPageSize(pageSize int) PagingOption {
return func(pr *PagingRequest) {
pr.pageSize = pageSize
}
}