github.com/ice-blockchain/go/src@v0.0.0-20240403114104-1564d284e521/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  	"log"
    11  	"os"
    12  	"reflect"
    13  	"strings"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  var writeSetCookiesTests = []struct {
    19  	Cookie *Cookie
    20  	Raw    string
    21  }{
    22  	{
    23  		&Cookie{Name: "cookie-1", Value: "v$1"},
    24  		"cookie-1=v$1",
    25  	},
    26  	{
    27  		&Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600},
    28  		"cookie-2=two; Max-Age=3600",
    29  	},
    30  	{
    31  		&Cookie{Name: "cookie-3", Value: "three", Domain: ".example.com"},
    32  		"cookie-3=three; Domain=example.com",
    33  	},
    34  	{
    35  		&Cookie{Name: "cookie-4", Value: "four", Path: "/restricted/"},
    36  		"cookie-4=four; Path=/restricted/",
    37  	},
    38  	{
    39  		&Cookie{Name: "cookie-5", Value: "five", Domain: "wrong;bad.abc"},
    40  		"cookie-5=five",
    41  	},
    42  	{
    43  		&Cookie{Name: "cookie-6", Value: "six", Domain: "bad-.abc"},
    44  		"cookie-6=six",
    45  	},
    46  	{
    47  		&Cookie{Name: "cookie-7", Value: "seven", Domain: "127.0.0.1"},
    48  		"cookie-7=seven; Domain=127.0.0.1",
    49  	},
    50  	{
    51  		&Cookie{Name: "cookie-8", Value: "eight", Domain: "::1"},
    52  		"cookie-8=eight",
    53  	},
    54  	{
    55  		&Cookie{Name: "cookie-9", Value: "expiring", Expires: time.Unix(1257894000, 0)},
    56  		"cookie-9=expiring; Expires=Tue, 10 Nov 2009 23:00:00 GMT",
    57  	},
    58  	// According to IETF 6265 Section 5.1.1.5, the year cannot be less than 1601
    59  	{
    60  		&Cookie{Name: "cookie-10", Value: "expiring-1601", Expires: time.Date(1601, 1, 1, 1, 1, 1, 1, time.UTC)},
    61  		"cookie-10=expiring-1601; Expires=Mon, 01 Jan 1601 01:01:01 GMT",
    62  	},
    63  	{
    64  		&Cookie{Name: "cookie-11", Value: "invalid-expiry", Expires: time.Date(1600, 1, 1, 1, 1, 1, 1, time.UTC)},
    65  		"cookie-11=invalid-expiry",
    66  	},
    67  	{
    68  		&Cookie{Name: "cookie-12", Value: "samesite-default", SameSite: SameSiteDefaultMode},
    69  		"cookie-12=samesite-default",
    70  	},
    71  	{
    72  		&Cookie{Name: "cookie-13", Value: "samesite-lax", SameSite: SameSiteLaxMode},
    73  		"cookie-13=samesite-lax; SameSite=Lax",
    74  	},
    75  	{
    76  		&Cookie{Name: "cookie-14", Value: "samesite-strict", SameSite: SameSiteStrictMode},
    77  		"cookie-14=samesite-strict; SameSite=Strict",
    78  	},
    79  	{
    80  		&Cookie{Name: "cookie-15", Value: "samesite-none", SameSite: SameSiteNoneMode},
    81  		"cookie-15=samesite-none; SameSite=None",
    82  	},
    83  	// The "special" cookies have values containing commas or spaces which
    84  	// are disallowed by RFC 6265 but are common in the wild.
    85  	{
    86  		&Cookie{Name: "special-1", Value: "a z"},
    87  		`special-1="a z"`,
    88  	},
    89  	{
    90  		&Cookie{Name: "special-2", Value: " z"},
    91  		`special-2=" z"`,
    92  	},
    93  	{
    94  		&Cookie{Name: "special-3", Value: "a "},
    95  		`special-3="a "`,
    96  	},
    97  	{
    98  		&Cookie{Name: "special-4", Value: " "},
    99  		`special-4=" "`,
   100  	},
   101  	{
   102  		&Cookie{Name: "special-5", Value: "a,z"},
   103  		`special-5="a,z"`,
   104  	},
   105  	{
   106  		&Cookie{Name: "special-6", Value: ",z"},
   107  		`special-6=",z"`,
   108  	},
   109  	{
   110  		&Cookie{Name: "special-7", Value: "a,"},
   111  		`special-7="a,"`,
   112  	},
   113  	{
   114  		&Cookie{Name: "special-8", Value: ","},
   115  		`special-8=","`,
   116  	},
   117  	{
   118  		&Cookie{Name: "empty-value", Value: ""},
   119  		`empty-value=`,
   120  	},
   121  	{
   122  		nil,
   123  		``,
   124  	},
   125  	{
   126  		&Cookie{Name: ""},
   127  		``,
   128  	},
   129  	{
   130  		&Cookie{Name: "\t"},
   131  		``,
   132  	},
   133  	{
   134  		&Cookie{Name: "\r"},
   135  		``,
   136  	},
   137  	{
   138  		&Cookie{Name: "a\nb", Value: "v"},
   139  		``,
   140  	},
   141  	{
   142  		&Cookie{Name: "a\nb", Value: "v"},
   143  		``,
   144  	},
   145  	{
   146  		&Cookie{Name: "a\rb", Value: "v"},
   147  		``,
   148  	},
   149  }
   150  
   151  func TestWriteSetCookies(t *testing.T) {
   152  	defer log.SetOutput(os.Stderr)
   153  	var logbuf strings.Builder
   154  	log.SetOutput(&logbuf)
   155  
   156  	for i, tt := range writeSetCookiesTests {
   157  		if g, e := tt.Cookie.String(), tt.Raw; g != e {
   158  			t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, e, g)
   159  			continue
   160  		}
   161  	}
   162  
   163  	if got, sub := logbuf.String(), "dropping domain attribute"; !strings.Contains(got, sub) {
   164  		t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
   165  	}
   166  }
   167  
   168  type headerOnlyResponseWriter Header
   169  
   170  func (ho headerOnlyResponseWriter) Header() Header {
   171  	return Header(ho)
   172  }
   173  
   174  func (ho headerOnlyResponseWriter) Write([]byte) (int, error) {
   175  	panic("NOIMPL")
   176  }
   177  
   178  func (ho headerOnlyResponseWriter) WriteHeader(int) {
   179  	panic("NOIMPL")
   180  }
   181  
   182  func TestSetCookie(t *testing.T) {
   183  	m := make(Header)
   184  	SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-1", Value: "one", Path: "/restricted/"})
   185  	SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600})
   186  	if l := len(m["Set-Cookie"]); l != 2 {
   187  		t.Fatalf("expected %d cookies, got %d", 2, l)
   188  	}
   189  	if g, e := m["Set-Cookie"][0], "cookie-1=one; Path=/restricted/"; g != e {
   190  		t.Errorf("cookie #1: want %q, got %q", e, g)
   191  	}
   192  	if g, e := m["Set-Cookie"][1], "cookie-2=two; Max-Age=3600"; g != e {
   193  		t.Errorf("cookie #2: want %q, got %q", e, g)
   194  	}
   195  }
   196  
   197  var addCookieTests = []struct {
   198  	Cookies []*Cookie
   199  	Raw     string
   200  }{
   201  	{
   202  		[]*Cookie{},
   203  		"",
   204  	},
   205  	{
   206  		[]*Cookie{{Name: "cookie-1", Value: "v$1"}},
   207  		"cookie-1=v$1",
   208  	},
   209  	{
   210  		[]*Cookie{
   211  			{Name: "cookie-1", Value: "v$1"},
   212  			{Name: "cookie-2", Value: "v$2"},
   213  			{Name: "cookie-3", Value: "v$3"},
   214  		},
   215  		"cookie-1=v$1; cookie-2=v$2; cookie-3=v$3",
   216  	},
   217  }
   218  
   219  func TestAddCookie(t *testing.T) {
   220  	for i, tt := range addCookieTests {
   221  		req, _ := NewRequest("GET", "http://example.com/", nil)
   222  		for _, c := range tt.Cookies {
   223  			req.AddCookie(c)
   224  		}
   225  		if g := req.Header.Get("Cookie"); g != tt.Raw {
   226  			t.Errorf("Test %d:\nwant: %s\n got: %s\n", i, tt.Raw, g)
   227  			continue
   228  		}
   229  	}
   230  }
   231  
   232  var readSetCookiesTests = []struct {
   233  	Header  Header
   234  	Cookies []*Cookie
   235  }{
   236  	{
   237  		Header{"Set-Cookie": {"Cookie-1=v$1"}},
   238  		[]*Cookie{{Name: "Cookie-1", Value: "v$1", Raw: "Cookie-1=v$1"}},
   239  	},
   240  	{
   241  		Header{"Set-Cookie": {"NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"}},
   242  		[]*Cookie{{
   243  			Name:       "NID",
   244  			Value:      "99=YsDT5i3E-CXax-",
   245  			Path:       "/",
   246  			Domain:     ".google.ch",
   247  			HttpOnly:   true,
   248  			Expires:    time.Date(2011, 11, 23, 1, 5, 3, 0, time.UTC),
   249  			RawExpires: "Wed, 23-Nov-2011 01:05:03 GMT",
   250  			Raw:        "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly",
   251  		}},
   252  	},
   253  	{
   254  		Header{"Set-Cookie": {".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"}},
   255  		[]*Cookie{{
   256  			Name:       ".ASPXAUTH",
   257  			Value:      "7E3AA",
   258  			Path:       "/",
   259  			Expires:    time.Date(2012, 3, 7, 14, 25, 6, 0, time.UTC),
   260  			RawExpires: "Wed, 07-Mar-2012 14:25:06 GMT",
   261  			HttpOnly:   true,
   262  			Raw:        ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly",
   263  		}},
   264  	},
   265  	{
   266  		Header{"Set-Cookie": {"ASP.NET_SessionId=foo; path=/; HttpOnly"}},
   267  		[]*Cookie{{
   268  			Name:     "ASP.NET_SessionId",
   269  			Value:    "foo",
   270  			Path:     "/",
   271  			HttpOnly: true,
   272  			Raw:      "ASP.NET_SessionId=foo; path=/; HttpOnly",
   273  		}},
   274  	},
   275  	{
   276  		Header{"Set-Cookie": {"samesitedefault=foo; SameSite"}},
   277  		[]*Cookie{{
   278  			Name:     "samesitedefault",
   279  			Value:    "foo",
   280  			SameSite: SameSiteDefaultMode,
   281  			Raw:      "samesitedefault=foo; SameSite",
   282  		}},
   283  	},
   284  	{
   285  		Header{"Set-Cookie": {"samesiteinvalidisdefault=foo; SameSite=invalid"}},
   286  		[]*Cookie{{
   287  			Name:     "samesiteinvalidisdefault",
   288  			Value:    "foo",
   289  			SameSite: SameSiteDefaultMode,
   290  			Raw:      "samesiteinvalidisdefault=foo; SameSite=invalid",
   291  		}},
   292  	},
   293  	{
   294  		Header{"Set-Cookie": {"samesitelax=foo; SameSite=Lax"}},
   295  		[]*Cookie{{
   296  			Name:     "samesitelax",
   297  			Value:    "foo",
   298  			SameSite: SameSiteLaxMode,
   299  			Raw:      "samesitelax=foo; SameSite=Lax",
   300  		}},
   301  	},
   302  	{
   303  		Header{"Set-Cookie": {"samesitestrict=foo; SameSite=Strict"}},
   304  		[]*Cookie{{
   305  			Name:     "samesitestrict",
   306  			Value:    "foo",
   307  			SameSite: SameSiteStrictMode,
   308  			Raw:      "samesitestrict=foo; SameSite=Strict",
   309  		}},
   310  	},
   311  	{
   312  		Header{"Set-Cookie": {"samesitenone=foo; SameSite=None"}},
   313  		[]*Cookie{{
   314  			Name:     "samesitenone",
   315  			Value:    "foo",
   316  			SameSite: SameSiteNoneMode,
   317  			Raw:      "samesitenone=foo; SameSite=None",
   318  		}},
   319  	},
   320  	// Make sure we can properly read back the Set-Cookie headers we create
   321  	// for values containing spaces or commas:
   322  	{
   323  		Header{"Set-Cookie": {`special-1=a z`}},
   324  		[]*Cookie{{Name: "special-1", Value: "a z", Raw: `special-1=a z`}},
   325  	},
   326  	{
   327  		Header{"Set-Cookie": {`special-2=" z"`}},
   328  		[]*Cookie{{Name: "special-2", Value: " z", Raw: `special-2=" z"`}},
   329  	},
   330  	{
   331  		Header{"Set-Cookie": {`special-3="a "`}},
   332  		[]*Cookie{{Name: "special-3", Value: "a ", Raw: `special-3="a "`}},
   333  	},
   334  	{
   335  		Header{"Set-Cookie": {`special-4=" "`}},
   336  		[]*Cookie{{Name: "special-4", Value: " ", Raw: `special-4=" "`}},
   337  	},
   338  	{
   339  		Header{"Set-Cookie": {`special-5=a,z`}},
   340  		[]*Cookie{{Name: "special-5", Value: "a,z", Raw: `special-5=a,z`}},
   341  	},
   342  	{
   343  		Header{"Set-Cookie": {`special-6=",z"`}},
   344  		[]*Cookie{{Name: "special-6", Value: ",z", Raw: `special-6=",z"`}},
   345  	},
   346  	{
   347  		Header{"Set-Cookie": {`special-7=a,`}},
   348  		[]*Cookie{{Name: "special-7", Value: "a,", Raw: `special-7=a,`}},
   349  	},
   350  	{
   351  		Header{"Set-Cookie": {`special-8=","`}},
   352  		[]*Cookie{{Name: "special-8", Value: ",", Raw: `special-8=","`}},
   353  	},
   354  	// Make sure we can properly read back the Set-Cookie headers
   355  	// for names containing spaces:
   356  	{
   357  		Header{"Set-Cookie": {`special-9 =","`}},
   358  		[]*Cookie{{Name: "special-9", Value: ",", Raw: `special-9 =","`}},
   359  	},
   360  
   361  	// TODO(bradfitz): users have reported seeing this in the
   362  	// wild, but do browsers handle it? RFC 6265 just says "don't
   363  	// do that" (section 3) and then never mentions header folding
   364  	// again.
   365  	// Header{"Set-Cookie": {"ASP.NET_SessionId=foo; path=/; HttpOnly, .ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"}},
   366  }
   367  
   368  func toJSON(v any) string {
   369  	b, err := json.Marshal(v)
   370  	if err != nil {
   371  		return fmt.Sprintf("%#v", v)
   372  	}
   373  	return string(b)
   374  }
   375  
   376  func TestReadSetCookies(t *testing.T) {
   377  	for i, tt := range readSetCookiesTests {
   378  		for n := 0; n < 2; n++ { // to verify readSetCookies doesn't mutate its input
   379  			c := readSetCookies(tt.Header)
   380  			if !reflect.DeepEqual(c, tt.Cookies) {
   381  				t.Errorf("#%d readSetCookies: have\n%s\nwant\n%s\n", i, toJSON(c), toJSON(tt.Cookies))
   382  				continue
   383  			}
   384  		}
   385  	}
   386  }
   387  
   388  var readCookiesTests = []struct {
   389  	Header  Header
   390  	Filter  string
   391  	Cookies []*Cookie
   392  }{
   393  	{
   394  		Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}},
   395  		"",
   396  		[]*Cookie{
   397  			{Name: "Cookie-1", Value: "v$1"},
   398  			{Name: "c2", Value: "v2"},
   399  		},
   400  	},
   401  	{
   402  		Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}},
   403  		"c2",
   404  		[]*Cookie{
   405  			{Name: "c2", Value: "v2"},
   406  		},
   407  	},
   408  	{
   409  		Header{"Cookie": {"Cookie-1=v$1; c2=v2"}},
   410  		"",
   411  		[]*Cookie{
   412  			{Name: "Cookie-1", Value: "v$1"},
   413  			{Name: "c2", Value: "v2"},
   414  		},
   415  	},
   416  	{
   417  		Header{"Cookie": {"Cookie-1=v$1; c2=v2"}},
   418  		"c2",
   419  		[]*Cookie{
   420  			{Name: "c2", Value: "v2"},
   421  		},
   422  	},
   423  	{
   424  		Header{"Cookie": {`Cookie-1="v$1"; c2="v2"`}},
   425  		"",
   426  		[]*Cookie{
   427  			{Name: "Cookie-1", Value: "v$1"},
   428  			{Name: "c2", Value: "v2"},
   429  		},
   430  	},
   431  	{
   432  		Header{"Cookie": {`Cookie-1="v$1"; c2=v2;`}},
   433  		"",
   434  		[]*Cookie{
   435  			{Name: "Cookie-1", Value: "v$1"},
   436  			{Name: "c2", Value: "v2"},
   437  		},
   438  	},
   439  	{
   440  		Header{"Cookie": {``}},
   441  		"",
   442  		[]*Cookie{},
   443  	},
   444  }
   445  
   446  func TestReadCookies(t *testing.T) {
   447  	for i, tt := range readCookiesTests {
   448  		for n := 0; n < 2; n++ { // to verify readCookies doesn't mutate its input
   449  			c := readCookies(tt.Header, tt.Filter)
   450  			if !reflect.DeepEqual(c, tt.Cookies) {
   451  				t.Errorf("#%d readCookies:\nhave: %s\nwant: %s\n", i, toJSON(c), toJSON(tt.Cookies))
   452  				continue
   453  			}
   454  		}
   455  	}
   456  }
   457  
   458  func TestSetCookieDoubleQuotes(t *testing.T) {
   459  	res := &Response{Header: Header{}}
   460  	res.Header.Add("Set-Cookie", `quoted0=none; max-age=30`)
   461  	res.Header.Add("Set-Cookie", `quoted1="cookieValue"; max-age=31`)
   462  	res.Header.Add("Set-Cookie", `quoted2=cookieAV; max-age="32"`)
   463  	res.Header.Add("Set-Cookie", `quoted3="both"; max-age="33"`)
   464  	got := res.Cookies()
   465  	want := []*Cookie{
   466  		{Name: "quoted0", Value: "none", MaxAge: 30},
   467  		{Name: "quoted1", Value: "cookieValue", MaxAge: 31},
   468  		{Name: "quoted2", Value: "cookieAV"},
   469  		{Name: "quoted3", Value: "both"},
   470  	}
   471  	if len(got) != len(want) {
   472  		t.Fatalf("got %d cookies, want %d", len(got), len(want))
   473  	}
   474  	for i, w := range want {
   475  		g := got[i]
   476  		if g.Name != w.Name || g.Value != w.Value || g.MaxAge != w.MaxAge {
   477  			t.Errorf("cookie #%d:\ngot  %v\nwant %v", i, g, w)
   478  		}
   479  	}
   480  }
   481  
   482  func TestCookieSanitizeValue(t *testing.T) {
   483  	defer log.SetOutput(os.Stderr)
   484  	var logbuf strings.Builder
   485  	log.SetOutput(&logbuf)
   486  
   487  	tests := []struct {
   488  		in, want string
   489  	}{
   490  		{"foo", "foo"},
   491  		{"foo;bar", "foobar"},
   492  		{"foo\\bar", "foobar"},
   493  		{"foo\"bar", "foobar"},
   494  		{"\x00\x7e\x7f\x80", "\x7e"},
   495  		{`"withquotes"`, "withquotes"},
   496  		{"a z", `"a z"`},
   497  		{" z", `" z"`},
   498  		{"a ", `"a "`},
   499  		{"a,z", `"a,z"`},
   500  		{",z", `",z"`},
   501  		{"a,", `"a,"`},
   502  	}
   503  	for _, tt := range tests {
   504  		if got := sanitizeCookieValue(tt.in); got != tt.want {
   505  			t.Errorf("sanitizeCookieValue(%q) = %q; want %q", tt.in, got, tt.want)
   506  		}
   507  	}
   508  
   509  	if got, sub := logbuf.String(), "dropping invalid bytes"; !strings.Contains(got, sub) {
   510  		t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
   511  	}
   512  }
   513  
   514  func TestCookieSanitizePath(t *testing.T) {
   515  	defer log.SetOutput(os.Stderr)
   516  	var logbuf strings.Builder
   517  	log.SetOutput(&logbuf)
   518  
   519  	tests := []struct {
   520  		in, want string
   521  	}{
   522  		{"/path", "/path"},
   523  		{"/path with space/", "/path with space/"},
   524  		{"/just;no;semicolon\x00orstuff/", "/justnosemicolonorstuff/"},
   525  	}
   526  	for _, tt := range tests {
   527  		if got := sanitizeCookiePath(tt.in); got != tt.want {
   528  			t.Errorf("sanitizeCookiePath(%q) = %q; want %q", tt.in, got, tt.want)
   529  		}
   530  	}
   531  
   532  	if got, sub := logbuf.String(), "dropping invalid bytes"; !strings.Contains(got, sub) {
   533  		t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
   534  	}
   535  }
   536  
   537  func TestCookieValid(t *testing.T) {
   538  	tests := []struct {
   539  		cookie *Cookie
   540  		valid  bool
   541  	}{
   542  		{nil, false},
   543  		{&Cookie{Name: ""}, false},
   544  		{&Cookie{Name: "invalid-value", Value: "foo\"bar"}, false},
   545  		{&Cookie{Name: "invalid-path", Path: "/foo;bar/"}, false},
   546  		{&Cookie{Name: "invalid-domain", Domain: "example.com:80"}, false},
   547  		{&Cookie{Name: "invalid-expiry", Value: "", Expires: time.Date(1600, 1, 1, 1, 1, 1, 1, time.UTC)}, false},
   548  		{&Cookie{Name: "valid-empty"}, true},
   549  		{&Cookie{Name: "valid-expires", Value: "foo", Path: "/bar", Domain: "example.com", Expires: time.Unix(0, 0)}, true},
   550  		{&Cookie{Name: "valid-max-age", Value: "foo", Path: "/bar", Domain: "example.com", MaxAge: 60}, true},
   551  		{&Cookie{Name: "valid-all-fields", Value: "foo", Path: "/bar", Domain: "example.com", Expires: time.Unix(0, 0), MaxAge: 0}, true},
   552  	}
   553  
   554  	for _, tt := range tests {
   555  		err := tt.cookie.Valid()
   556  		if err != nil && tt.valid {
   557  			t.Errorf("%#v.Valid() returned error %v; want nil", tt.cookie, err)
   558  		}
   559  		if err == nil && !tt.valid {
   560  			t.Errorf("%#v.Valid() returned nil; want error", tt.cookie)
   561  		}
   562  	}
   563  }
   564  
   565  func BenchmarkCookieString(b *testing.B) {
   566  	const wantCookieString = `cookie-9=i3e01nf61b6t23bvfmplnanol3; Path=/restricted/; Domain=example.com; Expires=Tue, 10 Nov 2009 23:00:00 GMT; Max-Age=3600`
   567  	c := &Cookie{
   568  		Name:    "cookie-9",
   569  		Value:   "i3e01nf61b6t23bvfmplnanol3",
   570  		Expires: time.Unix(1257894000, 0),
   571  		Path:    "/restricted/",
   572  		Domain:  ".example.com",
   573  		MaxAge:  3600,
   574  	}
   575  	var benchmarkCookieString string
   576  	b.ReportAllocs()
   577  	b.ResetTimer()
   578  	for i := 0; i < b.N; i++ {
   579  		benchmarkCookieString = c.String()
   580  	}
   581  	if have, want := benchmarkCookieString, wantCookieString; have != want {
   582  		b.Fatalf("Have: %v Want: %v", have, want)
   583  	}
   584  }
   585  
   586  func BenchmarkReadSetCookies(b *testing.B) {
   587  	header := Header{
   588  		"Set-Cookie": {
   589  			"NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly",
   590  			".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly",
   591  		},
   592  	}
   593  	wantCookies := []*Cookie{
   594  		{
   595  			Name:       "NID",
   596  			Value:      "99=YsDT5i3E-CXax-",
   597  			Path:       "/",
   598  			Domain:     ".google.ch",
   599  			HttpOnly:   true,
   600  			Expires:    time.Date(2011, 11, 23, 1, 5, 3, 0, time.UTC),
   601  			RawExpires: "Wed, 23-Nov-2011 01:05:03 GMT",
   602  			Raw:        "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly",
   603  		},
   604  		{
   605  			Name:       ".ASPXAUTH",
   606  			Value:      "7E3AA",
   607  			Path:       "/",
   608  			Expires:    time.Date(2012, 3, 7, 14, 25, 6, 0, time.UTC),
   609  			RawExpires: "Wed, 07-Mar-2012 14:25:06 GMT",
   610  			HttpOnly:   true,
   611  			Raw:        ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly",
   612  		},
   613  	}
   614  	var c []*Cookie
   615  	b.ReportAllocs()
   616  	b.ResetTimer()
   617  	for i := 0; i < b.N; i++ {
   618  		c = readSetCookies(header)
   619  	}
   620  	if !reflect.DeepEqual(c, wantCookies) {
   621  		b.Fatalf("readSetCookies:\nhave: %s\nwant: %s\n", toJSON(c), toJSON(wantCookies))
   622  	}
   623  }
   624  
   625  func BenchmarkReadCookies(b *testing.B) {
   626  	header := Header{
   627  		"Cookie": {
   628  			`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`,
   629  		},
   630  	}
   631  	wantCookies := []*Cookie{
   632  		{Name: "de", Value: ""},
   633  		{Name: "client_region", Value: "0"},
   634  		{Name: "rpld1", Value: "0:hispeed.ch|20:che|21:zh|22:zurich|23:47.36|24:8.53|"},
   635  		{Name: "rpld0", Value: "1:08|"},
   636  		{Name: "backplane-channel", Value: "newspaper.com:1471"},
   637  		{Name: "devicetype", Value: "0"},
   638  		{Name: "osfam", Value: "0"},
   639  		{Name: "rplmct", Value: "2"},
   640  		{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"},
   641  		{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"},
   642  	}
   643  	var c []*Cookie
   644  	b.ReportAllocs()
   645  	b.ResetTimer()
   646  	for i := 0; i < b.N; i++ {
   647  		c = readCookies(header, "")
   648  	}
   649  	if !reflect.DeepEqual(c, wantCookies) {
   650  		b.Fatalf("readCookies:\nhave: %s\nwant: %s\n", toJSON(c), toJSON(wantCookies))
   651  	}
   652  }