cookie: trim spaces from domain, path, and value
Change-Id: I87f72850b429d3c180e270808492be9cacc18ae2
diff --git a/cookie/cookie.go b/cookie/cookie.go
index bd7e490..baa5569 100644
--- a/cookie/cookie.go
+++ b/cookie/cookie.go
@@ -63,15 +63,15 @@
}
c := http.Cookie{
- Domain: fields[0],
- Name: fields[5],
- Path: fields[2],
+ Domain: strings.TrimSpace(fields[0]),
+ Name: strings.TrimSpace(fields[5]),
+ Path: strings.TrimSpace(fields[2]),
Expires: time.Unix(exp, 0),
Secure: fields[3] == "TRUE",
HttpOnly: httpOnly,
}
if len(fields) == 7 {
- c.Value = fields[6]
+ c.Value = strings.TrimSpace(fields[6])
}
result = append(result, &c)
diff --git a/cookie/cookie_test.go b/cookie/cookie_test.go
index 2aa4d53..e9162d7 100644
--- a/cookie/cookie_test.go
+++ b/cookie/cookie_test.go
@@ -17,16 +17,17 @@
import (
"bytes"
"net/http"
- "reflect"
"testing"
"time"
+
+ "github.com/kylelemons/godebug/pretty"
)
func TestParseCookieJar(t *testing.T) {
in := `# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file! Do not edit.
-#HttpOnly_login.netscape.com FALSE / FALSE 1467968199 XYZ
+#HttpOnly_login.netscape.com FALSE / FALSE 1467968199 XYZ
#HttpOnly_login.netscape.com FALSE / FALSE 1467968199 XYZ abc|pqr`
buf := bytes.NewBufferString(in)
@@ -55,7 +56,32 @@
},
}
- if !reflect.DeepEqual(got, want) {
- t.Errorf("got %#v, want %#v", got, want)
+ if diff := pretty.Compare(want, got); diff != "" {
+ t.Errorf("got diff %s", diff)
+ }
+}
+
+func TestSpaceDomain(t *testing.T) {
+ in := "hostname.domain.com \tFALSE\t / \tTRUE\t2147483647\t o \t secret "
+ buf := bytes.NewBufferString(in)
+ got, err := ParseCookieJar(buf)
+ if err != nil {
+ t.Fatalf("ParseCookieJar: %v", err)
+ }
+
+ want := []*http.Cookie{
+ {
+ Domain: "hostname.domain.com",
+ Path: "/",
+ Secure: true,
+ Expires: time.Unix(2147483647, 0),
+ Name: "o",
+ Value: "secret",
+ HttpOnly: false,
+ },
+ }
+
+ if diff := pretty.Compare(want, got); diff != "" {
+ t.Errorf("got diff %s", diff)
}
}