diff options
Diffstat (limited to 'libgo/go/http/cookie_test.go')
-rw-r--r-- | libgo/go/http/cookie_test.go | 163 |
1 files changed, 125 insertions, 38 deletions
diff --git a/libgo/go/http/cookie_test.go b/libgo/go/http/cookie_test.go index a3ae85cd6c9..d7aeda0be17 100644 --- a/libgo/go/http/cookie_test.go +++ b/libgo/go/http/cookie_test.go @@ -5,61 +5,104 @@ package http import ( - "bytes" "fmt" "json" + "os" "reflect" "testing" + "time" ) - var writeSetCookiesTests = []struct { - Cookies []*Cookie - Raw string + Cookie *Cookie + Raw string }{ { - []*Cookie{ - &Cookie{Name: "cookie-1", Value: "v$1"}, - &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600}, - &Cookie{Name: "cookie-3", Value: "three", Domain: ".example.com"}, - &Cookie{Name: "cookie-4", Value: "four", Path: "/restricted/"}, - }, - "Set-Cookie: cookie-1=v$1\r\n" + - "Set-Cookie: cookie-2=two; Max-Age=3600\r\n" + - "Set-Cookie: cookie-3=three; Domain=.example.com\r\n" + - "Set-Cookie: cookie-4=four; Path=/restricted/\r\n", + &Cookie{Name: "cookie-1", Value: "v$1"}, + "cookie-1=v$1", + }, + { + &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600}, + "cookie-2=two; Max-Age=3600", + }, + { + &Cookie{Name: "cookie-3", Value: "three", Domain: ".example.com"}, + "cookie-3=three; Domain=.example.com", + }, + { + &Cookie{Name: "cookie-4", Value: "four", Path: "/restricted/"}, + "cookie-4=four; Path=/restricted/", }, } func TestWriteSetCookies(t *testing.T) { for i, tt := range writeSetCookiesTests { - var w bytes.Buffer - writeSetCookies(&w, tt.Cookies) - seen := string(w.Bytes()) - if seen != tt.Raw { - t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, tt.Raw, seen) + if g, e := tt.Cookie.String(), tt.Raw; g != e { + t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, e, g) continue } } } -var writeCookiesTests = []struct { +type headerOnlyResponseWriter Header + +func (ho headerOnlyResponseWriter) Header() Header { + return Header(ho) +} + +func (ho headerOnlyResponseWriter) Write([]byte) (int, os.Error) { + panic("NOIMPL") +} + +func (ho headerOnlyResponseWriter) WriteHeader(int) { + panic("NOIMPL") +} + +func TestSetCookie(t *testing.T) { + m := make(Header) + SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-1", Value: "one", Path: "/restricted/"}) + SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600}) + if l := len(m["Set-Cookie"]); l != 2 { + t.Fatalf("expected %d cookies, got %d", 2, l) + } + if g, e := m["Set-Cookie"][0], "cookie-1=one; Path=/restricted/"; g != e { + t.Errorf("cookie #1: want %q, got %q", e, g) + } + if g, e := m["Set-Cookie"][1], "cookie-2=two; Max-Age=3600"; g != e { + t.Errorf("cookie #2: want %q, got %q", e, g) + } +} + +var addCookieTests = []struct { Cookies []*Cookie Raw string }{ { + []*Cookie{}, + "", + }, + { []*Cookie{&Cookie{Name: "cookie-1", Value: "v$1"}}, - "Cookie: cookie-1=v$1\r\n", + "cookie-1=v$1", + }, + { + []*Cookie{ + &Cookie{Name: "cookie-1", Value: "v$1"}, + &Cookie{Name: "cookie-2", Value: "v$2"}, + &Cookie{Name: "cookie-3", Value: "v$3"}, + }, + "cookie-1=v$1; cookie-2=v$2; cookie-3=v$3", }, } -func TestWriteCookies(t *testing.T) { - for i, tt := range writeCookiesTests { - var w bytes.Buffer - writeCookies(&w, tt.Cookies) - seen := string(w.Bytes()) - if seen != tt.Raw { - t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, tt.Raw, seen) +func TestAddCookie(t *testing.T) { + for i, tt := range addCookieTests { + req, _ := NewRequest("GET", "http://example.com/", nil) + for _, c := range tt.Cookies { + req.AddCookie(c) + } + if g := req.Header.Get("Cookie"); g != tt.Raw { + t.Errorf("Test %d:\nwant: %s\n got: %s\n", i, tt.Raw, g) continue } } @@ -73,6 +116,19 @@ var readSetCookiesTests = []struct { Header{"Set-Cookie": {"Cookie-1=v$1"}}, []*Cookie{&Cookie{Name: "Cookie-1", Value: "v$1", Raw: "Cookie-1=v$1"}}, }, + { + Header{"Set-Cookie": {"NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"}}, + []*Cookie{&Cookie{ + Name: "NID", + Value: "99=YsDT5i3E-CXax-", + Path: "/", + Domain: ".google.ch", + HttpOnly: true, + Expires: time.Time{Year: 2011, Month: 11, Day: 23, Hour: 1, Minute: 5, Second: 3, Weekday: 3, ZoneOffset: 0, Zone: "GMT"}, + RawExpires: "Wed, 23-Nov-2011 01:05:03 GMT", + Raw: "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly", + }}, + }, } func toJSON(v interface{}) string { @@ -85,30 +141,61 @@ func toJSON(v interface{}) string { func TestReadSetCookies(t *testing.T) { for i, tt := range readSetCookiesTests { - c := readSetCookies(tt.Header) - if !reflect.DeepEqual(c, tt.Cookies) { - t.Errorf("#%d readSetCookies: have\n%s\nwant\n%s\n", i, toJSON(c), toJSON(tt.Cookies)) - continue + for n := 0; n < 2; n++ { // to verify readSetCookies doesn't mutate its input + c := readSetCookies(tt.Header) + if !reflect.DeepEqual(c, tt.Cookies) { + t.Errorf("#%d readSetCookies: have\n%s\nwant\n%s\n", i, toJSON(c), toJSON(tt.Cookies)) + continue + } } } } var readCookiesTests = []struct { Header Header + Filter string Cookies []*Cookie }{ { - Header{"Cookie": {"Cookie-1=v$1"}}, - []*Cookie{&Cookie{Name: "Cookie-1", Value: "v$1"}}, + Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}}, + "", + []*Cookie{ + &Cookie{Name: "Cookie-1", Value: "v$1"}, + &Cookie{Name: "c2", Value: "v2"}, + }, + }, + { + Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}}, + "c2", + []*Cookie{ + &Cookie{Name: "c2", Value: "v2"}, + }, + }, + { + Header{"Cookie": {"Cookie-1=v$1; c2=v2"}}, + "", + []*Cookie{ + &Cookie{Name: "Cookie-1", Value: "v$1"}, + &Cookie{Name: "c2", Value: "v2"}, + }, + }, + { + Header{"Cookie": {"Cookie-1=v$1; c2=v2"}}, + "c2", + []*Cookie{ + &Cookie{Name: "c2", Value: "v2"}, + }, }, } func TestReadCookies(t *testing.T) { for i, tt := range readCookiesTests { - c := readCookies(tt.Header) - if !reflect.DeepEqual(c, tt.Cookies) { - t.Errorf("#%d readCookies: have\n%s\nwant\n%s\n", i, toJSON(c), toJSON(tt.Cookies)) - continue + for n := 0; n < 2; n++ { // to verify readCookies doesn't mutate its input + c := readCookies(tt.Header, tt.Filter) + if !reflect.DeepEqual(c, tt.Cookies) { + t.Errorf("#%d readCookies:\nhave: %s\nwant: %s\n", i, toJSON(c), toJSON(tt.Cookies)) + continue + } } } } |