-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add token pagination link header parser (#722)
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package keysetpagination | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/peterhellberg/link" | ||
) | ||
|
||
// PaginationResult represents a parsed result of the link HTTP header. | ||
type PaginationResult struct { | ||
// NextToken is the next page token. If it's empty, there is no next page. | ||
NextToken string | ||
|
||
// FirstToken is the first page token. | ||
FirstToken string | ||
} | ||
|
||
// ParseHeader parses the response header's Link. | ||
func ParseHeader(r *http.Response) *PaginationResult { | ||
links := link.ParseResponse(r) | ||
return &PaginationResult{ | ||
NextToken: findRel(links, "next"), | ||
FirstToken: findRel(links, "first"), | ||
} | ||
} | ||
|
||
func findRel(links link.Group, rel string) string { | ||
for idx, l := range links { | ||
if idx == rel { | ||
parsed, err := url.Parse(l.URI) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
return parsed.Query().Get("page_token") | ||
} | ||
} | ||
|
||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package keysetpagination | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseHeader(t *testing.T) { | ||
u, err := url.Parse("https://www.ory.sh/") | ||
require.NoError(t, err) | ||
|
||
t.Run("has next page", func(t *testing.T) { | ||
p := &Paginator{ | ||
defaultToken: StringPageToken("default"), | ||
token: StringPageToken("next"), | ||
size: 2, | ||
} | ||
|
||
r := httptest.NewRecorder() | ||
Header(r, u, p) | ||
|
||
result := ParseHeader(&http.Response{Header: r.Header()}) | ||
assert.Equal(t, "next", result.NextToken, r.Header()) | ||
assert.Equal(t, "default", result.FirstToken, r.Header()) | ||
}) | ||
|
||
t.Run("is last page", func(t *testing.T) { | ||
p := &Paginator{ | ||
defaultToken: StringPageToken("default"), | ||
size: 1, | ||
isLast: true, | ||
} | ||
|
||
r := httptest.NewRecorder() | ||
Header(r, u, p) | ||
|
||
result := ParseHeader(&http.Response{Header: r.Header()}) | ||
assert.Equal(t, "", result.NextToken, r.Header()) | ||
assert.Equal(t, "default", result.FirstToken, r.Header()) | ||
}) | ||
} |