github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/net/http/cookie_test.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package http 6 7 import ( 8 "encoding/json" 9 "fmt" 10 "reflect" 11 "testing" 12 "time" 13 ) 14 15 var writeSetCookiesTests = []struct { 16 Cookie *Cookie 17 Raw string 18 }{ 19 { 20 &Cookie{Name: "cookie-1", Value: "v$1"}, 21 "cookie-1=v$1", 22 }, 23 { 24 &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600}, 25 "cookie-2=two; Max-Age=3600", 26 }, 27 { 28 &Cookie{Name: "cookie-3", Value: "three", Domain: ".example.com"}, 29 "cookie-3=three; Domain=example.com", 30 }, 31 { 32 &Cookie{Name: "cookie-4", Value: "four", Path: "/restricted/"}, 33 "cookie-4=four; Path=/restricted/", 34 }, 35 { 36 &Cookie{Name: "cookie-5", Value: "five", Domain: "wrong;bad.abc"}, 37 "cookie-5=five", 38 }, 39 { 40 &Cookie{Name: "cookie-6", Value: "six", Domain: "bad-.abc"}, 41 "cookie-6=six", 42 }, 43 { 44 &Cookie{Name: "cookie-7", Value: "seven", Domain: "127.0.0.1"}, 45 "cookie-7=seven; Domain=127.0.0.1", 46 }, 47 { 48 &Cookie{Name: "cookie-8", Value: "eight", Domain: "::1"}, 49 "cookie-8=eight", 50 }, 51 } 52 53 func TestWriteSetCookies(t *testing.T) { 54 for i, tt := range writeSetCookiesTests { 55 if g, e := tt.Cookie.String(), tt.Raw; g != e { 56 t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, e, g) 57 continue 58 } 59 } 60 } 61 62 type headerOnlyResponseWriter Header 63 64 func (ho headerOnlyResponseWriter) Header() Header { 65 return Header(ho) 66 } 67 68 func (ho headerOnlyResponseWriter) Write([]byte) (int, error) { 69 panic("NOIMPL") 70 } 71 72 func (ho headerOnlyResponseWriter) WriteHeader(int) { 73 panic("NOIMPL") 74 } 75 76 func TestSetCookie(t *testing.T) { 77 m := make(Header) 78 SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-1", Value: "one", Path: "/restricted/"}) 79 SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600}) 80 if l := len(m["Set-Cookie"]); l != 2 { 81 t.Fatalf("expected %d cookies, got %d", 2, l) 82 } 83 if g, e := m["Set-Cookie"][0], "cookie-1=one; Path=/restricted/"; g != e { 84 t.Errorf("cookie #1: want %q, got %q", e, g) 85 } 86 if g, e := m["Set-Cookie"][1], "cookie-2=two; Max-Age=3600"; g != e { 87 t.Errorf("cookie #2: want %q, got %q", e, g) 88 } 89 } 90 91 var addCookieTests = []struct { 92 Cookies []*Cookie 93 Raw string 94 }{ 95 { 96 []*Cookie{}, 97 "", 98 }, 99 { 100 []*Cookie{{Name: "cookie-1", Value: "v$1"}}, 101 "cookie-1=v$1", 102 }, 103 { 104 []*Cookie{ 105 {Name: "cookie-1", Value: "v$1"}, 106 {Name: "cookie-2", Value: "v$2"}, 107 {Name: "cookie-3", Value: "v$3"}, 108 }, 109 "cookie-1=v$1; cookie-2=v$2; cookie-3=v$3", 110 }, 111 } 112 113 func TestAddCookie(t *testing.T) { 114 for i, tt := range addCookieTests { 115 req, _ := NewRequest("GET", "http://example.com/", nil) 116 for _, c := range tt.Cookies { 117 req.AddCookie(c) 118 } 119 if g := req.Header.Get("Cookie"); g != tt.Raw { 120 t.Errorf("Test %d:\nwant: %s\n got: %s\n", i, tt.Raw, g) 121 continue 122 } 123 } 124 } 125 126 var readSetCookiesTests = []struct { 127 Header Header 128 Cookies []*Cookie 129 }{ 130 { 131 Header{"Set-Cookie": {"Cookie-1=v$1"}}, 132 []*Cookie{{Name: "Cookie-1", Value: "v$1", Raw: "Cookie-1=v$1"}}, 133 }, 134 { 135 Header{"Set-Cookie": {"NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"}}, 136 []*Cookie{{ 137 Name: "NID", 138 Value: "99=YsDT5i3E-CXax-", 139 Path: "/", 140 Domain: ".google.ch", 141 HttpOnly: true, 142 Expires: time.Date(2011, 11, 23, 1, 5, 3, 0, time.UTC), 143 RawExpires: "Wed, 23-Nov-2011 01:05:03 GMT", 144 Raw: "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly", 145 }}, 146 }, 147 { 148 Header{"Set-Cookie": {".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"}}, 149 []*Cookie{{ 150 Name: ".ASPXAUTH", 151 Value: "7E3AA", 152 Path: "/", 153 Expires: time.Date(2012, 3, 7, 14, 25, 6, 0, time.UTC), 154 RawExpires: "Wed, 07-Mar-2012 14:25:06 GMT", 155 HttpOnly: true, 156 Raw: ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly", 157 }}, 158 }, 159 { 160 Header{"Set-Cookie": {"ASP.NET_SessionId=foo; path=/; HttpOnly"}}, 161 []*Cookie{{ 162 Name: "ASP.NET_SessionId", 163 Value: "foo", 164 Path: "/", 165 HttpOnly: true, 166 Raw: "ASP.NET_SessionId=foo; path=/; HttpOnly", 167 }}, 168 }, 169 170 // TODO(bradfitz): users have reported seeing this in the 171 // wild, but do browsers handle it? RFC 6265 just says "don't 172 // do that" (section 3) and then never mentions header folding 173 // again. 174 // Header{"Set-Cookie": {"ASP.NET_SessionId=foo; path=/; HttpOnly, .ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"}}, 175 } 176 177 func toJSON(v interface{}) string { 178 b, err := json.Marshal(v) 179 if err != nil { 180 return fmt.Sprintf("%#v", v) 181 } 182 return string(b) 183 } 184 185 func TestReadSetCookies(t *testing.T) { 186 for i, tt := range readSetCookiesTests { 187 for n := 0; n < 2; n++ { // to verify readSetCookies doesn't mutate its input 188 c := readSetCookies(tt.Header) 189 if !reflect.DeepEqual(c, tt.Cookies) { 190 t.Errorf("#%d readSetCookies: have\n%s\nwant\n%s\n", i, toJSON(c), toJSON(tt.Cookies)) 191 continue 192 } 193 } 194 } 195 } 196 197 var readCookiesTests = []struct { 198 Header Header 199 Filter string 200 Cookies []*Cookie 201 }{ 202 { 203 Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}}, 204 "", 205 []*Cookie{ 206 {Name: "Cookie-1", Value: "v$1"}, 207 {Name: "c2", Value: "v2"}, 208 }, 209 }, 210 { 211 Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}}, 212 "c2", 213 []*Cookie{ 214 {Name: "c2", Value: "v2"}, 215 }, 216 }, 217 { 218 Header{"Cookie": {"Cookie-1=v$1; c2=v2"}}, 219 "", 220 []*Cookie{ 221 {Name: "Cookie-1", Value: "v$1"}, 222 {Name: "c2", Value: "v2"}, 223 }, 224 }, 225 { 226 Header{"Cookie": {"Cookie-1=v$1; c2=v2"}}, 227 "c2", 228 []*Cookie{ 229 {Name: "c2", Value: "v2"}, 230 }, 231 }, 232 } 233 234 func TestReadCookies(t *testing.T) { 235 for i, tt := range readCookiesTests { 236 for n := 0; n < 2; n++ { // to verify readCookies doesn't mutate its input 237 c := readCookies(tt.Header, tt.Filter) 238 if !reflect.DeepEqual(c, tt.Cookies) { 239 t.Errorf("#%d readCookies:\nhave: %s\nwant: %s\n", i, toJSON(c), toJSON(tt.Cookies)) 240 continue 241 } 242 } 243 } 244 } 245 246 func TestCookieSanitizeValue(t *testing.T) { 247 tests := []struct { 248 in, want string 249 }{ 250 {"foo", "foo"}, 251 {"foo bar", "foobar"}, 252 {"\x00\x7e\x7f\x80", "\x7e"}, 253 {`"withquotes"`, "withquotes"}, 254 } 255 for _, tt := range tests { 256 if got := sanitizeCookieValue(tt.in); got != tt.want { 257 t.Errorf("sanitizeCookieValue(%q) = %q; want %q", tt.in, got, tt.want) 258 } 259 } 260 } 261 262 func TestCookieSanitizePath(t *testing.T) { 263 tests := []struct { 264 in, want string 265 }{ 266 {"/path", "/path"}, 267 {"/path with space/", "/path with space/"}, 268 {"/just;no;semicolon\x00orstuff/", "/justnosemicolonorstuff/"}, 269 } 270 for _, tt := range tests { 271 if got := sanitizeCookiePath(tt.in); got != tt.want { 272 t.Errorf("sanitizeCookiePath(%q) = %q; want %q", tt.in, got, tt.want) 273 } 274 } 275 }