From d8b94adbc6e8055baef4defc10d73d1be11c3660 Mon Sep 17 00:00:00 2001 From: Adam Lehechka <42357034+alehechka@users.noreply.github.com> Date: Wed, 11 May 2022 20:57:04 -0500 Subject: [PATCH] Unit Tests and a gin example (#11) * move gin example to dedicated example folder * included tests * add ContentType middleware function * update comments and func names * createResourceIdentifier unit tests * more unit tests --- documentation-wip.md | 51 +------ examples/gin.go | 68 +++++++++ jsonapi/constants.go | 8 + jsonapi/included_test.go | 45 ++++++ jsonapi/links.go | 32 ++-- jsonapi/links_internal_test.go | 197 ++++++++++++++++++++++++ jsonapi/node_test.go | 15 ++ jsonapi/relationships.go | 10 +- jsonapi/relationships_test.go | 231 +++++++++++++++++++++++++++++ middleware/gin_contentType.go | 12 ++ middleware/gin_contentType_test.go | 21 +++ middleware/gin_pagination.go | 4 +- middleware/gin_pagination_test.go | 4 +- 13 files changed, 624 insertions(+), 74 deletions(-) create mode 100644 examples/gin.go create mode 100644 jsonapi/included_test.go create mode 100644 jsonapi/links_internal_test.go create mode 100644 jsonapi/relationships_test.go create mode 100644 middleware/gin_contentType.go create mode 100644 middleware/gin_contentType_test.go diff --git a/documentation-wip.md b/documentation-wip.md index 34ec67a..5450fa6 100644 --- a/documentation-wip.md +++ b/documentation-wip.md @@ -22,56 +22,7 @@ go get github.com/alehechka/go-jsonapi This library does not create any unnecessary marshalling assumptions, it will simply take in `Response` or `CollectionResponse` objects and transform them into JSON:API structs that keep the same provided object as the `Attributes` value. This allows an overlaying marshalling implementation to take over after transformation. -
Example Implementation - -```go -package main - -import ( - "github.com/gin-gonic/gin" - "github.com/alehechka/go-jsonapi" -) - -func main() { - engine := gin.Default() - - engine.GET("/record", getRecords) - engine.GET("/record/:id", getRecord) - - engine.Run() -} - -type Record struct { - RecordID string `json:"-"` - // variables -} - -func (record Record) ID() string { - return record.RecordID -} - -func (record Record) Type() string { - return "records" -} - -func getRecords(ctx *gin.Context) { - records := getRecordsFromDatabase() - - ctx.JSON(200, jsonapi.CreateCollectionResponse(ctx.Request)(jsonapi.CollectionResponse{ - Nodes: records, - })) -} - -func getRecord(ctx *gin.Context) { - record := getRecordFromDatabase(ctx.Param("id")) - - ctx.JSON(200, jsonapi.CreateResponse(ctx.Request)(jsonapi.Response{ - Node: record, - })) -} -``` - -
+View examples here: [/examples](/examples) ### Interface Methods diff --git a/examples/gin.go b/examples/gin.go new file mode 100644 index 0000000..a99125a --- /dev/null +++ b/examples/gin.go @@ -0,0 +1,68 @@ +package examples + +import ( + "github.com/alehechka/go-jsonapi/jsonapi" + "github.com/gin-gonic/gin" +) + +func main() { + engine := gin.Default() + + engine.GET("/record", getRecords) + engine.GET("/record/:id", getRecord) + + engine.Run() +} + +type Record struct { + RecordID string `json:"-"` + FirstName string `json:"firstName"` + Age int `json:"age"` + IsAdmin bool `json:"isAdmin"` +} + +func (record Record) ID() string { + return record.RecordID +} + +func (record Record) Type() string { + return "records" +} + +func getRecords(ctx *gin.Context) { + + ctx.JSON(200, jsonapi.CreateCollectionResponse(ctx.Request)(jsonapi.CollectionResponse{ + Nodes: records(), + })) +} + +func getRecord(ctx *gin.Context) { + records := records() + recordID := ctx.Param("id") + + var record Record + for _, r := range records { + if r.RecordID == recordID { + record = r + } + } + + ctx.JSON(200, jsonapi.CreateResponse(ctx.Request)(jsonapi.Response{ + Node: record, + })) +} + +func records() []Record { + return []Record{{ + RecordID: "1234", + FirstName: "Joe", + Age: 25, + IsAdmin: true, + }, + { + RecordID: "4321", + FirstName: "Sally", + Age: 30, + IsAdmin: true, + }} +} diff --git a/jsonapi/constants.go b/jsonapi/constants.go index f39e48b..42a598c 100644 --- a/jsonapi/constants.go +++ b/jsonapi/constants.go @@ -21,6 +21,14 @@ const ( // Include query parameter used to request extra resources to include in response const Include string = "include" +// HTTP Header keys and values +const ( + // ContentType is the standard Content-Type header. + ContentType string = "Content-Type" + // MediaType is the standard JSON:API media type for the Content-Type header. + MediaType string = "application/vnd.api+json" +) + // Standard HTTP Headers const ( // ForwardedPrefix represents the prefix that is dropped when proxied through rest-api diff --git a/jsonapi/included_test.go b/jsonapi/included_test.go new file mode 100644 index 0000000..3ccff60 --- /dev/null +++ b/jsonapi/included_test.go @@ -0,0 +1,45 @@ +package jsonapi + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_transformIncluded_Nil(t *testing.T) { + included := transformIncluded([]Node{}, nil, baseURL) + + assert.Nil(t, included) +} + +func Test_transformIncluded(t *testing.T) { + included := transformIncluded([]Node{testObject}, []Node{}, baseURL) + + assert.NotNil(t, included) + assert.Equal(t, 1, len(included)) + + include := included[0] + + assert.Equal(t, testObject.ID(), include.ID) + assert.Equal(t, testObject.Type(), include.Type) + assert.Equal(t, testObject, include.Attributes) + assert.Equal(t, testObject.Meta(), include.Meta) + + assert.NotNil(t, include.Links) + assert.NotNil(t, include.Links[SelfKey]) + assert.Equal(t, baseURL+testObject.Links()[SelfKey].Href, include.Links[SelfKey]) +} + +func Test_transformIncludedNode(t *testing.T) { + include := transformIncludedNode(testObject, baseURL) + + assert.NotNil(t, include) + assert.Equal(t, testObject.ID(), include.ID) + assert.Equal(t, testObject.Type(), include.Type) + assert.Equal(t, testObject, include.Attributes) + assert.Equal(t, testObject.Meta(), include.Meta) + + assert.NotNil(t, include.Links) + assert.NotNil(t, include.Links[SelfKey]) + assert.Equal(t, baseURL+testObject.Links()[SelfKey].Href, include.Links[SelfKey]) +} diff --git a/jsonapi/links.go b/jsonapi/links.go index a1ab222..1b949e1 100644 --- a/jsonapi/links.go +++ b/jsonapi/links.go @@ -43,11 +43,11 @@ type Link struct { Queries Queries `json:"-"` } -// Links is a map of JsonAPILink objects +// Links is a map of Link objects type Links map[string]Link -// LinkMap should have values of type JsonAPILink or string -type LinkMap map[string]interface{} // JsonAPILink | string +// LinkMap should have values of type Link or string +type LinkMap map[string]interface{} // Link | string type Linkable interface { Links() Links @@ -81,7 +81,7 @@ func TransformLink(jsonLink Link, baseURL string) (link interface{}) { } func appendBaseURL(link Link, baseURL string) Link { - // only append baseURL if href is a relative URL + if IsRelativeURL(link.Href) { link.Href = fmt.Sprintf("%s%s", baseURL, link.Href) } @@ -90,21 +90,23 @@ func appendBaseURL(link Link, baseURL string) Link { } func substitutePathParams(link Link) Link { - if link.Params != nil && len(link.Params) > 0 { - pathParts := strings.Split(link.Href, "/") - - for index, pathPart := range pathParts { - if strings.HasPrefix(pathPart, ":") { - paramString := strings.TrimPrefix(pathPart, ":") - if param, exists := link.Params[paramString]; exists { - pathParts[index] = fmt.Sprintf("%v", param) - } + if link.Params == nil && len(link.Params) == 0 { + return link + } + + pathParts := strings.Split(link.Href, "/") + + for index, pathPart := range pathParts { + if strings.HasPrefix(pathPart, ":") { + paramString := strings.TrimPrefix(pathPart, ":") + if param, exists := link.Params[paramString]; exists { + pathParts[index] = fmt.Sprintf("%v", param) } } - - link.Href = strings.Join(pathParts, "/") } + link.Href = strings.Join(pathParts, "/") + return link } diff --git a/jsonapi/links_internal_test.go b/jsonapi/links_internal_test.go new file mode 100644 index 0000000..1b3654e --- /dev/null +++ b/jsonapi/links_internal_test.go @@ -0,0 +1,197 @@ +package jsonapi + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_appendBaseURL_RelativeURL(t *testing.T) { + link := Link{ + Href: "/example", + } + + transformed := appendBaseURL(link, baseURL) + + assert.NotEqual(t, link, transformed) + assert.Equal(t, baseURL+link.Href, transformed.Href) +} + +func Test_appendBaseURL_AbsoluteURL(t *testing.T) { + link := Link{ + Href: "http://absolute.example.com/example", + } + + transformed := appendBaseURL(link, baseURL) + + assert.Equal(t, link, transformed) + assert.Equal(t, link.Href, transformed.Href) +} + +func Test_substitutePathParams_NilParams(t *testing.T) { + link := Link{ + Href: "/example", + } + + transformed := substitutePathParams(link) + + assert.Equal(t, link, transformed) +} + +func Test_substitutePathParams_EmptyParams(t *testing.T) { + link := Link{ + Href: "/example", + Params: make(Params), + } + + transformed := substitutePathParams(link) + + assert.Equal(t, link, transformed) +} + +func Test_substitutePathParams_WithParams_NoPathParams(t *testing.T) { + link := Link{ + Href: "/example", + Params: Params{ + "id": 123, + }, + } + + transformed := substitutePathParams(link) + + assert.Equal(t, link, transformed) +} + +func Test_substitutePathParams_WithParams_WithPathParams(t *testing.T) { + link := Link{ + Href: "/example/:id", + Params: Params{ + "id": 123, + }, + } + + transformed := substitutePathParams(link) + + assert.NotEqual(t, link, transformed) + assert.Equal(t, "/example/123", transformed.Href) +} + +func Test_substitutePathParams_WithParams_WithExtraPathParams(t *testing.T) { + link := Link{ + Href: "/example/:id/children/:childID", + Params: Params{ + "id": 123, + }, + } + + transformed := substitutePathParams(link) + + assert.NotEqual(t, link, transformed) + assert.Equal(t, "/example/123/children/:childID", transformed.Href) +} + +func Test_substitutePathParams_WithParams_AbsoluteURL(t *testing.T) { + link := Link{ + Href: "http://example.com/example/:id", + Params: Params{ + "id": 123, + }, + } + + transformed := substitutePathParams(link) + + assert.NotEqual(t, link, transformed) + assert.Equal(t, "http://example.com/example/123", transformed.Href) +} + +func Test_appendQueryParams_InvalidURL(t *testing.T) { + link := Link{ + Href: "postgres://user:abc{DEf1=ghi@example.com:5432/db?sslmode=require", + Params: Params{ + "abc": 123, + }, + Queries: Queries{ + PageLimit.String(): 10, + }, + } + + transformed := appendQueryParams(link) + + assert.Equal(t, link, transformed) +} + +func Test_appendQueryParams_NoQueries(t *testing.T) { + link := Link{ + Href: "/example", + } + + transformed := appendQueryParams(link) + + assert.Equal(t, link, transformed) +} + +func Test_appendQueryParams_WithQueries(t *testing.T) { + link := Link{ + Href: "/example", + Queries: Queries{ + PageSize.String(): 123, + }, + } + + transformed := appendQueryParams(link) + + assert.NotEqual(t, link, transformed) + assert.Equal(t, link.Href+"?page[size]=123", transformed.Href) +} + +func Test_appendQueryParams_WithExistingQueries(t *testing.T) { + link := Link{ + Href: "/example?page[number]=5", + Queries: Queries{ + PageSize.String(): 123, + }, + } + + transformed := appendQueryParams(link) + + assert.NotEqual(t, link, transformed) + assert.Equal(t, link.Href+"&page[size]=123", transformed.Href) +} + +func Test_stringOrLinkObject_NilMeta(t *testing.T) { + link := Link{ + Href: "/example", + } + + transformed := stringOrLinkObject(link) + + href, ok := transformed.(string) + assert.True(t, ok) + assert.Equal(t, href, "/example") +} + +func Test_stringOrLinkObject_EmptyMeta(t *testing.T) { + link := Link{ + Href: "/example", + Meta: make(Meta), + } + + transformed := stringOrLinkObject(link) + + href, ok := transformed.(string) + assert.True(t, ok) + assert.Equal(t, href, "/example") +} + +func Test_stringOrLinkObject_WithMeta(t *testing.T) { + link := Link{ + Href: "/example", + Meta: Meta{"something": "cool"}, + } + + transformed := stringOrLinkObject(link) + + transformedLink, ok := transformed.(Link) + assert.True(t, ok) + assert.Equal(t, link, transformedLink) +} diff --git a/jsonapi/node_test.go b/jsonapi/node_test.go index 65e16eb..255dccc 100644 --- a/jsonapi/node_test.go +++ b/jsonapi/node_test.go @@ -77,6 +77,21 @@ func (d testStructMethods) Relationships() map[string]interface{} { } } +func (d testStructMethods) Data() interface{} { + return d +} + +func (d testStructMethods) RelationshipLinks(parentID string) Links { + return Links{ + SelfKey: Link{ + Href: "/path/to/resource/:id/child", + Params: Params{ + "id": parentID, + }, + }, + } +} + func Test_transformNode_Methods(t *testing.T) { node := testStructMethods{ TestID: "1234", diff --git a/jsonapi/relationships.go b/jsonapi/relationships.go index 688017a..27b2c3a 100644 --- a/jsonapi/relationships.go +++ b/jsonapi/relationships.go @@ -10,20 +10,20 @@ type internalResourceIdentifier struct { type internalRelationship struct { Links LinkMap `json:"links,omitempty"` - Data interface{} `json:"data"` // ResourceIdentifier | []ResourceIdentifier + Data interface{} `json:"data"` // internalResourceIdentifier | []internalResourceIdentifier Meta interface{} `json:"meta,omitempty"` } -type Relationship interface { +type Nodeable interface { Data() interface{} // Node | []Node } -type Relationable interface { +type Relationshipable interface { Relationships() map[string]interface{} // Node | []Node } func transformRelationships(node Node, baseURL string) (map[string]internalRelationship, []Node) { - if relationshipNode, isRelationable := node.(Relationable); isRelationable { + if relationshipNode, isRelationshipable := node.(Relationshipable); isRelationshipable { relationships := relationshipNode.Relationships() @@ -64,7 +64,7 @@ func transformRelationship(relationship interface{}, parentID string, baseURL st } func transformRelationshipData(r interface{}) (interface{}, []Node) { - if relationship, isRelationship := r.(Relationship); isRelationship { + if relationship, isNodeable := r.(Nodeable); isNodeable { return transformRelationNodes(relationship.Data()) } return transformRelationNodes(r) diff --git a/jsonapi/relationships_test.go b/jsonapi/relationships_test.go new file mode 100644 index 0000000..65cb9dd --- /dev/null +++ b/jsonapi/relationships_test.go @@ -0,0 +1,231 @@ +package jsonapi + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_transformRelationships_Relationshipable(t *testing.T) { + relationships, included := transformRelationships(testObject, baseURL) + + assert.NotNil(t, relationships) + + assert.NotNil(t, relationships["tests"]) + tests := relationships["tests"] + assert.Nil(t, tests.Meta) + assert.Nil(t, tests.Links) + assert.NotNil(t, tests.Data) + testsIdentifiers, ok := tests.Data.([]internalResourceIdentifier) + assert.True(t, ok) + assert.Equal(t, testObject.TestData[0].ID(), testsIdentifiers[0].ID) + assert.Equal(t, testObject.TestData[0].Type(), testsIdentifiers[0].Type) + assert.Nil(t, testsIdentifiers[0].Meta) + + assert.NotNil(t, relationships["test"]) + test := relationships["test"] + testIdentifiers, ok := test.Data.(internalResourceIdentifier) + assert.True(t, ok) + assert.Equal(t, testObject.SingleTest.ID(), testIdentifiers.ID) + assert.Equal(t, testObject.SingleTest.Type(), testIdentifiers.Type) + assert.Nil(t, testIdentifiers.Meta) + + assert.NotNil(t, included) + assert.Equal(t, 2, len(included)) + assert.Equal(t, testObject.TestData[0], included[0]) + assert.Equal(t, testObject.SingleTest, included[1]) +} + +func Test_transformRelationships_Nil(t *testing.T) { + test := testStruct{ + TestID: "1234", + Number: 123, + IsTest: true, + } + + relationships, included := transformRelationships(test, baseURL) + + assert.Nil(t, relationships) + assert.Nil(t, included) +} + +func Test_transformRelationship(t *testing.T) { + test := testStruct{ + TestID: "1234", + Number: 123, + IsTest: true, + } + + relationship, included := transformRelationship(test, "4321", baseURL) + + assert.NotNil(t, relationship) + assert.Nil(t, relationship.Links) + assert.Nil(t, relationship.Meta) + + identifier, ok := relationship.Data.(internalResourceIdentifier) + assert.True(t, ok) + assert.Equal(t, test.ID(), identifier.ID) + assert.Equal(t, test.Type(), identifier.Type) + + assert.NotNil(t, included) + assert.Equal(t, 1, len(included)) + assert.Equal(t, test, included[0]) +} + +func Test_transformRelationship_Methods(t *testing.T) { + + relationship, included := transformRelationship(testObject, "4321", baseURL) + + assert.NotNil(t, relationship) + + identifier, ok := relationship.Data.(internalResourceIdentifier) + assert.True(t, ok) + assert.Equal(t, testObject.ID(), identifier.ID) + assert.Equal(t, testObject.Type(), identifier.Type) + + assert.NotNil(t, relationship.Meta) + assert.Equal(t, testObject.Meta(), relationship.Meta) + + assert.NotNil(t, relationship.Links) + assert.NotNil(t, relationship.Links[SelfKey]) + assert.Equal(t, baseURL+"/path/to/resource/4321/child", relationship.Links[SelfKey]) + + assert.NotNil(t, included) + assert.Equal(t, 1, len(included)) + assert.Equal(t, testObject, included[0]) +} + +func Test_transformRelationshipData_Nodeable(t *testing.T) { + resource, included := transformRelationshipData(testObject) + + assert.NotNil(t, resource) + identifier, ok := resource.(internalResourceIdentifier) + assert.True(t, ok) + assert.Equal(t, testObject.ID(), identifier.ID) + assert.Equal(t, testObject.Type(), identifier.Type) + assert.NotNil(t, identifier.Meta) + assert.Equal(t, testObject.Meta(), identifier.Meta) + + assert.NotNil(t, included) + assert.Equal(t, 1, len(included)) + assert.Equal(t, testObject, included[0]) +} + +func Test_transformRelationshipData(t *testing.T) { + test := testStruct{ + TestID: "1234", + Number: 123, + IsTest: true, + } + + resource, included := transformRelationshipData(test) + + assert.NotNil(t, resource) + identifier, ok := resource.(internalResourceIdentifier) + assert.True(t, ok) + assert.Equal(t, test.ID(), identifier.ID) + assert.Equal(t, test.Type(), identifier.Type) + assert.Nil(t, identifier.Meta) + + assert.NotNil(t, included) + assert.Equal(t, 1, len(included)) + assert.Equal(t, test, included[0]) +} + +func Test_transformRelationNodes_PtrSlice(t *testing.T) { + resource, included := transformRelationNodes(&testObjects) + + assert.NotNil(t, resource) + identifiers, ok := resource.([]internalResourceIdentifier) + assert.True(t, ok) + assert.Equal(t, 1, len(identifiers)) + assert.Equal(t, testObject.ID(), identifiers[0].ID) + assert.Equal(t, testObject.Type(), identifiers[0].Type) + assert.NotNil(t, identifiers[0].Meta) + assert.Equal(t, testObject.Meta(), identifiers[0].Meta) + + assert.NotNil(t, included) + assert.Equal(t, 1, len(included)) + assert.Equal(t, testObject, included[0]) +} + +func Test_transformRelationNodes_PtrStruct(t *testing.T) { + resource, included := transformRelationNodes(&testObject) + + assert.NotNil(t, resource) + identifier, ok := resource.(internalResourceIdentifier) + assert.True(t, ok) + assert.Equal(t, testObject.ID(), identifier.ID) + assert.Equal(t, testObject.Type(), identifier.Type) + assert.NotNil(t, identifier.Meta) + assert.Equal(t, testObject.Meta(), identifier.Meta) + + assert.NotNil(t, included) + assert.Equal(t, 1, len(included)) + assert.Equal(t, testObject, included[0]) +} + +func Test_transformRelationNodes_Slice(t *testing.T) { + resource, included := transformRelationNodes(testObjects) + + assert.NotNil(t, resource) + identifiers, ok := resource.([]internalResourceIdentifier) + assert.True(t, ok) + assert.Equal(t, 1, len(identifiers)) + assert.Equal(t, testObject.ID(), identifiers[0].ID) + assert.Equal(t, testObject.Type(), identifiers[0].Type) + assert.NotNil(t, identifiers[0].Meta) + assert.Equal(t, testObject.Meta(), identifiers[0].Meta) + + assert.NotNil(t, included) + assert.Equal(t, 1, len(included)) + assert.Equal(t, testObject, included[0]) +} + +func Test_transformRelationNodes_Struct(t *testing.T) { + resource, included := transformRelationNodes(testObject) + + assert.NotNil(t, resource) + identifier, ok := resource.(internalResourceIdentifier) + assert.True(t, ok) + assert.Equal(t, testObject.ID(), identifier.ID) + assert.Equal(t, testObject.Type(), identifier.Type) + assert.NotNil(t, identifier.Meta) + assert.Equal(t, testObject.Meta(), identifier.Meta) + + assert.NotNil(t, included) + assert.Equal(t, 1, len(included)) + assert.Equal(t, testObject, included[0]) +} + +func Test_transformRelationNodes_Nil(t *testing.T) { + resource, included := transformRelationNodes("testObject") + + assert.Nil(t, resource) + assert.Nil(t, included) +} + +func Test_createResourceIdentifier(t *testing.T) { + test := testStruct{ + TestID: "1234", + Number: 123, + IsTest: true, + } + + resource := createResourceIdentifier(test) + + assert.NotNil(t, resource) + assert.Equal(t, testObject.ID(), resource.ID) + assert.Equal(t, testObject.Type(), resource.Type) + assert.Nil(t, resource.Meta) +} + +func Test_createResourceIdentifier_Meta(t *testing.T) { + resource := createResourceIdentifier(testObject) + + assert.NotNil(t, resource) + assert.Equal(t, testObject.ID(), resource.ID) + assert.Equal(t, testObject.Type(), resource.Type) + assert.NotNil(t, resource.Meta) + assert.Equal(t, testObject.Meta(), resource.Meta) +} diff --git a/middleware/gin_contentType.go b/middleware/gin_contentType.go new file mode 100644 index 0000000..156eef8 --- /dev/null +++ b/middleware/gin_contentType.go @@ -0,0 +1,12 @@ +package middleware + +import ( + "github.com/alehechka/go-jsonapi/jsonapi" + "github.com/gin-gonic/gin" +) + +// ContentType is a middleware function to set the Content-Type header to the official JSON:API header. +func ContentType(c *gin.Context) { + c.Header(jsonapi.ContentType, jsonapi.MediaType) + c.Next() +} diff --git a/middleware/gin_contentType_test.go b/middleware/gin_contentType_test.go new file mode 100644 index 0000000..3f3c85b --- /dev/null +++ b/middleware/gin_contentType_test.go @@ -0,0 +1,21 @@ +package middleware_test + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/alehechka/go-jsonapi/jsonapi" + "github.com/alehechka/go-jsonapi/middleware" + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" +) + +func Test_ContentType(t *testing.T) { + c, _ := gin.CreateTestContext(httptest.NewRecorder()) + c.Request, _ = http.NewRequest("GET", "/example", nil) + + middleware.ContentType(c) + + assert.Equal(t, jsonapi.MediaType, c.Writer.Header().Get(jsonapi.ContentType)) +} diff --git a/middleware/gin_pagination.go b/middleware/gin_pagination.go index 08aea49..a688166 100644 --- a/middleware/gin_pagination.go +++ b/middleware/gin_pagination.go @@ -35,8 +35,8 @@ func UnsupportedPagination(unsupportedOptions ...jsonapi.PaginationOption) gin.H } } -// ExceedsMaximumPaginationSize will short-circuit if one of the provided pagination query options exceeds the provided maximum. -func ExceedsMaximumPaginationSize(maxSize int) gin.HandlerFunc { +// MaximumPaginationSize will short-circuit if one of the provided pagination query options exceeds the provided maximum. +func MaximumPaginationSize(maxSize int) gin.HandlerFunc { return func(c *gin.Context) { errs := jsonapi.CheckExceedsMaximumPaginationSize(c.Request)(maxSize) diff --git a/middleware/gin_pagination_test.go b/middleware/gin_pagination_test.go index ce9c583..521f1d4 100644 --- a/middleware/gin_pagination_test.go +++ b/middleware/gin_pagination_test.go @@ -51,7 +51,7 @@ func Test_ExceedsMaximumPaginationSize_Abort(t *testing.T) { c, _ := gin.CreateTestContext(httptest.NewRecorder()) c.Request, _ = http.NewRequest("GET", "/?page[limit]=1000&page[size]=10", nil) - middleware.ExceedsMaximumPaginationSize(100)(c) + middleware.MaximumPaginationSize(100)(c) assert.Equal(t, true, c.IsAborted()) } @@ -60,7 +60,7 @@ func Test_ExceedsMaximumPaginationSize_Next(t *testing.T) { c, _ := gin.CreateTestContext(httptest.NewRecorder()) c.Request, _ = http.NewRequest("GET", "/?page[offset]=10&page[size]=10", nil) - middleware.ExceedsMaximumPaginationSize(100)(c) + middleware.MaximumPaginationSize(100)(c) assert.Equal(t, false, c.IsAborted()) }