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

Handle 'since' in addition to 'page' for pagination response #2135

Merged
merged 3 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,18 @@ func (r *Response) populatePageValues() {
}

page := q.Get("page")
since := q.Get("since")
before := q.Get("before")
after := q.Get("after")
if page == "" && before == "" && after == "" {

if page == "" && before == "" && after == "" && since == "" {
continue
}

if since != "" {
page = since
}

for _, segment := range segments[1:] {
switch strings.TrimSpace(segment) {
case `rel="next"`:
Expand Down
68 changes: 68 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,35 @@ func TestResponse_populatePageValues(t *testing.T) {
}
}

func TestResponse_populateSinceValues(t *testing.T) {
r := http.Response{
Header: http.Header{
"Link": {`<https://api.github.com/?since=1>; rel="first",` +
` <https://api.github.com/?since=2>; rel="prev",` +
` <https://api.github.com/?since=4>; rel="next",` +
` <https://api.github.com/?since=5>; rel="last"`,
},
},
}

response := newResponse(&r)
if got, want := response.FirstPage, 1; got != want {
t.Errorf("response.FirstPage: %v, want %v", got, want)
}
if got, want := response.PrevPage, 2; want != got {
t.Errorf("response.PrevPage: %v, want %v", got, want)
}
if got, want := response.NextPage, 4; want != got {
t.Errorf("response.NextPage: %v, want %v", got, want)
}
if got, want := response.LastPage, 5; want != got {
t.Errorf("response.LastPage: %v, want %v", got, want)
}
if got, want := response.NextPageToken, ""; want != got {
t.Errorf("response.NextPageToken: %v, want %v", got, want)
}
}

func TestResponse_cursorPagination(t *testing.T) {
r := http.Response{
Header: http.Header{
Expand Down Expand Up @@ -761,6 +790,45 @@ func TestResponse_populatePageValues_invalid(t *testing.T) {
}
}

func TestResponse_populateSinceValues_invalid(t *testing.T) {
r := http.Response{
Header: http.Header{
"Link": {`<https://api.github.com/?since=1>,` +
`<https://api.github.com/?since=abc>; rel="first",` +
`https://api.github.com/?since=2; rel="prev",` +
`<https://api.github.com/>; rel="next",` +
`<https://api.github.com/?since=>; rel="last"`,
},
},
}

response := newResponse(&r)
if got, want := response.FirstPage, 0; got != want {
t.Errorf("response.FirstPage: %v, want %v", got, want)
}
if got, want := response.PrevPage, 0; got != want {
t.Errorf("response.PrevPage: %v, want %v", got, want)
}
if got, want := response.NextPage, 0; got != want {
t.Errorf("response.NextPage: %v, want %v", got, want)
}
if got, want := response.LastPage, 0; got != want {
t.Errorf("response.LastPage: %v, want %v", got, want)
}

// more invalid URLs
r = http.Response{
Header: http.Header{
"Link": {`<https://api.github.com/%?since=2>; rel="first"`},
},
}

response = newResponse(&r)
if got, want := response.FirstPage, 0; got != want {
t.Errorf("response.FirstPage: %v, want %v", got, want)
}
}

func TestDo(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down