Black Lives Matter. Support the Equal Justice Initiative.

Source file src/net/url/url_test.go

Documentation: net/url

     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  	"bytes"
     9  	encodingPkg "encoding"
    10  	"encoding/gob"
    11  	"encoding/json"
    12  	"fmt"
    13  	"io"
    14  	"net"
    15  	"reflect"
    16  	"strings"
    17  	"testing"
    18  )
    19  
    20  type URLTest struct {
    21  	in        string
    22  	out       *URL   // expected parse
    23  	roundtrip string // expected result of reserializing the URL; empty means same as "in".
    24  }
    25  
    26  var urltests = []URLTest{
    27  	// no path
    28  	{
    29  		"http://www.google.com",
    30  		&URL{
    31  			Scheme: "http",
    32  			Host:   "www.google.com",
    33  		},
    34  		"",
    35  	},
    36  	// path
    37  	{
    38  		"http://www.google.com/",
    39  		&URL{
    40  			Scheme: "http",
    41  			Host:   "www.google.com",
    42  			Path:   "/",
    43  		},
    44  		"",
    45  	},
    46  	// path with hex escaping
    47  	{
    48  		"http://www.google.com/file%20one%26two",
    49  		&URL{
    50  			Scheme:  "http",
    51  			Host:    "www.google.com",
    52  			Path:    "/file one&two",
    53  			RawPath: "/file%20one%26two",
    54  		},
    55  		"",
    56  	},
    57  	// fragment with hex escaping
    58  	{
    59  		"http://www.google.com/#file%20one%26two",
    60  		&URL{
    61  			Scheme:      "http",
    62  			Host:        "www.google.com",
    63  			Path:        "/",
    64  			Fragment:    "file one&two",
    65  			RawFragment: "file%20one%26two",
    66  		},
    67  		"",
    68  	},
    69  	// user
    70  	{
    71  		"ftp://webmaster@www.google.com/",
    72  		&URL{
    73  			Scheme: "ftp",
    74  			User:   User("webmaster"),
    75  			Host:   "www.google.com",
    76  			Path:   "/",
    77  		},
    78  		"",
    79  	},
    80  	// escape sequence in username
    81  	{
    82  		"ftp://john%20doe@www.google.com/",
    83  		&URL{
    84  			Scheme: "ftp",
    85  			User:   User("john doe"),
    86  			Host:   "www.google.com",
    87  			Path:   "/",
    88  		},
    89  		"ftp://john%20doe@www.google.com/",
    90  	},
    91  	// empty query
    92  	{
    93  		"http://www.google.com/?",
    94  		&URL{
    95  			Scheme:     "http",
    96  			Host:       "www.google.com",
    97  			Path:       "/",
    98  			ForceQuery: true,
    99  		},
   100  		"",
   101  	},
   102  	// query ending in question mark (Issue 14573)
   103  	{
   104  		"http://www.google.com/?foo=bar?",
   105  		&URL{
   106  			Scheme:   "http",
   107  			Host:     "www.google.com",
   108  			Path:     "/",
   109  			RawQuery: "foo=bar?",
   110  		},
   111  		"",
   112  	},
   113  	// query
   114  	{
   115  		"http://www.google.com/?q=go+language",
   116  		&URL{
   117  			Scheme:   "http",
   118  			Host:     "www.google.com",
   119  			Path:     "/",
   120  			RawQuery: "q=go+language",
   121  		},
   122  		"",
   123  	},
   124  	// query with hex escaping: NOT parsed
   125  	{
   126  		"http://www.google.com/?q=go%20language",
   127  		&URL{
   128  			Scheme:   "http",
   129  			Host:     "www.google.com",
   130  			Path:     "/",
   131  			RawQuery: "q=go%20language",
   132  		},
   133  		"",
   134  	},
   135  	// %20 outside query
   136  	{
   137  		"http://www.google.com/a%20b?q=c+d",
   138  		&URL{
   139  			Scheme:   "http",
   140  			Host:     "www.google.com",
   141  			Path:     "/a b",
   142  			RawQuery: "q=c+d",
   143  		},
   144  		"",
   145  	},
   146  	// path without leading /, so no parsing
   147  	{
   148  		"http:www.google.com/?q=go+language",
   149  		&URL{
   150  			Scheme:   "http",
   151  			Opaque:   "www.google.com/",
   152  			RawQuery: "q=go+language",
   153  		},
   154  		"http:www.google.com/?q=go+language",
   155  	},
   156  	// path without leading /, so no parsing
   157  	{
   158  		"http:%2f%2fwww.google.com/?q=go+language",
   159  		&URL{
   160  			Scheme:   "http",
   161  			Opaque:   "%2f%2fwww.google.com/",
   162  			RawQuery: "q=go+language",
   163  		},
   164  		"http:%2f%2fwww.google.com/?q=go+language",
   165  	},
   166  	// non-authority with path
   167  	{
   168  		"mailto:/webmaster@golang.org",
   169  		&URL{
   170  			Scheme: "mailto",
   171  			Path:   "/webmaster@golang.org",
   172  		},
   173  		"mailto:///webmaster@golang.org", // unfortunate compromise
   174  	},
   175  	// non-authority
   176  	{
   177  		"mailto:webmaster@golang.org",
   178  		&URL{
   179  			Scheme: "mailto",
   180  			Opaque: "webmaster@golang.org",
   181  		},
   182  		"",
   183  	},
   184  	// unescaped :// in query should not create a scheme
   185  	{
   186  		"/foo?query=http://bad",
   187  		&URL{
   188  			Path:     "/foo",
   189  			RawQuery: "query=http://bad",
   190  		},
   191  		"",
   192  	},
   193  	// leading // without scheme should create an authority
   194  	{
   195  		"//foo",
   196  		&URL{
   197  			Host: "foo",
   198  		},
   199  		"",
   200  	},
   201  	// leading // without scheme, with userinfo, path, and query
   202  	{
   203  		"//user@foo/path?a=b",
   204  		&URL{
   205  			User:     User("user"),
   206  			Host:     "foo",
   207  			Path:     "/path",
   208  			RawQuery: "a=b",
   209  		},
   210  		"",
   211  	},
   212  	// Three leading slashes isn't an authority, but doesn't return an error.
   213  	// (We can't return an error, as this code is also used via
   214  	// ServeHTTP -> ReadRequest -> Parse, which is arguably a
   215  	// different URL parsing context, but currently shares the
   216  	// same codepath)
   217  	{
   218  		"///threeslashes",
   219  		&URL{
   220  			Path: "///threeslashes",
   221  		},
   222  		"",
   223  	},
   224  	{
   225  		"http://user:password@google.com",
   226  		&URL{
   227  			Scheme: "http",
   228  			User:   UserPassword("user", "password"),
   229  			Host:   "google.com",
   230  		},
   231  		"http://user:password@google.com",
   232  	},
   233  	// unescaped @ in username should not confuse host
   234  	{
   235  		"http://j@ne:password@google.com",
   236  		&URL{
   237  			Scheme: "http",
   238  			User:   UserPassword("j@ne", "password"),
   239  			Host:   "google.com",
   240  		},
   241  		"http://j%40ne:password@google.com",
   242  	},
   243  	// unescaped @ in password should not confuse host
   244  	{
   245  		"http://jane:p@ssword@google.com",
   246  		&URL{
   247  			Scheme: "http",
   248  			User:   UserPassword("jane", "p@ssword"),
   249  			Host:   "google.com",
   250  		},
   251  		"http://jane:p%40ssword@google.com",
   252  	},
   253  	{
   254  		"http://j@ne:password@google.com/p@th?q=@go",
   255  		&URL{
   256  			Scheme:   "http",
   257  			User:     UserPassword("j@ne", "password"),
   258  			Host:     "google.com",
   259  			Path:     "/p@th",
   260  			RawQuery: "q=@go",
   261  		},
   262  		"http://j%40ne:password@google.com/p@th?q=@go",
   263  	},
   264  	{
   265  		"http://www.google.com/?q=go+language#foo",
   266  		&URL{
   267  			Scheme:   "http",
   268  			Host:     "www.google.com",
   269  			Path:     "/",
   270  			RawQuery: "q=go+language",
   271  			Fragment: "foo",
   272  		},
   273  		"",
   274  	},
   275  	{
   276  		"http://www.google.com/?q=go+language#foo&bar",
   277  		&URL{
   278  			Scheme:   "http",
   279  			Host:     "www.google.com",
   280  			Path:     "/",
   281  			RawQuery: "q=go+language",
   282  			Fragment: "foo&bar",
   283  		},
   284  		"http://www.google.com/?q=go+language#foo&bar",
   285  	},
   286  	{
   287  		"http://www.google.com/?q=go+language#foo%26bar",
   288  		&URL{
   289  			Scheme:      "http",
   290  			Host:        "www.google.com",
   291  			Path:        "/",
   292  			RawQuery:    "q=go+language",
   293  			Fragment:    "foo&bar",
   294  			RawFragment: "foo%26bar",
   295  		},
   296  		"http://www.google.com/?q=go+language#foo%26bar",
   297  	},
   298  	{
   299  		"file:///home/adg/rabbits",
   300  		&URL{
   301  			Scheme: "file",
   302  			Host:   "",
   303  			Path:   "/home/adg/rabbits",
   304  		},
   305  		"file:///home/adg/rabbits",
   306  	},
   307  	// "Windows" paths are no exception to the rule.
   308  	// See golang.org/issue/6027, especially comment #9.
   309  	{
   310  		"file:///C:/FooBar/Baz.txt",
   311  		&URL{
   312  			Scheme: "file",
   313  			Host:   "",
   314  			Path:   "/C:/FooBar/Baz.txt",
   315  		},
   316  		"file:///C:/FooBar/Baz.txt",
   317  	},
   318  	// case-insensitive scheme
   319  	{
   320  		"MaIlTo:webmaster@golang.org",
   321  		&URL{
   322  			Scheme: "mailto",
   323  			Opaque: "webmaster@golang.org",
   324  		},
   325  		"mailto:webmaster@golang.org",
   326  	},
   327  	// Relative path
   328  	{
   329  		"a/b/c",
   330  		&URL{
   331  			Path: "a/b/c",
   332  		},
   333  		"a/b/c",
   334  	},
   335  	// escaped '?' in username and password
   336  	{
   337  		"http://%3Fam:pa%3Fsword@google.com",
   338  		&URL{
   339  			Scheme: "http",
   340  			User:   UserPassword("?am", "pa?sword"),
   341  			Host:   "google.com",
   342  		},
   343  		"",
   344  	},
   345  	// host subcomponent; IPv4 address in RFC 3986
   346  	{
   347  		"http://192.168.0.1/",
   348  		&URL{
   349  			Scheme: "http",
   350  			Host:   "192.168.0.1",
   351  			Path:   "/",
   352  		},
   353  		"",
   354  	},
   355  	// host and port subcomponents; IPv4 address in RFC 3986
   356  	{
   357  		"http://192.168.0.1:8080/",
   358  		&URL{
   359  			Scheme: "http",
   360  			Host:   "192.168.0.1:8080",
   361  			Path:   "/",
   362  		},
   363  		"",
   364  	},
   365  	// host subcomponent; IPv6 address in RFC 3986
   366  	{
   367  		"http://[fe80::1]/",
   368  		&URL{
   369  			Scheme: "http",
   370  			Host:   "[fe80::1]",
   371  			Path:   "/",
   372  		},
   373  		"",
   374  	},
   375  	// host and port subcomponents; IPv6 address in RFC 3986
   376  	{
   377  		"http://[fe80::1]:8080/",
   378  		&URL{
   379  			Scheme: "http",
   380  			Host:   "[fe80::1]:8080",
   381  			Path:   "/",
   382  		},
   383  		"",
   384  	},
   385  	// host subcomponent; IPv6 address with zone identifier in RFC 6874
   386  	{
   387  		"http://[fe80::1%25en0]/", // alphanum zone identifier
   388  		&URL{
   389  			Scheme: "http",
   390  			Host:   "[fe80::1%en0]",
   391  			Path:   "/",
   392  		},
   393  		"",
   394  	},
   395  	// host and port subcomponents; IPv6 address with zone identifier in RFC 6874
   396  	{
   397  		"http://[fe80::1%25en0]:8080/", // alphanum zone identifier
   398  		&URL{
   399  			Scheme: "http",
   400  			Host:   "[fe80::1%en0]:8080",
   401  			Path:   "/",
   402  		},
   403  		"",
   404  	},
   405  	// host subcomponent; IPv6 address with zone identifier in RFC 6874
   406  	{
   407  		"http://[fe80::1%25%65%6e%301-._~]/", // percent-encoded+unreserved zone identifier
   408  		&URL{
   409  			Scheme: "http",
   410  			Host:   "[fe80::1%en01-._~]",
   411  			Path:   "/",
   412  		},
   413  		"http://[fe80::1%25en01-._~]/",
   414  	},
   415  	// host and port subcomponents; IPv6 address with zone identifier in RFC 6874
   416  	{
   417  		"http://[fe80::1%25%65%6e%301-._~]:8080/", // percent-encoded+unreserved zone identifier
   418  		&URL{
   419  			Scheme: "http",
   420  			Host:   "[fe80::1%en01-._~]:8080",
   421  			Path:   "/",
   422  		},
   423  		"http://[fe80::1%25en01-._~]:8080/",
   424  	},
   425  	// alternate escapings of path survive round trip
   426  	{
   427  		"http://rest.rsc.io/foo%2fbar/baz%2Fquux?alt=media",
   428  		&URL{
   429  			Scheme:   "http",
   430  			Host:     "rest.rsc.io",
   431  			Path:     "/foo/bar/baz/quux",
   432  			RawPath:  "/foo%2fbar/baz%2Fquux",
   433  			RawQuery: "alt=media",
   434  		},
   435  		"",
   436  	},
   437  	// issue 12036
   438  	{
   439  		"mysql://a,b,c/bar",
   440  		&URL{
   441  			Scheme: "mysql",
   442  			Host:   "a,b,c",
   443  			Path:   "/bar",
   444  		},
   445  		"",
   446  	},
   447  	// worst case host, still round trips
   448  	{
   449  		"scheme://!$&'()*+,;=hello!:1/path",
   450  		&URL{
   451  			Scheme: "scheme",
   452  			Host:   "!$&'()*+,;=hello!:1",
   453  			Path:   "/path",
   454  		},
   455  		"",
   456  	},
   457  	// worst case path, still round trips
   458  	{
   459  		"http://host/!$&'()*+,;=:@[hello]",
   460  		&URL{
   461  			Scheme:  "http",
   462  			Host:    "host",
   463  			Path:    "/!$&'()*+,;=:@[hello]",
   464  			RawPath: "/!$&'()*+,;=:@[hello]",
   465  		},
   466  		"",
   467  	},
   468  	// golang.org/issue/5684
   469  	{
   470  		"http://example.com/oid/[order_id]",
   471  		&URL{
   472  			Scheme:  "http",
   473  			Host:    "example.com",
   474  			Path:    "/oid/[order_id]",
   475  			RawPath: "/oid/[order_id]",
   476  		},
   477  		"",
   478  	},
   479  	// golang.org/issue/12200 (colon with empty port)
   480  	{
   481  		"http://192.168.0.2:8080/foo",
   482  		&URL{
   483  			Scheme: "http",
   484  			Host:   "192.168.0.2:8080",
   485  			Path:   "/foo",
   486  		},
   487  		"",
   488  	},
   489  	{
   490  		"http://192.168.0.2:/foo",
   491  		&URL{
   492  			Scheme: "http",
   493  			Host:   "192.168.0.2:",
   494  			Path:   "/foo",
   495  		},
   496  		"",
   497  	},
   498  	{
   499  		// Malformed IPv6 but still accepted.
   500  		"http://2b01:e34:ef40:7730:8e70:5aff:fefe:edac:8080/foo",
   501  		&URL{
   502  			Scheme: "http",
   503  			Host:   "2b01:e34:ef40:7730:8e70:5aff:fefe:edac:8080",
   504  			Path:   "/foo",
   505  		},
   506  		"",
   507  	},
   508  	{
   509  		// Malformed IPv6 but still accepted.
   510  		"http://2b01:e34:ef40:7730:8e70:5aff:fefe:edac:/foo",
   511  		&URL{
   512  			Scheme: "http",
   513  			Host:   "2b01:e34:ef40:7730:8e70:5aff:fefe:edac:",
   514  			Path:   "/foo",
   515  		},
   516  		"",
   517  	},
   518  	{
   519  		"http://[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:8080/foo",
   520  		&URL{
   521  			Scheme: "http",
   522  			Host:   "[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:8080",
   523  			Path:   "/foo",
   524  		},
   525  		"",
   526  	},
   527  	{
   528  		"http://[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:/foo",
   529  		&URL{
   530  			Scheme: "http",
   531  			Host:   "[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:",
   532  			Path:   "/foo",
   533  		},
   534  		"",
   535  	},
   536  	// golang.org/issue/7991 and golang.org/issue/12719 (non-ascii %-encoded in host)
   537  	{
   538  		"http://hello.世界.com/foo",
   539  		&URL{
   540  			Scheme: "http",
   541  			Host:   "hello.世界.com",
   542  			Path:   "/foo",
   543  		},
   544  		"http://hello.%E4%B8%96%E7%95%8C.com/foo",
   545  	},
   546  	{
   547  		"http://hello.%e4%b8%96%e7%95%8c.com/foo",
   548  		&URL{
   549  			Scheme: "http",
   550  			Host:   "hello.世界.com",
   551  			Path:   "/foo",
   552  		},
   553  		"http://hello.%E4%B8%96%E7%95%8C.com/foo",
   554  	},
   555  	{
   556  		"http://hello.%E4%B8%96%E7%95%8C.com/foo",
   557  		&URL{
   558  			Scheme: "http",
   559  			Host:   "hello.世界.com",
   560  			Path:   "/foo",
   561  		},
   562  		"",
   563  	},
   564  	// golang.org/issue/10433 (path beginning with //)
   565  	{
   566  		"http://example.com//foo",
   567  		&URL{
   568  			Scheme: "http",
   569  			Host:   "example.com",
   570  			Path:   "//foo",
   571  		},
   572  		"",
   573  	},
   574  	// test that we can reparse the host names we accept.
   575  	{
   576  		"myscheme://authority<\"hi\">/foo",
   577  		&URL{
   578  			Scheme: "myscheme",
   579  			Host:   "authority<\"hi\">",
   580  			Path:   "/foo",
   581  		},
   582  		"",
   583  	},
   584  	// spaces in hosts are disallowed but escaped spaces in IPv6 scope IDs are grudgingly OK.
   585  	// This happens on Windows.
   586  	// golang.org/issue/14002
   587  	{
   588  		"tcp://[2020::2020:20:2020:2020%25Windows%20Loves%20Spaces]:2020",
   589  		&URL{
   590  			Scheme: "tcp",
   591  			Host:   "[2020::2020:20:2020:2020%Windows Loves Spaces]:2020",
   592  		},
   593  		"",
   594  	},
   595  	// test we can roundtrip magnet url
   596  	// fix issue https://golang.org/issue/20054
   597  	{
   598  		"magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
   599  		&URL{
   600  			Scheme:   "magnet",
   601  			Host:     "",
   602  			Path:     "",
   603  			RawQuery: "xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
   604  		},
   605  		"magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
   606  	},
   607  	{
   608  		"mailto:?subject=hi",
   609  		&URL{
   610  			Scheme:   "mailto",
   611  			Host:     "",
   612  			Path:     "",
   613  			RawQuery: "subject=hi",
   614  		},
   615  		"mailto:?subject=hi",
   616  	},
   617  }
   618  
   619  // more useful string for debugging than fmt's struct printer
   620  func ufmt(u *URL) string {
   621  	var user, pass interface{}
   622  	if u.User != nil {
   623  		user = u.User.Username()
   624  		if p, ok := u.User.Password(); ok {
   625  			pass = p
   626  		}
   627  	}
   628  	return fmt.Sprintf("opaque=%q, scheme=%q, user=%#v, pass=%#v, host=%q, path=%q, rawpath=%q, rawq=%q, frag=%q, rawfrag=%q, forcequery=%v",
   629  		u.Opaque, u.Scheme, user, pass, u.Host, u.Path, u.RawPath, u.RawQuery, u.Fragment, u.RawFragment, u.ForceQuery)
   630  }
   631  
   632  func BenchmarkString(b *testing.B) {
   633  	b.StopTimer()
   634  	b.ReportAllocs()
   635  	for _, tt := range urltests {
   636  		u, err := Parse(tt.in)
   637  		if err != nil {
   638  			b.Errorf("Parse(%q) returned error %s", tt.in, err)
   639  			continue
   640  		}
   641  		if tt.roundtrip == "" {
   642  			continue
   643  		}
   644  		b.StartTimer()
   645  		var g string
   646  		for i := 0; i < b.N; i++ {
   647  			g = u.String()
   648  		}
   649  		b.StopTimer()
   650  		if w := tt.roundtrip; b.N > 0 && g != w {
   651  			b.Errorf("Parse(%q).String() == %q, want %q", tt.in, g, w)
   652  		}
   653  	}
   654  }
   655  
   656  func TestParse(t *testing.T) {
   657  	for _, tt := range urltests {
   658  		u, err := Parse(tt.in)
   659  		if err != nil {
   660  			t.Errorf("Parse(%q) returned error %v", tt.in, err)
   661  			continue
   662  		}
   663  		if !reflect.DeepEqual(u, tt.out) {
   664  			t.Errorf("Parse(%q):\n\tgot  %v\n\twant %v\n", tt.in, ufmt(u), ufmt(tt.out))
   665  		}
   666  	}
   667  }
   668  
   669  const pathThatLooksSchemeRelative = "//not.a.user@not.a.host/just/a/path"
   670  
   671  var parseRequestURLTests = []struct {
   672  	url           string
   673  	expectedValid bool
   674  }{
   675  	{"http://foo.com", true},
   676  	{"http://foo.com/", true},
   677  	{"http://foo.com/path", true},
   678  	{"/", true},
   679  	{pathThatLooksSchemeRelative, true},
   680  	{"//not.a.user@%66%6f%6f.com/just/a/path/also", true},
   681  	{"*", true},
   682  	{"http://192.168.0.1/", true},
   683  	{"http://192.168.0.1:8080/", true},
   684  	{"http://[fe80::1]/", true},
   685  	{"http://[fe80::1]:8080/", true},
   686  
   687  	// Tests exercising RFC 6874 compliance:
   688  	{"http://[fe80::1%25en0]/", true},                 // with alphanum zone identifier
   689  	{"http://[fe80::1%25en0]:8080/", true},            // with alphanum zone identifier
   690  	{"http://[fe80::1%25%65%6e%301-._~]/", true},      // with percent-encoded+unreserved zone identifier
   691  	{"http://[fe80::1%25%65%6e%301-._~]:8080/", true}, // with percent-encoded+unreserved zone identifier
   692  
   693  	{"foo.html", false},
   694  	{"../dir/", false},
   695  	{" http://foo.com", false},
   696  	{"http://192.168.0.%31/", false},
   697  	{"http://192.168.0.%31:8080/", false},
   698  	{"http://[fe80::%31]/", false},
   699  	{"http://[fe80::%31]:8080/", false},
   700  	{"http://[fe80::%31%25en0]/", false},
   701  	{"http://[fe80::%31%25en0]:8080/", false},
   702  
   703  	// These two cases are valid as textual representations as
   704  	// described in RFC 4007, but are not valid as address
   705  	// literals with IPv6 zone identifiers in URIs as described in
   706  	// RFC 6874.
   707  	{"http://[fe80::1%en0]/", false},
   708  	{"http://[fe80::1%en0]:8080/", false},
   709  }
   710  
   711  func TestParseRequestURI(t *testing.T) {
   712  	for _, test := range parseRequestURLTests {
   713  		_, err := ParseRequestURI(test.url)
   714  		if test.expectedValid && err != nil {
   715  			t.Errorf("ParseRequestURI(%q) gave err %v; want no error", test.url, err)
   716  		} else if !test.expectedValid && err == nil {
   717  			t.Errorf("ParseRequestURI(%q) gave nil error; want some error", test.url)
   718  		}
   719  	}
   720  
   721  	url, err := ParseRequestURI(pathThatLooksSchemeRelative)
   722  	if err != nil {
   723  		t.Fatalf("Unexpected error %v", err)
   724  	}
   725  	if url.Path != pathThatLooksSchemeRelative {
   726  		t.Errorf("ParseRequestURI path:\ngot  %q\nwant %q", url.Path, pathThatLooksSchemeRelative)
   727  	}
   728  }
   729  
   730  var stringURLTests = []struct {
   731  	url  URL
   732  	want string
   733  }{
   734  	// No leading slash on path should prepend slash on String() call
   735  	{
   736  		url: URL{
   737  			Scheme: "http",
   738  			Host:   "www.google.com",
   739  			Path:   "search",
   740  		},
   741  		want: "http://www.google.com/search",
   742  	},
   743  	// Relative path with first element containing ":" should be prepended with "./", golang.org/issue/17184
   744  	{
   745  		url: URL{
   746  			Path: "this:that",
   747  		},
   748  		want: "./this:that",
   749  	},
   750  	// Relative path with second element containing ":" should not be prepended with "./"
   751  	{
   752  		url: URL{
   753  			Path: "here/this:that",
   754  		},
   755  		want: "here/this:that",
   756  	},
   757  	// Non-relative path with first element containing ":" should not be prepended with "./"
   758  	{
   759  		url: URL{
   760  			Scheme: "http",
   761  			Host:   "www.google.com",
   762  			Path:   "this:that",
   763  		},
   764  		want: "http://www.google.com/this:that",
   765  	},
   766  }
   767  
   768  func TestURLString(t *testing.T) {
   769  	for _, tt := range urltests {
   770  		u, err := Parse(tt.in)
   771  		if err != nil {
   772  			t.Errorf("Parse(%q) returned error %s", tt.in, err)
   773  			continue
   774  		}
   775  		expected := tt.in
   776  		if tt.roundtrip != "" {
   777  			expected = tt.roundtrip
   778  		}
   779  		s := u.String()
   780  		if s != expected {
   781  			t.Errorf("Parse(%q).String() == %q (expected %q)", tt.in, s, expected)
   782  		}
   783  	}
   784  
   785  	for _, tt := range stringURLTests {
   786  		if got := tt.url.String(); got != tt.want {
   787  			t.Errorf("%+v.String() = %q; want %q", tt.url, got, tt.want)
   788  		}
   789  	}
   790  }
   791  
   792  func TestURLRedacted(t *testing.T) {
   793  	cases := []struct {
   794  		name string
   795  		url  *URL
   796  		want string
   797  	}{
   798  		{
   799  			name: "non-blank Password",
   800  			url: &URL{
   801  				Scheme: "http",
   802  				Host:   "host.tld",
   803  				Path:   "this:that",
   804  				User:   UserPassword("user", "password"),
   805  			},
   806  			want: "http://user:xxxxx@host.tld/this:that",
   807  		},
   808  		{
   809  			name: "blank Password",
   810  			url: &URL{
   811  				Scheme: "http",
   812  				Host:   "host.tld",
   813  				Path:   "this:that",
   814  				User:   User("user"),
   815  			},
   816  			want: "http://user@host.tld/this:that",
   817  		},
   818  		{
   819  			name: "nil User",
   820  			url: &URL{
   821  				Scheme: "http",
   822  				Host:   "host.tld",
   823  				Path:   "this:that",
   824  				User:   UserPassword("", "password"),
   825  			},
   826  			want: "http://:xxxxx@host.tld/this:that",
   827  		},
   828  		{
   829  			name: "blank Username, blank Password",
   830  			url: &URL{
   831  				Scheme: "http",
   832  				Host:   "host.tld",
   833  				Path:   "this:that",
   834  			},
   835  			want: "http://host.tld/this:that",
   836  		},
   837  		{
   838  			name: "empty URL",
   839  			url:  &URL{},
   840  			want: "",
   841  		},
   842  		{
   843  			name: "nil URL",
   844  			url:  nil,
   845  			want: "",
   846  		},
   847  	}
   848  
   849  	for _, tt := range cases {
   850  		t := t
   851  		t.Run(tt.name, func(t *testing.T) {
   852  			if g, w := tt.url.Redacted(), tt.want; g != w {
   853  				t.Fatalf("got: %q\nwant: %q", g, w)
   854  			}
   855  		})
   856  	}
   857  }
   858  
   859  type EscapeTest struct {
   860  	in  string
   861  	out string
   862  	err error
   863  }
   864  
   865  var unescapeTests = []EscapeTest{
   866  	{
   867  		"",
   868  		"",
   869  		nil,
   870  	},
   871  	{
   872  		"abc",
   873  		"abc",
   874  		nil,
   875  	},
   876  	{
   877  		"1%41",
   878  		"1A",
   879  		nil,
   880  	},
   881  	{
   882  		"1%41%42%43",
   883  		"1ABC",
   884  		nil,
   885  	},
   886  	{
   887  		"%4a",
   888  		"J",
   889  		nil,
   890  	},
   891  	{
   892  		"%6F",
   893  		"o",
   894  		nil,
   895  	},
   896  	{
   897  		"%", // not enough characters after %
   898  		"",
   899  		EscapeError("%"),
   900  	},
   901  	{
   902  		"%a", // not enough characters after %
   903  		"",
   904  		EscapeError("%a"),
   905  	},
   906  	{
   907  		"%1", // not enough characters after %
   908  		"",
   909  		EscapeError("%1"),
   910  	},
   911  	{
   912  		"123%45%6", // not enough characters after %
   913  		"",
   914  		EscapeError("%6"),
   915  	},
   916  	{
   917  		"%zzzzz", // invalid hex digits
   918  		"",
   919  		EscapeError("%zz"),
   920  	},
   921  	{
   922  		"a+b",
   923  		"a b",
   924  		nil,
   925  	},
   926  	{
   927  		"a%20b",
   928  		"a b",
   929  		nil,
   930  	},
   931  }
   932  
   933  func TestUnescape(t *testing.T) {
   934  	for _, tt := range unescapeTests {
   935  		actual, err := QueryUnescape(tt.in)
   936  		if actual != tt.out || (err != nil) != (tt.err != nil) {
   937  			t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", tt.in, actual, err, tt.out, tt.err)
   938  		}
   939  
   940  		in := tt.in
   941  		out := tt.out
   942  		if strings.Contains(tt.in, "+") {
   943  			in = strings.ReplaceAll(tt.in, "+", "%20")
   944  			actual, err := PathUnescape(in)
   945  			if actual != tt.out || (err != nil) != (tt.err != nil) {
   946  				t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", in, actual, err, tt.out, tt.err)
   947  			}
   948  			if tt.err == nil {
   949  				s, err := QueryUnescape(strings.ReplaceAll(tt.in, "+", "XXX"))
   950  				if err != nil {
   951  					continue
   952  				}
   953  				in = tt.in
   954  				out = strings.ReplaceAll(s, "XXX", "+")
   955  			}
   956  		}
   957  
   958  		actual, err = PathUnescape(in)
   959  		if actual != out || (err != nil) != (tt.err != nil) {
   960  			t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", in, actual, err, out, tt.err)
   961  		}
   962  	}
   963  }
   964  
   965  var queryEscapeTests = []EscapeTest{
   966  	{
   967  		"",
   968  		"",
   969  		nil,
   970  	},
   971  	{
   972  		"abc",
   973  		"abc",
   974  		nil,
   975  	},
   976  	{
   977  		"one two",
   978  		"one+two",
   979  		nil,
   980  	},
   981  	{
   982  		"10%",
   983  		"10%25",
   984  		nil,
   985  	},
   986  	{
   987  		" ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
   988  		"+%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",
   989  		nil,
   990  	},
   991  }
   992  
   993  func TestQueryEscape(t *testing.T) {
   994  	for _, tt := range queryEscapeTests {
   995  		actual := QueryEscape(tt.in)
   996  		if tt.out != actual {
   997  			t.Errorf("QueryEscape(%q) = %q, want %q", tt.in, actual, tt.out)
   998  		}
   999  
  1000  		// for bonus points, verify that escape:unescape is an identity.
  1001  		roundtrip, err := QueryUnescape(actual)
  1002  		if roundtrip != tt.in || err != nil {
  1003  			t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]")
  1004  		}
  1005  	}
  1006  }
  1007  
  1008  var pathEscapeTests = []EscapeTest{
  1009  	{
  1010  		"",
  1011  		"",
  1012  		nil,
  1013  	},
  1014  	{
  1015  		"abc",
  1016  		"abc",
  1017  		nil,
  1018  	},
  1019  	{
  1020  		"abc+def",
  1021  		"abc+def",
  1022  		nil,
  1023  	},
  1024  	{
  1025  		"a/b",
  1026  		"a%2Fb",
  1027  		nil,
  1028  	},
  1029  	{
  1030  		"one two",
  1031  		"one%20two",
  1032  		nil,
  1033  	},
  1034  	{
  1035  		"10%",
  1036  		"10%25",
  1037  		nil,
  1038  	},
  1039  	{
  1040  		" ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
  1041  		"%20%3F&=%23+%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09:%2F@$%27%28%29%2A%2C%3B",
  1042  		nil,
  1043  	},
  1044  }
  1045  
  1046  func TestPathEscape(t *testing.T) {
  1047  	for _, tt := range pathEscapeTests {
  1048  		actual := PathEscape(tt.in)
  1049  		if tt.out != actual {
  1050  			t.Errorf("PathEscape(%q) = %q, want %q", tt.in, actual, tt.out)
  1051  		}
  1052  
  1053  		// for bonus points, verify that escape:unescape is an identity.
  1054  		roundtrip, err := PathUnescape(actual)
  1055  		if roundtrip != tt.in || err != nil {
  1056  			t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]")
  1057  		}
  1058  	}
  1059  }
  1060  
  1061  //var userinfoTests = []UserinfoTest{
  1062  //	{"user", "password", "user:password"},
  1063  //	{"foo:bar", "~!@#$%^&*()_+{}|[]\\-=`:;'\"<>?,./",
  1064  //		"foo%3Abar:~!%40%23$%25%5E&*()_+%7B%7D%7C%5B%5D%5C-=%60%3A;'%22%3C%3E?,.%2F"},
  1065  //}
  1066  
  1067  type EncodeQueryTest struct {
  1068  	m        Values
  1069  	expected string
  1070  }
  1071  
  1072  var encodeQueryTests = []EncodeQueryTest{
  1073  	{nil, ""},
  1074  	{Values{"q": {"puppies"}, "oe": {"utf8"}}, "oe=utf8&q=puppies"},
  1075  	{Values{"q": {"dogs", "&", "7"}}, "q=dogs&q=%26&q=7"},
  1076  	{Values{
  1077  		"a": {"a1", "a2", "a3"},
  1078  		"b": {"b1", "b2", "b3"},
  1079  		"c": {"c1", "c2", "c3"},
  1080  	}, "a=a1&a=a2&a=a3&b=b1&b=b2&b=b3&c=c1&c=c2&c=c3"},
  1081  }
  1082  
  1083  func TestEncodeQuery(t *testing.T) {
  1084  	for _, tt := range encodeQueryTests {
  1085  		if q := tt.m.Encode(); q != tt.expected {
  1086  			t.Errorf(`EncodeQuery(%+v) = %q, want %q`, tt.m, q, tt.expected)
  1087  		}
  1088  	}
  1089  }
  1090  
  1091  var resolvePathTests = []struct {
  1092  	base, ref, expected string
  1093  }{
  1094  	{"a/b", ".", "/a/"},
  1095  	{"a/b", "c", "/a/c"},
  1096  	{"a/b", "..", "/"},
  1097  	{"a/", "..", "/"},
  1098  	{"a/", "../..", "/"},
  1099  	{"a/b/c", "..", "/a/"},
  1100  	{"a/b/c", "../d", "/a/d"},
  1101  	{"a/b/c", ".././d", "/a/d"},
  1102  	{"a/b", "./..", "/"},
  1103  	{"a/./b", ".", "/a/"},
  1104  	{"a/../", ".", "/"},
  1105  	{"a/.././b", "c", "/c"},
  1106  }
  1107  
  1108  func TestResolvePath(t *testing.T) {
  1109  	for _, test := range resolvePathTests {
  1110  		got := resolvePath(test.base, test.ref)
  1111  		if got != test.expected {
  1112  			t.Errorf("For %q + %q got %q; expected %q", test.base, test.ref, got, test.expected)
  1113  		}
  1114  	}
  1115  }
  1116  
  1117  var resolveReferenceTests = []struct {
  1118  	base, rel, expected string
  1119  }{
  1120  	// Absolute URL references
  1121  	{"http://foo.com?a=b", "https://bar.com/", "https://bar.com/"},
  1122  	{"http://foo.com/", "https://bar.com/?a=b", "https://bar.com/?a=b"},
  1123  	{"http://foo.com/", "https://bar.com/?", "https://bar.com/?"},
  1124  	{"http://foo.com/bar", "mailto:foo@example.com", "mailto:foo@example.com"},
  1125  
  1126  	// Path-absolute references
  1127  	{"http://foo.com/bar", "/baz", "http://foo.com/baz"},
  1128  	{"http://foo.com/bar?a=b#f", "/baz", "http://foo.com/baz"},
  1129  	{"http://foo.com/bar?a=b", "/baz?", "http://foo.com/baz?"},
  1130  	{"http://foo.com/bar?a=b", "/baz?c=d", "http://foo.com/baz?c=d"},
  1131  
  1132  	// Multiple slashes
  1133  	{"http://foo.com/bar", "http://foo.com//baz", "http://foo.com//baz"},
  1134  	{"http://foo.com/bar", "http://foo.com///baz/quux", "http://foo.com///baz/quux"},
  1135  
  1136  	// Scheme-relative
  1137  	{"https://foo.com/bar?a=b", "//bar.com/quux", "https://bar.com/quux"},
  1138  
  1139  	// Path-relative references:
  1140  
  1141  	// ... current directory
  1142  	{"http://foo.com", ".", "http://foo.com/"},
  1143  	{"http://foo.com/bar", ".", "http://foo.com/"},
  1144  	{"http://foo.com/bar/", ".", "http://foo.com/bar/"},
  1145  
  1146  	// ... going down
  1147  	{"http://foo.com", "bar", "http://foo.com/bar"},
  1148  	{"http://foo.com/", "bar", "http://foo.com/bar"},
  1149  	{"http://foo.com/bar/baz", "quux", "http://foo.com/bar/quux"},
  1150  
  1151  	// ... going up
  1152  	{"http://foo.com/bar/baz", "../quux", "http://foo.com/quux"},
  1153  	{"http://foo.com/bar/baz", "../../../../../quux", "http://foo.com/quux"},
  1154  	{"http://foo.com/bar", "..", "http://foo.com/"},
  1155  	{"http://foo.com/bar/baz", "./..", "http://foo.com/"},
  1156  	// ".." in the middle (issue 3560)
  1157  	{"http://foo.com/bar/baz", "quux/dotdot/../tail", "http://foo.com/bar/quux/tail"},
  1158  	{"http://foo.com/bar/baz", "quux/./dotdot/../tail", "http://foo.com/bar/quux/tail"},
  1159  	{"http://foo.com/bar/baz", "quux/./dotdot/.././tail", "http://foo.com/bar/quux/tail"},
  1160  	{"http://foo.com/bar/baz", "quux/./dotdot/./../tail", "http://foo.com/bar/quux/tail"},
  1161  	{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/././../../tail", "http://foo.com/bar/quux/tail"},
  1162  	{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/./.././../tail", "http://foo.com/bar/quux/tail"},
  1163  	{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/dotdot/./../../.././././tail", "http://foo.com/bar/quux/tail"},
  1164  	{"http://foo.com/bar/baz", "quux/./dotdot/../dotdot/../dot/./tail/..", "http://foo.com/bar/quux/dot/"},
  1165  
  1166  	// Remove any dot-segments prior to forming the target URI.
  1167  	// http://tools.ietf.org/html/rfc3986#section-5.2.4
  1168  	{"http://foo.com/dot/./dotdot/../foo/bar", "../baz", "http://foo.com/dot/baz"},
  1169  
  1170  	// Triple dot isn't special
  1171  	{"http://foo.com/bar", "...", "http://foo.com/..."},
  1172  
  1173  	// Fragment
  1174  	{"http://foo.com/bar", ".#frag", "http://foo.com/#frag"},
  1175  	{"http://example.org/", "#!$&%27()*+,;=", "http://example.org/#!$&%27()*+,;="},
  1176  
  1177  	// Paths with escaping (issue 16947).
  1178  	{"http://foo.com/foo%2fbar/", "../baz", "http://foo.com/baz"},
  1179  	{"http://foo.com/1/2%2f/3%2f4/5", "../../a/b/c", "http://foo.com/1/a/b/c"},
  1180  	{"http://foo.com/1/2/3", "./a%2f../../b/..%2fc", "http://foo.com/1/2/b/..%2fc"},
  1181  	{"http://foo.com/1/2%2f/3%2f4/5", "./a%2f../b/../c", "http://foo.com/1/2%2f/3%2f4/a%2f../c"},
  1182  	{"http://foo.com/foo%20bar/", "../baz", "http://foo.com/baz"},
  1183  	{"http://foo.com/foo", "../bar%2fbaz", "http://foo.com/bar%2fbaz"},
  1184  	{"http://foo.com/foo%2dbar/", "./baz-quux", "http://foo.com/foo%2dbar/baz-quux"},
  1185  
  1186  	// RFC 3986: Normal Examples
  1187  	// http://tools.ietf.org/html/rfc3986#section-5.4.1
  1188  	{"http://a/b/c/d;p?q", "g:h", "g:h"},
  1189  	{"http://a/b/c/d;p?q", "g", "http://a/b/c/g"},
  1190  	{"http://a/b/c/d;p?q", "./g", "http://a/b/c/g"},
  1191  	{"http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"},
  1192  	{"http://a/b/c/d;p?q", "/g", "http://a/g"},
  1193  	{"http://a/b/c/d;p?q", "//g", "http://g"},
  1194  	{"http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y"},
  1195  	{"http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"},
  1196  	{"http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s"},
  1197  	{"http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"},
  1198  	{"http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"},
  1199  	{"http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"},
  1200  	{"http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"},
  1201  	{"http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"},
  1202  	{"http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q"},
  1203  	{"http://a/b/c/d;p?q", ".", "http://a/b/c/"},
  1204  	{"http://a/b/c/d;p?q", "./", "http://a/b/c/"},
  1205  	{"http://a/b/c/d;p?q", "..", "http://a/b/"},
  1206  	{"http://a/b/c/d;p?q", "../", "http://a/b/"},
  1207  	{"http://a/b/c/d;p?q", "../g", "http://a/b/g"},
  1208  	{"http://a/b/c/d;p?q", "../..", "http://a/"},
  1209  	{"http://a/b/c/d;p?q", "../../", "http://a/"},
  1210  	{"http://a/b/c/d;p?q", "../../g", "http://a/g"},
  1211  
  1212  	// RFC 3986: Abnormal Examples
  1213  	// http://tools.ietf.org/html/rfc3986#section-5.4.2
  1214  	{"http://a/b/c/d;p?q", "../../../g", "http://a/g"},
  1215  	{"http://a/b/c/d;p?q", "../../../../g", "http://a/g"},
  1216  	{"http://a/b/c/d;p?q", "/./g", "http://a/g"},
  1217  	{"http://a/b/c/d;p?q", "/../g", "http://a/g"},
  1218  	{"http://a/b/c/d;p?q", "g.", "http://a/b/c/g."},
  1219  	{"http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"},
  1220  	{"http://a/b/c/d;p?q", "g..", "http://a/b/c/g.."},
  1221  	{"http://a/b/c/d;p?q", "..g", "http://a/b/c/..g"},
  1222  	{"http://a/b/c/d;p?q", "./../g", "http://a/b/g"},
  1223  	{"http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"},
  1224  	{"http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"},
  1225  	{"http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h"},
  1226  	{"http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"},
  1227  	{"http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y"},
  1228  	{"http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x"},
  1229  	{"http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x"},
  1230  	{"http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x"},
  1231  	{"http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x"},
  1232  
  1233  	// Extras.
  1234  	{"https://a/b/c/d;p?q", "//g?q", "https://g?q"},
  1235  	{"https://a/b/c/d;p?q", "//g#s", "https://g#s"},
  1236  	{"https://a/b/c/d;p?q", "//g/d/e/f?y#s", "https://g/d/e/f?y#s"},
  1237  	{"https://a/b/c/d;p#s", "?y", "https://a/b/c/d;p?y"},
  1238  	{"https://a/b/c/d;p?q#s", "?y", "https://a/b/c/d;p?y"},
  1239  }
  1240  
  1241  func TestResolveReference(t *testing.T) {
  1242  	mustParse := func(url string) *URL {
  1243  		u, err := Parse(url)
  1244  		if err != nil {
  1245  			t.Fatalf("Parse(%q) got err %v", url, err)
  1246  		}
  1247  		return u
  1248  	}
  1249  	opaque := &URL{Scheme: "scheme", Opaque: "opaque"}
  1250  	for _, test := range resolveReferenceTests {
  1251  		base := mustParse(test.base)
  1252  		rel := mustParse(test.rel)
  1253  		url := base.ResolveReference(rel)
  1254  		if got := url.String(); got != test.expected {
  1255  			t.Errorf("URL(%q).ResolveReference(%q)\ngot  %q\nwant %q", test.base, test.rel, got, test.expected)
  1256  		}
  1257  		// Ensure that new instances are returned.
  1258  		if base == url {
  1259  			t.Errorf("Expected URL.ResolveReference to return new URL instance.")
  1260  		}
  1261  		// Test the convenience wrapper too.
  1262  		url, err := base.Parse(test.rel)
  1263  		if err != nil {
  1264  			t.Errorf("URL(%q).Parse(%q) failed: %v", test.base, test.rel, err)
  1265  		} else if got := url.String(); got != test.expected {
  1266  			t.Errorf("URL(%q).Parse(%q)\ngot  %q\nwant %q", test.base, test.rel, got, test.expected)
  1267  		} else if base == url {
  1268  			// Ensure that new instances are returned for the wrapper too.
  1269  			t.Errorf("Expected URL.Parse to return new URL instance.")
  1270  		}
  1271  		// Ensure Opaque resets the URL.
  1272  		url = base.ResolveReference(opaque)
  1273  		if *url != *opaque {
  1274  			t.Errorf("ResolveReference failed to resolve opaque URL:\ngot  %#v\nwant %#v", url, opaque)
  1275  		}
  1276  		// Test the convenience wrapper with an opaque URL too.
  1277  		url, err = base.Parse("scheme:opaque")
  1278  		if err != nil {
  1279  			t.Errorf(`URL(%q).Parse("scheme:opaque") failed: %v`, test.base, err)
  1280  		} else if *url != *opaque {
  1281  			t.Errorf("Parse failed to resolve opaque URL:\ngot  %#v\nwant %#v", opaque, url)
  1282  		} else if base == url {
  1283  			// Ensure that new instances are returned, again.
  1284  			t.Errorf("Expected URL.Parse to return new URL instance.")
  1285  		}
  1286  	}
  1287  }
  1288  
  1289  func TestQueryValues(t *testing.T) {
  1290  	u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2")
  1291  	v := u.Query()
  1292  	if len(v) != 2 {
  1293  		t.Errorf("got %d keys in Query values, want 2", len(v))
  1294  	}
  1295  	if g, e := v.Get("foo"), "bar"; g != e {
  1296  		t.Errorf("Get(foo) = %q, want %q", g, e)
  1297  	}
  1298  	// Case sensitive:
  1299  	if g, e := v.Get("Foo"), ""; g != e {
  1300  		t.Errorf("Get(Foo) = %q, want %q", g, e)
  1301  	}
  1302  	if g, e := v.Get("bar"), "1"; g != e {
  1303  		t.Errorf("Get(bar) = %q, want %q", g, e)
  1304  	}
  1305  	if g, e := v.Get("baz"), ""; g != e {
  1306  		t.Errorf("Get(baz) = %q, want %q", g, e)
  1307  	}
  1308  	v.Del("bar")
  1309  	if g, e := v.Get("bar"), ""; g != e {
  1310  		t.Errorf("second Get(bar) = %q, want %q", g, e)
  1311  	}
  1312  }
  1313  
  1314  type parseTest struct {
  1315  	query string
  1316  	out   Values
  1317  }
  1318  
  1319  var parseTests = []parseTest{
  1320  	{
  1321  		query: "a=1&b=2",
  1322  		out:   Values{"a": []string{"1"}, "b": []string{"2"}},
  1323  	},
  1324  	{
  1325  		query: "a=1&a=2&a=banana",
  1326  		out:   Values{"a": []string{"1", "2", "banana"}},
  1327  	},
  1328  	{
  1329  		query: "ascii=%3Ckey%3A+0x90%3E",
  1330  		out:   Values{"ascii": []string{"<key: 0x90>"}},
  1331  	},
  1332  	{
  1333  		query: "a=1;b=2",
  1334  		out:   Values{"a": []string{"1"}, "b": []string{"2"}},
  1335  	},
  1336  	{
  1337  		query: "a=1&a=2;a=banana",
  1338  		out:   Values{"a": []string{"1", "2", "banana"}},
  1339  	},
  1340  }
  1341  
  1342  func TestParseQuery(t *testing.T) {
  1343  	for i, test := range parseTests {
  1344  		form, err := ParseQuery(test.query)
  1345  		if err != nil {
  1346  			t.Errorf("test %d: Unexpected error: %v", i, err)
  1347  			continue
  1348  		}
  1349  		if len(form) != len(test.out) {
  1350  			t.Errorf("test %d: len(form) = %d, want %d", i, len(form), len(test.out))
  1351  		}
  1352  		for k, evs := range test.out {
  1353  			vs, ok := form[k]
  1354  			if !ok {
  1355  				t.Errorf("test %d: Missing key %q", i, k)
  1356  				continue
  1357  			}
  1358  			if len(vs) != len(evs) {
  1359  				t.Errorf("test %d: len(form[%q]) = %d, want %d", i, k, len(vs), len(evs))
  1360  				continue
  1361  			}
  1362  			for j, ev := range evs {
  1363  				if v := vs[j]; v != ev {
  1364  					t.Errorf("test %d: form[%q][%d] = %q, want %q", i, k, j, v, ev)
  1365  				}
  1366  			}
  1367  		}
  1368  	}
  1369  }
  1370  
  1371  type RequestURITest struct {
  1372  	url *URL
  1373  	out string
  1374  }
  1375  
  1376  var requritests = []RequestURITest{
  1377  	{
  1378  		&URL{
  1379  			Scheme: "http",
  1380  			Host:   "example.com",
  1381  			Path:   "",
  1382  		},
  1383  		"/",
  1384  	},
  1385  	{
  1386  		&URL{
  1387  			Scheme: "http",
  1388  			Host:   "example.com",
  1389  			Path:   "/a b",
  1390  		},
  1391  		"/a%20b",
  1392  	},
  1393  	// golang.org/issue/4860 variant 1
  1394  	{
  1395  		&URL{
  1396  			Scheme: "http",
  1397  			Host:   "example.com",
  1398  			Opaque: "/%2F/%2F/",
  1399  		},
  1400  		"/%2F/%2F/",
  1401  	},
  1402  	// golang.org/issue/4860 variant 2
  1403  	{
  1404  		&URL{
  1405  			Scheme: "http",
  1406  			Host:   "example.com",
  1407  			Opaque: "//other.example.com/%2F/%2F/",
  1408  		},
  1409  		"http://other.example.com/%2F/%2F/",
  1410  	},
  1411  	// better fix for issue 4860
  1412  	{
  1413  		&URL{
  1414  			Scheme:  "http",
  1415  			Host:    "example.com",
  1416  			Path:    "/////",
  1417  			RawPath: "/%2F/%2F/",
  1418  		},
  1419  		"/%2F/%2F/",
  1420  	},
  1421  	{
  1422  		&URL{
  1423  			Scheme:  "http",
  1424  			Host:    "example.com",
  1425  			Path:    "/////",
  1426  			RawPath: "/WRONG/", // ignored because doesn't match Path
  1427  		},
  1428  		"/////",
  1429  	},
  1430  	{
  1431  		&URL{
  1432  			Scheme:   "http",
  1433  			Host:     "example.com",
  1434  			Path:     "/a b",
  1435  			RawQuery: "q=go+language",
  1436  		},
  1437  		"/a%20b?q=go+language",
  1438  	},
  1439  	{
  1440  		&URL{
  1441  			Scheme:   "http",
  1442  			Host:     "example.com",
  1443  			Path:     "/a b",
  1444  			RawPath:  "/a b", // ignored because invalid
  1445  			RawQuery: "q=go+language",
  1446  		},
  1447  		"/a%20b?q=go+language",
  1448  	},
  1449  	{
  1450  		&URL{
  1451  			Scheme:   "http",
  1452  			Host:     "example.com",
  1453  			Path:     "/a?b",
  1454  			RawPath:  "/a?b", // ignored because invalid
  1455  			RawQuery: "q=go+language",
  1456  		},
  1457  		"/a%3Fb?q=go+language",
  1458  	},
  1459  	{
  1460  		&URL{
  1461  			Scheme: "myschema",
  1462  			Opaque: "opaque",
  1463  		},
  1464  		"opaque",
  1465  	},
  1466  	{
  1467  		&URL{
  1468  			Scheme:   "myschema",
  1469  			Opaque:   "opaque",
  1470  			RawQuery: "q=go+language",
  1471  		},
  1472  		"opaque?q=go+language",
  1473  	},
  1474  	{
  1475  		&URL{
  1476  			Scheme: "http",
  1477  			Host:   "example.com",
  1478  			Path:   "//foo",
  1479  		},
  1480  		"//foo",
  1481  	},
  1482  	{
  1483  		&URL{
  1484  			Scheme:     "http",
  1485  			Host:       "example.com",
  1486  			Path:       "/foo",
  1487  			ForceQuery: true,
  1488  		},
  1489  		"/foo?",
  1490  	},
  1491  }
  1492  
  1493  func TestRequestURI(t *testing.T) {
  1494  	for _, tt := range requritests {
  1495  		s := tt.url.RequestURI()
  1496  		if s != tt.out {
  1497  			t.Errorf("%#v.RequestURI() == %q (expected %q)", tt.url, s, tt.out)
  1498  		}
  1499  	}
  1500  }
  1501  
  1502  func TestParseFailure(t *testing.T) {
  1503  	// Test that the first parse error is returned.
  1504  	const url = "%gh&%ij"
  1505  	_, err := ParseQuery(url)
  1506  	errStr := fmt.Sprint(err)
  1507  	if !strings.Contains(errStr, "%gh") {
  1508  		t.Errorf(`ParseQuery(%q) returned error %q, want something containing %q"`, url, errStr, "%gh")
  1509  	}
  1510  }
  1511  
  1512  func TestParseErrors(t *testing.T) {
  1513  	tests := []struct {
  1514  		in      string
  1515  		wantErr bool
  1516  	}{
  1517  		{"http://[::1]", false},
  1518  		{"http://[::1]:80", false},
  1519  		{"http://[::1]:namedport", true}, // rfc3986 3.2.3
  1520  		{"http://x:namedport", true},     // rfc3986 3.2.3
  1521  		{"http://[::1]/", false},
  1522  		{"http://[::1]a", true},
  1523  		{"http://[::1]%23", true},
  1524  		{"http://[::1%25en0]", false},    // valid zone id
  1525  		{"http://[::1]:", false},         // colon, but no port OK
  1526  		{"http://x:", false},             // colon, but no port OK
  1527  		{"http://[::1]:%38%30", true},    // not allowed: % encoding only for non-ASCII
  1528  		{"http://[::1%25%41]", false},    // RFC 6874 allows over-escaping in zone
  1529  		{"http://[%10::1]", true},        // no %xx escapes in IP address
  1530  		{"http://[::1]/%48", false},      // %xx in path is fine
  1531  		{"http://%41:8080/", true},       // not allowed: % encoding only for non-ASCII
  1532  		{"mysql://x@y(z:123)/foo", true}, // not well-formed per RFC 3986, golang.org/issue/33646
  1533  		{"mysql://x@y(1.2.3.4:123)/foo", true},
  1534  
  1535  		{" http://foo.com", true},  // invalid character in schema
  1536  		{"ht tp://foo.com", true},  // invalid character in schema
  1537  		{"ahttp://foo.com", false}, // valid schema characters
  1538  		{"1http://foo.com", true},  // invalid character in schema
  1539  
  1540  		{"http://[]%20%48%54%54%50%2f%31%2e%31%0a%4d%79%48%65%61%64%65%72%3a%20%31%32%33%0a%0a/", true}, // golang.org/issue/11208
  1541  		{"http://a b.com/", true},    // no space in host name please
  1542  		{"cache_object://foo", true}, // scheme cannot have _, relative path cannot have : in first segment
  1543  		{"cache_object:foo", true},
  1544  		{"cache_object:foo/bar", true},
  1545  		{"cache_object/:foo/bar", false},
  1546  	}
  1547  	for _, tt := range tests {
  1548  		u, err := Parse(tt.in)
  1549  		if tt.wantErr {
  1550  			if err == nil {
  1551  				t.Errorf("Parse(%q) = %#v; want an error", tt.in, u)
  1552  			}
  1553  			continue
  1554  		}
  1555  		if err != nil {
  1556  			t.Errorf("Parse(%q) = %v; want no error", tt.in, err)
  1557  		}
  1558  	}
  1559  }
  1560  
  1561  // Issue 11202
  1562  func TestStarRequest(t *testing.T) {
  1563  	u, err := Parse("*")
  1564  	if err != nil {
  1565  		t.Fatal(err)
  1566  	}
  1567  	if got, want := u.RequestURI(), "*"; got != want {
  1568  		t.Errorf("RequestURI = %q; want %q", got, want)
  1569  	}
  1570  }
  1571  
  1572  type shouldEscapeTest struct {
  1573  	in     byte
  1574  	mode   encoding
  1575  	escape bool
  1576  }
  1577  
  1578  var shouldEscapeTests = []shouldEscapeTest{
  1579  	// Unreserved characters (§2.3)
  1580  	{'a', encodePath, false},
  1581  	{'a', encodeUserPassword, false},
  1582  	{'a', encodeQueryComponent, false},
  1583  	{'a', encodeFragment, false},
  1584  	{'a', encodeHost, false},
  1585  	{'z', encodePath, false},
  1586  	{'A', encodePath, false},
  1587  	{'Z', encodePath, false},
  1588  	{'0', encodePath, false},
  1589  	{'9', encodePath, false},
  1590  	{'-', encodePath, false},
  1591  	{'-', encodeUserPassword, false},
  1592  	{'-', encodeQueryComponent, false},
  1593  	{'-', encodeFragment, false},
  1594  	{'.', encodePath, false},
  1595  	{'_', encodePath, false},
  1596  	{'~', encodePath, false},
  1597  
  1598  	// User information (§3.2.1)
  1599  	{':', encodeUserPassword, true},
  1600  	{'/', encodeUserPassword, true},
  1601  	{'?', encodeUserPassword, true},
  1602  	{'@', encodeUserPassword, true},
  1603  	{'$', encodeUserPassword, false},
  1604  	{'&', encodeUserPassword, false},
  1605  	{'+', encodeUserPassword, false},
  1606  	{',', encodeUserPassword, false},
  1607  	{';', encodeUserPassword, false},
  1608  	{'=', encodeUserPassword, false},
  1609  
  1610  	// Host (IP address, IPv6 address, registered name, port suffix; §3.2.2)
  1611  	{'!', encodeHost, false},
  1612  	{'$', encodeHost, false},
  1613  	{'&', encodeHost, false},
  1614  	{'\'', encodeHost, false},
  1615  	{'(', encodeHost, false},
  1616  	{')', encodeHost, false},
  1617  	{'*', encodeHost, false},
  1618  	{'+', encodeHost, false},
  1619  	{',', encodeHost, false},
  1620  	{';', encodeHost, false},
  1621  	{'=', encodeHost, false},
  1622  	{':', encodeHost, false},
  1623  	{'[', encodeHost, false},
  1624  	{']', encodeHost, false},
  1625  	{'0', encodeHost, false},
  1626  	{'9', encodeHost, false},
  1627  	{'A', encodeHost, false},
  1628  	{'z', encodeHost, false},
  1629  	{'_', encodeHost, false},
  1630  	{'-', encodeHost, false},
  1631  	{'.', encodeHost, false},
  1632  }
  1633  
  1634  func TestShouldEscape(t *testing.T) {
  1635  	for _, tt := range shouldEscapeTests {
  1636  		if shouldEscape(tt.in, tt.mode) != tt.escape {
  1637  			t.Errorf("shouldEscape(%q, %v) returned %v; expected %v", tt.in, tt.mode, !tt.escape, tt.escape)
  1638  		}
  1639  	}
  1640  }
  1641  
  1642  type timeoutError struct {
  1643  	timeout bool
  1644  }
  1645  
  1646  func (e *timeoutError) Error() string { return "timeout error" }
  1647  func (e *timeoutError) Timeout() bool { return e.timeout }
  1648  
  1649  type temporaryError struct {
  1650  	temporary bool
  1651  }
  1652  
  1653  func (e *temporaryError) Error() string   { return "temporary error" }
  1654  func (e *temporaryError) Temporary() bool { return e.temporary }
  1655  
  1656  type timeoutTemporaryError struct {
  1657  	timeoutError
  1658  	temporaryError
  1659  }
  1660  
  1661  func (e *timeoutTemporaryError) Error() string { return "timeout/temporary error" }
  1662  
  1663  var netErrorTests = []struct {
  1664  	err       error
  1665  	timeout   bool
  1666  	temporary bool
  1667  }{{
  1668  	err:       &Error{"Get", "http://google.com/", &timeoutError{timeout: true}},
  1669  	timeout:   true,
  1670  	temporary: false,
  1671  }, {
  1672  	err:       &Error{"Get", "http://google.com/", &timeoutError{timeout: false}},
  1673  	timeout:   false,
  1674  	temporary: false,
  1675  }, {
  1676  	err:       &Error{"Get", "http://google.com/", &temporaryError{temporary: true}},
  1677  	timeout:   false,
  1678  	temporary: true,
  1679  }, {
  1680  	err:       &Error{"Get", "http://google.com/", &temporaryError{temporary: false}},
  1681  	timeout:   false,
  1682  	temporary: false,
  1683  }, {
  1684  	err:       &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: true}, temporaryError{temporary: true}}},
  1685  	timeout:   true,
  1686  	temporary: true,
  1687  }, {
  1688  	err:       &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: false}, temporaryError{temporary: true}}},
  1689  	timeout:   false,
  1690  	temporary: true,
  1691  }, {
  1692  	err:       &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: true}, temporaryError{temporary: false}}},
  1693  	timeout:   true,
  1694  	temporary: false,
  1695  }, {
  1696  	err:       &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: false}, temporaryError{temporary: false}}},
  1697  	timeout:   false,
  1698  	temporary: false,
  1699  }, {
  1700  	err:       &Error{"Get", "http://google.com/", io.EOF},
  1701  	timeout:   false,
  1702  	temporary: false,
  1703  }}
  1704  
  1705  // Test that url.Error implements net.Error and that it forwards
  1706  func TestURLErrorImplementsNetError(t *testing.T) {
  1707  	for i, tt := range netErrorTests {
  1708  		err, ok := tt.err.(net.Error)
  1709  		if !ok {
  1710  			t.Errorf("%d: %T does not implement net.Error", i+1, tt.err)
  1711  			continue
  1712  		}
  1713  		if err.Timeout() != tt.timeout {
  1714  			t.Errorf("%d: err.Timeout(): got %v, want %v", i+1, err.Timeout(), tt.timeout)
  1715  			continue
  1716  		}
  1717  		if err.Temporary() != tt.temporary {
  1718  			t.Errorf("%d: err.Temporary(): got %v, want %v", i+1, err.Temporary(), tt.temporary)
  1719  		}
  1720  	}
  1721  }
  1722  
  1723  func TestURLHostnameAndPort(t *testing.T) {
  1724  	tests := []struct {
  1725  		in   string // URL.Host field
  1726  		host string
  1727  		port string
  1728  	}{
  1729  		{"foo.com:80", "foo.com", "80"},
  1730  		{"foo.com", "foo.com", ""},
  1731  		{"foo.com:", "foo.com", ""},
  1732  		{"FOO.COM", "FOO.COM", ""}, // no canonicalization
  1733  		{"1.2.3.4", "1.2.3.4", ""},
  1734  		{"1.2.3.4:80", "1.2.3.4", "80"},
  1735  		{"[1:2:3:4]", "1:2:3:4", ""},
  1736  		{"[1:2:3:4]:80", "1:2:3:4", "80"},
  1737  		{"[::1]:80", "::1", "80"},
  1738  		{"[::1]", "::1", ""},
  1739  		{"[::1]:", "::1", ""},
  1740  		{"localhost", "localhost", ""},
  1741  		{"localhost:443", "localhost", "443"},
  1742  		{"some.super.long.domain.example.org:8080", "some.super.long.domain.example.org", "8080"},
  1743  		{"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "17000"},
  1744  		{"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", ""},
  1745  
  1746  		// Ensure that even when not valid, Host is one of "Hostname",
  1747  		// "Hostname:Port", "[Hostname]" or "[Hostname]:Port".
  1748  		// See https://golang.org/issue/29098.
  1749  		{"[google.com]:80", "google.com", "80"},
  1750  		{"google.com]:80", "google.com]", "80"},
  1751  		{"google.com:80_invalid_port", "google.com:80_invalid_port", ""},
  1752  		{"[::1]extra]:80", "::1]extra", "80"},
  1753  		{"google.com]extra:extra", "google.com]extra:extra", ""},
  1754  	}
  1755  	for _, tt := range tests {
  1756  		u := &URL{Host: tt.in}
  1757  		host, port := u.Hostname(), u.Port()
  1758  		if host != tt.host {
  1759  			t.Errorf("Hostname for Host %q = %q; want %q", tt.in, host, tt.host)
  1760  		}
  1761  		if port != tt.port {
  1762  			t.Errorf("Port for Host %q = %q; want %q", tt.in, port, tt.port)
  1763  		}
  1764  	}
  1765  }
  1766  
  1767  var _ encodingPkg.BinaryMarshaler = (*URL)(nil)
  1768  var _ encodingPkg.BinaryUnmarshaler = (*URL)(nil)
  1769  
  1770  func TestJSON(t *testing.T) {
  1771  	u, err := Parse("https://www.google.com/x?y=z")
  1772  	if err != nil {
  1773  		t.Fatal(err)
  1774  	}
  1775  	js, err := json.Marshal(u)
  1776  	if err != nil {
  1777  		t.Fatal(err)
  1778  	}
  1779  
  1780  	// If only we could implement TextMarshaler/TextUnmarshaler,
  1781  	// this would work:
  1782  	//
  1783  	// if string(js) != strconv.Quote(u.String()) {
  1784  	// 	t.Errorf("json encoding: %s\nwant: %s\n", js, strconv.Quote(u.String()))
  1785  	// }
  1786  
  1787  	u1 := new(URL)
  1788  	err = json.Unmarshal(js, u1)
  1789  	if err != nil {
  1790  		t.Fatal(err)
  1791  	}
  1792  	if u1.String() != u.String() {
  1793  		t.Errorf("json decoded to: %s\nwant: %s\n", u1, u)
  1794  	}
  1795  }
  1796  
  1797  func TestGob(t *testing.T) {
  1798  	u, err := Parse("https://www.google.com/x?y=z")
  1799  	if err != nil {
  1800  		t.Fatal(err)
  1801  	}
  1802  	var w bytes.Buffer
  1803  	err = gob.NewEncoder(&w).Encode(u)
  1804  	if err != nil {
  1805  		t.Fatal(err)
  1806  	}
  1807  
  1808  	u1 := new(URL)
  1809  	err = gob.NewDecoder(&w).Decode(u1)
  1810  	if err != nil {
  1811  		t.Fatal(err)
  1812  	}
  1813  	if u1.String() != u.String() {
  1814  		t.Errorf("json decoded to: %s\nwant: %s\n", u1, u)
  1815  	}
  1816  }
  1817  
  1818  func TestNilUser(t *testing.T) {
  1819  	defer func() {
  1820  		if v := recover(); v != nil {
  1821  			t.Fatalf("unexpected panic: %v", v)
  1822  		}
  1823  	}()
  1824  
  1825  	u, err := Parse("http://foo.com/")
  1826  
  1827  	if err != nil {
  1828  		t.Fatalf("parse err: %v", err)
  1829  	}
  1830  
  1831  	if v := u.User.Username(); v != "" {
  1832  		t.Fatalf("expected empty username, got %s", v)
  1833  	}
  1834  
  1835  	if v, ok := u.User.Password(); v != "" || ok {
  1836  		t.Fatalf("expected empty password, got %s (%v)", v, ok)
  1837  	}
  1838  
  1839  	if v := u.User.String(); v != "" {
  1840  		t.Fatalf("expected empty string, got %s", v)
  1841  	}
  1842  }
  1843  
  1844  func TestInvalidUserPassword(t *testing.T) {
  1845  	_, err := Parse("http://user^:passwo^rd@foo.com/")
  1846  	if got, wantsub := fmt.Sprint(err), "net/url: invalid userinfo"; !strings.Contains(got, wantsub) {
  1847  		t.Errorf("error = %q; want substring %q", got, wantsub)
  1848  	}
  1849  }
  1850  
  1851  func TestRejectControlCharacters(t *testing.T) {
  1852  	tests := []string{
  1853  		"http://foo.com/?foo\nbar",
  1854  		"http\r://foo.com/",
  1855  		"http://foo\x7f.com/",
  1856  	}
  1857  	for _, s := range tests {
  1858  		_, err := Parse(s)
  1859  		const wantSub = "net/url: invalid control character in URL"
  1860  		if got := fmt.Sprint(err); !strings.Contains(got, wantSub) {
  1861  			t.Errorf("Parse(%q) error = %q; want substring %q", s, got, wantSub)
  1862  		}
  1863  	}
  1864  
  1865  	// But don't reject non-ASCII CTLs, at least for now:
  1866  	if _, err := Parse("http://foo.com/ctl\x80"); err != nil {
  1867  		t.Errorf("error parsing URL with non-ASCII control byte: %v", err)
  1868  	}
  1869  
  1870  }
  1871  
  1872  var escapeBenchmarks = []struct {
  1873  	unescaped string
  1874  	query     string
  1875  	path      string
  1876  }{
  1877  	{
  1878  		unescaped: "one two",
  1879  		query:     "one+two",
  1880  		path:      "one%20two",
  1881  	},
  1882  	{
  1883  		unescaped: "Фотки собак",
  1884  		query:     "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8+%D1%81%D0%BE%D0%B1%D0%B0%D0%BA",
  1885  		path:      "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8%20%D1%81%D0%BE%D0%B1%D0%B0%D0%BA",
  1886  	},
  1887  
  1888  	{
  1889  		unescaped: "shortrun(break)shortrun",
  1890  		query:     "shortrun%28break%29shortrun",
  1891  		path:      "shortrun%28break%29shortrun",
  1892  	},
  1893  
  1894  	{
  1895  		unescaped: "longerrunofcharacters(break)anotherlongerrunofcharacters",
  1896  		query:     "longerrunofcharacters%28break%29anotherlongerrunofcharacters",
  1897  		path:      "longerrunofcharacters%28break%29anotherlongerrunofcharacters",
  1898  	},
  1899  
  1900  	{
  1901  		unescaped: strings.Repeat("padded/with+various%characters?that=need$some@escaping+paddedsowebreak/256bytes", 4),
  1902  		query:     strings.Repeat("padded%2Fwith%2Bvarious%25characters%3Fthat%3Dneed%24some%40escaping%2Bpaddedsowebreak%2F256bytes", 4),
  1903  		path:      strings.Repeat("padded%2Fwith+various%25characters%3Fthat=need$some@escaping+paddedsowebreak%2F256bytes", 4),
  1904  	},
  1905  }
  1906  
  1907  func BenchmarkQueryEscape(b *testing.B) {
  1908  	for _, tc := range escapeBenchmarks {
  1909  		b.Run("", func(b *testing.B) {
  1910  			b.ReportAllocs()
  1911  			var g string
  1912  			for i := 0; i < b.N; i++ {
  1913  				g = QueryEscape(tc.unescaped)
  1914  			}
  1915  			b.StopTimer()
  1916  			if g != tc.query {
  1917  				b.Errorf("QueryEscape(%q) == %q, want %q", tc.unescaped, g, tc.query)
  1918  			}
  1919  
  1920  		})
  1921  	}
  1922  }
  1923  
  1924  func BenchmarkPathEscape(b *testing.B) {
  1925  	for _, tc := range escapeBenchmarks {
  1926  		b.Run("", func(b *testing.B) {
  1927  			b.ReportAllocs()
  1928  			var g string
  1929  			for i := 0; i < b.N; i++ {
  1930  				g = PathEscape(tc.unescaped)
  1931  			}
  1932  			b.StopTimer()
  1933  			if g != tc.path {
  1934  				b.Errorf("PathEscape(%q) == %q, want %q", tc.unescaped, g, tc.path)
  1935  			}
  1936  
  1937  		})
  1938  	}
  1939  }
  1940  
  1941  func BenchmarkQueryUnescape(b *testing.B) {
  1942  	for _, tc := range escapeBenchmarks {
  1943  		b.Run("", func(b *testing.B) {
  1944  			b.ReportAllocs()
  1945  			var g string
  1946  			for i := 0; i < b.N; i++ {
  1947  				g, _ = QueryUnescape(tc.query)
  1948  			}
  1949  			b.StopTimer()
  1950  			if g != tc.unescaped {
  1951  				b.Errorf("QueryUnescape(%q) == %q, want %q", tc.query, g, tc.unescaped)
  1952  			}
  1953  
  1954  		})
  1955  	}
  1956  }
  1957  
  1958  func BenchmarkPathUnescape(b *testing.B) {
  1959  	for _, tc := range escapeBenchmarks {
  1960  		b.Run("", func(b *testing.B) {
  1961  			b.ReportAllocs()
  1962  			var g string
  1963  			for i := 0; i < b.N; i++ {
  1964  				g, _ = PathUnescape(tc.path)
  1965  			}
  1966  			b.StopTimer()
  1967  			if g != tc.unescaped {
  1968  				b.Errorf("PathUnescape(%q) == %q, want %q", tc.path, g, tc.unescaped)
  1969  			}
  1970  
  1971  		})
  1972  	}
  1973  }
  1974  
  1975  var sink string
  1976  
  1977  func BenchmarkSplit(b *testing.B) {
  1978  	url := "http://www.google.com/?q=go+language#foo%26bar"
  1979  	for i := 0; i < b.N; i++ {
  1980  		sink, sink = split(url, '#', true)
  1981  	}
  1982  }
  1983  

View as plain text