github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/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  }
   283  
   284  // more useful string for debugging than fmt's struct printer
   285  func ufmt(u *URL) string {
   286  	var user, pass interface{}
   287  	if u.User != nil {
   288  		user = u.User.Username()
   289  		if p, ok := u.User.Password(); ok {
   290  			pass = p
   291  		}
   292  	}
   293  	return fmt.Sprintf("opaque=%q, scheme=%q, user=%#v, pass=%#v, host=%q, path=%q, rawq=%q, frag=%q",
   294  		u.Opaque, u.Scheme, user, pass, u.Host, u.Path, u.RawQuery, u.Fragment)
   295  }
   296  
   297  func DoTest(t *testing.T, parse func(string) (*URL, error), name string, tests []URLTest) {
   298  	for _, tt := range tests {
   299  		u, err := parse(tt.in)
   300  		if err != nil {
   301  			t.Errorf("%s(%q) returned error %s", name, tt.in, err)
   302  			continue
   303  		}
   304  		if !reflect.DeepEqual(u, tt.out) {
   305  			t.Errorf("%s(%q):\n\thave %v\n\twant %v\n",
   306  				name, tt.in, ufmt(u), ufmt(tt.out))
   307  		}
   308  	}
   309  }
   310  
   311  func BenchmarkString(b *testing.B) {
   312  	b.StopTimer()
   313  	b.ReportAllocs()
   314  	for _, tt := range urltests {
   315  		u, err := Parse(tt.in)
   316  		if err != nil {
   317  			b.Errorf("Parse(%q) returned error %s", tt.in, err)
   318  			continue
   319  		}
   320  		if tt.roundtrip == "" {
   321  			continue
   322  		}
   323  		b.StartTimer()
   324  		var g string
   325  		for i := 0; i < b.N; i++ {
   326  			g = u.String()
   327  		}
   328  		b.StopTimer()
   329  		if w := tt.roundtrip; g != w {
   330  			b.Errorf("Parse(%q).String() == %q, want %q", tt.in, g, w)
   331  		}
   332  	}
   333  }
   334  
   335  func TestParse(t *testing.T) {
   336  	DoTest(t, Parse, "Parse", urltests)
   337  }
   338  
   339  const pathThatLooksSchemeRelative = "//not.a.user@not.a.host/just/a/path"
   340  
   341  var parseRequestURLTests = []struct {
   342  	url           string
   343  	expectedValid bool
   344  }{
   345  	{"http://foo.com", true},
   346  	{"http://foo.com/", true},
   347  	{"http://foo.com/path", true},
   348  	{"/", true},
   349  	{pathThatLooksSchemeRelative, true},
   350  	{"//not.a.user@%66%6f%6f.com/just/a/path/also", true},
   351  	{"foo.html", false},
   352  	{"../dir/", false},
   353  	{"*", true},
   354  }
   355  
   356  func TestParseRequestURI(t *testing.T) {
   357  	for _, test := range parseRequestURLTests {
   358  		_, err := ParseRequestURI(test.url)
   359  		valid := err == nil
   360  		if valid != test.expectedValid {
   361  			t.Errorf("Expected valid=%v for %q; got %v", test.expectedValid, test.url, valid)
   362  		}
   363  	}
   364  
   365  	url, err := ParseRequestURI(pathThatLooksSchemeRelative)
   366  	if err != nil {
   367  		t.Fatalf("Unexpected error %v", err)
   368  	}
   369  	if url.Path != pathThatLooksSchemeRelative {
   370  		t.Errorf("Expected path %q; got %q", pathThatLooksSchemeRelative, url.Path)
   371  	}
   372  }
   373  
   374  func DoTestString(t *testing.T, parse func(string) (*URL, error), name string, tests []URLTest) {
   375  	for _, tt := range tests {
   376  		u, err := parse(tt.in)
   377  		if err != nil {
   378  			t.Errorf("%s(%q) returned error %s", name, tt.in, err)
   379  			continue
   380  		}
   381  		expected := tt.in
   382  		if len(tt.roundtrip) > 0 {
   383  			expected = tt.roundtrip
   384  		}
   385  		s := u.String()
   386  		if s != expected {
   387  			t.Errorf("%s(%q).String() == %q (expected %q)", name, tt.in, s, expected)
   388  		}
   389  	}
   390  }
   391  
   392  func TestURLString(t *testing.T) {
   393  	DoTestString(t, Parse, "Parse", urltests)
   394  
   395  	// no leading slash on path should prepend
   396  	// slash on String() call
   397  	noslash := URLTest{
   398  		"http://www.google.com/search",
   399  		&URL{
   400  			Scheme: "http",
   401  			Host:   "www.google.com",
   402  			Path:   "search",
   403  		},
   404  		"",
   405  	}
   406  	s := noslash.out.String()
   407  	if s != noslash.in {
   408  		t.Errorf("Expected %s; go %s", noslash.in, s)
   409  	}
   410  }
   411  
   412  type EscapeTest struct {
   413  	in  string
   414  	out string
   415  	err error
   416  }
   417  
   418  var unescapeTests = []EscapeTest{
   419  	{
   420  		"",
   421  		"",
   422  		nil,
   423  	},
   424  	{
   425  		"abc",
   426  		"abc",
   427  		nil,
   428  	},
   429  	{
   430  		"1%41",
   431  		"1A",
   432  		nil,
   433  	},
   434  	{
   435  		"1%41%42%43",
   436  		"1ABC",
   437  		nil,
   438  	},
   439  	{
   440  		"%4a",
   441  		"J",
   442  		nil,
   443  	},
   444  	{
   445  		"%6F",
   446  		"o",
   447  		nil,
   448  	},
   449  	{
   450  		"%", // not enough characters after %
   451  		"",
   452  		EscapeError("%"),
   453  	},
   454  	{
   455  		"%a", // not enough characters after %
   456  		"",
   457  		EscapeError("%a"),
   458  	},
   459  	{
   460  		"%1", // not enough characters after %
   461  		"",
   462  		EscapeError("%1"),
   463  	},
   464  	{
   465  		"123%45%6", // not enough characters after %
   466  		"",
   467  		EscapeError("%6"),
   468  	},
   469  	{
   470  		"%zzzzz", // invalid hex digits
   471  		"",
   472  		EscapeError("%zz"),
   473  	},
   474  }
   475  
   476  func TestUnescape(t *testing.T) {
   477  	for _, tt := range unescapeTests {
   478  		actual, err := QueryUnescape(tt.in)
   479  		if actual != tt.out || (err != nil) != (tt.err != nil) {
   480  			t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", tt.in, actual, err, tt.out, tt.err)
   481  		}
   482  	}
   483  }
   484  
   485  var escapeTests = []EscapeTest{
   486  	{
   487  		"",
   488  		"",
   489  		nil,
   490  	},
   491  	{
   492  		"abc",
   493  		"abc",
   494  		nil,
   495  	},
   496  	{
   497  		"one two",
   498  		"one+two",
   499  		nil,
   500  	},
   501  	{
   502  		"10%",
   503  		"10%25",
   504  		nil,
   505  	},
   506  	{
   507  		" ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
   508  		"+%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",
   509  		nil,
   510  	},
   511  }
   512  
   513  func TestEscape(t *testing.T) {
   514  	for _, tt := range escapeTests {
   515  		actual := QueryEscape(tt.in)
   516  		if tt.out != actual {
   517  			t.Errorf("QueryEscape(%q) = %q, want %q", tt.in, actual, tt.out)
   518  		}
   519  
   520  		// for bonus points, verify that escape:unescape is an identity.
   521  		roundtrip, err := QueryUnescape(actual)
   522  		if roundtrip != tt.in || err != nil {
   523  			t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]")
   524  		}
   525  	}
   526  }
   527  
   528  //var userinfoTests = []UserinfoTest{
   529  //	{"user", "password", "user:password"},
   530  //	{"foo:bar", "~!@#$%^&*()_+{}|[]\\-=`:;'\"<>?,./",
   531  //		"foo%3Abar:~!%40%23$%25%5E&*()_+%7B%7D%7C%5B%5D%5C-=%60%3A;'%22%3C%3E?,.%2F"},
   532  //}
   533  
   534  type EncodeQueryTest struct {
   535  	m        Values
   536  	expected string
   537  }
   538  
   539  var encodeQueryTests = []EncodeQueryTest{
   540  	{nil, ""},
   541  	{Values{"q": {"puppies"}, "oe": {"utf8"}}, "oe=utf8&q=puppies"},
   542  	{Values{"q": {"dogs", "&", "7"}}, "q=dogs&q=%26&q=7"},
   543  	{Values{
   544  		"a": {"a1", "a2", "a3"},
   545  		"b": {"b1", "b2", "b3"},
   546  		"c": {"c1", "c2", "c3"},
   547  	}, "a=a1&a=a2&a=a3&b=b1&b=b2&b=b3&c=c1&c=c2&c=c3"},
   548  }
   549  
   550  func TestEncodeQuery(t *testing.T) {
   551  	for _, tt := range encodeQueryTests {
   552  		if q := tt.m.Encode(); q != tt.expected {
   553  			t.Errorf(`EncodeQuery(%+v) = %q, want %q`, tt.m, q, tt.expected)
   554  		}
   555  	}
   556  }
   557  
   558  var resolvePathTests = []struct {
   559  	base, ref, expected string
   560  }{
   561  	{"a/b", ".", "/a/"},
   562  	{"a/b", "c", "/a/c"},
   563  	{"a/b", "..", "/"},
   564  	{"a/", "..", "/"},
   565  	{"a/", "../..", "/"},
   566  	{"a/b/c", "..", "/a/"},
   567  	{"a/b/c", "../d", "/a/d"},
   568  	{"a/b/c", ".././d", "/a/d"},
   569  	{"a/b", "./..", "/"},
   570  	{"a/./b", ".", "/a/"},
   571  	{"a/../", ".", "/"},
   572  	{"a/.././b", "c", "/c"},
   573  }
   574  
   575  func TestResolvePath(t *testing.T) {
   576  	for _, test := range resolvePathTests {
   577  		got := resolvePath(test.base, test.ref)
   578  		if got != test.expected {
   579  			t.Errorf("For %q + %q got %q; expected %q", test.base, test.ref, got, test.expected)
   580  		}
   581  	}
   582  }
   583  
   584  var resolveReferenceTests = []struct {
   585  	base, rel, expected string
   586  }{
   587  	// Absolute URL references
   588  	{"http://foo.com?a=b", "https://bar.com/", "https://bar.com/"},
   589  	{"http://foo.com/", "https://bar.com/?a=b", "https://bar.com/?a=b"},
   590  	{"http://foo.com/bar", "mailto:foo@example.com", "mailto:foo@example.com"},
   591  
   592  	// Path-absolute references
   593  	{"http://foo.com/bar", "/baz", "http://foo.com/baz"},
   594  	{"http://foo.com/bar?a=b#f", "/baz", "http://foo.com/baz"},
   595  	{"http://foo.com/bar?a=b", "/baz?c=d", "http://foo.com/baz?c=d"},
   596  
   597  	// Scheme-relative
   598  	{"https://foo.com/bar?a=b", "//bar.com/quux", "https://bar.com/quux"},
   599  
   600  	// Path-relative references:
   601  
   602  	// ... current directory
   603  	{"http://foo.com", ".", "http://foo.com/"},
   604  	{"http://foo.com/bar", ".", "http://foo.com/"},
   605  	{"http://foo.com/bar/", ".", "http://foo.com/bar/"},
   606  
   607  	// ... going down
   608  	{"http://foo.com", "bar", "http://foo.com/bar"},
   609  	{"http://foo.com/", "bar", "http://foo.com/bar"},
   610  	{"http://foo.com/bar/baz", "quux", "http://foo.com/bar/quux"},
   611  
   612  	// ... going up
   613  	{"http://foo.com/bar/baz", "../quux", "http://foo.com/quux"},
   614  	{"http://foo.com/bar/baz", "../../../../../quux", "http://foo.com/quux"},
   615  	{"http://foo.com/bar", "..", "http://foo.com/"},
   616  	{"http://foo.com/bar/baz", "./..", "http://foo.com/"},
   617  	// ".." in the middle (issue 3560)
   618  	{"http://foo.com/bar/baz", "quux/dotdot/../tail", "http://foo.com/bar/quux/tail"},
   619  	{"http://foo.com/bar/baz", "quux/./dotdot/../tail", "http://foo.com/bar/quux/tail"},
   620  	{"http://foo.com/bar/baz", "quux/./dotdot/.././tail", "http://foo.com/bar/quux/tail"},
   621  	{"http://foo.com/bar/baz", "quux/./dotdot/./../tail", "http://foo.com/bar/quux/tail"},
   622  	{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/././../../tail", "http://foo.com/bar/quux/tail"},
   623  	{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/./.././../tail", "http://foo.com/bar/quux/tail"},
   624  	{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/dotdot/./../../.././././tail", "http://foo.com/bar/quux/tail"},
   625  	{"http://foo.com/bar/baz", "quux/./dotdot/../dotdot/../dot/./tail/..", "http://foo.com/bar/quux/dot/"},
   626  
   627  	// Remove any dot-segments prior to forming the target URI.
   628  	// http://tools.ietf.org/html/rfc3986#section-5.2.4
   629  	{"http://foo.com/dot/./dotdot/../foo/bar", "../baz", "http://foo.com/dot/baz"},
   630  
   631  	// Triple dot isn't special
   632  	{"http://foo.com/bar", "...", "http://foo.com/..."},
   633  
   634  	// Fragment
   635  	{"http://foo.com/bar", ".#frag", "http://foo.com/#frag"},
   636  
   637  	// RFC 3986: Normal Examples
   638  	// http://tools.ietf.org/html/rfc3986#section-5.4.1
   639  	{"http://a/b/c/d;p?q", "g:h", "g:h"},
   640  	{"http://a/b/c/d;p?q", "g", "http://a/b/c/g"},
   641  	{"http://a/b/c/d;p?q", "./g", "http://a/b/c/g"},
   642  	{"http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"},
   643  	{"http://a/b/c/d;p?q", "/g", "http://a/g"},
   644  	{"http://a/b/c/d;p?q", "//g", "http://g"},
   645  	{"http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y"},
   646  	{"http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"},
   647  	{"http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s"},
   648  	{"http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"},
   649  	{"http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"},
   650  	{"http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"},
   651  	{"http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"},
   652  	{"http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"},
   653  	{"http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q"},
   654  	{"http://a/b/c/d;p?q", ".", "http://a/b/c/"},
   655  	{"http://a/b/c/d;p?q", "./", "http://a/b/c/"},
   656  	{"http://a/b/c/d;p?q", "..", "http://a/b/"},
   657  	{"http://a/b/c/d;p?q", "../", "http://a/b/"},
   658  	{"http://a/b/c/d;p?q", "../g", "http://a/b/g"},
   659  	{"http://a/b/c/d;p?q", "../..", "http://a/"},
   660  	{"http://a/b/c/d;p?q", "../../", "http://a/"},
   661  	{"http://a/b/c/d;p?q", "../../g", "http://a/g"},
   662  
   663  	// RFC 3986: Abnormal Examples
   664  	// http://tools.ietf.org/html/rfc3986#section-5.4.2
   665  	{"http://a/b/c/d;p?q", "../../../g", "http://a/g"},
   666  	{"http://a/b/c/d;p?q", "../../../../g", "http://a/g"},
   667  	{"http://a/b/c/d;p?q", "/./g", "http://a/g"},
   668  	{"http://a/b/c/d;p?q", "/../g", "http://a/g"},
   669  	{"http://a/b/c/d;p?q", "g.", "http://a/b/c/g."},
   670  	{"http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"},
   671  	{"http://a/b/c/d;p?q", "g..", "http://a/b/c/g.."},
   672  	{"http://a/b/c/d;p?q", "..g", "http://a/b/c/..g"},
   673  	{"http://a/b/c/d;p?q", "./../g", "http://a/b/g"},
   674  	{"http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"},
   675  	{"http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"},
   676  	{"http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h"},
   677  	{"http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"},
   678  	{"http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y"},
   679  	{"http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x"},
   680  	{"http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x"},
   681  	{"http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x"},
   682  	{"http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x"},
   683  
   684  	// Extras.
   685  	{"https://a/b/c/d;p?q", "//g?q", "https://g?q"},
   686  	{"https://a/b/c/d;p?q", "//g#s", "https://g#s"},
   687  	{"https://a/b/c/d;p?q", "//g/d/e/f?y#s", "https://g/d/e/f?y#s"},
   688  	{"https://a/b/c/d;p#s", "?y", "https://a/b/c/d;p?y"},
   689  	{"https://a/b/c/d;p?q#s", "?y", "https://a/b/c/d;p?y"},
   690  }
   691  
   692  func TestResolveReference(t *testing.T) {
   693  	mustParse := func(url string) *URL {
   694  		u, err := Parse(url)
   695  		if err != nil {
   696  			t.Fatalf("Expected URL to parse: %q, got error: %v", url, err)
   697  		}
   698  		return u
   699  	}
   700  	opaque := &URL{Scheme: "scheme", Opaque: "opaque"}
   701  	for _, test := range resolveReferenceTests {
   702  		base := mustParse(test.base)
   703  		rel := mustParse(test.rel)
   704  		url := base.ResolveReference(rel)
   705  		if url.String() != test.expected {
   706  			t.Errorf("URL(%q).ResolveReference(%q) == %q, got %q", test.base, test.rel, test.expected, url.String())
   707  		}
   708  		// Ensure that new instances are returned.
   709  		if base == url {
   710  			t.Errorf("Expected URL.ResolveReference to return new URL instance.")
   711  		}
   712  		// Test the convenience wrapper too.
   713  		url, err := base.Parse(test.rel)
   714  		if err != nil {
   715  			t.Errorf("URL(%q).Parse(%q) failed: %v", test.base, test.rel, err)
   716  		} else if url.String() != test.expected {
   717  			t.Errorf("URL(%q).Parse(%q) == %q, got %q", test.base, test.rel, test.expected, url.String())
   718  		} else if base == url {
   719  			// Ensure that new instances are returned for the wrapper too.
   720  			t.Errorf("Expected URL.Parse to return new URL instance.")
   721  		}
   722  		// Ensure Opaque resets the URL.
   723  		url = base.ResolveReference(opaque)
   724  		if *url != *opaque {
   725  			t.Errorf("ResolveReference failed to resolve opaque URL: want %#v, got %#v", url, opaque)
   726  		}
   727  		// Test the convenience wrapper with an opaque URL too.
   728  		url, err = base.Parse("scheme:opaque")
   729  		if err != nil {
   730  			t.Errorf(`URL(%q).Parse("scheme:opaque") failed: %v`, test.base, err)
   731  		} else if *url != *opaque {
   732  			t.Errorf("Parse failed to resolve opaque URL: want %#v, got %#v", url, opaque)
   733  		} else if base == url {
   734  			// Ensure that new instances are returned, again.
   735  			t.Errorf("Expected URL.Parse to return new URL instance.")
   736  		}
   737  	}
   738  }
   739  
   740  func TestQueryValues(t *testing.T) {
   741  	u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2")
   742  	v := u.Query()
   743  	if len(v) != 2 {
   744  		t.Errorf("got %d keys in Query values, want 2", len(v))
   745  	}
   746  	if g, e := v.Get("foo"), "bar"; g != e {
   747  		t.Errorf("Get(foo) = %q, want %q", g, e)
   748  	}
   749  	// Case sensitive:
   750  	if g, e := v.Get("Foo"), ""; g != e {
   751  		t.Errorf("Get(Foo) = %q, want %q", g, e)
   752  	}
   753  	if g, e := v.Get("bar"), "1"; g != e {
   754  		t.Errorf("Get(bar) = %q, want %q", g, e)
   755  	}
   756  	if g, e := v.Get("baz"), ""; g != e {
   757  		t.Errorf("Get(baz) = %q, want %q", g, e)
   758  	}
   759  	v.Del("bar")
   760  	if g, e := v.Get("bar"), ""; g != e {
   761  		t.Errorf("second Get(bar) = %q, want %q", g, e)
   762  	}
   763  }
   764  
   765  type parseTest struct {
   766  	query string
   767  	out   Values
   768  }
   769  
   770  var parseTests = []parseTest{
   771  	{
   772  		query: "a=1&b=2",
   773  		out:   Values{"a": []string{"1"}, "b": []string{"2"}},
   774  	},
   775  	{
   776  		query: "a=1&a=2&a=banana",
   777  		out:   Values{"a": []string{"1", "2", "banana"}},
   778  	},
   779  	{
   780  		query: "ascii=%3Ckey%3A+0x90%3E",
   781  		out:   Values{"ascii": []string{"<key: 0x90>"}},
   782  	},
   783  	{
   784  		query: "a=1;b=2",
   785  		out:   Values{"a": []string{"1"}, "b": []string{"2"}},
   786  	},
   787  	{
   788  		query: "a=1&a=2;a=banana",
   789  		out:   Values{"a": []string{"1", "2", "banana"}},
   790  	},
   791  }
   792  
   793  func TestParseQuery(t *testing.T) {
   794  	for i, test := range parseTests {
   795  		form, err := ParseQuery(test.query)
   796  		if err != nil {
   797  			t.Errorf("test %d: Unexpected error: %v", i, err)
   798  			continue
   799  		}
   800  		if len(form) != len(test.out) {
   801  			t.Errorf("test %d: len(form) = %d, want %d", i, len(form), len(test.out))
   802  		}
   803  		for k, evs := range test.out {
   804  			vs, ok := form[k]
   805  			if !ok {
   806  				t.Errorf("test %d: Missing key %q", i, k)
   807  				continue
   808  			}
   809  			if len(vs) != len(evs) {
   810  				t.Errorf("test %d: len(form[%q]) = %d, want %d", i, k, len(vs), len(evs))
   811  				continue
   812  			}
   813  			for j, ev := range evs {
   814  				if v := vs[j]; v != ev {
   815  					t.Errorf("test %d: form[%q][%d] = %q, want %q", i, k, j, v, ev)
   816  				}
   817  			}
   818  		}
   819  	}
   820  }
   821  
   822  type RequestURITest struct {
   823  	url *URL
   824  	out string
   825  }
   826  
   827  var requritests = []RequestURITest{
   828  	{
   829  		&URL{
   830  			Scheme: "http",
   831  			Host:   "example.com",
   832  			Path:   "",
   833  		},
   834  		"/",
   835  	},
   836  	{
   837  		&URL{
   838  			Scheme: "http",
   839  			Host:   "example.com",
   840  			Path:   "/a b",
   841  		},
   842  		"/a%20b",
   843  	},
   844  	// golang.org/issue/4860 variant 1
   845  	{
   846  		&URL{
   847  			Scheme: "http",
   848  			Host:   "example.com",
   849  			Opaque: "/%2F/%2F/",
   850  		},
   851  		"/%2F/%2F/",
   852  	},
   853  	// golang.org/issue/4860 variant 2
   854  	{
   855  		&URL{
   856  			Scheme: "http",
   857  			Host:   "example.com",
   858  			Opaque: "//other.example.com/%2F/%2F/",
   859  		},
   860  		"http://other.example.com/%2F/%2F/",
   861  	},
   862  	{
   863  		&URL{
   864  			Scheme:   "http",
   865  			Host:     "example.com",
   866  			Path:     "/a b",
   867  			RawQuery: "q=go+language",
   868  		},
   869  		"/a%20b?q=go+language",
   870  	},
   871  	{
   872  		&URL{
   873  			Scheme: "myschema",
   874  			Opaque: "opaque",
   875  		},
   876  		"opaque",
   877  	},
   878  	{
   879  		&URL{
   880  			Scheme:   "myschema",
   881  			Opaque:   "opaque",
   882  			RawQuery: "q=go+language",
   883  		},
   884  		"opaque?q=go+language",
   885  	},
   886  }
   887  
   888  func TestRequestURI(t *testing.T) {
   889  	for _, tt := range requritests {
   890  		s := tt.url.RequestURI()
   891  		if s != tt.out {
   892  			t.Errorf("%#v.RequestURI() == %q (expected %q)", tt.url, s, tt.out)
   893  		}
   894  	}
   895  }
   896  
   897  func TestParseFailure(t *testing.T) {
   898  	// Test that the first parse error is returned.
   899  	const url = "%gh&%ij"
   900  	_, err := ParseQuery(url)
   901  	errStr := fmt.Sprint(err)
   902  	if !strings.Contains(errStr, "%gh") {
   903  		t.Errorf(`ParseQuery(%q) returned error %q, want something containing %q"`, url, errStr, "%gh")
   904  	}
   905  }