github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/net/url/url_test.go (about)

     1  // Copyright 2009 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 url
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  type URLTest struct {
    15  	in        string
    16  	out       *URL
    17  	roundtrip string // expected result of reserializing the URL; empty means same as "in".
    18  }
    19  
    20  var urltests = []URLTest{
    21  	// no path
    22  	{
    23  		"http://www.google.com",
    24  		&URL{
    25  			Scheme: "http",
    26  			Host:   "www.google.com",
    27  		},
    28  		"",
    29  	},
    30  	// path
    31  	{
    32  		"http://www.google.com/",
    33  		&URL{
    34  			Scheme: "http",
    35  			Host:   "www.google.com",
    36  			Path:   "/",
    37  		},
    38  		"",
    39  	},
    40  	// path with hex escaping
    41  	{
    42  		"http://www.google.com/file%20one%26two",
    43  		&URL{
    44  			Scheme: "http",
    45  			Host:   "www.google.com",
    46  			Path:   "/file one&two",
    47  		},
    48  		"http://www.google.com/file%20one&two",
    49  	},
    50  	// user
    51  	{
    52  		"ftp://webmaster@www.google.com/",
    53  		&URL{
    54  			Scheme: "ftp",
    55  			User:   User("webmaster"),
    56  			Host:   "www.google.com",
    57  			Path:   "/",
    58  		},
    59  		"",
    60  	},
    61  	// escape sequence in username
    62  	{
    63  		"ftp://john%20doe@www.google.com/",
    64  		&URL{
    65  			Scheme: "ftp",
    66  			User:   User("john doe"),
    67  			Host:   "www.google.com",
    68  			Path:   "/",
    69  		},
    70  		"ftp://john%20doe@www.google.com/",
    71  	},
    72  	// query
    73  	{
    74  		"http://www.google.com/?q=go+language",
    75  		&URL{
    76  			Scheme:   "http",
    77  			Host:     "www.google.com",
    78  			Path:     "/",
    79  			RawQuery: "q=go+language",
    80  		},
    81  		"",
    82  	},
    83  	// query with hex escaping: NOT parsed
    84  	{
    85  		"http://www.google.com/?q=go%20language",
    86  		&URL{
    87  			Scheme:   "http",
    88  			Host:     "www.google.com",
    89  			Path:     "/",
    90  			RawQuery: "q=go%20language",
    91  		},
    92  		"",
    93  	},
    94  	// %20 outside query
    95  	{
    96  		"http://www.google.com/a%20b?q=c+d",
    97  		&URL{
    98  			Scheme:   "http",
    99  			Host:     "www.google.com",
   100  			Path:     "/a b",
   101  			RawQuery: "q=c+d",
   102  		},
   103  		"",
   104  	},
   105  	// path without leading /, so no parsing
   106  	{
   107  		"http:www.google.com/?q=go+language",
   108  		&URL{
   109  			Scheme:   "http",
   110  			Opaque:   "www.google.com/",
   111  			RawQuery: "q=go+language",
   112  		},
   113  		"http:www.google.com/?q=go+language",
   114  	},
   115  	// path without leading /, so no parsing
   116  	{
   117  		"http:%2f%2fwww.google.com/?q=go+language",
   118  		&URL{
   119  			Scheme:   "http",
   120  			Opaque:   "%2f%2fwww.google.com/",
   121  			RawQuery: "q=go+language",
   122  		},
   123  		"http:%2f%2fwww.google.com/?q=go+language",
   124  	},
   125  	// non-authority with path
   126  	{
   127  		"mailto:/webmaster@golang.org",
   128  		&URL{
   129  			Scheme: "mailto",
   130  			Path:   "/webmaster@golang.org",
   131  		},
   132  		"mailto:///webmaster@golang.org", // unfortunate compromise
   133  	},
   134  	// non-authority
   135  	{
   136  		"mailto:webmaster@golang.org",
   137  		&URL{
   138  			Scheme: "mailto",
   139  			Opaque: "webmaster@golang.org",
   140  		},
   141  		"",
   142  	},
   143  	// unescaped :// in query should not create a scheme
   144  	{
   145  		"/foo?query=http://bad",
   146  		&URL{
   147  			Path:     "/foo",
   148  			RawQuery: "query=http://bad",
   149  		},
   150  		"",
   151  	},
   152  	// leading // without scheme should create an authority
   153  	{
   154  		"//foo",
   155  		&URL{
   156  			Host: "foo",
   157  		},
   158  		"",
   159  	},
   160  	// leading // without scheme, with userinfo, path, and query
   161  	{
   162  		"//user@foo/path?a=b",
   163  		&URL{
   164  			User:     User("user"),
   165  			Host:     "foo",
   166  			Path:     "/path",
   167  			RawQuery: "a=b",
   168  		},
   169  		"",
   170  	},
   171  	// Three leading slashes isn't an authority, but doesn't return an error.
   172  	// (We can't return an error, as this code is also used via
   173  	// ServeHTTP -> ReadRequest -> Parse, which is arguably a
   174  	// different URL parsing context, but currently shares the
   175  	// same codepath)
   176  	{
   177  		"///threeslashes",
   178  		&URL{
   179  			Path: "///threeslashes",
   180  		},
   181  		"",
   182  	},
   183  	{
   184  		"http://user:password@google.com",
   185  		&URL{
   186  			Scheme: "http",
   187  			User:   UserPassword("user", "password"),
   188  			Host:   "google.com",
   189  		},
   190  		"http://user:password@google.com",
   191  	},
   192  	// unescaped @ in username should not confuse host
   193  	{
   194  		"http://j@ne:password@google.com",
   195  		&URL{
   196  			Scheme: "http",
   197  			User:   UserPassword("j@ne", "password"),
   198  			Host:   "google.com",
   199  		},
   200  		"http://j%40ne:password@google.com",
   201  	},
   202  	// unescaped @ in password should not confuse host
   203  	{
   204  		"http://jane:p@ssword@google.com",
   205  		&URL{
   206  			Scheme: "http",
   207  			User:   UserPassword("jane", "p@ssword"),
   208  			Host:   "google.com",
   209  		},
   210  		"http://jane:p%40ssword@google.com",
   211  	},
   212  	{
   213  		"http://j@ne:password@google.com/p@th?q=@go",
   214  		&URL{
   215  			Scheme:   "http",
   216  			User:     UserPassword("j@ne", "password"),
   217  			Host:     "google.com",
   218  			Path:     "/p@th",
   219  			RawQuery: "q=@go",
   220  		},
   221  		"http://j%40ne:password@google.com/p@th?q=@go",
   222  	},
   223  	{
   224  		"http://www.google.com/?q=go+language#foo",
   225  		&URL{
   226  			Scheme:   "http",
   227  			Host:     "www.google.com",
   228  			Path:     "/",
   229  			RawQuery: "q=go+language",
   230  			Fragment: "foo",
   231  		},
   232  		"",
   233  	},
   234  	{
   235  		"http://www.google.com/?q=go+language#foo%26bar",
   236  		&URL{
   237  			Scheme:   "http",
   238  			Host:     "www.google.com",
   239  			Path:     "/",
   240  			RawQuery: "q=go+language",
   241  			Fragment: "foo&bar",
   242  		},
   243  		"http://www.google.com/?q=go+language#foo&bar",
   244  	},
   245  	{
   246  		"file:///home/adg/rabbits",
   247  		&URL{
   248  			Scheme: "file",
   249  			Host:   "",
   250  			Path:   "/home/adg/rabbits",
   251  		},
   252  		"file:///home/adg/rabbits",
   253  	},
   254  	// "Windows" paths are no exception to the rule.
   255  	// See golang.org/issue/6027, especially comment #9.
   256  	{
   257  		"file:///C:/FooBar/Baz.txt",
   258  		&URL{
   259  			Scheme: "file",
   260  			Host:   "",
   261  			Path:   "/C:/FooBar/Baz.txt",
   262  		},
   263  		"file:///C:/FooBar/Baz.txt",
   264  	},
   265  	// case-insensitive scheme
   266  	{
   267  		"MaIlTo:webmaster@golang.org",
   268  		&URL{
   269  			Scheme: "mailto",
   270  			Opaque: "webmaster@golang.org",
   271  		},
   272  		"mailto:webmaster@golang.org",
   273  	},
   274  	// Relative path
   275  	{
   276  		"a/b/c",
   277  		&URL{
   278  			Path: "a/b/c",
   279  		},
   280  		"a/b/c",
   281  	},
   282  	// escaped '?' in username and password
   283  	{
   284  		"http://%3Fam:pa%3Fsword@google.com",
   285  		&URL{
   286  			Scheme: "http",
   287  			User:   UserPassword("?am", "pa?sword"),
   288  			Host:   "google.com",
   289  		},
   290  		"",
   291  	},
   292  }
   293  
   294  // more useful string for debugging than fmt's struct printer
   295  func ufmt(u *URL) string {
   296  	var user, pass interface{}
   297  	if u.User != nil {
   298  		user = u.User.Username()
   299  		if p, ok := u.User.Password(); ok {
   300  			pass = p
   301  		}
   302  	}
   303  	return fmt.Sprintf("opaque=%q, scheme=%q, user=%#v, pass=%#v, host=%q, path=%q, rawq=%q, frag=%q",
   304  		u.Opaque, u.Scheme, user, pass, u.Host, u.Path, u.RawQuery, u.Fragment)
   305  }
   306  
   307  func DoTest(t *testing.T, parse func(string) (*URL, error), name string, tests []URLTest) {
   308  	for _, tt := range tests {
   309  		u, err := parse(tt.in)
   310  		if err != nil {
   311  			t.Errorf("%s(%q) returned error %s", name, tt.in, err)
   312  			continue
   313  		}
   314  		if !reflect.DeepEqual(u, tt.out) {
   315  			t.Errorf("%s(%q):\n\thave %v\n\twant %v\n",
   316  				name, tt.in, ufmt(u), ufmt(tt.out))
   317  		}
   318  	}
   319  }
   320  
   321  func BenchmarkString(b *testing.B) {
   322  	b.StopTimer()
   323  	b.ReportAllocs()
   324  	for _, tt := range urltests {
   325  		u, err := Parse(tt.in)
   326  		if err != nil {
   327  			b.Errorf("Parse(%q) returned error %s", tt.in, err)
   328  			continue
   329  		}
   330  		if tt.roundtrip == "" {
   331  			continue
   332  		}
   333  		b.StartTimer()
   334  		var g string
   335  		for i := 0; i < b.N; i++ {
   336  			g = u.String()
   337  		}
   338  		b.StopTimer()
   339  		if w := tt.roundtrip; g != w {
   340  			b.Errorf("Parse(%q).String() == %q, want %q", tt.in, g, w)
   341  		}
   342  	}
   343  }
   344  
   345  func TestParse(t *testing.T) {
   346  	DoTest(t, Parse, "Parse", urltests)
   347  }
   348  
   349  const pathThatLooksSchemeRelative = "//not.a.user@not.a.host/just/a/path"
   350  
   351  var parseRequestURLTests = []struct {
   352  	url           string
   353  	expectedValid bool
   354  }{
   355  	{"http://foo.com", true},
   356  	{"http://foo.com/", true},
   357  	{"http://foo.com/path", true},
   358  	{"/", true},
   359  	{pathThatLooksSchemeRelative, true},
   360  	{"//not.a.user@%66%6f%6f.com/just/a/path/also", true},
   361  	{"foo.html", false},
   362  	{"../dir/", false},
   363  	{"*", true},
   364  }
   365  
   366  func TestParseRequestURI(t *testing.T) {
   367  	for _, test := range parseRequestURLTests {
   368  		_, err := ParseRequestURI(test.url)
   369  		valid := err == nil
   370  		if valid != test.expectedValid {
   371  			t.Errorf("Expected valid=%v for %q; got %v", test.expectedValid, test.url, valid)
   372  		}
   373  	}
   374  
   375  	url, err := ParseRequestURI(pathThatLooksSchemeRelative)
   376  	if err != nil {
   377  		t.Fatalf("Unexpected error %v", err)
   378  	}
   379  	if url.Path != pathThatLooksSchemeRelative {
   380  		t.Errorf("Expected path %q; got %q", pathThatLooksSchemeRelative, url.Path)
   381  	}
   382  }
   383  
   384  func DoTestString(t *testing.T, parse func(string) (*URL, error), name string, tests []URLTest) {
   385  	for _, tt := range tests {
   386  		u, err := parse(tt.in)
   387  		if err != nil {
   388  			t.Errorf("%s(%q) returned error %s", name, tt.in, err)
   389  			continue
   390  		}
   391  		expected := tt.in
   392  		if len(tt.roundtrip) > 0 {
   393  			expected = tt.roundtrip
   394  		}
   395  		s := u.String()
   396  		if s != expected {
   397  			t.Errorf("%s(%q).String() == %q (expected %q)", name, tt.in, s, expected)
   398  		}
   399  	}
   400  }
   401  
   402  func TestURLString(t *testing.T) {
   403  	DoTestString(t, Parse, "Parse", urltests)
   404  
   405  	// no leading slash on path should prepend
   406  	// slash on String() call
   407  	noslash := URLTest{
   408  		"http://www.google.com/search",
   409  		&URL{
   410  			Scheme: "http",
   411  			Host:   "www.google.com",
   412  			Path:   "search",
   413  		},
   414  		"",
   415  	}
   416  	s := noslash.out.String()
   417  	if s != noslash.in {
   418  		t.Errorf("Expected %s; go %s", noslash.in, s)
   419  	}
   420  }
   421  
   422  type EscapeTest struct {
   423  	in  string
   424  	out string
   425  	err error
   426  }
   427  
   428  var unescapeTests = []EscapeTest{
   429  	{
   430  		"",
   431  		"",
   432  		nil,
   433  	},
   434  	{
   435  		"abc",
   436  		"abc",
   437  		nil,
   438  	},
   439  	{
   440  		"1%41",
   441  		"1A",
   442  		nil,
   443  	},
   444  	{
   445  		"1%41%42%43",
   446  		"1ABC",
   447  		nil,
   448  	},
   449  	{
   450  		"%4a",
   451  		"J",
   452  		nil,
   453  	},
   454  	{
   455  		"%6F",
   456  		"o",
   457  		nil,
   458  	},
   459  	{
   460  		"%", // not enough characters after %
   461  		"",
   462  		EscapeError("%"),
   463  	},
   464  	{
   465  		"%a", // not enough characters after %
   466  		"",
   467  		EscapeError("%a"),
   468  	},
   469  	{
   470  		"%1", // not enough characters after %
   471  		"",
   472  		EscapeError("%1"),
   473  	},
   474  	{
   475  		"123%45%6", // not enough characters after %
   476  		"",
   477  		EscapeError("%6"),
   478  	},
   479  	{
   480  		"%zzzzz", // invalid hex digits
   481  		"",
   482  		EscapeError("%zz"),
   483  	},
   484  }
   485  
   486  func TestUnescape(t *testing.T) {
   487  	for _, tt := range unescapeTests {
   488  		actual, err := QueryUnescape(tt.in)
   489  		if actual != tt.out || (err != nil) != (tt.err != nil) {
   490  			t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", tt.in, actual, err, tt.out, tt.err)
   491  		}
   492  	}
   493  }
   494  
   495  var escapeTests = []EscapeTest{
   496  	{
   497  		"",
   498  		"",
   499  		nil,
   500  	},
   501  	{
   502  		"abc",
   503  		"abc",
   504  		nil,
   505  	},
   506  	{
   507  		"one two",
   508  		"one+two",
   509  		nil,
   510  	},
   511  	{
   512  		"10%",
   513  		"10%25",
   514  		nil,
   515  	},
   516  	{
   517  		" ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
   518  		"+%3F%26%3D%23%2B%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09%3A%2F%40%24%27%28%29%2A%2C%3B",
   519  		nil,
   520  	},
   521  }
   522  
   523  func TestEscape(t *testing.T) {
   524  	for _, tt := range escapeTests {
   525  		actual := QueryEscape(tt.in)
   526  		if tt.out != actual {
   527  			t.Errorf("QueryEscape(%q) = %q, want %q", tt.in, actual, tt.out)
   528  		}
   529  
   530  		// for bonus points, verify that escape:unescape is an identity.
   531  		roundtrip, err := QueryUnescape(actual)
   532  		if roundtrip != tt.in || err != nil {
   533  			t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]")
   534  		}
   535  	}
   536  }
   537  
   538  //var userinfoTests = []UserinfoTest{
   539  //	{"user", "password", "user:password"},
   540  //	{"foo:bar", "~!@#$%^&*()_+{}|[]\\-=`:;'\"<>?,./",
   541  //		"foo%3Abar:~!%40%23$%25%5E&*()_+%7B%7D%7C%5B%5D%5C-=%60%3A;'%22%3C%3E?,.%2F"},
   542  //}
   543  
   544  type EncodeQueryTest struct {
   545  	m        Values
   546  	expected string
   547  }
   548  
   549  var encodeQueryTests = []EncodeQueryTest{
   550  	{nil, ""},
   551  	{Values{"q": {"puppies"}, "oe": {"utf8"}}, "oe=utf8&q=puppies"},
   552  	{Values{"q": {"dogs", "&", "7"}}, "q=dogs&q=%26&q=7"},
   553  	{Values{
   554  		"a": {"a1", "a2", "a3"},
   555  		"b": {"b1", "b2", "b3"},
   556  		"c": {"c1", "c2", "c3"},
   557  	}, "a=a1&a=a2&a=a3&b=b1&b=b2&b=b3&c=c1&c=c2&c=c3"},
   558  }
   559  
   560  func TestEncodeQuery(t *testing.T) {
   561  	for _, tt := range encodeQueryTests {
   562  		if q := tt.m.Encode(); q != tt.expected {
   563  			t.Errorf(`EncodeQuery(%+v) = %q, want %q`, tt.m, q, tt.expected)
   564  		}
   565  	}
   566  }
   567  
   568  var resolvePathTests = []struct {
   569  	base, ref, expected string
   570  }{
   571  	{"a/b", ".", "/a/"},
   572  	{"a/b", "c", "/a/c"},
   573  	{"a/b", "..", "/"},
   574  	{"a/", "..", "/"},
   575  	{"a/", "../..", "/"},
   576  	{"a/b/c", "..", "/a/"},
   577  	{"a/b/c", "../d", "/a/d"},
   578  	{"a/b/c", ".././d", "/a/d"},
   579  	{"a/b", "./..", "/"},
   580  	{"a/./b", ".", "/a/"},
   581  	{"a/../", ".", "/"},
   582  	{"a/.././b", "c", "/c"},
   583  }
   584  
   585  func TestResolvePath(t *testing.T) {
   586  	for _, test := range resolvePathTests {
   587  		got := resolvePath(test.base, test.ref)
   588  		if got != test.expected {
   589  			t.Errorf("For %q + %q got %q; expected %q", test.base, test.ref, got, test.expected)
   590  		}
   591  	}
   592  }
   593  
   594  var resolveReferenceTests = []struct {
   595  	base, rel, expected string
   596  }{
   597  	// Absolute URL references
   598  	{"http://foo.com?a=b", "https://bar.com/", "https://bar.com/"},
   599  	{"http://foo.com/", "https://bar.com/?a=b", "https://bar.com/?a=b"},
   600  	{"http://foo.com/bar", "mailto:foo@example.com", "mailto:foo@example.com"},
   601  
   602  	// Path-absolute references
   603  	{"http://foo.com/bar", "/baz", "http://foo.com/baz"},
   604  	{"http://foo.com/bar?a=b#f", "/baz", "http://foo.com/baz"},
   605  	{"http://foo.com/bar?a=b", "/baz?c=d", "http://foo.com/baz?c=d"},
   606  
   607  	// Scheme-relative
   608  	{"https://foo.com/bar?a=b", "//bar.com/quux", "https://bar.com/quux"},
   609  
   610  	// Path-relative references:
   611  
   612  	// ... current directory
   613  	{"http://foo.com", ".", "http://foo.com/"},
   614  	{"http://foo.com/bar", ".", "http://foo.com/"},
   615  	{"http://foo.com/bar/", ".", "http://foo.com/bar/"},
   616  
   617  	// ... going down
   618  	{"http://foo.com", "bar", "http://foo.com/bar"},
   619  	{"http://foo.com/", "bar", "http://foo.com/bar"},
   620  	{"http://foo.com/bar/baz", "quux", "http://foo.com/bar/quux"},
   621  
   622  	// ... going up
   623  	{"http://foo.com/bar/baz", "../quux", "http://foo.com/quux"},
   624  	{"http://foo.com/bar/baz", "../../../../../quux", "http://foo.com/quux"},
   625  	{"http://foo.com/bar", "..", "http://foo.com/"},
   626  	{"http://foo.com/bar/baz", "./..", "http://foo.com/"},
   627  	// ".." in the middle (issue 3560)
   628  	{"http://foo.com/bar/baz", "quux/dotdot/../tail", "http://foo.com/bar/quux/tail"},
   629  	{"http://foo.com/bar/baz", "quux/./dotdot/../tail", "http://foo.com/bar/quux/tail"},
   630  	{"http://foo.com/bar/baz", "quux/./dotdot/.././tail", "http://foo.com/bar/quux/tail"},
   631  	{"http://foo.com/bar/baz", "quux/./dotdot/./../tail", "http://foo.com/bar/quux/tail"},
   632  	{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/././../../tail", "http://foo.com/bar/quux/tail"},
   633  	{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/./.././../tail", "http://foo.com/bar/quux/tail"},
   634  	{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/dotdot/./../../.././././tail", "http://foo.com/bar/quux/tail"},
   635  	{"http://foo.com/bar/baz", "quux/./dotdot/../dotdot/../dot/./tail/..", "http://foo.com/bar/quux/dot/"},
   636  
   637  	// Remove any dot-segments prior to forming the target URI.
   638  	// http://tools.ietf.org/html/rfc3986#section-5.2.4
   639  	{"http://foo.com/dot/./dotdot/../foo/bar", "../baz", "http://foo.com/dot/baz"},
   640  
   641  	// Triple dot isn't special
   642  	{"http://foo.com/bar", "...", "http://foo.com/..."},
   643  
   644  	// Fragment
   645  	{"http://foo.com/bar", ".#frag", "http://foo.com/#frag"},
   646  
   647  	// RFC 3986: Normal Examples
   648  	// http://tools.ietf.org/html/rfc3986#section-5.4.1
   649  	{"http://a/b/c/d;p?q", "g:h", "g:h"},
   650  	{"http://a/b/c/d;p?q", "g", "http://a/b/c/g"},
   651  	{"http://a/b/c/d;p?q", "./g", "http://a/b/c/g"},
   652  	{"http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"},
   653  	{"http://a/b/c/d;p?q", "/g", "http://a/g"},
   654  	{"http://a/b/c/d;p?q", "//g", "http://g"},
   655  	{"http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y"},
   656  	{"http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"},
   657  	{"http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s"},
   658  	{"http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"},
   659  	{"http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"},
   660  	{"http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"},
   661  	{"http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"},
   662  	{"http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"},
   663  	{"http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q"},
   664  	{"http://a/b/c/d;p?q", ".", "http://a/b/c/"},
   665  	{"http://a/b/c/d;p?q", "./", "http://a/b/c/"},
   666  	{"http://a/b/c/d;p?q", "..", "http://a/b/"},
   667  	{"http://a/b/c/d;p?q", "../", "http://a/b/"},
   668  	{"http://a/b/c/d;p?q", "../g", "http://a/b/g"},
   669  	{"http://a/b/c/d;p?q", "../..", "http://a/"},
   670  	{"http://a/b/c/d;p?q", "../../", "http://a/"},
   671  	{"http://a/b/c/d;p?q", "../../g", "http://a/g"},
   672  
   673  	// RFC 3986: Abnormal Examples
   674  	// http://tools.ietf.org/html/rfc3986#section-5.4.2
   675  	{"http://a/b/c/d;p?q", "../../../g", "http://a/g"},
   676  	{"http://a/b/c/d;p?q", "../../../../g", "http://a/g"},
   677  	{"http://a/b/c/d;p?q", "/./g", "http://a/g"},
   678  	{"http://a/b/c/d;p?q", "/../g", "http://a/g"},
   679  	{"http://a/b/c/d;p?q", "g.", "http://a/b/c/g."},
   680  	{"http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"},
   681  	{"http://a/b/c/d;p?q", "g..", "http://a/b/c/g.."},
   682  	{"http://a/b/c/d;p?q", "..g", "http://a/b/c/..g"},
   683  	{"http://a/b/c/d;p?q", "./../g", "http://a/b/g"},
   684  	{"http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"},
   685  	{"http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"},
   686  	{"http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h"},
   687  	{"http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"},
   688  	{"http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y"},
   689  	{"http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x"},
   690  	{"http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x"},
   691  	{"http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x"},
   692  	{"http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x"},
   693  
   694  	// Extras.
   695  	{"https://a/b/c/d;p?q", "//g?q", "https://g?q"},
   696  	{"https://a/b/c/d;p?q", "//g#s", "https://g#s"},
   697  	{"https://a/b/c/d;p?q", "//g/d/e/f?y#s", "https://g/d/e/f?y#s"},
   698  	{"https://a/b/c/d;p#s", "?y", "https://a/b/c/d;p?y"},
   699  	{"https://a/b/c/d;p?q#s", "?y", "https://a/b/c/d;p?y"},
   700  }
   701  
   702  func TestResolveReference(t *testing.T) {
   703  	mustParse := func(url string) *URL {
   704  		u, err := Parse(url)
   705  		if err != nil {
   706  			t.Fatalf("Expected URL to parse: %q, got error: %v", url, err)
   707  		}
   708  		return u
   709  	}
   710  	opaque := &URL{Scheme: "scheme", Opaque: "opaque"}
   711  	for _, test := range resolveReferenceTests {
   712  		base := mustParse(test.base)
   713  		rel := mustParse(test.rel)
   714  		url := base.ResolveReference(rel)
   715  		if url.String() != test.expected {
   716  			t.Errorf("URL(%q).ResolveReference(%q) == %q, got %q", test.base, test.rel, test.expected, url.String())
   717  		}
   718  		// Ensure that new instances are returned.
   719  		if base == url {
   720  			t.Errorf("Expected URL.ResolveReference to return new URL instance.")
   721  		}
   722  		// Test the convenience wrapper too.
   723  		url, err := base.Parse(test.rel)
   724  		if err != nil {
   725  			t.Errorf("URL(%q).Parse(%q) failed: %v", test.base, test.rel, err)
   726  		} else if url.String() != test.expected {
   727  			t.Errorf("URL(%q).Parse(%q) == %q, got %q", test.base, test.rel, test.expected, url.String())
   728  		} else if base == url {
   729  			// Ensure that new instances are returned for the wrapper too.
   730  			t.Errorf("Expected URL.Parse to return new URL instance.")
   731  		}
   732  		// Ensure Opaque resets the URL.
   733  		url = base.ResolveReference(opaque)
   734  		if *url != *opaque {
   735  			t.Errorf("ResolveReference failed to resolve opaque URL: want %#v, got %#v", url, opaque)
   736  		}
   737  		// Test the convenience wrapper with an opaque URL too.
   738  		url, err = base.Parse("scheme:opaque")
   739  		if err != nil {
   740  			t.Errorf(`URL(%q).Parse("scheme:opaque") failed: %v`, test.base, err)
   741  		} else if *url != *opaque {
   742  			t.Errorf("Parse failed to resolve opaque URL: want %#v, got %#v", url, opaque)
   743  		} else if base == url {
   744  			// Ensure that new instances are returned, again.
   745  			t.Errorf("Expected URL.Parse to return new URL instance.")
   746  		}
   747  	}
   748  }
   749  
   750  func TestQueryValues(t *testing.T) {
   751  	u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2")
   752  	v := u.Query()
   753  	if len(v) != 2 {
   754  		t.Errorf("got %d keys in Query values, want 2", len(v))
   755  	}
   756  	if g, e := v.Get("foo"), "bar"; g != e {
   757  		t.Errorf("Get(foo) = %q, want %q", g, e)
   758  	}
   759  	// Case sensitive:
   760  	if g, e := v.Get("Foo"), ""; g != e {
   761  		t.Errorf("Get(Foo) = %q, want %q", g, e)
   762  	}
   763  	if g, e := v.Get("bar"), "1"; g != e {
   764  		t.Errorf("Get(bar) = %q, want %q", g, e)
   765  	}
   766  	if g, e := v.Get("baz"), ""; g != e {
   767  		t.Errorf("Get(baz) = %q, want %q", g, e)
   768  	}
   769  	v.Del("bar")
   770  	if g, e := v.Get("bar"), ""; g != e {
   771  		t.Errorf("second Get(bar) = %q, want %q", g, e)
   772  	}
   773  }
   774  
   775  type parseTest struct {
   776  	query string
   777  	out   Values
   778  }
   779  
   780  var parseTests = []parseTest{
   781  	{
   782  		query: "a=1&b=2",
   783  		out:   Values{"a": []string{"1"}, "b": []string{"2"}},
   784  	},
   785  	{
   786  		query: "a=1&a=2&a=banana",
   787  		out:   Values{"a": []string{"1", "2", "banana"}},
   788  	},
   789  	{
   790  		query: "ascii=%3Ckey%3A+0x90%3E",
   791  		out:   Values{"ascii": []string{"<key: 0x90>"}},
   792  	},
   793  	{
   794  		query: "a=1;b=2",
   795  		out:   Values{"a": []string{"1"}, "b": []string{"2"}},
   796  	},
   797  	{
   798  		query: "a=1&a=2;a=banana",
   799  		out:   Values{"a": []string{"1", "2", "banana"}},
   800  	},
   801  }
   802  
   803  func TestParseQuery(t *testing.T) {
   804  	for i, test := range parseTests {
   805  		form, err := ParseQuery(test.query)
   806  		if err != nil {
   807  			t.Errorf("test %d: Unexpected error: %v", i, err)
   808  			continue
   809  		}
   810  		if len(form) != len(test.out) {
   811  			t.Errorf("test %d: len(form) = %d, want %d", i, len(form), len(test.out))
   812  		}
   813  		for k, evs := range test.out {
   814  			vs, ok := form[k]
   815  			if !ok {
   816  				t.Errorf("test %d: Missing key %q", i, k)
   817  				continue
   818  			}
   819  			if len(vs) != len(evs) {
   820  				t.Errorf("test %d: len(form[%q]) = %d, want %d", i, k, len(vs), len(evs))
   821  				continue
   822  			}
   823  			for j, ev := range evs {
   824  				if v := vs[j]; v != ev {
   825  					t.Errorf("test %d: form[%q][%d] = %q, want %q", i, k, j, v, ev)
   826  				}
   827  			}
   828  		}
   829  	}
   830  }
   831  
   832  type RequestURITest struct {
   833  	url *URL
   834  	out string
   835  }
   836  
   837  var requritests = []RequestURITest{
   838  	{
   839  		&URL{
   840  			Scheme: "http",
   841  			Host:   "example.com",
   842  			Path:   "",
   843  		},
   844  		"/",
   845  	},
   846  	{
   847  		&URL{
   848  			Scheme: "http",
   849  			Host:   "example.com",
   850  			Path:   "/a b",
   851  		},
   852  		"/a%20b",
   853  	},
   854  	// golang.org/issue/4860 variant 1
   855  	{
   856  		&URL{
   857  			Scheme: "http",
   858  			Host:   "example.com",
   859  			Opaque: "/%2F/%2F/",
   860  		},
   861  		"/%2F/%2F/",
   862  	},
   863  	// golang.org/issue/4860 variant 2
   864  	{
   865  		&URL{
   866  			Scheme: "http",
   867  			Host:   "example.com",
   868  			Opaque: "//other.example.com/%2F/%2F/",
   869  		},
   870  		"http://other.example.com/%2F/%2F/",
   871  	},
   872  	{
   873  		&URL{
   874  			Scheme:   "http",
   875  			Host:     "example.com",
   876  			Path:     "/a b",
   877  			RawQuery: "q=go+language",
   878  		},
   879  		"/a%20b?q=go+language",
   880  	},
   881  	{
   882  		&URL{
   883  			Scheme: "myschema",
   884  			Opaque: "opaque",
   885  		},
   886  		"opaque",
   887  	},
   888  	{
   889  		&URL{
   890  			Scheme:   "myschema",
   891  			Opaque:   "opaque",
   892  			RawQuery: "q=go+language",
   893  		},
   894  		"opaque?q=go+language",
   895  	},
   896  }
   897  
   898  func TestRequestURI(t *testing.T) {
   899  	for _, tt := range requritests {
   900  		s := tt.url.RequestURI()
   901  		if s != tt.out {
   902  			t.Errorf("%#v.RequestURI() == %q (expected %q)", tt.url, s, tt.out)
   903  		}
   904  	}
   905  }
   906  
   907  func TestParseFailure(t *testing.T) {
   908  	// Test that the first parse error is returned.
   909  	const url = "%gh&%ij"
   910  	_, err := ParseQuery(url)
   911  	errStr := fmt.Sprint(err)
   912  	if !strings.Contains(errStr, "%gh") {
   913  		t.Errorf(`ParseQuery(%q) returned error %q, want something containing %q"`, url, errStr, "%gh")
   914  	}
   915  }
   916  
   917  type shouldEscapeTest struct {
   918  	in     byte
   919  	mode   encoding
   920  	escape bool
   921  }
   922  
   923  var shouldEscapeTests = []shouldEscapeTest{
   924  	// Unreserved characters (§2.3)
   925  	{'a', encodePath, false},
   926  	{'a', encodeUserPassword, false},
   927  	{'a', encodeQueryComponent, false},
   928  	{'a', encodeFragment, false},
   929  	{'z', encodePath, false},
   930  	{'A', encodePath, false},
   931  	{'Z', encodePath, false},
   932  	{'0', encodePath, false},
   933  	{'9', encodePath, false},
   934  	{'-', encodePath, false},
   935  	{'-', encodeUserPassword, false},
   936  	{'-', encodeQueryComponent, false},
   937  	{'-', encodeFragment, false},
   938  	{'.', encodePath, false},
   939  	{'_', encodePath, false},
   940  	{'~', encodePath, false},
   941  
   942  	// User information (§3.2.1)
   943  	{':', encodeUserPassword, true},
   944  	{'/', encodeUserPassword, true},
   945  	{'?', encodeUserPassword, true},
   946  	{'@', encodeUserPassword, true},
   947  	{'$', encodeUserPassword, false},
   948  	{'&', encodeUserPassword, false},
   949  	{'+', encodeUserPassword, false},
   950  	{',', encodeUserPassword, false},
   951  	{';', encodeUserPassword, false},
   952  	{'=', encodeUserPassword, false},
   953  }
   954  
   955  func TestShouldEscape(t *testing.T) {
   956  	for _, tt := range shouldEscapeTests {
   957  		if shouldEscape(tt.in, tt.mode) != tt.escape {
   958  			t.Errorf("shouldEscape(%q, %v) returned %v; expected %v", tt.in, tt.mode, !tt.escape, tt.escape)
   959  		}
   960  	}
   961  }