diff --git a/godo_test.go b/godo_test.go index b1777d4a..f1a2eafe 100644 --- a/godo_test.go +++ b/godo_test.go @@ -611,6 +611,32 @@ func checkCurrentPage(t *testing.T, resp *Response, expectedPage int) { } } +func checkNextPageToken(t *testing.T, resp *Response, expectedNextPageToken string) { + t.Helper() + links := resp.Links + pageToken, err := links.NextPageToken() + if err != nil { + t.Fatal(err) + } + + if pageToken != expectedNextPageToken { + t.Fatalf("expected next page token to be '%s', was '%s'", expectedNextPageToken, pageToken) + } +} + +func checkPreviousPageToken(t *testing.T, resp *Response, expectedPreviousPageToken string) { + t.Helper() + links := resp.Links + pageToken, err := links.PrevPageToken() + if err != nil { + t.Fatal(err) + } + + if pageToken != expectedPreviousPageToken { + t.Fatalf("expected previous page token to be '%s', was '%s'", expectedPreviousPageToken, pageToken) + } +} + func TestDo_completion_callback(t *testing.T) { setup() defer teardown() diff --git a/links_test.go b/links_test.go index 2c7753d3..303b5e6a 100644 --- a/links_test.go +++ b/links_test.go @@ -130,25 +130,32 @@ func TestLinks_ParseMissing(t *testing.T) { func TestLinks_ParseURL(t *testing.T) { type linkTest struct { - name, url string - expected int + name, url string + expectedPage int + expectedPageToken string } linkTests := []linkTest{ { - name: "prev", - url: "https://api.digitalocean.com/v2/droplets/?page=1", - expected: 1, + name: "prev", + url: "https://api.digitalocean.com/v2/droplets/?page=1", + expectedPage: 1, }, { - name: "last", - url: "https://api.digitalocean.com/v2/droplets/?page=5", - expected: 5, + name: "last", + url: "https://api.digitalocean.com/v2/droplets/?page=5", + expectedPage: 5, }, { - name: "nexta", - url: "https://api.digitalocean.com/v2/droplets/?page=2", - expected: 2, + name: "next", + url: "https://api.digitalocean.com/v2/droplets/?page=2", + expectedPage: 2, + }, + { + name: "page token", + url: "https://api.digitalocean.com/v2/droplets/?page=2&page_token=aaa", + expectedPage: 2, + expectedPageToken: "aaa", }, } @@ -158,9 +165,15 @@ func TestLinks_ParseURL(t *testing.T) { t.Fatal(err) } - if p != lT.expected { + if p != lT.expectedPage { t.Errorf("expected page for '%s' to be '%d', was '%d'", - lT.url, lT.expected, p) + lT.url, lT.expectedPage, p) + } + + pageToken, err := pageTokenFromURL(lT.url) + if pageToken != lT.expectedPageToken { + t.Errorf("expected pageToken for '%s' to be '%s', was '%s'", + lT.url, lT.expectedPageToken, pageToken) } } @@ -197,3 +210,47 @@ func TestLinks_ParseEmptyString(t *testing.T) { } } } + +func TestLinks_NextPageToken(t *testing.T) { + t.Run("happy token", func(t *testing.T) { + checkNextPageToken(t, &Response{Links: &Links{ + Pages: &Pages{ + Next: "https://api.digitalocean.com/v2/droplets/?page_token=aaa", + }, + }}, "aaa") + }) + t.Run("empty token", func(t *testing.T) { + checkNextPageToken(t, &Response{Links: &Links{ + Pages: &Pages{ + Next: "https://api.digitalocean.com/v2/droplets/", + }, + }}, "") + }) + t.Run("no next page", func(t *testing.T) { + checkNextPageToken(t, &Response{Links: &Links{ + Pages: &Pages{}, + }}, "") + }) +} + +func TestLinks_ParseNextPageToken(t *testing.T) { + t.Run("happy token", func(t *testing.T) { + checkPreviousPageToken(t, &Response{Links: &Links{ + Pages: &Pages{ + Prev: "https://api.digitalocean.com/v2/droplets/?page_token=aaa", + }, + }}, "aaa") + }) + t.Run("empty token", func(t *testing.T) { + checkPreviousPageToken(t, &Response{Links: &Links{ + Pages: &Pages{ + Prev: "https://api.digitalocean.com/v2/droplets/", + }, + }}, "") + }) + t.Run("no next page", func(t *testing.T) { + checkPreviousPageToken(t, &Response{Links: &Links{ + Pages: &Pages{}, + }}, "") + }) +}