-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
168 additions
and
24 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
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,42 @@ | ||
package desec | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/peterhellberg/link" | ||
) | ||
|
||
// Cursors allows to retrieve the next (or previous) page. | ||
// https://desec.readthedocs.io/en/latest/dns/rrsets.html#pagination | ||
type Cursors struct { | ||
First string | ||
Prev string | ||
Next string | ||
} | ||
|
||
func parseCursor(h http.Header) (*Cursors, error) { | ||
links := link.ParseHeader(h) | ||
|
||
c := &Cursors{} | ||
|
||
for s, l := range links { | ||
uri, err := url.ParseRequestURI(l.URI) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
query := uri.Query() | ||
|
||
switch s { | ||
case "first": | ||
c.First = query.Get("cursor") | ||
case "prev": | ||
c.Prev = query.Get("cursor") | ||
case "next": | ||
c.Next = query.Get("cursor") | ||
} | ||
} | ||
|
||
return c, nil | ||
} |
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,51 @@ | ||
package desec | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_parseCursor(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
header string | ||
expected *Cursors | ||
}{ | ||
{ | ||
desc: "all cursors", | ||
header: `<https://desec.io/api/v1/domains/{domain}/rrsets/?cursor=>; rel="first", <https://desec.io/api/v1/domains/{domain}/rrsets/?cursor=:prev_cursor>; rel="prev", <https://desec.io/api/v1/domains/{domain}/rrsets/?cursor=:next_cursor>; rel="next"`, | ||
expected: &Cursors{First: "", Prev: ":prev_cursor", Next: ":next_cursor"}, | ||
}, | ||
{ | ||
desc: "first page", | ||
header: `<https://desec.io/api/v1/domains/{domain}/rrsets/?cursor=>; rel="first", <https://desec.io/api/v1/domains/{domain}/rrsets/?cursor=:next_cursor>; rel="next"`, | ||
expected: &Cursors{First: "", Prev: "", Next: ":next_cursor"}, | ||
}, | ||
{ | ||
desc: "last page", | ||
header: `<https://desec.io/api/v1/domains/{domain}/rrsets/?cursor=>; rel="first", <https://desec.io/api/v1/domains/{domain}/rrsets/?cursor=:prev_cursor>; rel="prev"`, | ||
expected: &Cursors{First: "", Prev: ":prev_cursor", Next: ""}, | ||
}, | ||
{ | ||
desc: "empty", | ||
header: ``, | ||
expected: &Cursors{First: "", Prev: "", Next: ""}, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
t.Run(test.desc, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
h := http.Header{} | ||
h.Set("Link", test.header) | ||
|
||
cursor, err := parseCursor(h) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, test.expected, cursor) | ||
}) | ||
} | ||
} |
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