2015-12-10 15:33:58 +01:00
|
|
|
package linkheader_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2016-03-28 17:25:11 +02:00
|
|
|
"github.com/tomnomnom/linkheader"
|
2015-12-10 15:33:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleParse() {
|
2015-12-10 15:43:51 +01:00
|
|
|
header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
|
|
|
|
"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
|
2015-12-10 15:33:58 +01:00
|
|
|
links := linkheader.Parse(header)
|
|
|
|
|
|
|
|
for _, link := range links {
|
|
|
|
fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// URL: https://api.github.com/user/58276/repos?page=2; Rel: next
|
|
|
|
// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleParseMultiple() {
|
|
|
|
headers := []string{
|
|
|
|
"<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"",
|
|
|
|
"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\"",
|
|
|
|
}
|
|
|
|
links := linkheader.ParseMultiple(headers)
|
|
|
|
|
|
|
|
for _, link := range links {
|
|
|
|
fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// URL: https://api.github.com/user/58276/repos?page=2; Rel: next
|
|
|
|
// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
|
|
|
|
}
|
|
|
|
|
2015-12-10 15:43:51 +01:00
|
|
|
func ExampleLinks_FilterByRel() {
|
|
|
|
header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
|
|
|
|
"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
|
2015-12-10 15:33:58 +01:00
|
|
|
links := linkheader.Parse(header)
|
|
|
|
|
|
|
|
for _, link := range links.FilterByRel("last") {
|
|
|
|
fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
|
|
|
|
|
|
|
|
}
|
2016-06-17 01:23:46 +02:00
|
|
|
|
|
|
|
func ExampleLink_String() {
|
|
|
|
link := linkheader.Link{
|
|
|
|
URL: "http://example.com/page/2",
|
|
|
|
Rel: "next",
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Link: %s\n", link.String())
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Link: <http://example.com/page/2>; rel="next"
|
|
|
|
}
|