Fix to return nil if parse empty string
This commit is contained in:
parent
236df730ed
commit
0d46e1b419
8
main.go
8
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 {
|
|||
// <url>; rel="foo", <url>; rel="bar"; wat="dis"
|
||||
// returning a slice of Link structs
|
||||
func Parse(raw string) Links {
|
||||
links := make(Links, 0)
|
||||
var links Links
|
||||
|
||||
// One chunk: <url>; rel="foo"
|
||||
for _, chunk := range strings.Split(raw, ",") {
|
||||
|
@ -110,8 +114,10 @@ func Parse(raw string) Links {
|
|||
|
||||
}
|
||||
|
||||
if link.URL != "" && link.Rel != "" {
|
||||
links = append(links, link)
|
||||
}
|
||||
}
|
||||
|
||||
return links
|
||||
}
|
||||
|
|
|
@ -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 := "<https://api.github.com/user/9287/repos?page=1&per_page=100>; rel=\"prev\"; pet=\"cat\""
|
||||
links := Parse(header)
|
||||
|
|
Loading…
Reference in New Issue
Block a user