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