Source file
src/net/http/cookie_test.go
1
2
3
4
5 package http
6
7 import (
8 "encoding/json"
9 "errors"
10 "fmt"
11 "log"
12 "os"
13 "reflect"
14 "strings"
15 "testing"
16 "time"
17 )
18
19 var writeSetCookiesTests = []struct {
20 Cookie *Cookie
21 Raw string
22 }{
23 {
24 &Cookie{Name: "cookie-1", Value: "v$1"},
25 "cookie-1=v$1",
26 },
27 {
28 &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600},
29 "cookie-2=two; Max-Age=3600",
30 },
31 {
32 &Cookie{Name: "cookie-3", Value: "three", Domain: ".example.com"},
33 "cookie-3=three; Domain=example.com",
34 },
35 {
36 &Cookie{Name: "cookie-4", Value: "four", Path: "/restricted/"},
37 "cookie-4=four; Path=/restricted/",
38 },
39 {
40 &Cookie{Name: "cookie-5", Value: "five", Domain: "wrong;bad.abc"},
41 "cookie-5=five",
42 },
43 {
44 &Cookie{Name: "cookie-6", Value: "six", Domain: "bad-.abc"},
45 "cookie-6=six",
46 },
47 {
48 &Cookie{Name: "cookie-7", Value: "seven", Domain: "127.0.0.1"},
49 "cookie-7=seven; Domain=127.0.0.1",
50 },
51 {
52 &Cookie{Name: "cookie-8", Value: "eight", Domain: "::1"},
53 "cookie-8=eight",
54 },
55 {
56 &Cookie{Name: "cookie-9", Value: "expiring", Expires: time.Unix(1257894000, 0)},
57 "cookie-9=expiring; Expires=Tue, 10 Nov 2009 23:00:00 GMT",
58 },
59
60 {
61 &Cookie{Name: "cookie-10", Value: "expiring-1601", Expires: time.Date(1601, 1, 1, 1, 1, 1, 1, time.UTC)},
62 "cookie-10=expiring-1601; Expires=Mon, 01 Jan 1601 01:01:01 GMT",
63 },
64 {
65 &Cookie{Name: "cookie-11", Value: "invalid-expiry", Expires: time.Date(1600, 1, 1, 1, 1, 1, 1, time.UTC)},
66 "cookie-11=invalid-expiry",
67 },
68 {
69 &Cookie{Name: "cookie-12", Value: "samesite-default", SameSite: SameSiteDefaultMode},
70 "cookie-12=samesite-default",
71 },
72 {
73 &Cookie{Name: "cookie-13", Value: "samesite-lax", SameSite: SameSiteLaxMode},
74 "cookie-13=samesite-lax; SameSite=Lax",
75 },
76 {
77 &Cookie{Name: "cookie-14", Value: "samesite-strict", SameSite: SameSiteStrictMode},
78 "cookie-14=samesite-strict; SameSite=Strict",
79 },
80 {
81 &Cookie{Name: "cookie-15", Value: "samesite-none", SameSite: SameSiteNoneMode},
82 "cookie-15=samesite-none; SameSite=None",
83 },
84
85
86 {
87 &Cookie{Name: "special-1", Value: "a z"},
88 `special-1="a z"`,
89 },
90 {
91 &Cookie{Name: "special-2", Value: " z"},
92 `special-2=" z"`,
93 },
94 {
95 &Cookie{Name: "special-3", Value: "a "},
96 `special-3="a "`,
97 },
98 {
99 &Cookie{Name: "special-4", Value: " "},
100 `special-4=" "`,
101 },
102 {
103 &Cookie{Name: "special-5", Value: "a,z"},
104 `special-5="a,z"`,
105 },
106 {
107 &Cookie{Name: "special-6", Value: ",z"},
108 `special-6=",z"`,
109 },
110 {
111 &Cookie{Name: "special-7", Value: "a,"},
112 `special-7="a,"`,
113 },
114 {
115 &Cookie{Name: "special-8", Value: ","},
116 `special-8=","`,
117 },
118 {
119 &Cookie{Name: "empty-value", Value: ""},
120 `empty-value=`,
121 },
122 {
123 nil,
124 ``,
125 },
126 {
127 &Cookie{Name: ""},
128 ``,
129 },
130 {
131 &Cookie{Name: "\t"},
132 ``,
133 },
134 {
135 &Cookie{Name: "\r"},
136 ``,
137 },
138 {
139 &Cookie{Name: "a\nb", Value: "v"},
140 ``,
141 },
142 {
143 &Cookie{Name: "a\nb", Value: "v"},
144 ``,
145 },
146 {
147 &Cookie{Name: "a\rb", Value: "v"},
148 ``,
149 },
150
151 {
152 &Cookie{Name: "cookie", Value: "quoted", Quoted: true},
153 `cookie="quoted"`,
154 },
155 {
156 &Cookie{Name: "cookie", Value: "quoted with spaces", Quoted: true},
157 `cookie="quoted with spaces"`,
158 },
159 {
160 &Cookie{Name: "cookie", Value: "quoted,with,commas", Quoted: true},
161 `cookie="quoted,with,commas"`,
162 },
163 }
164
165 func TestWriteSetCookies(t *testing.T) {
166 defer log.SetOutput(os.Stderr)
167 var logbuf strings.Builder
168 log.SetOutput(&logbuf)
169
170 for i, tt := range writeSetCookiesTests {
171 if g, e := tt.Cookie.String(), tt.Raw; g != e {
172 t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, e, g)
173 }
174 }
175
176 if got, sub := logbuf.String(), "dropping domain attribute"; !strings.Contains(got, sub) {
177 t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
178 }
179 }
180
181 type headerOnlyResponseWriter Header
182
183 func (ho headerOnlyResponseWriter) Header() Header {
184 return Header(ho)
185 }
186
187 func (ho headerOnlyResponseWriter) Write([]byte) (int, error) {
188 panic("NOIMPL")
189 }
190
191 func (ho headerOnlyResponseWriter) WriteHeader(int) {
192 panic("NOIMPL")
193 }
194
195 func TestSetCookie(t *testing.T) {
196 m := make(Header)
197 SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-1", Value: "one", Path: "/restricted/"})
198 SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600})
199 if l := len(m["Set-Cookie"]); l != 2 {
200 t.Fatalf("expected %d cookies, got %d", 2, l)
201 }
202 if g, e := m["Set-Cookie"][0], "cookie-1=one; Path=/restricted/"; g != e {
203 t.Errorf("cookie #1: want %q, got %q", e, g)
204 }
205 if g, e := m["Set-Cookie"][1], "cookie-2=two; Max-Age=3600"; g != e {
206 t.Errorf("cookie #2: want %q, got %q", e, g)
207 }
208 }
209
210 var addCookieTests = []struct {
211 Cookies []*Cookie
212 Raw string
213 }{
214 {
215 []*Cookie{},
216 "",
217 },
218 {
219 []*Cookie{{Name: "cookie-1", Value: "v$1"}},
220 "cookie-1=v$1",
221 },
222 {
223 []*Cookie{
224 {Name: "cookie-1", Value: "v$1"},
225 {Name: "cookie-2", Value: "v$2"},
226 {Name: "cookie-3", Value: "v$3"},
227 },
228 "cookie-1=v$1; cookie-2=v$2; cookie-3=v$3",
229 },
230
231 {
232 []*Cookie{
233 {Name: "cookie-1", Value: "quoted", Quoted: true},
234 {Name: "cookie-2", Value: "quoted with spaces", Quoted: true},
235 {Name: "cookie-3", Value: "quoted,with,commas", Quoted: true},
236 },
237 `cookie-1="quoted"; cookie-2="quoted with spaces"; cookie-3="quoted,with,commas"`,
238 },
239 }
240
241 func TestAddCookie(t *testing.T) {
242 for i, tt := range addCookieTests {
243 req, _ := NewRequest("GET", "http://example.com/", nil)
244 for _, c := range tt.Cookies {
245 req.AddCookie(c)
246 }
247 if g := req.Header.Get("Cookie"); g != tt.Raw {
248 t.Errorf("Test %d:\nwant: %s\n got: %s\n", i, tt.Raw, g)
249 }
250 }
251 }
252
253 var readSetCookiesTests = []struct {
254 Header Header
255 Cookies []*Cookie
256 }{
257 {
258 Header{"Set-Cookie": {"Cookie-1=v$1"}},
259 []*Cookie{{Name: "Cookie-1", Value: "v$1", Raw: "Cookie-1=v$1"}},
260 },
261 {
262 Header{"Set-Cookie": {"NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"}},
263 []*Cookie{{
264 Name: "NID",
265 Value: "99=YsDT5i3E-CXax-",
266 Path: "/",
267 Domain: ".google.ch",
268 HttpOnly: true,
269 Expires: time.Date(2011, 11, 23, 1, 5, 3, 0, time.UTC),
270 RawExpires: "Wed, 23-Nov-2011 01:05:03 GMT",
271 Raw: "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly",
272 }},
273 },
274 {
275 Header{"Set-Cookie": {".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"}},
276 []*Cookie{{
277 Name: ".ASPXAUTH",
278 Value: "7E3AA",
279 Path: "/",
280 Expires: time.Date(2012, 3, 7, 14, 25, 6, 0, time.UTC),
281 RawExpires: "Wed, 07-Mar-2012 14:25:06 GMT",
282 HttpOnly: true,
283 Raw: ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly",
284 }},
285 },
286 {
287 Header{"Set-Cookie": {"ASP.NET_SessionId=foo; path=/; HttpOnly"}},
288 []*Cookie{{
289 Name: "ASP.NET_SessionId",
290 Value: "foo",
291 Path: "/",
292 HttpOnly: true,
293 Raw: "ASP.NET_SessionId=foo; path=/; HttpOnly",
294 }},
295 },
296 {
297 Header{"Set-Cookie": {"samesitedefault=foo; SameSite"}},
298 []*Cookie{{
299 Name: "samesitedefault",
300 Value: "foo",
301 SameSite: SameSiteDefaultMode,
302 Raw: "samesitedefault=foo; SameSite",
303 }},
304 },
305 {
306 Header{"Set-Cookie": {"samesiteinvalidisdefault=foo; SameSite=invalid"}},
307 []*Cookie{{
308 Name: "samesiteinvalidisdefault",
309 Value: "foo",
310 SameSite: SameSiteDefaultMode,
311 Raw: "samesiteinvalidisdefault=foo; SameSite=invalid",
312 }},
313 },
314 {
315 Header{"Set-Cookie": {"samesitelax=foo; SameSite=Lax"}},
316 []*Cookie{{
317 Name: "samesitelax",
318 Value: "foo",
319 SameSite: SameSiteLaxMode,
320 Raw: "samesitelax=foo; SameSite=Lax",
321 }},
322 },
323 {
324 Header{"Set-Cookie": {"samesitestrict=foo; SameSite=Strict"}},
325 []*Cookie{{
326 Name: "samesitestrict",
327 Value: "foo",
328 SameSite: SameSiteStrictMode,
329 Raw: "samesitestrict=foo; SameSite=Strict",
330 }},
331 },
332 {
333 Header{"Set-Cookie": {"samesitenone=foo; SameSite=None"}},
334 []*Cookie{{
335 Name: "samesitenone",
336 Value: "foo",
337 SameSite: SameSiteNoneMode,
338 Raw: "samesitenone=foo; SameSite=None",
339 }},
340 },
341
342
343 {
344 Header{"Set-Cookie": {`special-1=a z`}},
345 []*Cookie{{Name: "special-1", Value: "a z", Raw: `special-1=a z`}},
346 },
347 {
348 Header{"Set-Cookie": {`special-2=" z"`}},
349 []*Cookie{{Name: "special-2", Value: " z", Quoted: true, Raw: `special-2=" z"`}},
350 },
351 {
352 Header{"Set-Cookie": {`special-3="a "`}},
353 []*Cookie{{Name: "special-3", Value: "a ", Quoted: true, Raw: `special-3="a "`}},
354 },
355 {
356 Header{"Set-Cookie": {`special-4=" "`}},
357 []*Cookie{{Name: "special-4", Value: " ", Quoted: true, Raw: `special-4=" "`}},
358 },
359 {
360 Header{"Set-Cookie": {`special-5=a,z`}},
361 []*Cookie{{Name: "special-5", Value: "a,z", Raw: `special-5=a,z`}},
362 },
363 {
364 Header{"Set-Cookie": {`special-6=",z"`}},
365 []*Cookie{{Name: "special-6", Value: ",z", Quoted: true, Raw: `special-6=",z"`}},
366 },
367 {
368 Header{"Set-Cookie": {`special-7=a,`}},
369 []*Cookie{{Name: "special-7", Value: "a,", Raw: `special-7=a,`}},
370 },
371 {
372 Header{"Set-Cookie": {`special-8=","`}},
373 []*Cookie{{Name: "special-8", Value: ",", Quoted: true, Raw: `special-8=","`}},
374 },
375
376
377 {
378 Header{"Set-Cookie": {`special-9 =","`}},
379 []*Cookie{{Name: "special-9", Value: ",", Quoted: true, Raw: `special-9 =","`}},
380 },
381
382 {
383 Header{"Set-Cookie": {`cookie="quoted"`}},
384 []*Cookie{{Name: "cookie", Value: "quoted", Quoted: true, Raw: `cookie="quoted"`}},
385 },
386
387
388
389
390
391
392 }
393
394 func toJSON(v any) string {
395 b, err := json.Marshal(v)
396 if err != nil {
397 return fmt.Sprintf("%#v", v)
398 }
399 return string(b)
400 }
401
402 func TestReadSetCookies(t *testing.T) {
403 for i, tt := range readSetCookiesTests {
404 for n := 0; n < 2; n++ {
405 c := readSetCookies(tt.Header)
406 if !reflect.DeepEqual(c, tt.Cookies) {
407 t.Errorf("#%d readSetCookies: have\n%s\nwant\n%s\n", i, toJSON(c), toJSON(tt.Cookies))
408 }
409 }
410 }
411 }
412
413 var readCookiesTests = []struct {
414 Header Header
415 Filter string
416 Cookies []*Cookie
417 }{
418 {
419 Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}},
420 "",
421 []*Cookie{
422 {Name: "Cookie-1", Value: "v$1"},
423 {Name: "c2", Value: "v2"},
424 },
425 },
426 {
427 Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}},
428 "c2",
429 []*Cookie{
430 {Name: "c2", Value: "v2"},
431 },
432 },
433 {
434 Header{"Cookie": {"Cookie-1=v$1; c2=v2"}},
435 "",
436 []*Cookie{
437 {Name: "Cookie-1", Value: "v$1"},
438 {Name: "c2", Value: "v2"},
439 },
440 },
441 {
442 Header{"Cookie": {"Cookie-1=v$1; c2=v2"}},
443 "c2",
444 []*Cookie{
445 {Name: "c2", Value: "v2"},
446 },
447 },
448 {
449 Header{"Cookie": {`Cookie-1="v$1"; c2="v2"`}},
450 "",
451 []*Cookie{
452 {Name: "Cookie-1", Value: "v$1", Quoted: true},
453 {Name: "c2", Value: "v2", Quoted: true},
454 },
455 },
456 {
457 Header{"Cookie": {`Cookie-1="v$1"; c2=v2;`}},
458 "",
459 []*Cookie{
460 {Name: "Cookie-1", Value: "v$1", Quoted: true},
461 {Name: "c2", Value: "v2"},
462 },
463 },
464 {
465 Header{"Cookie": {``}},
466 "",
467 []*Cookie{},
468 },
469 }
470
471 func TestReadCookies(t *testing.T) {
472 for i, tt := range readCookiesTests {
473 for n := 0; n < 2; n++ {
474 c := readCookies(tt.Header, tt.Filter)
475 if !reflect.DeepEqual(c, tt.Cookies) {
476 t.Errorf("#%d readCookies:\nhave: %s\nwant: %s\n", i, toJSON(c), toJSON(tt.Cookies))
477 }
478 }
479 }
480 }
481
482 func TestSetCookieDoubleQuotes(t *testing.T) {
483 res := &Response{Header: Header{}}
484 res.Header.Add("Set-Cookie", `quoted0=none; max-age=30`)
485 res.Header.Add("Set-Cookie", `quoted1="cookieValue"; max-age=31`)
486 res.Header.Add("Set-Cookie", `quoted2=cookieAV; max-age="32"`)
487 res.Header.Add("Set-Cookie", `quoted3="both"; max-age="33"`)
488 got := res.Cookies()
489 want := []*Cookie{
490 {Name: "quoted0", Value: "none", MaxAge: 30},
491 {Name: "quoted1", Value: "cookieValue", MaxAge: 31},
492 {Name: "quoted2", Value: "cookieAV"},
493 {Name: "quoted3", Value: "both"},
494 }
495 if len(got) != len(want) {
496 t.Fatalf("got %d cookies, want %d", len(got), len(want))
497 }
498 for i, w := range want {
499 g := got[i]
500 if g.Name != w.Name || g.Value != w.Value || g.MaxAge != w.MaxAge {
501 t.Errorf("cookie #%d:\ngot %v\nwant %v", i, g, w)
502 }
503 }
504 }
505
506 func TestCookieSanitizeValue(t *testing.T) {
507 defer log.SetOutput(os.Stderr)
508 var logbuf strings.Builder
509 log.SetOutput(&logbuf)
510
511 tests := []struct {
512 in string
513 quoted bool
514 want string
515 }{
516 {"foo", false, "foo"},
517 {"foo;bar", false, "foobar"},
518 {"foo\\bar", false, "foobar"},
519 {"foo\"bar", false, "foobar"},
520 {"\x00\x7e\x7f\x80", false, "\x7e"},
521 {`withquotes`, true, `"withquotes"`},
522 {`"withquotes"`, true, `"withquotes"`},
523 {"a z", false, `"a z"`},
524 {" z", false, `" z"`},
525 {"a ", false, `"a "`},
526 {"a,z", false, `"a,z"`},
527 {",z", false, `",z"`},
528 {"a,", false, `"a,"`},
529 }
530 for _, tt := range tests {
531 if got := sanitizeCookieValue(tt.in, tt.quoted); got != tt.want {
532 t.Errorf("sanitizeCookieValue(%q) = %q; want %q", tt.in, got, tt.want)
533 }
534 }
535
536 if got, sub := logbuf.String(), "dropping invalid bytes"; !strings.Contains(got, sub) {
537 t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
538 }
539 }
540
541 func TestCookieSanitizePath(t *testing.T) {
542 defer log.SetOutput(os.Stderr)
543 var logbuf strings.Builder
544 log.SetOutput(&logbuf)
545
546 tests := []struct {
547 in, want string
548 }{
549 {"/path", "/path"},
550 {"/path with space/", "/path with space/"},
551 {"/just;no;semicolon\x00orstuff/", "/justnosemicolonorstuff/"},
552 }
553 for _, tt := range tests {
554 if got := sanitizeCookiePath(tt.in); got != tt.want {
555 t.Errorf("sanitizeCookiePath(%q) = %q; want %q", tt.in, got, tt.want)
556 }
557 }
558
559 if got, sub := logbuf.String(), "dropping invalid bytes"; !strings.Contains(got, sub) {
560 t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
561 }
562 }
563
564 func TestCookieValid(t *testing.T) {
565 tests := []struct {
566 cookie *Cookie
567 valid bool
568 }{
569 {nil, false},
570 {&Cookie{Name: ""}, false},
571 {&Cookie{Name: "invalid-value", Value: "foo\"bar"}, false},
572 {&Cookie{Name: "invalid-path", Path: "/foo;bar/"}, false},
573 {&Cookie{Name: "invalid-domain", Domain: "example.com:80"}, false},
574 {&Cookie{Name: "invalid-expiry", Value: "", Expires: time.Date(1600, 1, 1, 1, 1, 1, 1, time.UTC)}, false},
575 {&Cookie{Name: "valid-empty"}, true},
576 {&Cookie{Name: "valid-expires", Value: "foo", Path: "/bar", Domain: "example.com", Expires: time.Unix(0, 0)}, true},
577 {&Cookie{Name: "valid-max-age", Value: "foo", Path: "/bar", Domain: "example.com", MaxAge: 60}, true},
578 {&Cookie{Name: "valid-all-fields", Value: "foo", Path: "/bar", Domain: "example.com", Expires: time.Unix(0, 0), MaxAge: 0}, true},
579 }
580
581 for _, tt := range tests {
582 err := tt.cookie.Valid()
583 if err != nil && tt.valid {
584 t.Errorf("%#v.Valid() returned error %v; want nil", tt.cookie, err)
585 }
586 if err == nil && !tt.valid {
587 t.Errorf("%#v.Valid() returned nil; want error", tt.cookie)
588 }
589 }
590 }
591
592 func BenchmarkCookieString(b *testing.B) {
593 const wantCookieString = `cookie-9=i3e01nf61b6t23bvfmplnanol3; Path=/restricted/; Domain=example.com; Expires=Tue, 10 Nov 2009 23:00:00 GMT; Max-Age=3600`
594 c := &Cookie{
595 Name: "cookie-9",
596 Value: "i3e01nf61b6t23bvfmplnanol3",
597 Expires: time.Unix(1257894000, 0),
598 Path: "/restricted/",
599 Domain: ".example.com",
600 MaxAge: 3600,
601 }
602 var benchmarkCookieString string
603 b.ReportAllocs()
604 b.ResetTimer()
605 for i := 0; i < b.N; i++ {
606 benchmarkCookieString = c.String()
607 }
608 if have, want := benchmarkCookieString, wantCookieString; have != want {
609 b.Fatalf("Have: %v Want: %v", have, want)
610 }
611 }
612
613 func BenchmarkReadSetCookies(b *testing.B) {
614 header := Header{
615 "Set-Cookie": {
616 "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly",
617 ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly",
618 },
619 }
620 wantCookies := []*Cookie{
621 {
622 Name: "NID",
623 Value: "99=YsDT5i3E-CXax-",
624 Path: "/",
625 Domain: ".google.ch",
626 HttpOnly: true,
627 Expires: time.Date(2011, 11, 23, 1, 5, 3, 0, time.UTC),
628 RawExpires: "Wed, 23-Nov-2011 01:05:03 GMT",
629 Raw: "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly",
630 },
631 {
632 Name: ".ASPXAUTH",
633 Value: "7E3AA",
634 Path: "/",
635 Expires: time.Date(2012, 3, 7, 14, 25, 6, 0, time.UTC),
636 RawExpires: "Wed, 07-Mar-2012 14:25:06 GMT",
637 HttpOnly: true,
638 Raw: ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly",
639 },
640 }
641 var c []*Cookie
642 b.ReportAllocs()
643 b.ResetTimer()
644 for i := 0; i < b.N; i++ {
645 c = readSetCookies(header)
646 }
647 if !reflect.DeepEqual(c, wantCookies) {
648 b.Fatalf("readSetCookies:\nhave: %s\nwant: %s\n", toJSON(c), toJSON(wantCookies))
649 }
650 }
651
652 func BenchmarkReadCookies(b *testing.B) {
653 header := Header{
654 "Cookie": {
655 `de=; client_region=0; rpld1=0:hispeed.ch|20:che|21:zh|22:zurich|23:47.36|24:8.53|; rpld0=1:08|; backplane-channel=newspaper.com:1471; devicetype=0; osfam=0; rplmct=2; s_pers=%20s_vmonthnum%3D1472680800496%2526vn%253D1%7C1472680800496%3B%20s_nr%3D1471686767664-New%7C1474278767664%3B%20s_lv%3D1471686767669%7C1566294767669%3B%20s_lv_s%3DFirst%2520Visit%7C1471688567669%3B%20s_monthinvisit%3Dtrue%7C1471688567677%3B%20gvp_p5%3Dsports%253Ablog%253Aearly-lead%2520-%2520184693%2520-%252020160820%2520-%2520u-s%7C1471688567681%3B%20gvp_p51%3Dwp%2520-%2520sports%7C1471688567684%3B; s_sess=%20s_wp_ep%3Dhomepage%3B%20s._ref%3Dhttps%253A%252F%252Fwww.google.ch%252F%3B%20s_cc%3Dtrue%3B%20s_ppvl%3Dsports%25253Ablog%25253Aearly-lead%252520-%252520184693%252520-%25252020160820%252520-%252520u-lawyer%252C12%252C12%252C502%252C1231%252C502%252C1680%252C1050%252C2%252CP%3B%20s_ppv%3Dsports%25253Ablog%25253Aearly-lead%252520-%252520184693%252520-%25252020160820%252520-%252520u-s-lawyer%252C12%252C12%252C502%252C1231%252C502%252C1680%252C1050%252C2%252CP%3B%20s_dslv%3DFirst%2520Visit%3B%20s_sq%3Dwpninewspapercom%253D%252526pid%25253Dsports%2525253Ablog%2525253Aearly-lead%25252520-%25252520184693%25252520-%2525252020160820%25252520-%25252520u-s%252526pidt%25253D1%252526oid%25253Dhttps%2525253A%2525252F%2525252Fwww.newspaper.com%2525252F%2525253Fnid%2525253Dmenu_nav_homepage%252526ot%25253DA%3B`,
656 },
657 }
658 wantCookies := []*Cookie{
659 {Name: "de", Value: ""},
660 {Name: "client_region", Value: "0"},
661 {Name: "rpld1", Value: "0:hispeed.ch|20:che|21:zh|22:zurich|23:47.36|24:8.53|"},
662 {Name: "rpld0", Value: "1:08|"},
663 {Name: "backplane-channel", Value: "newspaper.com:1471"},
664 {Name: "devicetype", Value: "0"},
665 {Name: "osfam", Value: "0"},
666 {Name: "rplmct", Value: "2"},
667 {Name: "s_pers", Value: "%20s_vmonthnum%3D1472680800496%2526vn%253D1%7C1472680800496%3B%20s_nr%3D1471686767664-New%7C1474278767664%3B%20s_lv%3D1471686767669%7C1566294767669%3B%20s_lv_s%3DFirst%2520Visit%7C1471688567669%3B%20s_monthinvisit%3Dtrue%7C1471688567677%3B%20gvp_p5%3Dsports%253Ablog%253Aearly-lead%2520-%2520184693%2520-%252020160820%2520-%2520u-s%7C1471688567681%3B%20gvp_p51%3Dwp%2520-%2520sports%7C1471688567684%3B"},
668 {Name: "s_sess", Value: "%20s_wp_ep%3Dhomepage%3B%20s._ref%3Dhttps%253A%252F%252Fwww.google.ch%252F%3B%20s_cc%3Dtrue%3B%20s_ppvl%3Dsports%25253Ablog%25253Aearly-lead%252520-%252520184693%252520-%25252020160820%252520-%252520u-lawyer%252C12%252C12%252C502%252C1231%252C502%252C1680%252C1050%252C2%252CP%3B%20s_ppv%3Dsports%25253Ablog%25253Aearly-lead%252520-%252520184693%252520-%25252020160820%252520-%252520u-s-lawyer%252C12%252C12%252C502%252C1231%252C502%252C1680%252C1050%252C2%252CP%3B%20s_dslv%3DFirst%2520Visit%3B%20s_sq%3Dwpninewspapercom%253D%252526pid%25253Dsports%2525253Ablog%2525253Aearly-lead%25252520-%25252520184693%25252520-%2525252020160820%25252520-%25252520u-s%252526pidt%25253D1%252526oid%25253Dhttps%2525253A%2525252F%2525252Fwww.newspaper.com%2525252F%2525253Fnid%2525253Dmenu_nav_homepage%252526ot%25253DA%3B"},
669 }
670 var c []*Cookie
671 b.ReportAllocs()
672 b.ResetTimer()
673 for i := 0; i < b.N; i++ {
674 c = readCookies(header, "")
675 }
676 if !reflect.DeepEqual(c, wantCookies) {
677 b.Fatalf("readCookies:\nhave: %s\nwant: %s\n", toJSON(c), toJSON(wantCookies))
678 }
679 }
680
681 func TestParseCookie(t *testing.T) {
682 tests := []struct {
683 line string
684 cookies []*Cookie
685 err error
686 }{
687 {
688 line: "Cookie-1=v$1",
689 cookies: []*Cookie{{Name: "Cookie-1", Value: "v$1"}},
690 },
691 {
692 line: "Cookie-1=v$1;c2=v2",
693 cookies: []*Cookie{{Name: "Cookie-1", Value: "v$1"}, {Name: "c2", Value: "v2"}},
694 },
695 {
696 line: `Cookie-1="v$1";c2="v2"`,
697 cookies: []*Cookie{{Name: "Cookie-1", Value: "v$1", Quoted: true}, {Name: "c2", Value: "v2", Quoted: true}},
698 },
699 {
700 line: "k1=",
701 cookies: []*Cookie{{Name: "k1", Value: ""}},
702 },
703 {
704 line: "",
705 err: errBlankCookie,
706 },
707 {
708 line: "equal-not-found",
709 err: errEqualNotFoundInCookie,
710 },
711 {
712 line: "=v1",
713 err: errInvalidCookieName,
714 },
715 {
716 line: "k1=\\",
717 err: errInvalidCookieValue,
718 },
719 }
720 for i, tt := range tests {
721 gotCookies, gotErr := ParseCookie(tt.line)
722 if !errors.Is(gotErr, tt.err) {
723 t.Errorf("#%d ParseCookie got error %v, want error %v", i, gotErr, tt.err)
724 }
725 if !reflect.DeepEqual(gotCookies, tt.cookies) {
726 t.Errorf("#%d ParseCookie:\ngot cookies: %s\nwant cookies: %s\n", i, toJSON(gotCookies), toJSON(tt.cookies))
727 }
728 }
729 }
730
731 func TestParseSetCookie(t *testing.T) {
732 tests := []struct {
733 line string
734 cookie *Cookie
735 err error
736 }{
737 {
738 line: "Cookie-1=v$1",
739 cookie: &Cookie{Name: "Cookie-1", Value: "v$1", Raw: "Cookie-1=v$1"},
740 },
741 {
742 line: "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly",
743 cookie: &Cookie{
744 Name: "NID",
745 Value: "99=YsDT5i3E-CXax-",
746 Path: "/",
747 Domain: ".google.ch",
748 HttpOnly: true,
749 Expires: time.Date(2011, 11, 23, 1, 5, 3, 0, time.UTC),
750 RawExpires: "Wed, 23-Nov-2011 01:05:03 GMT",
751 Raw: "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly",
752 },
753 },
754 {
755 line: ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly",
756 cookie: &Cookie{
757 Name: ".ASPXAUTH",
758 Value: "7E3AA",
759 Path: "/",
760 Expires: time.Date(2012, 3, 7, 14, 25, 6, 0, time.UTC),
761 RawExpires: "Wed, 07-Mar-2012 14:25:06 GMT",
762 HttpOnly: true,
763 Raw: ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly",
764 },
765 },
766 {
767 line: "ASP.NET_SessionId=foo; path=/; HttpOnly",
768 cookie: &Cookie{
769 Name: "ASP.NET_SessionId",
770 Value: "foo",
771 Path: "/",
772 HttpOnly: true,
773 Raw: "ASP.NET_SessionId=foo; path=/; HttpOnly",
774 },
775 },
776 {
777 line: "samesitedefault=foo; SameSite",
778 cookie: &Cookie{
779 Name: "samesitedefault",
780 Value: "foo",
781 SameSite: SameSiteDefaultMode,
782 Raw: "samesitedefault=foo; SameSite",
783 },
784 },
785 {
786 line: "samesiteinvalidisdefault=foo; SameSite=invalid",
787 cookie: &Cookie{
788 Name: "samesiteinvalidisdefault",
789 Value: "foo",
790 SameSite: SameSiteDefaultMode,
791 Raw: "samesiteinvalidisdefault=foo; SameSite=invalid",
792 },
793 },
794 {
795 line: "samesitelax=foo; SameSite=Lax",
796 cookie: &Cookie{
797 Name: "samesitelax",
798 Value: "foo",
799 SameSite: SameSiteLaxMode,
800 Raw: "samesitelax=foo; SameSite=Lax",
801 },
802 },
803 {
804 line: "samesitestrict=foo; SameSite=Strict",
805 cookie: &Cookie{
806 Name: "samesitestrict",
807 Value: "foo",
808 SameSite: SameSiteStrictMode,
809 Raw: "samesitestrict=foo; SameSite=Strict",
810 },
811 },
812 {
813 line: "samesitenone=foo; SameSite=None",
814 cookie: &Cookie{
815 Name: "samesitenone",
816 Value: "foo",
817 SameSite: SameSiteNoneMode,
818 Raw: "samesitenone=foo; SameSite=None",
819 },
820 },
821
822
823 {
824 line: `special-1=a z`,
825 cookie: &Cookie{Name: "special-1", Value: "a z", Raw: `special-1=a z`},
826 },
827 {
828 line: `special-2=" z"`,
829 cookie: &Cookie{Name: "special-2", Value: " z", Quoted: true, Raw: `special-2=" z"`},
830 },
831 {
832 line: `special-3="a "`,
833 cookie: &Cookie{Name: "special-3", Value: "a ", Quoted: true, Raw: `special-3="a "`},
834 },
835 {
836 line: `special-4=" "`,
837 cookie: &Cookie{Name: "special-4", Value: " ", Quoted: true, Raw: `special-4=" "`},
838 },
839 {
840 line: `special-5=a,z`,
841 cookie: &Cookie{Name: "special-5", Value: "a,z", Raw: `special-5=a,z`},
842 },
843 {
844 line: `special-6=",z"`,
845 cookie: &Cookie{Name: "special-6", Value: ",z", Quoted: true, Raw: `special-6=",z"`},
846 },
847 {
848 line: `special-7=a,`,
849 cookie: &Cookie{Name: "special-7", Value: "a,", Raw: `special-7=a,`},
850 },
851 {
852 line: `special-8=","`,
853 cookie: &Cookie{Name: "special-8", Value: ",", Quoted: true, Raw: `special-8=","`},
854 },
855
856
857 {
858 line: `special-9 =","`,
859 cookie: &Cookie{Name: "special-9", Value: ",", Quoted: true, Raw: `special-9 =","`},
860 },
861 {
862 line: "",
863 err: errBlankCookie,
864 },
865 {
866 line: "equal-not-found",
867 err: errEqualNotFoundInCookie,
868 },
869 {
870 line: "=v1",
871 err: errInvalidCookieName,
872 },
873 {
874 line: "k1=\\",
875 err: errInvalidCookieValue,
876 },
877 }
878 for i, tt := range tests {
879 gotCookie, gotErr := ParseSetCookie(tt.line)
880 if !errors.Is(gotErr, tt.err) {
881 t.Errorf("#%d ParseSetCookie got error %v, want error %v", i, gotErr, tt.err)
882 continue
883 }
884 if !reflect.DeepEqual(gotCookie, tt.cookie) {
885 t.Errorf("#%d ParseSetCookie:\ngot cookie: %s\nwant cookie: %s\n", i, toJSON(gotCookie), toJSON(tt.cookie))
886 }
887 }
888 }
889
View as plain text