gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/gmhttp/cookiejar/jar_test.go (about)

     1  // Copyright 2013 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 cookiejar
     6  
     7  import (
     8  	"fmt"
     9  	"net/url"
    10  	"sort"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	http "gitee.com/zhaochuninhefei/gmgo/gmhttp"
    16  )
    17  
    18  // tNow is the synthetic current time used as now during testing.
    19  var tNow = time.Date(2013, 1, 1, 12, 0, 0, 0, time.UTC)
    20  
    21  // testPSL implements PublicSuffixList with just two rules: "co.uk"
    22  // and the default rule "*".
    23  // The implementation has two intentional bugs:
    24  //    PublicSuffix("www.buggy.psl") == "xy"
    25  //    PublicSuffix("www2.buggy.psl") == "com"
    26  type testPSL struct{}
    27  
    28  func (testPSL) String() string {
    29  	return "testPSL"
    30  }
    31  func (testPSL) PublicSuffix(d string) string {
    32  	if d == "co.uk" || strings.HasSuffix(d, ".co.uk") {
    33  		return "co.uk"
    34  	}
    35  	if d == "www.buggy.psl" {
    36  		return "xy"
    37  	}
    38  	if d == "www2.buggy.psl" {
    39  		return "com"
    40  	}
    41  	return d[strings.LastIndex(d, ".")+1:]
    42  }
    43  
    44  // newTestJar creates an empty Jar with testPSL as the public suffix list.
    45  func newTestJar() *Jar {
    46  	jar, err := New(&Options{PublicSuffixList: testPSL{}})
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  	return jar
    51  }
    52  
    53  var hasDotSuffixTests = [...]struct {
    54  	s, suffix string
    55  }{
    56  	{"", ""},
    57  	{"", "."},
    58  	{"", "x"},
    59  	{".", ""},
    60  	{".", "."},
    61  	{".", ".."},
    62  	{".", "x"},
    63  	{".", "x."},
    64  	{".", ".x"},
    65  	{".", ".x."},
    66  	{"x", ""},
    67  	{"x", "."},
    68  	{"x", ".."},
    69  	{"x", "x"},
    70  	{"x", "x."},
    71  	{"x", ".x"},
    72  	{"x", ".x."},
    73  	{".x", ""},
    74  	{".x", "."},
    75  	{".x", ".."},
    76  	{".x", "x"},
    77  	{".x", "x."},
    78  	{".x", ".x"},
    79  	{".x", ".x."},
    80  	{"x.", ""},
    81  	{"x.", "."},
    82  	{"x.", ".."},
    83  	{"x.", "x"},
    84  	{"x.", "x."},
    85  	{"x.", ".x"},
    86  	{"x.", ".x."},
    87  	{"com", ""},
    88  	{"com", "m"},
    89  	{"com", "om"},
    90  	{"com", "com"},
    91  	{"com", ".com"},
    92  	{"com", "x.com"},
    93  	{"com", "xcom"},
    94  	{"com", "xorg"},
    95  	{"com", "org"},
    96  	{"com", "rg"},
    97  	{"foo.com", ""},
    98  	{"foo.com", "m"},
    99  	{"foo.com", "om"},
   100  	{"foo.com", "com"},
   101  	{"foo.com", ".com"},
   102  	{"foo.com", "o.com"},
   103  	{"foo.com", "oo.com"},
   104  	{"foo.com", "foo.com"},
   105  	{"foo.com", ".foo.com"},
   106  	{"foo.com", "x.foo.com"},
   107  	{"foo.com", "xfoo.com"},
   108  	{"foo.com", "xfoo.org"},
   109  	{"foo.com", "foo.org"},
   110  	{"foo.com", "oo.org"},
   111  	{"foo.com", "o.org"},
   112  	{"foo.com", ".org"},
   113  	{"foo.com", "org"},
   114  	{"foo.com", "rg"},
   115  }
   116  
   117  func TestHasDotSuffix(t *testing.T) {
   118  	for _, tc := range hasDotSuffixTests {
   119  		got := hasDotSuffix(tc.s, tc.suffix)
   120  		want := strings.HasSuffix(tc.s, "."+tc.suffix)
   121  		if got != want {
   122  			t.Errorf("s=%q, suffix=%q: got %v, want %v", tc.s, tc.suffix, got, want)
   123  		}
   124  	}
   125  }
   126  
   127  var canonicalHostTests = map[string]string{
   128  	"www.example.com":         "www.example.com",
   129  	"WWW.EXAMPLE.COM":         "www.example.com",
   130  	"wWw.eXAmple.CoM":         "www.example.com",
   131  	"www.example.com:80":      "www.example.com",
   132  	"192.168.0.10":            "192.168.0.10",
   133  	"192.168.0.5:8080":        "192.168.0.5",
   134  	"2001:4860:0:2001::68":    "2001:4860:0:2001::68",
   135  	"[2001:4860:0:::68]:8080": "2001:4860:0:::68",
   136  	"www.bücher.de":           "www.xn--bcher-kva.de",
   137  	"www.example.com.":        "www.example.com",
   138  	// TODO: Fix canonicalHost so that all of the following malformed
   139  	// domain names trigger an error. (This list is not exhaustive, e.g.
   140  	// malformed internationalized domain names are missing.)
   141  	".":                       "",
   142  	"..":                      ".",
   143  	"...":                     "..",
   144  	".net":                    ".net",
   145  	".net.":                   ".net",
   146  	"a..":                     "a.",
   147  	"b.a..":                   "b.a.",
   148  	"weird.stuff...":          "weird.stuff..",
   149  	"[bad.unmatched.bracket:": "error",
   150  }
   151  
   152  func TestCanonicalHost(t *testing.T) {
   153  	for h, want := range canonicalHostTests {
   154  		got, err := canonicalHost(h)
   155  		if want == "error" {
   156  			if err == nil {
   157  				t.Errorf("%q: got %q and nil error, want non-nil", h, got)
   158  			}
   159  			continue
   160  		}
   161  		if err != nil {
   162  			t.Errorf("%q: %v", h, err)
   163  			continue
   164  		}
   165  		if got != want {
   166  			t.Errorf("%q: got %q, want %q", h, got, want)
   167  			continue
   168  		}
   169  	}
   170  }
   171  
   172  var hasPortTests = map[string]bool{
   173  	"www.example.com":      false,
   174  	"www.example.com:80":   true,
   175  	"127.0.0.1":            false,
   176  	"127.0.0.1:8080":       true,
   177  	"2001:4860:0:2001::68": false,
   178  	"[2001::0:::68]:80":    true,
   179  }
   180  
   181  func TestHasPort(t *testing.T) {
   182  	for host, want := range hasPortTests {
   183  		if got := hasPort(host); got != want {
   184  			t.Errorf("%q: got %t, want %t", host, got, want)
   185  		}
   186  	}
   187  }
   188  
   189  var jarKeyTests = map[string]string{
   190  	"foo.www.example.com": "example.com",
   191  	"www.example.com":     "example.com",
   192  	"example.com":         "example.com",
   193  	"com":                 "com",
   194  	"foo.www.bbc.co.uk":   "bbc.co.uk",
   195  	"www.bbc.co.uk":       "bbc.co.uk",
   196  	"bbc.co.uk":           "bbc.co.uk",
   197  	"co.uk":               "co.uk",
   198  	"uk":                  "uk",
   199  	"192.168.0.5":         "192.168.0.5",
   200  	"www.buggy.psl":       "www.buggy.psl",
   201  	"www2.buggy.psl":      "buggy.psl",
   202  	// The following are actual outputs of canonicalHost for
   203  	// malformed inputs to canonicalHost (see above).
   204  	"":              "",
   205  	".":             ".",
   206  	"..":            ".",
   207  	".net":          ".net",
   208  	"a.":            "a.",
   209  	"b.a.":          "a.",
   210  	"weird.stuff..": ".",
   211  }
   212  
   213  func TestJarKey(t *testing.T) {
   214  	for host, want := range jarKeyTests {
   215  		if got := jarKey(host, testPSL{}); got != want {
   216  			t.Errorf("%q: got %q, want %q", host, got, want)
   217  		}
   218  	}
   219  }
   220  
   221  var jarKeyNilPSLTests = map[string]string{
   222  	"foo.www.example.com": "example.com",
   223  	"www.example.com":     "example.com",
   224  	"example.com":         "example.com",
   225  	"com":                 "com",
   226  	"foo.www.bbc.co.uk":   "co.uk",
   227  	"www.bbc.co.uk":       "co.uk",
   228  	"bbc.co.uk":           "co.uk",
   229  	"co.uk":               "co.uk",
   230  	"uk":                  "uk",
   231  	"192.168.0.5":         "192.168.0.5",
   232  	// The following are actual outputs of canonicalHost for
   233  	// malformed inputs to canonicalHost.
   234  	"":              "",
   235  	".":             ".",
   236  	"..":            "..",
   237  	".net":          ".net",
   238  	"a.":            "a.",
   239  	"b.a.":          "a.",
   240  	"weird.stuff..": "stuff..",
   241  }
   242  
   243  func TestJarKeyNilPSL(t *testing.T) {
   244  	for host, want := range jarKeyNilPSLTests {
   245  		if got := jarKey(host, nil); got != want {
   246  			t.Errorf("%q: got %q, want %q", host, got, want)
   247  		}
   248  	}
   249  }
   250  
   251  var isIPTests = map[string]bool{
   252  	"127.0.0.1":            true,
   253  	"1.2.3.4":              true,
   254  	"2001:4860:0:2001::68": true,
   255  	"example.com":          false,
   256  	"1.1.1.300":            false,
   257  	"www.foo.bar.net":      false,
   258  	"123.foo.bar.net":      false,
   259  }
   260  
   261  func TestIsIP(t *testing.T) {
   262  	for host, want := range isIPTests {
   263  		if got := isIP(host); got != want {
   264  			t.Errorf("%q: got %t, want %t", host, got, want)
   265  		}
   266  	}
   267  }
   268  
   269  var defaultPathTests = map[string]string{
   270  	"/":           "/",
   271  	"/abc":        "/",
   272  	"/abc/":       "/abc",
   273  	"/abc/xyz":    "/abc",
   274  	"/abc/xyz/":   "/abc/xyz",
   275  	"/a/b/c.html": "/a/b",
   276  	"":            "/",
   277  	"strange":     "/",
   278  	"//":          "/",
   279  	"/a//b":       "/a/",
   280  	"/a/./b":      "/a/.",
   281  	"/a/../b":     "/a/..",
   282  }
   283  
   284  func TestDefaultPath(t *testing.T) {
   285  	for path, want := range defaultPathTests {
   286  		if got := defaultPath(path); got != want {
   287  			t.Errorf("%q: got %q, want %q", path, got, want)
   288  		}
   289  	}
   290  }
   291  
   292  var domainAndTypeTests = [...]struct {
   293  	host         string // host Set-Cookie header was received from
   294  	domain       string // domain attribute in Set-Cookie header
   295  	wantDomain   string // expected domain of cookie
   296  	wantHostOnly bool   // expected host-cookie flag
   297  	wantErr      error  // expected error
   298  }{
   299  	{"www.example.com", "", "www.example.com", true, nil},
   300  	{"127.0.0.1", "", "127.0.0.1", true, nil},
   301  	{"2001:4860:0:2001::68", "", "2001:4860:0:2001::68", true, nil},
   302  	{"www.example.com", "example.com", "example.com", false, nil},
   303  	{"www.example.com", ".example.com", "example.com", false, nil},
   304  	{"www.example.com", "www.example.com", "www.example.com", false, nil},
   305  	{"www.example.com", ".www.example.com", "www.example.com", false, nil},
   306  	{"foo.sso.example.com", "sso.example.com", "sso.example.com", false, nil},
   307  	{"bar.co.uk", "bar.co.uk", "bar.co.uk", false, nil},
   308  	{"foo.bar.co.uk", ".bar.co.uk", "bar.co.uk", false, nil},
   309  	{"127.0.0.1", "127.0.0.1", "", false, errNoHostname},
   310  	{"2001:4860:0:2001::68", "2001:4860:0:2001::68", "2001:4860:0:2001::68", false, errNoHostname},
   311  	{"www.example.com", ".", "", false, errMalformedDomain},
   312  	{"www.example.com", "..", "", false, errMalformedDomain},
   313  	{"www.example.com", "other.com", "", false, errIllegalDomain},
   314  	{"www.example.com", "com", "", false, errIllegalDomain},
   315  	{"www.example.com", ".com", "", false, errIllegalDomain},
   316  	{"foo.bar.co.uk", ".co.uk", "", false, errIllegalDomain},
   317  	{"127.www.0.0.1", "127.0.0.1", "", false, errIllegalDomain},
   318  	{"com", "", "com", true, nil},
   319  	{"com", "com", "com", true, nil},
   320  	{"com", ".com", "com", true, nil},
   321  	{"co.uk", "", "co.uk", true, nil},
   322  	{"co.uk", "co.uk", "co.uk", true, nil},
   323  	{"co.uk", ".co.uk", "co.uk", true, nil},
   324  }
   325  
   326  func TestDomainAndType(t *testing.T) {
   327  	jar := newTestJar()
   328  	for _, tc := range domainAndTypeTests {
   329  		domain, hostOnly, err := jar.domainAndType(tc.host, tc.domain)
   330  		if err != tc.wantErr {
   331  			t.Errorf("%q/%q: got %q error, want %q",
   332  				tc.host, tc.domain, err, tc.wantErr)
   333  			continue
   334  		}
   335  		if err != nil {
   336  			continue
   337  		}
   338  		if domain != tc.wantDomain || hostOnly != tc.wantHostOnly {
   339  			t.Errorf("%q/%q: got %q/%t want %q/%t",
   340  				tc.host, tc.domain, domain, hostOnly,
   341  				tc.wantDomain, tc.wantHostOnly)
   342  		}
   343  	}
   344  }
   345  
   346  // expiresIn creates an expires attribute delta seconds from tNow.
   347  func expiresIn(delta int) string {
   348  	t := tNow.Add(time.Duration(delta) * time.Second)
   349  	return "expires=" + t.Format(time.RFC1123)
   350  }
   351  
   352  // mustParseURL parses s to an URL and panics on error.
   353  func mustParseURL(s string) *url.URL {
   354  	u, err := url.Parse(s)
   355  	if err != nil || u.Scheme == "" || u.Host == "" {
   356  		panic(fmt.Sprintf("Unable to parse URL %s.", s))
   357  	}
   358  	return u
   359  }
   360  
   361  // jarTest encapsulates the following actions on a jar:
   362  //   1. Perform SetCookies with fromURL and the cookies from setCookies.
   363  //      (Done at time tNow + 0 ms.)
   364  //   2. Check that the entries in the jar matches content.
   365  //      (Done at time tNow + 1001 ms.)
   366  //   3. For each query in tests: Check that Cookies with toURL yields the
   367  //      cookies in want.
   368  //      (Query n done at tNow + (n+2)*1001 ms.)
   369  type jarTest struct {
   370  	description string   // The description of what this test is supposed to test
   371  	fromURL     string   // The full URL of the request from which Set-Cookie headers where received
   372  	setCookies  []string // All the cookies received from fromURL
   373  	content     string   // The whole (non-expired) content of the jar
   374  	queries     []query  // Queries to test the Jar.Cookies method
   375  }
   376  
   377  // query contains one test of the cookies returned from Jar.Cookies.
   378  type query struct {
   379  	toURL string // the URL in the Cookies call
   380  	want  string // the expected list of cookies (order matters)
   381  }
   382  
   383  // run runs the jarTest.
   384  func (test jarTest) run(t *testing.T, jar *Jar) {
   385  	now := tNow
   386  
   387  	// Populate jar with cookies.
   388  	setCookies := make([]*http.Cookie, len(test.setCookies))
   389  	for i, cs := range test.setCookies {
   390  		cookies := (&http.Response{Header: http.Header{"Set-Cookie": {cs}}}).Cookies()
   391  		if len(cookies) != 1 {
   392  			panic(fmt.Sprintf("Wrong cookie line %q: %#v", cs, cookies))
   393  		}
   394  		setCookies[i] = cookies[0]
   395  	}
   396  	jar.setCookies(mustParseURL(test.fromURL), setCookies, now)
   397  	now = now.Add(1001 * time.Millisecond)
   398  
   399  	// Serialize non-expired entries in the form "name1=val1 name2=val2".
   400  	var cs []string
   401  	for _, submap := range jar.entries {
   402  		for _, cookie := range submap {
   403  			if !cookie.Expires.After(now) {
   404  				continue
   405  			}
   406  			cs = append(cs, cookie.Name+"="+cookie.Value)
   407  		}
   408  	}
   409  	sort.Strings(cs)
   410  	got := strings.Join(cs, " ")
   411  
   412  	// Make sure jar content matches our expectations.
   413  	if got != test.content {
   414  		t.Errorf("Test %q Content\ngot  %q\nwant %q",
   415  			test.description, got, test.content)
   416  	}
   417  
   418  	// Test different calls to Cookies.
   419  	for i, query := range test.queries {
   420  		now = now.Add(1001 * time.Millisecond)
   421  		var s []string
   422  		for _, c := range jar.cookies(mustParseURL(query.toURL), now) {
   423  			s = append(s, c.Name+"="+c.Value)
   424  		}
   425  		if got := strings.Join(s, " "); got != query.want {
   426  			t.Errorf("Test %q #%d\ngot  %q\nwant %q", test.description, i, got, query.want)
   427  		}
   428  	}
   429  }
   430  
   431  // basicsTests contains fundamental tests. Each jarTest has to be performed on
   432  // a fresh, empty Jar.
   433  //goland:noinspection HttpUrlsUsage
   434  var basicsTests = [...]jarTest{
   435  	{
   436  		"Retrieval of a plain host cookie.",
   437  		"http://www.host.test/",
   438  		[]string{"A=a"},
   439  		"A=a",
   440  		[]query{
   441  			{"http://www.host.test", "A=a"},
   442  			{"http://www.host.test/", "A=a"},
   443  			{"http://www.host.test/some/path", "A=a"},
   444  			{"https://www.host.test", "A=a"},
   445  			{"https://www.host.test/", "A=a"},
   446  			{"https://www.host.test/some/path", "A=a"},
   447  			{"ftp://www.host.test", ""},
   448  			{"ftp://www.host.test/", ""},
   449  			{"ftp://www.host.test/some/path", ""},
   450  			{"http://www.other.org", ""},
   451  			{"http://sibling.host.test", ""},
   452  			{"http://deep.www.host.test", ""},
   453  		},
   454  	},
   455  	{
   456  		"Secure cookies are not returned to http.",
   457  		"http://www.host.test/",
   458  		[]string{"A=a; secure"},
   459  		"A=a",
   460  		[]query{
   461  			{"http://www.host.test", ""},
   462  			{"http://www.host.test/", ""},
   463  			{"http://www.host.test/some/path", ""},
   464  			{"https://www.host.test", "A=a"},
   465  			{"https://www.host.test/", "A=a"},
   466  			{"https://www.host.test/some/path", "A=a"},
   467  		},
   468  	},
   469  	{
   470  		"Explicit path.",
   471  		"http://www.host.test/",
   472  		[]string{"A=a; path=/some/path"},
   473  		"A=a",
   474  		[]query{
   475  			{"http://www.host.test", ""},
   476  			{"http://www.host.test/", ""},
   477  			{"http://www.host.test/some", ""},
   478  			{"http://www.host.test/some/", ""},
   479  			{"http://www.host.test/some/path", "A=a"},
   480  			{"http://www.host.test/some/paths", ""},
   481  			{"http://www.host.test/some/path/foo", "A=a"},
   482  			{"http://www.host.test/some/path/foo/", "A=a"},
   483  		},
   484  	},
   485  	{
   486  		"Implicit path #1: path is a directory.",
   487  		"http://www.host.test/some/path/",
   488  		[]string{"A=a"},
   489  		"A=a",
   490  		[]query{
   491  			{"http://www.host.test", ""},
   492  			{"http://www.host.test/", ""},
   493  			{"http://www.host.test/some", ""},
   494  			{"http://www.host.test/some/", ""},
   495  			{"http://www.host.test/some/path", "A=a"},
   496  			{"http://www.host.test/some/paths", ""},
   497  			{"http://www.host.test/some/path/foo", "A=a"},
   498  			{"http://www.host.test/some/path/foo/", "A=a"},
   499  		},
   500  	},
   501  	{
   502  		"Implicit path #2: path is not a directory.",
   503  		"http://www.host.test/some/path/index.html",
   504  		[]string{"A=a"},
   505  		"A=a",
   506  		[]query{
   507  			{"http://www.host.test", ""},
   508  			{"http://www.host.test/", ""},
   509  			{"http://www.host.test/some", ""},
   510  			{"http://www.host.test/some/", ""},
   511  			{"http://www.host.test/some/path", "A=a"},
   512  			{"http://www.host.test/some/paths", ""},
   513  			{"http://www.host.test/some/path/foo", "A=a"},
   514  			{"http://www.host.test/some/path/foo/", "A=a"},
   515  		},
   516  	},
   517  	{
   518  		"Implicit path #3: no path in URL at all.",
   519  		"http://www.host.test",
   520  		[]string{"A=a"},
   521  		"A=a",
   522  		[]query{
   523  			{"http://www.host.test", "A=a"},
   524  			{"http://www.host.test/", "A=a"},
   525  			{"http://www.host.test/some/path", "A=a"},
   526  		},
   527  	},
   528  	{
   529  		"Cookies are sorted by path length.",
   530  		"http://www.host.test/",
   531  		[]string{
   532  			"A=a; path=/foo/bar",
   533  			"B=b; path=/foo/bar/baz/qux",
   534  			"C=c; path=/foo/bar/baz",
   535  			"D=d; path=/foo"},
   536  		"A=a B=b C=c D=d",
   537  		[]query{
   538  			{"http://www.host.test/foo/bar/baz/qux", "B=b C=c A=a D=d"},
   539  			{"http://www.host.test/foo/bar/baz/", "C=c A=a D=d"},
   540  			{"http://www.host.test/foo/bar", "A=a D=d"},
   541  		},
   542  	},
   543  	{
   544  		"Creation time determines sorting on same length paths.",
   545  		"http://www.host.test/",
   546  		[]string{
   547  			"A=a; path=/foo/bar",
   548  			"X=x; path=/foo/bar",
   549  			"Y=y; path=/foo/bar/baz/qux",
   550  			"B=b; path=/foo/bar/baz/qux",
   551  			"C=c; path=/foo/bar/baz",
   552  			"W=w; path=/foo/bar/baz",
   553  			"Z=z; path=/foo",
   554  			"D=d; path=/foo"},
   555  		"A=a B=b C=c D=d W=w X=x Y=y Z=z",
   556  		[]query{
   557  			{"http://www.host.test/foo/bar/baz/qux", "Y=y B=b C=c W=w A=a X=x Z=z D=d"},
   558  			{"http://www.host.test/foo/bar/baz/", "C=c W=w A=a X=x Z=z D=d"},
   559  			{"http://www.host.test/foo/bar", "A=a X=x Z=z D=d"},
   560  		},
   561  	},
   562  	{
   563  		"Sorting of same-name cookies.",
   564  		"http://www.host.test/",
   565  		[]string{
   566  			"A=1; path=/",
   567  			"A=2; path=/path",
   568  			"A=3; path=/quux",
   569  			"A=4; path=/path/foo",
   570  			"A=5; domain=.host.test; path=/path",
   571  			"A=6; domain=.host.test; path=/quux",
   572  			"A=7; domain=.host.test; path=/path/foo",
   573  		},
   574  		"A=1 A=2 A=3 A=4 A=5 A=6 A=7",
   575  		[]query{
   576  			{"http://www.host.test/path", "A=2 A=5 A=1"},
   577  			{"http://www.host.test/path/foo", "A=4 A=7 A=2 A=5 A=1"},
   578  		},
   579  	},
   580  	{
   581  		"Disallow domain cookie on public suffix.",
   582  		"http://www.bbc.co.uk",
   583  		[]string{
   584  			"a=1",
   585  			"b=2; domain=co.uk",
   586  		},
   587  		"a=1",
   588  		[]query{{"http://www.bbc.co.uk", "a=1"}},
   589  	},
   590  	{
   591  		"Host cookie on IP.",
   592  		"http://192.168.0.10",
   593  		[]string{"a=1"},
   594  		"a=1",
   595  		[]query{{"http://192.168.0.10", "a=1"}},
   596  	},
   597  	{
   598  		"Port is ignored #1.",
   599  		"http://www.host.test/",
   600  		[]string{"a=1"},
   601  		"a=1",
   602  		[]query{
   603  			{"http://www.host.test", "a=1"},
   604  			{"http://www.host.test:8080/", "a=1"},
   605  		},
   606  	},
   607  	{
   608  		"Port is ignored #2.",
   609  		"http://www.host.test:8080/",
   610  		[]string{"a=1"},
   611  		"a=1",
   612  		[]query{
   613  			{"http://www.host.test", "a=1"},
   614  			{"http://www.host.test:8080/", "a=1"},
   615  			{"http://www.host.test:1234/", "a=1"},
   616  		},
   617  	},
   618  }
   619  
   620  func TestBasics(t *testing.T) {
   621  	for _, test := range basicsTests {
   622  		jar := newTestJar()
   623  		test.run(t, jar)
   624  	}
   625  }
   626  
   627  // updateAndDeleteTests contains jarTests which must be performed on the same
   628  // Jar.
   629  //goland:noinspection HttpUrlsUsage
   630  var updateAndDeleteTests = [...]jarTest{
   631  	{
   632  		"Set initial cookies.",
   633  		"http://www.host.test",
   634  		[]string{
   635  			"a=1",
   636  			"b=2; secure",
   637  			"c=3; httponly",
   638  			"d=4; secure; httponly"},
   639  		"a=1 b=2 c=3 d=4",
   640  		[]query{
   641  			{"http://www.host.test", "a=1 c=3"},
   642  			{"https://www.host.test", "a=1 b=2 c=3 d=4"},
   643  		},
   644  	},
   645  	{
   646  		"Update value via http.",
   647  		"http://www.host.test",
   648  		[]string{
   649  			"a=w",
   650  			"b=x; secure",
   651  			"c=y; httponly",
   652  			"d=z; secure; httponly"},
   653  		"a=w b=x c=y d=z",
   654  		[]query{
   655  			{"http://www.host.test", "a=w c=y"},
   656  			{"https://www.host.test", "a=w b=x c=y d=z"},
   657  		},
   658  	},
   659  	{
   660  		"Clear Secure flag from a http.",
   661  		"http://www.host.test/",
   662  		[]string{
   663  			"b=xx",
   664  			"d=zz; httponly"},
   665  		"a=w b=xx c=y d=zz",
   666  		[]query{{"http://www.host.test", "a=w b=xx c=y d=zz"}},
   667  	},
   668  	{
   669  		"Delete all.",
   670  		"http://www.host.test/",
   671  		[]string{
   672  			"a=1; max-Age=-1",                    // delete via MaxAge
   673  			"b=2; " + expiresIn(-10),             // delete via Expires
   674  			"c=2; max-age=-1; " + expiresIn(-10), // delete via both
   675  			"d=4; max-age=-1; " + expiresIn(10)}, // MaxAge takes precedence
   676  		"",
   677  		[]query{{"http://www.host.test", ""}},
   678  	},
   679  	{
   680  		"Refill #1.",
   681  		"http://www.host.test",
   682  		[]string{
   683  			"A=1",
   684  			"A=2; path=/foo",
   685  			"A=3; domain=.host.test",
   686  			"A=4; path=/foo; domain=.host.test"},
   687  		"A=1 A=2 A=3 A=4",
   688  		[]query{{"http://www.host.test/foo", "A=2 A=4 A=1 A=3"}},
   689  	},
   690  	{
   691  		"Refill #2.",
   692  		"http://www.google.com",
   693  		[]string{
   694  			"A=6",
   695  			"A=7; path=/foo",
   696  			"A=8; domain=.google.com",
   697  			"A=9; path=/foo; domain=.google.com"},
   698  		"A=1 A=2 A=3 A=4 A=6 A=7 A=8 A=9",
   699  		[]query{
   700  			{"http://www.host.test/foo", "A=2 A=4 A=1 A=3"},
   701  			{"http://www.google.com/foo", "A=7 A=9 A=6 A=8"},
   702  		},
   703  	},
   704  	{
   705  		"Delete A7.",
   706  		"http://www.google.com",
   707  		[]string{"A=; path=/foo; max-age=-1"},
   708  		"A=1 A=2 A=3 A=4 A=6 A=8 A=9",
   709  		[]query{
   710  			{"http://www.host.test/foo", "A=2 A=4 A=1 A=3"},
   711  			{"http://www.google.com/foo", "A=9 A=6 A=8"},
   712  		},
   713  	},
   714  	{
   715  		"Delete A4.",
   716  		"http://www.host.test",
   717  		[]string{"A=; path=/foo; domain=host.test; max-age=-1"},
   718  		"A=1 A=2 A=3 A=6 A=8 A=9",
   719  		[]query{
   720  			{"http://www.host.test/foo", "A=2 A=1 A=3"},
   721  			{"http://www.google.com/foo", "A=9 A=6 A=8"},
   722  		},
   723  	},
   724  	{
   725  		"Delete A6.",
   726  		"http://www.google.com",
   727  		[]string{"A=; max-age=-1"},
   728  		"A=1 A=2 A=3 A=8 A=9",
   729  		[]query{
   730  			{"http://www.host.test/foo", "A=2 A=1 A=3"},
   731  			{"http://www.google.com/foo", "A=9 A=8"},
   732  		},
   733  	},
   734  	{
   735  		"Delete A3.",
   736  		"http://www.host.test",
   737  		[]string{"A=; domain=host.test; max-age=-1"},
   738  		"A=1 A=2 A=8 A=9",
   739  		[]query{
   740  			{"http://www.host.test/foo", "A=2 A=1"},
   741  			{"http://www.google.com/foo", "A=9 A=8"},
   742  		},
   743  	},
   744  	{
   745  		"No cross-domain delete.",
   746  		"http://www.host.test",
   747  		[]string{
   748  			"A=; domain=google.com; max-age=-1",
   749  			"A=; path=/foo; domain=google.com; max-age=-1"},
   750  		"A=1 A=2 A=8 A=9",
   751  		[]query{
   752  			{"http://www.host.test/foo", "A=2 A=1"},
   753  			{"http://www.google.com/foo", "A=9 A=8"},
   754  		},
   755  	},
   756  	{
   757  		"Delete A8 and A9.",
   758  		"http://www.google.com",
   759  		[]string{
   760  			"A=; domain=google.com; max-age=-1",
   761  			"A=; path=/foo; domain=google.com; max-age=-1"},
   762  		"A=1 A=2",
   763  		[]query{
   764  			{"http://www.host.test/foo", "A=2 A=1"},
   765  			{"http://www.google.com/foo", ""},
   766  		},
   767  	},
   768  }
   769  
   770  func TestUpdateAndDelete(t *testing.T) {
   771  	jar := newTestJar()
   772  	for _, test := range updateAndDeleteTests {
   773  		test.run(t, jar)
   774  	}
   775  }
   776  
   777  func TestExpiration(t *testing.T) {
   778  	jar := newTestJar()
   779  	jarTest{
   780  		"Expiration.",
   781  		"http://www.host.test",
   782  		[]string{
   783  			"a=1",
   784  			"b=2; max-age=3",
   785  			"c=3; " + expiresIn(3),
   786  			"d=4; max-age=5",
   787  			"e=5; " + expiresIn(5),
   788  			"f=6; max-age=100",
   789  		},
   790  		"a=1 b=2 c=3 d=4 e=5 f=6", // executed at t0 + 1001 ms
   791  		[]query{
   792  			{"http://www.host.test", "a=1 b=2 c=3 d=4 e=5 f=6"}, // t0 + 2002 ms
   793  			{"http://www.host.test", "a=1 d=4 e=5 f=6"},         // t0 + 3003 ms
   794  			{"http://www.host.test", "a=1 d=4 e=5 f=6"},         // t0 + 4004 ms
   795  			{"http://www.host.test", "a=1 f=6"},                 // t0 + 5005 ms
   796  			{"http://www.host.test", "a=1 f=6"},                 // t0 + 6006 ms
   797  		},
   798  	}.run(t, jar)
   799  }
   800  
   801  //
   802  // Tests derived from Chromium's cookie_store_unittest.h.
   803  //
   804  
   805  // See http://src.chromium.org/viewvc/chrome/trunk/src/net/cookies/cookie_store_unittest.h?revision=159685&content-type=text/plain
   806  // Some of the original tests are in a bad condition (e.g.
   807  // DomainWithTrailingDotTest) or are not RFC 6265 conforming (e.g.
   808  // TestNonDottedAndTLD #1 and #6) and have not been ported.
   809  
   810  // chromiumBasicsTests contains fundamental tests. Each jarTest has to be
   811  // performed on a fresh, empty Jar.
   812  //goland:noinspection HttpUrlsUsage
   813  var chromiumBasicsTests = [...]jarTest{
   814  	{
   815  		"DomainWithTrailingDotTest.",
   816  		"http://www.google.com/",
   817  		[]string{
   818  			"a=1; domain=.www.google.com.",
   819  			"b=2; domain=.www.google.com.."},
   820  		"",
   821  		[]query{
   822  			{"http://www.google.com", ""},
   823  		},
   824  	},
   825  	{
   826  		"ValidSubdomainTest #1.",
   827  		"http://a.b.c.d.com",
   828  		[]string{
   829  			"a=1; domain=.a.b.c.d.com",
   830  			"b=2; domain=.b.c.d.com",
   831  			"c=3; domain=.c.d.com",
   832  			"d=4; domain=.d.com"},
   833  		"a=1 b=2 c=3 d=4",
   834  		[]query{
   835  			{"http://a.b.c.d.com", "a=1 b=2 c=3 d=4"},
   836  			{"http://b.c.d.com", "b=2 c=3 d=4"},
   837  			{"http://c.d.com", "c=3 d=4"},
   838  			{"http://d.com", "d=4"},
   839  		},
   840  	},
   841  	{
   842  		"ValidSubdomainTest #2.",
   843  		"http://a.b.c.d.com",
   844  		[]string{
   845  			"a=1; domain=.a.b.c.d.com",
   846  			"b=2; domain=.b.c.d.com",
   847  			"c=3; domain=.c.d.com",
   848  			"d=4; domain=.d.com",
   849  			"X=bcd; domain=.b.c.d.com",
   850  			"X=cd; domain=.c.d.com"},
   851  		"X=bcd X=cd a=1 b=2 c=3 d=4",
   852  		[]query{
   853  			{"http://b.c.d.com", "b=2 c=3 d=4 X=bcd X=cd"},
   854  			{"http://c.d.com", "c=3 d=4 X=cd"},
   855  		},
   856  	},
   857  	{
   858  		"InvalidDomainTest #1.",
   859  		"http://foo.bar.com",
   860  		[]string{
   861  			"a=1; domain=.yo.foo.bar.com",
   862  			"b=2; domain=.foo.com",
   863  			"c=3; domain=.bar.foo.com",
   864  			"d=4; domain=.foo.bar.com.net",
   865  			"e=5; domain=ar.com",
   866  			"f=6; domain=.",
   867  			"g=7; domain=/",
   868  			"h=8; domain=http://foo.bar.com",
   869  			"i=9; domain=..foo.bar.com",
   870  			"j=10; domain=..bar.com",
   871  			"k=11; domain=.foo.bar.com?blah",
   872  			"l=12; domain=.foo.bar.com/blah",
   873  			"m=12; domain=.foo.bar.com:80",
   874  			"n=14; domain=.foo.bar.com:",
   875  			"o=15; domain=.foo.bar.com#sup",
   876  		},
   877  		"", // Jar is empty.
   878  		[]query{{"http://foo.bar.com", ""}},
   879  	},
   880  	{
   881  		"InvalidDomainTest #2.",
   882  		"http://foo.com.com",
   883  		[]string{"a=1; domain=.foo.com.com.com"},
   884  		"",
   885  		[]query{{"http://foo.bar.com", ""}},
   886  	},
   887  	{
   888  		"DomainWithoutLeadingDotTest #1.",
   889  		"http://manage.hosted.filefront.com",
   890  		[]string{"a=1; domain=filefront.com"},
   891  		"a=1",
   892  		[]query{{"http://www.filefront.com", "a=1"}},
   893  	},
   894  	{
   895  		"DomainWithoutLeadingDotTest #2.",
   896  		"http://www.google.com",
   897  		[]string{"a=1; domain=www.google.com"},
   898  		"a=1",
   899  		[]query{
   900  			{"http://www.google.com", "a=1"},
   901  			{"http://sub.www.google.com", "a=1"},
   902  			{"http://something-else.com", ""},
   903  		},
   904  	},
   905  	{
   906  		"CaseInsensitiveDomainTest.",
   907  		"http://www.google.com",
   908  		[]string{
   909  			"a=1; domain=.GOOGLE.COM",
   910  			"b=2; domain=.www.gOOgLE.coM"},
   911  		"a=1 b=2",
   912  		[]query{{"http://www.google.com", "a=1 b=2"}},
   913  	},
   914  	{
   915  		"TestIpAddress #1.",
   916  		"http://1.2.3.4/foo",
   917  		[]string{"a=1; path=/"},
   918  		"a=1",
   919  		[]query{{"http://1.2.3.4/foo", "a=1"}},
   920  	},
   921  	{
   922  		"TestIpAddress #2.",
   923  		"http://1.2.3.4/foo",
   924  		[]string{
   925  			"a=1; domain=.1.2.3.4",
   926  			"b=2; domain=.3.4"},
   927  		"",
   928  		[]query{{"http://1.2.3.4/foo", ""}},
   929  	},
   930  	{
   931  		"TestIpAddress #3.",
   932  		"http://1.2.3.4/foo",
   933  		[]string{"a=1; domain=1.2.3.4"},
   934  		"",
   935  		[]query{{"http://1.2.3.4/foo", ""}},
   936  	},
   937  	{
   938  		"TestNonDottedAndTLD #2.",
   939  		"http://com./index.html",
   940  		[]string{"a=1"},
   941  		"a=1",
   942  		[]query{
   943  			{"http://com./index.html", "a=1"},
   944  			{"http://no-cookies.com./index.html", ""},
   945  		},
   946  	},
   947  	{
   948  		"TestNonDottedAndTLD #3.",
   949  		"http://a.b",
   950  		[]string{
   951  			"a=1; domain=.b",
   952  			"b=2; domain=b"},
   953  		"",
   954  		[]query{{"http://bar.foo", ""}},
   955  	},
   956  	{
   957  		"TestNonDottedAndTLD #4.",
   958  		"http://google.com",
   959  		[]string{
   960  			"a=1; domain=.com",
   961  			"b=2; domain=com"},
   962  		"",
   963  		[]query{{"http://google.com", ""}},
   964  	},
   965  	{
   966  		"TestNonDottedAndTLD #5.",
   967  		"http://google.co.uk",
   968  		[]string{
   969  			"a=1; domain=.co.uk",
   970  			"b=2; domain=.uk"},
   971  		"",
   972  		[]query{
   973  			{"http://google.co.uk", ""},
   974  			{"http://else.co.com", ""},
   975  			{"http://else.uk", ""},
   976  		},
   977  	},
   978  	{
   979  		"TestHostEndsWithDot.",
   980  		"http://www.google.com",
   981  		[]string{
   982  			"a=1",
   983  			"b=2; domain=.www.google.com."},
   984  		"a=1",
   985  		[]query{{"http://www.google.com", "a=1"}},
   986  	},
   987  	{
   988  		"PathTest",
   989  		"http://www.google.izzle",
   990  		[]string{"a=1; path=/wee"},
   991  		"a=1",
   992  		[]query{
   993  			{"http://www.google.izzle/wee", "a=1"},
   994  			{"http://www.google.izzle/wee/", "a=1"},
   995  			{"http://www.google.izzle/wee/war", "a=1"},
   996  			{"http://www.google.izzle/wee/war/more/more", "a=1"},
   997  			{"http://www.google.izzle/weehee", ""},
   998  			{"http://www.google.izzle/", ""},
   999  		},
  1000  	},
  1001  }
  1002  
  1003  func TestChromiumBasics(t *testing.T) {
  1004  	for _, test := range chromiumBasicsTests {
  1005  		jar := newTestJar()
  1006  		test.run(t, jar)
  1007  	}
  1008  }
  1009  
  1010  // chromiumDomainTests contains jarTests which must be executed all on the
  1011  // same Jar.
  1012  var chromiumDomainTests = [...]jarTest{
  1013  	{
  1014  		"Fill #1.",
  1015  		"http://www.google.izzle",
  1016  		[]string{"A=B"},
  1017  		"A=B",
  1018  		[]query{{"http://www.google.izzle", "A=B"}},
  1019  	},
  1020  	{
  1021  		"Fill #2.",
  1022  		"http://www.google.izzle",
  1023  		[]string{"C=D; domain=.google.izzle"},
  1024  		"A=B C=D",
  1025  		[]query{{"http://www.google.izzle", "A=B C=D"}},
  1026  	},
  1027  	{
  1028  		"Verify A is a host cookie and not accessible from subdomain.",
  1029  		"http://unused.nil",
  1030  		[]string{},
  1031  		"A=B C=D",
  1032  		[]query{{"http://foo.www.google.izzle", "C=D"}},
  1033  	},
  1034  	{
  1035  		"Verify domain cookies are found on proper domain.",
  1036  		"http://www.google.izzle",
  1037  		[]string{"E=F; domain=.www.google.izzle"},
  1038  		"A=B C=D E=F",
  1039  		[]query{{"http://www.google.izzle", "A=B C=D E=F"}},
  1040  	},
  1041  	{
  1042  		"Leading dots in domain attributes are optional.",
  1043  		"http://www.google.izzle",
  1044  		[]string{"G=H; domain=www.google.izzle"},
  1045  		"A=B C=D E=F G=H",
  1046  		[]query{{"http://www.google.izzle", "A=B C=D E=F G=H"}},
  1047  	},
  1048  	{
  1049  		"Verify domain enforcement works #1.",
  1050  		"http://www.google.izzle",
  1051  		[]string{"K=L; domain=.bar.www.google.izzle"},
  1052  		"A=B C=D E=F G=H",
  1053  		[]query{{"http://bar.www.google.izzle", "C=D E=F G=H"}},
  1054  	},
  1055  	{
  1056  		"Verify domain enforcement works #2.",
  1057  		"http://unused.nil",
  1058  		[]string{},
  1059  		"A=B C=D E=F G=H",
  1060  		[]query{{"http://www.google.izzle", "A=B C=D E=F G=H"}},
  1061  	},
  1062  }
  1063  
  1064  func TestChromiumDomain(t *testing.T) {
  1065  	jar := newTestJar()
  1066  	for _, test := range chromiumDomainTests {
  1067  		test.run(t, jar)
  1068  	}
  1069  
  1070  }
  1071  
  1072  // chromiumDeletionTests must be performed all on the same Jar.
  1073  //goland:noinspection HttpUrlsUsage
  1074  var chromiumDeletionTests = [...]jarTest{
  1075  	{
  1076  		"Create session cookie a1.",
  1077  		"http://www.google.com",
  1078  		[]string{"a=1"},
  1079  		"a=1",
  1080  		[]query{{"http://www.google.com", "a=1"}},
  1081  	},
  1082  	{
  1083  		"Delete sc a1 via MaxAge.",
  1084  		"http://www.google.com",
  1085  		[]string{"a=1; max-age=-1"},
  1086  		"",
  1087  		[]query{{"http://www.google.com", ""}},
  1088  	},
  1089  	{
  1090  		"Create session cookie b2.",
  1091  		"http://www.google.com",
  1092  		[]string{"b=2"},
  1093  		"b=2",
  1094  		[]query{{"http://www.google.com", "b=2"}},
  1095  	},
  1096  	{
  1097  		"Delete sc b2 via Expires.",
  1098  		"http://www.google.com",
  1099  		[]string{"b=2; " + expiresIn(-10)},
  1100  		"",
  1101  		[]query{{"http://www.google.com", ""}},
  1102  	},
  1103  	{
  1104  		"Create persistent cookie c3.",
  1105  		"http://www.google.com",
  1106  		[]string{"c=3; max-age=3600"},
  1107  		"c=3",
  1108  		[]query{{"http://www.google.com", "c=3"}},
  1109  	},
  1110  	{
  1111  		"Delete pc c3 via MaxAge.",
  1112  		"http://www.google.com",
  1113  		[]string{"c=3; max-age=-1"},
  1114  		"",
  1115  		[]query{{"http://www.google.com", ""}},
  1116  	},
  1117  	{
  1118  		"Create persistent cookie d4.",
  1119  		"http://www.google.com",
  1120  		[]string{"d=4; max-age=3600"},
  1121  		"d=4",
  1122  		[]query{{"http://www.google.com", "d=4"}},
  1123  	},
  1124  	{
  1125  		"Delete pc d4 via Expires.",
  1126  		"http://www.google.com",
  1127  		[]string{"d=4; " + expiresIn(-10)},
  1128  		"",
  1129  		[]query{{"http://www.google.com", ""}},
  1130  	},
  1131  }
  1132  
  1133  func TestChromiumDeletion(t *testing.T) {
  1134  	jar := newTestJar()
  1135  	for _, test := range chromiumDeletionTests {
  1136  		test.run(t, jar)
  1137  	}
  1138  }
  1139  
  1140  // domainHandlingTests tests and documents the rules for domain handling.
  1141  // Each test must be performed on an empty new Jar.
  1142  //goland:noinspection HttpUrlsUsage
  1143  var domainHandlingTests = [...]jarTest{
  1144  	{
  1145  		"Host cookie",
  1146  		"http://www.host.test",
  1147  		[]string{"a=1"},
  1148  		"a=1",
  1149  		[]query{
  1150  			{"http://www.host.test", "a=1"},
  1151  			{"http://host.test", ""},
  1152  			{"http://bar.host.test", ""},
  1153  			{"http://foo.www.host.test", ""},
  1154  			{"http://other.test", ""},
  1155  			{"http://test", ""},
  1156  		},
  1157  	},
  1158  	{
  1159  		"Domain cookie #1",
  1160  		"http://www.host.test",
  1161  		[]string{"a=1; domain=host.test"},
  1162  		"a=1",
  1163  		[]query{
  1164  			{"http://www.host.test", "a=1"},
  1165  			{"http://host.test", "a=1"},
  1166  			{"http://bar.host.test", "a=1"},
  1167  			{"http://foo.www.host.test", "a=1"},
  1168  			{"http://other.test", ""},
  1169  			{"http://test", ""},
  1170  		},
  1171  	},
  1172  	{
  1173  		"Domain cookie #2",
  1174  		"http://www.host.test",
  1175  		[]string{"a=1; domain=.host.test"},
  1176  		"a=1",
  1177  		[]query{
  1178  			{"http://www.host.test", "a=1"},
  1179  			{"http://host.test", "a=1"},
  1180  			{"http://bar.host.test", "a=1"},
  1181  			{"http://foo.www.host.test", "a=1"},
  1182  			{"http://other.test", ""},
  1183  			{"http://test", ""},
  1184  		},
  1185  	},
  1186  	{
  1187  		"Host cookie on IDNA domain #1",
  1188  		"http://www.bücher.test",
  1189  		[]string{"a=1"},
  1190  		"a=1",
  1191  		[]query{
  1192  			{"http://www.bücher.test", "a=1"},
  1193  			{"http://www.xn--bcher-kva.test", "a=1"},
  1194  			{"http://bücher.test", ""},
  1195  			{"http://xn--bcher-kva.test", ""},
  1196  			{"http://bar.bücher.test", ""},
  1197  			{"http://bar.xn--bcher-kva.test", ""},
  1198  			{"http://foo.www.bücher.test", ""},
  1199  			{"http://foo.www.xn--bcher-kva.test", ""},
  1200  			{"http://other.test", ""},
  1201  			{"http://test", ""},
  1202  		},
  1203  	},
  1204  	{
  1205  		"Host cookie on IDNA domain #2",
  1206  		"http://www.xn--bcher-kva.test",
  1207  		[]string{"a=1"},
  1208  		"a=1",
  1209  		[]query{
  1210  			{"http://www.bücher.test", "a=1"},
  1211  			{"http://www.xn--bcher-kva.test", "a=1"},
  1212  			{"http://bücher.test", ""},
  1213  			{"http://xn--bcher-kva.test", ""},
  1214  			{"http://bar.bücher.test", ""},
  1215  			{"http://bar.xn--bcher-kva.test", ""},
  1216  			{"http://foo.www.bücher.test", ""},
  1217  			{"http://foo.www.xn--bcher-kva.test", ""},
  1218  			{"http://other.test", ""},
  1219  			{"http://test", ""},
  1220  		},
  1221  	},
  1222  	{
  1223  		"Domain cookie on IDNA domain #1",
  1224  		"http://www.bücher.test",
  1225  		[]string{"a=1; domain=xn--bcher-kva.test"},
  1226  		"a=1",
  1227  		[]query{
  1228  			{"http://www.bücher.test", "a=1"},
  1229  			{"http://www.xn--bcher-kva.test", "a=1"},
  1230  			{"http://bücher.test", "a=1"},
  1231  			{"http://xn--bcher-kva.test", "a=1"},
  1232  			{"http://bar.bücher.test", "a=1"},
  1233  			{"http://bar.xn--bcher-kva.test", "a=1"},
  1234  			{"http://foo.www.bücher.test", "a=1"},
  1235  			{"http://foo.www.xn--bcher-kva.test", "a=1"},
  1236  			{"http://other.test", ""},
  1237  			{"http://test", ""},
  1238  		},
  1239  	},
  1240  	{
  1241  		"Domain cookie on IDNA domain #2",
  1242  		"http://www.xn--bcher-kva.test",
  1243  		[]string{"a=1; domain=xn--bcher-kva.test"},
  1244  		"a=1",
  1245  		[]query{
  1246  			{"http://www.bücher.test", "a=1"},
  1247  			{"http://www.xn--bcher-kva.test", "a=1"},
  1248  			{"http://bücher.test", "a=1"},
  1249  			{"http://xn--bcher-kva.test", "a=1"},
  1250  			{"http://bar.bücher.test", "a=1"},
  1251  			{"http://bar.xn--bcher-kva.test", "a=1"},
  1252  			{"http://foo.www.bücher.test", "a=1"},
  1253  			{"http://foo.www.xn--bcher-kva.test", "a=1"},
  1254  			{"http://other.test", ""},
  1255  			{"http://test", ""},
  1256  		},
  1257  	},
  1258  	{
  1259  		"Host cookie on TLD.",
  1260  		"http://com",
  1261  		[]string{"a=1"},
  1262  		"a=1",
  1263  		[]query{
  1264  			{"http://com", "a=1"},
  1265  			{"http://any.com", ""},
  1266  			{"http://any.test", ""},
  1267  		},
  1268  	},
  1269  	{
  1270  		"Domain cookie on TLD becomes a host cookie.",
  1271  		"http://com",
  1272  		[]string{"a=1; domain=com"},
  1273  		"a=1",
  1274  		[]query{
  1275  			{"http://com", "a=1"},
  1276  			{"http://any.com", ""},
  1277  			{"http://any.test", ""},
  1278  		},
  1279  	},
  1280  	{
  1281  		"Host cookie on public suffix.",
  1282  		"http://co.uk",
  1283  		[]string{"a=1"},
  1284  		"a=1",
  1285  		[]query{
  1286  			{"http://co.uk", "a=1"},
  1287  			{"http://uk", ""},
  1288  			{"http://some.co.uk", ""},
  1289  			{"http://foo.some.co.uk", ""},
  1290  			{"http://any.uk", ""},
  1291  		},
  1292  	},
  1293  	{
  1294  		"Domain cookie on public suffix is ignored.",
  1295  		"http://some.co.uk",
  1296  		[]string{"a=1; domain=co.uk"},
  1297  		"",
  1298  		[]query{
  1299  			{"http://co.uk", ""},
  1300  			{"http://uk", ""},
  1301  			{"http://some.co.uk", ""},
  1302  			{"http://foo.some.co.uk", ""},
  1303  			{"http://any.uk", ""},
  1304  		},
  1305  	},
  1306  }
  1307  
  1308  func TestDomainHandling(t *testing.T) {
  1309  	for _, test := range domainHandlingTests {
  1310  		jar := newTestJar()
  1311  		test.run(t, jar)
  1312  	}
  1313  }
  1314  
  1315  func TestIssue19384(t *testing.T) {
  1316  	cookies := []*http.Cookie{{Name: "name", Value: "value"}}
  1317  	for _, host := range []string{"", ".", "..", "..."} {
  1318  		jar, _ := New(nil)
  1319  		u := &url.URL{Scheme: "http", Host: host, Path: "/"}
  1320  		if got := jar.Cookies(u); len(got) != 0 {
  1321  			t.Errorf("host %q, got %v", host, got)
  1322  		}
  1323  		jar.SetCookies(u, cookies)
  1324  		if got := jar.Cookies(u); len(got) != 1 || got[0].Value != "value" {
  1325  			t.Errorf("host %q, got %v", host, got)
  1326  		}
  1327  	}
  1328  }