Skip to content

Commit 898d7b1

Browse files
committed
Better test pagination and fix bug
1 parent 140511a commit 898d7b1

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

pagination.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func NewLink(linkheader string) *Link {
2323
link := &Link{}
2424
links := strings.SplitN(linkheader, ",", 2)
2525
for _, page := range links {
26-
data := strings.SplitN(page, ";", 3)
26+
data := strings.SplitN(page, ";", 4)
2727

28-
pagelink := strings.TrimLeft(data[0], "<")
28+
pagelink := strings.TrimLeft(strings.TrimSpace(data[0]), "<")
2929
pagelink = strings.TrimRight(pagelink, ">")
3030

31-
pagetype := strings.Trim(strings.Split(data[1], "=")[1], "\"")
32-
results, err := strconv.ParseBool(strings.Split(data[2], "=")[1])
31+
pagetype := strings.Trim(strings.Split(data[1], "=")[1], `"`)
32+
results, err := strconv.ParseBool(strings.Trim(strings.Split(strings.TrimSpace(data[2]), "=")[1], `"`))
3333
if err != nil {
3434
results = false
3535
}

pagination_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sentry
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLinkPagination(t *testing.T) {
8+
examplelink := `<https://sentry.io/api/0/projects/the-interstellar-jurisdiction/pump-station/releases/?&cursor=100:-1:1>; rel="previous"; results="true"; cursor="100:-1:1", <https://sentry.io/api/0/projects/the-interstellar-jurisdiction/pump-station/releases/?&cursor=100:1:0>; rel="next"; results="true"; cursor="100:1:0`
9+
10+
link := NewLink(examplelink)
11+
12+
if link.Next.URL != "https://sentry.io/api/0/projects/the-interstellar-jurisdiction/pump-station/releases/?&cursor=100:1:0" {
13+
t.Errorf("Link next isnt correct: %s", link.Next.URL)
14+
}
15+
16+
if link.Previous.URL != "https://sentry.io/api/0/projects/the-interstellar-jurisdiction/pump-station/releases/?&cursor=100:-1:1" {
17+
t.Errorf("Link previous isnt correct: %s", link.Previous.URL)
18+
}
19+
20+
if !link.Next.Results {
21+
t.Error("Results should be set to true for next")
22+
}
23+
24+
if !link.Previous.Results {
25+
t.Error("Results should be set to true for previous")
26+
}
27+
28+
}

0 commit comments

Comments
 (0)