diff --git a/main.go b/main.go index fa2cb7a..2a82a13 100644 --- a/main.go +++ b/main.go @@ -23,20 +23,20 @@ func (l Link) HasParam(key string) bool { return false } -// Param returns the value of a parameter, or an error on failure -func (l Link) Param(key string) (string, error) { +// Param returns the value of a parameter if it exists +func (l Link) Param(key string) string { for k, v := range l.Params { if key == k { - return v, nil + return v } } - return "", fmt.Errorf("Could not find param '%s'", key) + return "" } // String returns the string representation of a link func (l Link) String() string { - p := make([]string, len(l.Params)) + p := make([]string, 0, len(l.Params)) for k, v := range l.Params { p = append(p, fmt.Sprintf("%s=\"%s\"", k, v)) } diff --git a/main_test.go b/main_test.go index 1b569f3..7b7ac77 100644 --- a/main_test.go +++ b/main_test.go @@ -51,17 +51,14 @@ func TestLinkMethods(t *testing.T) { t.Errorf("Link should not have param 'foo'") } - val, err := link.Param("pet") - if err != nil { - t.Errorf("Error value should be nil") - } + val := link.Param("pet") if val != "cat" { t.Errorf("Link should have param pet=\"cat\"") } - _, err = link.Param("foo") - if err == nil { - t.Errorf("Error value should not be nil") + val = link.Param("foo") + if val != "" { + t.Errorf("Link should not have value for param 'foo'") } } @@ -108,15 +105,13 @@ func TestLinkToString(t *testing.T) { l := Link{ URL: "http://example.com/page/2", Rel: "next", + Params: map[string]string{ + "foo": "bar", + }, } have := l.String() - want := "; rel=\"next\"" - if have != want { - t.Errorf("Want `%s`; have `%s`", want, have) - } - parsed := Parse(have) if len(parsed) != 1 { @@ -130,6 +125,10 @@ func TestLinkToString(t *testing.T) { if parsed[0].Rel != l.Rel { t.Errorf("Re-parsed link header should have matching rel, but has `%s`", parsed[0].Rel) } + + if parsed[0].Param("foo") != "bar" { + t.Errorf("Re-parsed link header should have foo=\"bar\" but doesn't") + } } func TestLinksToString(t *testing.T) {