diff --git a/examples_test.go b/examples_test.go index f6e4300..2dc48f4 100644 --- a/examples_test.go +++ b/examples_test.go @@ -61,3 +61,16 @@ func ExampleLink_String() { // Output: // Link: ; rel="next" } + +func ExampleLinks_String() { + + links := linkheader.Links{ + {URL: "http://example.com/page/3", Rel: "next"}, + {URL: "http://example.com/page/1", Rel: "last"}, + } + + fmt.Printf("Link: %s\n", links.String()) + + // Output: + // Link: ; rel="next", ; rel="last" +} diff --git a/main.go b/main.go index a86f2ed..fa2cb7a 100644 --- a/main.go +++ b/main.go @@ -33,8 +33,7 @@ func (l Link) Param(key string) (string, error) { return "", fmt.Errorf("Could not find param '%s'", key) } -// String returns the string representation of a Link header -// for use in HTTP responses +// String returns the string representation of a link func (l Link) String() string { p := make([]string, len(l.Params)) @@ -61,6 +60,16 @@ func (l Links) FilterByRel(r string) Links { return links } +// String returns the string representation of multiple Links +// for use in HTTP responses etc +func (l Links) String() string { + strs := make([]string, 0) + for _, link := range l { + strs = append(strs, link.String()) + } + return strings.Join(strs, ", ") +} + // Parse parses a raw Link header in the form: // ; rel="foo", ; rel="bar"; wat="dis" // returning a slice of Link structs diff --git a/main_test.go b/main_test.go index 8ee2389..1b569f3 100644 --- a/main_test.go +++ b/main_test.go @@ -131,3 +131,18 @@ func TestLinkToString(t *testing.T) { t.Errorf("Re-parsed link header should have matching rel, but has `%s`", parsed[0].Rel) } } + +func TestLinksToString(t *testing.T) { + ls := Links{ + {URL: "http://example.com/page/3", Rel: "next"}, + {URL: "http://example.com/page/1", Rel: "last"}, + } + + have := ls.String() + + want := "; rel=\"next\", ; rel=\"last\"" + + if have != want { + t.Errorf("Want `%s`, have `%s`", want, have) + } +}