Skip to content

Commit

Permalink
Handle 'since' in addition to 'page' for pagination response (#2135)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlangdon committed Nov 27, 2021
1 parent 59e8a37 commit c6b75c1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
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

0 comments on commit c6b75c1

Please sign in to comment.