From 0d46e1b419057da6e02f9e1b956145c3bc652892 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Thu, 4 May 2017 16:38:13 +0900 Subject: [PATCH 1/2] Fix to return nil if parse empty string --- main.go | 10 ++++++++-- main_test.go | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index ce3b24a..944637e 100644 --- a/main.go +++ b/main.go @@ -63,6 +63,10 @@ func (l Links) FilterByRel(r string) Links { // String returns the string representation of multiple Links // for use in HTTP responses etc func (l Links) String() string { + if l == nil { + return fmt.Sprint(nil) + } + var strs []string for _, link := range l { strs = append(strs, link.String()) @@ -74,7 +78,7 @@ func (l Links) String() string { // ; rel="foo", ; rel="bar"; wat="dis" // returning a slice of Link structs func Parse(raw string) Links { - links := make(Links, 0) + var links Links // One chunk: ; rel="foo" for _, chunk := range strings.Split(raw, ",") { @@ -110,7 +114,9 @@ func Parse(raw string) Links { } - links = append(links, link) + if link.URL != "" && link.Rel != "" { + links = append(links, link) + } } return links diff --git a/main_test.go b/main_test.go index 6c8a4a0..d9eef51 100644 --- a/main_test.go +++ b/main_test.go @@ -38,6 +38,13 @@ func TestSimple(t *testing.T) { } +func TestEmpty(t *testing.T) { + links := Parse("") + if links != nil { + t.Errorf("Return value should be nil, but was %s", len(links)) + } +} + func TestLinkMethods(t *testing.T) { header := "; rel=\"prev\"; pet=\"cat\"" links := Parse(header) From 1baa7d93fa7c82c3eed1693f8c0f21a5fc57b08d Mon Sep 17 00:00:00 2001 From: Tom Hudson Date: Thu, 4 May 2017 09:43:35 +0000 Subject: [PATCH 2/2] Fixes links with no rel attribute; removes old Go versions from travis --- .travis.yml | 10 +--------- README.mkd | 2 +- main.go | 2 +- main_test.go | 14 ++++++++++++++ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index d4b0de6..cfda086 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,6 @@ language: go go: - - 1.5 - 1.6 + - 1.7 - tip - -install: - - ./script/bootstrap - -script: - - ./script/test -# - ./script/lint - diff --git a/README.mkd b/README.mkd index 8331934..2a949ca 100644 --- a/README.mkd +++ b/README.mkd @@ -1,6 +1,6 @@ # Golang Link Header Parser -Library for parsing HTTP Link headers. Requires Go 1.2 or higher. +Library for parsing HTTP Link headers. Requires Go 1.6 or higher. Docs can be found on [the GoDoc page](https://godoc.org/github.com/tomnomnom/linkheader). diff --git a/main.go b/main.go index 944637e..57b8c17 100644 --- a/main.go +++ b/main.go @@ -114,7 +114,7 @@ func Parse(raw string) Links { } - if link.URL != "" && link.Rel != "" { + if link.URL != "" { links = append(links, link) } } diff --git a/main_test.go b/main_test.go index d9eef51..2d75426 100644 --- a/main_test.go +++ b/main_test.go @@ -45,6 +45,20 @@ func TestEmpty(t *testing.T) { } } +// Although not often seen in the wild, the grammar in RFC 5988 suggests that it's +// valid for a link header to have nothing but a URL. +func TestNoRel(t *testing.T) { + links := Parse("") + + if len(links) != 1 { + t.Fatalf("Length of links should be 1, but was %d", len(links)) + } + + if links[0].URL != "http://example.com" { + t.Errorf("URL should be http://example.com, but was %s", links[0].URL) + } +} + func TestLinkMethods(t *testing.T) { header := "; rel=\"prev\"; pet=\"cat\"" links := Parse(header)