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.
22 out *URL // expected parse
23 roundtrip string // expected result of reserializing the URL; empty means same as "in".
26 var urltests = []URLTest{
29 "http://www.google.com",
32 Host: "www.google.com",
38 "http://www.google.com/",
41 Host: "www.google.com",
46 // path with hex escaping
48 "http://www.google.com/file%20one%26two",
51 Host: "www.google.com",
52 Path: "/file one&two",
53 RawPath: "/file%20one%26two",
57 // fragment with hex escaping
59 "http://www.google.com/#file%20one%26two",
62 Host: "www.google.com",
64 Fragment: "file one&two",
65 RawFragment: "file%20one%26two",
71 "ftp://webmaster@www.google.com/",
74 User: User("webmaster"),
75 Host: "www.google.com",
80 // escape sequence in username
82 "ftp://john%20doe@www.google.com/",
85 User: User("john doe"),
86 Host: "www.google.com",
89 "ftp://john%20doe@www.google.com/",
93 "http://www.google.com/?",
96 Host: "www.google.com",
102 // query ending in question mark (Issue 14573)
104 "http://www.google.com/?foo=bar?",
107 Host: "www.google.com",
109 RawQuery: "foo=bar?",
115 "http://www.google.com/?q=go+language",
118 Host: "www.google.com",
120 RawQuery: "q=go+language",
124 // query with hex escaping: NOT parsed
126 "http://www.google.com/?q=go%20language",
129 Host: "www.google.com",
131 RawQuery: "q=go%20language",
137 "http://www.google.com/a%20b?q=c+d",
140 Host: "www.google.com",
146 // path without leading /, so no parsing
148 "http:www.google.com/?q=go+language",
151 Opaque: "www.google.com/",
152 RawQuery: "q=go+language",
154 "http:www.google.com/?q=go+language",
156 // path without leading /, so no parsing
158 "http:%2f%2fwww.google.com/?q=go+language",
161 Opaque: "%2f%2fwww.google.com/",
162 RawQuery: "q=go+language",
164 "http:%2f%2fwww.google.com/?q=go+language",
166 // non-authority with path; see golang.org/issue/46059
168 "mailto:/webmaster@golang.org",
171 Path: "/webmaster@golang.org",
178 "mailto:webmaster@golang.org",
181 Opaque: "webmaster@golang.org",
185 // unescaped :// in query should not create a scheme
187 "/foo?query=http://bad",
190 RawQuery: "query=http://bad",
194 // leading // without scheme should create an authority
202 // leading // without scheme, with userinfo, path, and query
204 "//user@foo/path?a=b",
213 // Three leading slashes isn't an authority, but doesn't return an error.
214 // (We can't return an error, as this code is also used via
215 // ServeHTTP -> ReadRequest -> Parse, which is arguably a
216 // different URL parsing context, but currently shares the
221 Path: "///threeslashes",
226 "http://user:password@google.com",
229 User: UserPassword("user", "password"),
232 "http://user:password@google.com",
234 // unescaped @ in username should not confuse host
236 "http://j@ne:password@google.com",
239 User: UserPassword("j@ne", "password"),
242 "http://j%40ne:password@google.com",
244 // unescaped @ in password should not confuse host
246 "http://jane:p@ssword@google.com",
249 User: UserPassword("jane", "p@ssword"),
252 "http://jane:p%40ssword@google.com",
255 "http://j@ne:password@google.com/p@th?q=@go",
258 User: UserPassword("j@ne", "password"),
263 "http://j%40ne:password@google.com/p@th?q=@go",
266 "http://www.google.com/?q=go+language#foo",
269 Host: "www.google.com",
271 RawQuery: "q=go+language",
277 "http://www.google.com/?q=go+language#foo&bar",
280 Host: "www.google.com",
282 RawQuery: "q=go+language",
285 "http://www.google.com/?q=go+language#foo&bar",
288 "http://www.google.com/?q=go+language#foo%26bar",
291 Host: "www.google.com",
293 RawQuery: "q=go+language",
295 RawFragment: "foo%26bar",
297 "http://www.google.com/?q=go+language#foo%26bar",
300 "file:///home/adg/rabbits",
304 Path: "/home/adg/rabbits",
306 "file:///home/adg/rabbits",
308 // "Windows" paths are no exception to the rule.
309 // See golang.org/issue/6027, especially comment #9.
311 "file:///C:/FooBar/Baz.txt",
315 Path: "/C:/FooBar/Baz.txt",
317 "file:///C:/FooBar/Baz.txt",
319 // case-insensitive scheme
321 "MaIlTo:webmaster@golang.org",
324 Opaque: "webmaster@golang.org",
326 "mailto:webmaster@golang.org",
336 // escaped '?' in username and password
338 "http://%3Fam:pa%3Fsword@google.com",
341 User: UserPassword("?am", "pa?sword"),
346 // host subcomponent; IPv4 address in RFC 3986
348 "http://192.168.0.1/",
356 // host and port subcomponents; IPv4 address in RFC 3986
358 "http://192.168.0.1:8080/",
361 Host: "192.168.0.1:8080",
366 // host subcomponent; IPv6 address in RFC 3986
376 // host and port subcomponents; IPv6 address in RFC 3986
378 "http://[fe80::1]:8080/",
381 Host: "[fe80::1]:8080",
386 // host subcomponent; IPv6 address with zone identifier in RFC 6874
388 "http://[fe80::1%25en0]/", // alphanum zone identifier
391 Host: "[fe80::1%en0]",
396 // host and port subcomponents; IPv6 address with zone identifier in RFC 6874
398 "http://[fe80::1%25en0]:8080/", // alphanum zone identifier
401 Host: "[fe80::1%en0]:8080",
406 // host subcomponent; IPv6 address with zone identifier in RFC 6874
408 "http://[fe80::1%25%65%6e%301-._~]/", // percent-encoded+unreserved zone identifier
411 Host: "[fe80::1%en01-._~]",
414 "http://[fe80::1%25en01-._~]/",
416 // host and port subcomponents; IPv6 address with zone identifier in RFC 6874
418 "http://[fe80::1%25%65%6e%301-._~]:8080/", // percent-encoded+unreserved zone identifier
421 Host: "[fe80::1%en01-._~]:8080",
424 "http://[fe80::1%25en01-._~]:8080/",
426 // alternate escapings of path survive round trip
428 "http://rest.rsc.io/foo%2fbar/baz%2Fquux?alt=media",
432 Path: "/foo/bar/baz/quux",
433 RawPath: "/foo%2fbar/baz%2Fquux",
434 RawQuery: "alt=media",
448 // worst case host, still round trips
450 "scheme://!$&'()*+,;=hello!:1/path",
453 Host: "!$&'()*+,;=hello!:1",
458 // worst case path, still round trips
460 "http://host/!$&'()*+,;=:@[hello]",
464 Path: "/!$&'()*+,;=:@[hello]",
465 RawPath: "/!$&'()*+,;=:@[hello]",
469 // golang.org/issue/5684
471 "http://example.com/oid/[order_id]",
475 Path: "/oid/[order_id]",
476 RawPath: "/oid/[order_id]",
480 // golang.org/issue/12200 (colon with empty port)
482 "http://192.168.0.2:8080/foo",
485 Host: "192.168.0.2:8080",
491 "http://192.168.0.2:/foo",
494 Host: "192.168.0.2:",
500 // Malformed IPv6 but still accepted.
501 "http://2b01:e34:ef40:7730:8e70:5aff:fefe:edac:8080/foo",
504 Host: "2b01:e34:ef40:7730:8e70:5aff:fefe:edac:8080",
510 // Malformed IPv6 but still accepted.
511 "http://2b01:e34:ef40:7730:8e70:5aff:fefe:edac:/foo",
514 Host: "2b01:e34:ef40:7730:8e70:5aff:fefe:edac:",
520 "http://[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:8080/foo",
523 Host: "[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:8080",
529 "http://[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:/foo",
532 Host: "[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:",
537 // golang.org/issue/7991 and golang.org/issue/12719 (non-ascii %-encoded in host)
539 "http://hello.世界.com/foo",
542 Host: "hello.世界.com",
545 "http://hello.%E4%B8%96%E7%95%8C.com/foo",
548 "http://hello.%e4%b8%96%e7%95%8c.com/foo",
551 Host: "hello.世界.com",
554 "http://hello.%E4%B8%96%E7%95%8C.com/foo",
557 "http://hello.%E4%B8%96%E7%95%8C.com/foo",
560 Host: "hello.世界.com",
565 // golang.org/issue/10433 (path beginning with //)
567 "http://example.com//foo",
575 // test that we can reparse the host names we accept.
577 "myscheme://authority<\"hi\">/foo",
580 Host: "authority<\"hi\">",
585 // spaces in hosts are disallowed but escaped spaces in IPv6 scope IDs are grudgingly OK.
586 // This happens on Windows.
587 // golang.org/issue/14002
589 "tcp://[2020::2020:20:2020:2020%25Windows%20Loves%20Spaces]:2020",
592 Host: "[2020::2020:20:2020:2020%Windows Loves Spaces]:2020",
596 // test we can roundtrip magnet url
597 // fix issue https://golang.org/issue/20054
599 "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
604 RawQuery: "xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
606 "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
609 "mailto:?subject=hi",
614 RawQuery: "subject=hi",
616 "mailto:?subject=hi",
620 // more useful string for debugging than fmt's struct printer
621 func ufmt(u *URL) string {
624 user = u.User.Username()
625 if p, ok := u.User.Password(); ok {
629 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, omithost=%t",
630 u.Opaque, u.Scheme, user, pass, u.Host, u.Path, u.RawPath, u.RawQuery, u.Fragment, u.RawFragment, u.ForceQuery, u.OmitHost)
633 func BenchmarkString(b *testing.B) {
636 for _, tt := range urltests {
637 u, err := Parse(tt.in)
639 b.Errorf("Parse(%q) returned error %s", tt.in, err)
642 if tt.roundtrip == "" {
647 for i := 0; i < b.N; i++ {
651 if w := tt.roundtrip; b.N > 0 && g != w {
652 b.Errorf("Parse(%q).String() == %q, want %q", tt.in, g, w)
657 func TestParse(t *testing.T) {
658 for _, tt := range urltests {
659 u, err := Parse(tt.in)
661 t.Errorf("Parse(%q) returned error %v", tt.in, err)
664 if !reflect.DeepEqual(u, tt.out) {
665 t.Errorf("Parse(%q):\n\tgot %v\n\twant %v\n", tt.in, ufmt(u), ufmt(tt.out))
670 const pathThatLooksSchemeRelative = "//not.a.user@not.a.host/just/a/path"
672 var parseRequestURLTests = []struct {
676 {"http://foo.com", true},
677 {"http://foo.com/", true},
678 {"http://foo.com/path", true},
680 {pathThatLooksSchemeRelative, true},
681 {"//not.a.user@%66%6f%6f.com/just/a/path/also", true},
683 {"http://192.168.0.1/", true},
684 {"http://192.168.0.1:8080/", true},
685 {"http://[fe80::1]/", true},
686 {"http://[fe80::1]:8080/", true},
688 // Tests exercising RFC 6874 compliance:
689 {"http://[fe80::1%25en0]/", true}, // with alphanum zone identifier
690 {"http://[fe80::1%25en0]:8080/", true}, // with alphanum zone identifier
691 {"http://[fe80::1%25%65%6e%301-._~]/", true}, // with percent-encoded+unreserved zone identifier
692 {"http://[fe80::1%25%65%6e%301-._~]:8080/", true}, // with percent-encoded+unreserved zone identifier
696 {" http://foo.com", false},
697 {"http://192.168.0.%31/", false},
698 {"http://192.168.0.%31:8080/", false},
699 {"http://[fe80::%31]/", false},
700 {"http://[fe80::%31]:8080/", false},
701 {"http://[fe80::%31%25en0]/", false},
702 {"http://[fe80::%31%25en0]:8080/", false},
704 // These two cases are valid as textual representations as
705 // described in RFC 4007, but are not valid as address
706 // literals with IPv6 zone identifiers in URIs as described in
708 {"http://[fe80::1%en0]/", false},
709 {"http://[fe80::1%en0]:8080/", false},
712 func TestParseRequestURI(t *testing.T) {
713 for _, test := range parseRequestURLTests {
714 _, err := ParseRequestURI(test.url)
715 if test.expectedValid && err != nil {
716 t.Errorf("ParseRequestURI(%q) gave err %v; want no error", test.url, err)
717 } else if !test.expectedValid && err == nil {
718 t.Errorf("ParseRequestURI(%q) gave nil error; want some error", test.url)
722 url, err := ParseRequestURI(pathThatLooksSchemeRelative)
724 t.Fatalf("Unexpected error %v", err)
726 if url.Path != pathThatLooksSchemeRelative {
727 t.Errorf("ParseRequestURI path:\ngot %q\nwant %q", url.Path, pathThatLooksSchemeRelative)
731 var stringURLTests = []struct {
735 // No leading slash on path should prepend slash on String() call
739 Host: "www.google.com",
742 want: "http://www.google.com/search",
744 // Relative path with first element containing ":" should be prepended with "./", golang.org/issue/17184
751 // Relative path with second element containing ":" should not be prepended with "./"
754 Path: "here/this:that",
756 want: "here/this:that",
758 // Non-relative path with first element containing ":" should not be prepended with "./"
762 Host: "www.google.com",
765 want: "http://www.google.com/this:that",
769 func TestURLString(t *testing.T) {
770 for _, tt := range urltests {
771 u, err := Parse(tt.in)
773 t.Errorf("Parse(%q) returned error %s", tt.in, err)
777 if tt.roundtrip != "" {
778 expected = tt.roundtrip
782 t.Errorf("Parse(%q).String() == %q (expected %q)", tt.in, s, expected)
786 for _, tt := range stringURLTests {
787 if got := tt.url.String(); got != tt.want {
788 t.Errorf("%+v.String() = %q; want %q", tt.url, got, tt.want)
793 func TestURLRedacted(t *testing.T) {
800 name: "non-blank Password",
805 User: UserPassword("user", "password"),
807 want: "http://user:xxxxx@host.tld/this:that",
810 name: "blank Password",
817 want: "http://user@host.tld/this:that",
825 User: UserPassword("", "password"),
827 want: "http://:xxxxx@host.tld/this:that",
830 name: "blank Username, blank Password",
836 want: "http://host.tld/this:that",
850 for _, tt := range cases {
852 t.Run(tt.name, func(t *testing.T) {
853 if g, w := tt.url.Redacted(), tt.want; g != w {
854 t.Fatalf("got: %q\nwant: %q", g, w)
860 type EscapeTest struct {
866 var unescapeTests = []EscapeTest{
898 "%", // not enough characters after %
903 "%a", // not enough characters after %
908 "%1", // not enough characters after %
913 "123%45%6", // not enough characters after %
918 "%zzzzz", // invalid hex digits
934 func TestUnescape(t *testing.T) {
935 for _, tt := range unescapeTests {
936 actual, err := QueryUnescape(tt.in)
937 if actual != tt.out || (err != nil) != (tt.err != nil) {
938 t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", tt.in, actual, err, tt.out, tt.err)
943 if strings.Contains(tt.in, "+") {
944 in = strings.ReplaceAll(tt.in, "+", "%20")
945 actual, err := PathUnescape(in)
946 if actual != tt.out || (err != nil) != (tt.err != nil) {
947 t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", in, actual, err, tt.out, tt.err)
950 s, err := QueryUnescape(strings.ReplaceAll(tt.in, "+", "XXX"))
955 out = strings.ReplaceAll(s, "XXX", "+")
959 actual, err = PathUnescape(in)
960 if actual != out || (err != nil) != (tt.err != nil) {
961 t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", in, actual, err, out, tt.err)
966 var queryEscapeTests = []EscapeTest{
988 " ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
989 "+%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",
994 func TestQueryEscape(t *testing.T) {
995 for _, tt := range queryEscapeTests {
996 actual := QueryEscape(tt.in)
997 if tt.out != actual {
998 t.Errorf("QueryEscape(%q) = %q, want %q", tt.in, actual, tt.out)
1001 // for bonus points, verify that escape:unescape is an identity.
1002 roundtrip, err := QueryUnescape(actual)
1003 if roundtrip != tt.in || err != nil {
1004 t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]")
1009 var pathEscapeTests = []EscapeTest{
1041 " ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
1042 "%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",
1047 func TestPathEscape(t *testing.T) {
1048 for _, tt := range pathEscapeTests {
1049 actual := PathEscape(tt.in)
1050 if tt.out != actual {
1051 t.Errorf("PathEscape(%q) = %q, want %q", tt.in, actual, tt.out)
1054 // for bonus points, verify that escape:unescape is an identity.
1055 roundtrip, err := PathUnescape(actual)
1056 if roundtrip != tt.in || err != nil {
1057 t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]")
1062 //var userinfoTests = []UserinfoTest{
1063 // {"user", "password", "user:password"},
1064 // {"foo:bar", "~!@#$%^&*()_+{}|[]\\-=`:;'\"<>?,./",
1065 // "foo%3Abar:~!%40%23$%25%5E&*()_+%7B%7D%7C%5B%5D%5C-=%60%3A;'%22%3C%3E?,.%2F"},
1068 type EncodeQueryTest struct {
1073 var encodeQueryTests = []EncodeQueryTest{
1075 {Values{"q": {"puppies"}, "oe": {"utf8"}}, "oe=utf8&q=puppies"},
1076 {Values{"q": {"dogs", "&", "7"}}, "q=dogs&q=%26&q=7"},
1078 "a": {"a1", "a2", "a3"},
1079 "b": {"b1", "b2", "b3"},
1080 "c": {"c1", "c2", "c3"},
1081 }, "a=a1&a=a2&a=a3&b=b1&b=b2&b=b3&c=c1&c=c2&c=c3"},
1084 func TestEncodeQuery(t *testing.T) {
1085 for _, tt := range encodeQueryTests {
1086 if q := tt.m.Encode(); q != tt.expected {
1087 t.Errorf(`EncodeQuery(%+v) = %q, want %q`, tt.m, q, tt.expected)
1092 var resolvePathTests = []struct {
1093 base, ref, expected string
1095 {"a/b", ".", "/a/"},
1096 {"a/b", "c", "/a/c"},
1099 {"a/", "../..", "/"},
1100 {"a/b/c", "..", "/a/"},
1101 {"a/b/c", "../d", "/a/d"},
1102 {"a/b/c", ".././d", "/a/d"},
1103 {"a/b", "./..", "/"},
1104 {"a/./b", ".", "/a/"},
1105 {"a/../", ".", "/"},
1106 {"a/.././b", "c", "/c"},
1109 func TestResolvePath(t *testing.T) {
1110 for _, test := range resolvePathTests {
1111 got := resolvePath(test.base, test.ref)
1112 if got != test.expected {
1113 t.Errorf("For %q + %q got %q; expected %q", test.base, test.ref, got, test.expected)
1118 func BenchmarkResolvePath(b *testing.B) {
1120 for i := 0; i < b.N; i++ {
1121 resolvePath("a/b/c", ".././d")
1125 var resolveReferenceTests = []struct {
1126 base, rel, expected string
1128 // Absolute URL references
1129 {"http://foo.com?a=b", "https://bar.com/", "https://bar.com/"},
1130 {"http://foo.com/", "https://bar.com/?a=b", "https://bar.com/?a=b"},
1131 {"http://foo.com/", "https://bar.com/?", "https://bar.com/?"},
1132 {"http://foo.com/bar", "mailto:foo@example.com", "mailto:foo@example.com"},
1134 // Path-absolute references
1135 {"http://foo.com/bar", "/baz", "http://foo.com/baz"},
1136 {"http://foo.com/bar?a=b#f", "/baz", "http://foo.com/baz"},
1137 {"http://foo.com/bar?a=b", "/baz?", "http://foo.com/baz?"},
1138 {"http://foo.com/bar?a=b", "/baz?c=d", "http://foo.com/baz?c=d"},
1141 {"http://foo.com/bar", "http://foo.com//baz", "http://foo.com//baz"},
1142 {"http://foo.com/bar", "http://foo.com///baz/quux", "http://foo.com///baz/quux"},
1145 {"https://foo.com/bar?a=b", "//bar.com/quux", "https://bar.com/quux"},
1147 // Path-relative references:
1149 // ... current directory
1150 {"http://foo.com", ".", "http://foo.com/"},
1151 {"http://foo.com/bar", ".", "http://foo.com/"},
1152 {"http://foo.com/bar/", ".", "http://foo.com/bar/"},
1155 {"http://foo.com", "bar", "http://foo.com/bar"},
1156 {"http://foo.com/", "bar", "http://foo.com/bar"},
1157 {"http://foo.com/bar/baz", "quux", "http://foo.com/bar/quux"},
1160 {"http://foo.com/bar/baz", "../quux", "http://foo.com/quux"},
1161 {"http://foo.com/bar/baz", "../../../../../quux", "http://foo.com/quux"},
1162 {"http://foo.com/bar", "..", "http://foo.com/"},
1163 {"http://foo.com/bar/baz", "./..", "http://foo.com/"},
1164 // ".." in the middle (issue 3560)
1165 {"http://foo.com/bar/baz", "quux/dotdot/../tail", "http://foo.com/bar/quux/tail"},
1166 {"http://foo.com/bar/baz", "quux/./dotdot/../tail", "http://foo.com/bar/quux/tail"},
1167 {"http://foo.com/bar/baz", "quux/./dotdot/.././tail", "http://foo.com/bar/quux/tail"},
1168 {"http://foo.com/bar/baz", "quux/./dotdot/./../tail", "http://foo.com/bar/quux/tail"},
1169 {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/././../../tail", "http://foo.com/bar/quux/tail"},
1170 {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/./.././../tail", "http://foo.com/bar/quux/tail"},
1171 {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/dotdot/./../../.././././tail", "http://foo.com/bar/quux/tail"},
1172 {"http://foo.com/bar/baz", "quux/./dotdot/../dotdot/../dot/./tail/..", "http://foo.com/bar/quux/dot/"},
1174 // Remove any dot-segments prior to forming the target URI.
1175 // https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
1176 {"http://foo.com/dot/./dotdot/../foo/bar", "../baz", "http://foo.com/dot/baz"},
1178 // Triple dot isn't special
1179 {"http://foo.com/bar", "...", "http://foo.com/..."},
1182 {"http://foo.com/bar", ".#frag", "http://foo.com/#frag"},
1183 {"http://example.org/", "#!$&%27()*+,;=", "http://example.org/#!$&%27()*+,;="},
1185 // Paths with escaping (issue 16947).
1186 {"http://foo.com/foo%2fbar/", "../baz", "http://foo.com/baz"},
1187 {"http://foo.com/1/2%2f/3%2f4/5", "../../a/b/c", "http://foo.com/1/a/b/c"},
1188 {"http://foo.com/1/2/3", "./a%2f../../b/..%2fc", "http://foo.com/1/2/b/..%2fc"},
1189 {"http://foo.com/1/2%2f/3%2f4/5", "./a%2f../b/../c", "http://foo.com/1/2%2f/3%2f4/a%2f../c"},
1190 {"http://foo.com/foo%20bar/", "../baz", "http://foo.com/baz"},
1191 {"http://foo.com/foo", "../bar%2fbaz", "http://foo.com/bar%2fbaz"},
1192 {"http://foo.com/foo%2dbar/", "./baz-quux", "http://foo.com/foo%2dbar/baz-quux"},
1194 // RFC 3986: Normal Examples
1195 // https://datatracker.ietf.org/doc/html/rfc3986#section-5.4.1
1196 {"http://a/b/c/d;p?q", "g:h", "g:h"},
1197 {"http://a/b/c/d;p?q", "g", "http://a/b/c/g"},
1198 {"http://a/b/c/d;p?q", "./g", "http://a/b/c/g"},
1199 {"http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"},
1200 {"http://a/b/c/d;p?q", "/g", "http://a/g"},
1201 {"http://a/b/c/d;p?q", "//g", "http://g"},
1202 {"http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y"},
1203 {"http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"},
1204 {"http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s"},
1205 {"http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"},
1206 {"http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"},
1207 {"http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"},
1208 {"http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"},
1209 {"http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"},
1210 {"http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q"},
1211 {"http://a/b/c/d;p?q", ".", "http://a/b/c/"},
1212 {"http://a/b/c/d;p?q", "./", "http://a/b/c/"},
1213 {"http://a/b/c/d;p?q", "..", "http://a/b/"},
1214 {"http://a/b/c/d;p?q", "../", "http://a/b/"},
1215 {"http://a/b/c/d;p?q", "../g", "http://a/b/g"},
1216 {"http://a/b/c/d;p?q", "../..", "http://a/"},
1217 {"http://a/b/c/d;p?q", "../../", "http://a/"},
1218 {"http://a/b/c/d;p?q", "../../g", "http://a/g"},
1220 // RFC 3986: Abnormal Examples
1221 // https://datatracker.ietf.org/doc/html/rfc3986#section-5.4.2
1222 {"http://a/b/c/d;p?q", "../../../g", "http://a/g"},
1223 {"http://a/b/c/d;p?q", "../../../../g", "http://a/g"},
1224 {"http://a/b/c/d;p?q", "/./g", "http://a/g"},
1225 {"http://a/b/c/d;p?q", "/../g", "http://a/g"},
1226 {"http://a/b/c/d;p?q", "g.", "http://a/b/c/g."},
1227 {"http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"},
1228 {"http://a/b/c/d;p?q", "g..", "http://a/b/c/g.."},
1229 {"http://a/b/c/d;p?q", "..g", "http://a/b/c/..g"},
1230 {"http://a/b/c/d;p?q", "./../g", "http://a/b/g"},
1231 {"http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"},
1232 {"http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"},
1233 {"http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h"},
1234 {"http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"},
1235 {"http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y"},
1236 {"http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x"},
1237 {"http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x"},
1238 {"http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x"},
1239 {"http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x"},
1242 {"https://a/b/c/d;p?q", "//g?q", "https://g?q"},
1243 {"https://a/b/c/d;p?q", "//g#s", "https://g#s"},
1244 {"https://a/b/c/d;p?q", "//g/d/e/f?y#s", "https://g/d/e/f?y#s"},
1245 {"https://a/b/c/d;p#s", "?y", "https://a/b/c/d;p?y"},
1246 {"https://a/b/c/d;p?q#s", "?y", "https://a/b/c/d;p?y"},
1248 // Empty path and query but with ForceQuery (issue 46033).
1249 {"https://a/b/c/d;p?q#s", "?", "https://a/b/c/d;p?"},
1252 func TestResolveReference(t *testing.T) {
1253 mustParse := func(url string) *URL {
1254 u, err := Parse(url)
1256 t.Fatalf("Parse(%q) got err %v", url, err)
1260 opaque := &URL{Scheme: "scheme", Opaque: "opaque"}
1261 for _, test := range resolveReferenceTests {
1262 base := mustParse(test.base)
1263 rel := mustParse(test.rel)
1264 url := base.ResolveReference(rel)
1265 if got := url.String(); got != test.expected {
1266 t.Errorf("URL(%q).ResolveReference(%q)\ngot %q\nwant %q", test.base, test.rel, got, test.expected)
1268 // Ensure that new instances are returned.
1270 t.Errorf("Expected URL.ResolveReference to return new URL instance.")
1272 // Test the convenience wrapper too.
1273 url, err := base.Parse(test.rel)
1275 t.Errorf("URL(%q).Parse(%q) failed: %v", test.base, test.rel, err)
1276 } else if got := url.String(); got != test.expected {
1277 t.Errorf("URL(%q).Parse(%q)\ngot %q\nwant %q", test.base, test.rel, got, test.expected)
1278 } else if base == url {
1279 // Ensure that new instances are returned for the wrapper too.
1280 t.Errorf("Expected URL.Parse to return new URL instance.")
1282 // Ensure Opaque resets the URL.
1283 url = base.ResolveReference(opaque)
1284 if *url != *opaque {
1285 t.Errorf("ResolveReference failed to resolve opaque URL:\ngot %#v\nwant %#v", url, opaque)
1287 // Test the convenience wrapper with an opaque URL too.
1288 url, err = base.Parse("scheme:opaque")
1290 t.Errorf(`URL(%q).Parse("scheme:opaque") failed: %v`, test.base, err)
1291 } else if *url != *opaque {
1292 t.Errorf("Parse failed to resolve opaque URL:\ngot %#v\nwant %#v", opaque, url)
1293 } else if base == url {
1294 // Ensure that new instances are returned, again.
1295 t.Errorf("Expected URL.Parse to return new URL instance.")
1300 func TestQueryValues(t *testing.T) {
1301 u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2&baz")
1304 t.Errorf("got %d keys in Query values, want 3", len(v))
1306 if g, e := v.Get("foo"), "bar"; g != e {
1307 t.Errorf("Get(foo) = %q, want %q", g, e)
1310 if g, e := v.Get("Foo"), ""; g != e {
1311 t.Errorf("Get(Foo) = %q, want %q", g, e)
1313 if g, e := v.Get("bar"), "1"; g != e {
1314 t.Errorf("Get(bar) = %q, want %q", g, e)
1316 if g, e := v.Get("baz"), ""; g != e {
1317 t.Errorf("Get(baz) = %q, want %q", g, e)
1319 if h, e := v.Has("foo"), true; h != e {
1320 t.Errorf("Has(foo) = %t, want %t", h, e)
1322 if h, e := v.Has("bar"), true; h != e {
1323 t.Errorf("Has(bar) = %t, want %t", h, e)
1325 if h, e := v.Has("baz"), true; h != e {
1326 t.Errorf("Has(baz) = %t, want %t", h, e)
1328 if h, e := v.Has("noexist"), false; h != e {
1329 t.Errorf("Has(noexist) = %t, want %t", h, e)
1332 if g, e := v.Get("bar"), ""; g != e {
1333 t.Errorf("second Get(bar) = %q, want %q", g, e)
1337 type parseTest struct {
1343 var parseTests = []parseTest{
1346 out: Values{"a": []string{"1"}},
1351 out: Values{"a": []string{"1"}, "b": []string{"2"}},
1355 query: "a=1&a=2&a=banana",
1356 out: Values{"a": []string{"1", "2", "banana"}},
1360 query: "ascii=%3Ckey%3A+0x90%3E",
1361 out: Values{"ascii": []string{"<key: 0x90>"}},
1372 query: "a=%3B", // hex encoding for semicolon
1373 out: Values{"a": []string{";"}},
1378 out: Values{"a;b": []string{"1"}},
1382 query: "a=1&a=2;a=banana",
1383 out: Values{"a": []string{"1"}},
1388 out: Values{"c": []string{"1"}},
1392 query: "a=1&b=2;a=3&c=4",
1393 out: Values{"a": []string{"1"}, "c": []string{"4"}},
1397 query: "a=1&b=2;c=3",
1398 out: Values{"a": []string{"1"}},
1413 out: Values{"a": []string{"1"}},
1418 out: Values{"b": []string{"2"}},
1423 out: Values{"a": []string{"1"}},
1428 func TestParseQuery(t *testing.T) {
1429 for _, test := range parseTests {
1430 t.Run(test.query, func(t *testing.T) {
1431 form, err := ParseQuery(test.query)
1432 if test.ok != (err == nil) {
1437 t.Errorf("Unexpected error: %v, want %v", err, want)
1439 if len(form) != len(test.out) {
1440 t.Errorf("len(form) = %d, want %d", len(form), len(test.out))
1442 for k, evs := range test.out {
1445 t.Errorf("Missing key %q", k)
1448 if len(vs) != len(evs) {
1449 t.Errorf("len(form[%q]) = %d, want %d", k, len(vs), len(evs))
1452 for j, ev := range evs {
1453 if v := vs[j]; v != ev {
1454 t.Errorf("form[%q][%d] = %q, want %q", k, j, v, ev)
1462 type RequestURITest struct {
1467 var requritests = []RequestURITest{
1471 Host: "example.com",
1479 Host: "example.com",
1484 // golang.org/issue/4860 variant 1
1488 Host: "example.com",
1489 Opaque: "/%2F/%2F/",
1493 // golang.org/issue/4860 variant 2
1497 Host: "example.com",
1498 Opaque: "//other.example.com/%2F/%2F/",
1500 "http://other.example.com/%2F/%2F/",
1502 // better fix for issue 4860
1506 Host: "example.com",
1508 RawPath: "/%2F/%2F/",
1515 Host: "example.com",
1517 RawPath: "/WRONG/", // ignored because doesn't match Path
1524 Host: "example.com",
1526 RawQuery: "q=go+language",
1528 "/a%20b?q=go+language",
1533 Host: "example.com",
1535 RawPath: "/a b", // ignored because invalid
1536 RawQuery: "q=go+language",
1538 "/a%20b?q=go+language",
1543 Host: "example.com",
1545 RawPath: "/a?b", // ignored because invalid
1546 RawQuery: "q=go+language",
1548 "/a%3Fb?q=go+language",
1561 RawQuery: "q=go+language",
1563 "opaque?q=go+language",
1568 Host: "example.com",
1576 Host: "example.com",
1584 func TestRequestURI(t *testing.T) {
1585 for _, tt := range requritests {
1586 s := tt.url.RequestURI()
1588 t.Errorf("%#v.RequestURI() == %q (expected %q)", tt.url, s, tt.out)
1593 func TestParseFailure(t *testing.T) {
1594 // Test that the first parse error is returned.
1595 const url = "%gh&%ij"
1596 _, err := ParseQuery(url)
1597 errStr := fmt.Sprint(err)
1598 if !strings.Contains(errStr, "%gh") {
1599 t.Errorf(`ParseQuery(%q) returned error %q, want something containing %q"`, url, errStr, "%gh")
1603 func TestParseErrors(t *testing.T) {
1608 {"http://[::1]", false},
1609 {"http://[::1]:80", false},
1610 {"http://[::1]:namedport", true}, // rfc3986 3.2.3
1611 {"http://x:namedport", true}, // rfc3986 3.2.3
1612 {"http://[::1]/", false},
1613 {"http://[::1]a", true},
1614 {"http://[::1]%23", true},
1615 {"http://[::1%25en0]", false}, // valid zone id
1616 {"http://[::1]:", false}, // colon, but no port OK
1617 {"http://x:", false}, // colon, but no port OK
1618 {"http://[::1]:%38%30", true}, // not allowed: % encoding only for non-ASCII
1619 {"http://[::1%25%41]", false}, // RFC 6874 allows over-escaping in zone
1620 {"http://[%10::1]", true}, // no %xx escapes in IP address
1621 {"http://[::1]/%48", false}, // %xx in path is fine
1622 {"http://%41:8080/", true}, // not allowed: % encoding only for non-ASCII
1623 {"mysql://x@y(z:123)/foo", true}, // not well-formed per RFC 3986, golang.org/issue/33646
1624 {"mysql://x@y(1.2.3.4:123)/foo", true},
1626 {" http://foo.com", true}, // invalid character in schema
1627 {"ht tp://foo.com", true}, // invalid character in schema
1628 {"ahttp://foo.com", false}, // valid schema characters
1629 {"1http://foo.com", true}, // invalid character in schema
1631 {"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
1632 {"http://a b.com/", true}, // no space in host name please
1633 {"cache_object://foo", true}, // scheme cannot have _, relative path cannot have : in first segment
1634 {"cache_object:foo", true},
1635 {"cache_object:foo/bar", true},
1636 {"cache_object/:foo/bar", false},
1638 for _, tt := range tests {
1639 u, err := Parse(tt.in)
1642 t.Errorf("Parse(%q) = %#v; want an error", tt.in, u)
1647 t.Errorf("Parse(%q) = %v; want no error", tt.in, err)
1653 func TestStarRequest(t *testing.T) {
1654 u, err := Parse("*")
1658 if got, want := u.RequestURI(), "*"; got != want {
1659 t.Errorf("RequestURI = %q; want %q", got, want)
1663 type shouldEscapeTest struct {
1669 var shouldEscapeTests = []shouldEscapeTest{
1670 // Unreserved characters (§2.3)
1671 {'a', encodePath, false},
1672 {'a', encodeUserPassword, false},
1673 {'a', encodeQueryComponent, false},
1674 {'a', encodeFragment, false},
1675 {'a', encodeHost, false},
1676 {'z', encodePath, false},
1677 {'A', encodePath, false},
1678 {'Z', encodePath, false},
1679 {'0', encodePath, false},
1680 {'9', encodePath, false},
1681 {'-', encodePath, false},
1682 {'-', encodeUserPassword, false},
1683 {'-', encodeQueryComponent, false},
1684 {'-', encodeFragment, false},
1685 {'.', encodePath, false},
1686 {'_', encodePath, false},
1687 {'~', encodePath, false},
1689 // User information (§3.2.1)
1690 {':', encodeUserPassword, true},
1691 {'/', encodeUserPassword, true},
1692 {'?', encodeUserPassword, true},
1693 {'@', encodeUserPassword, true},
1694 {'$', encodeUserPassword, false},
1695 {'&', encodeUserPassword, false},
1696 {'+', encodeUserPassword, false},
1697 {',', encodeUserPassword, false},
1698 {';', encodeUserPassword, false},
1699 {'=', encodeUserPassword, false},
1701 // Host (IP address, IPv6 address, registered name, port suffix; §3.2.2)
1702 {'!', encodeHost, false},
1703 {'$', encodeHost, false},
1704 {'&', encodeHost, false},
1705 {'\'', encodeHost, false},
1706 {'(', encodeHost, false},
1707 {')', encodeHost, false},
1708 {'*', encodeHost, false},
1709 {'+', encodeHost, false},
1710 {',', encodeHost, false},
1711 {';', encodeHost, false},
1712 {'=', encodeHost, false},
1713 {':', encodeHost, false},
1714 {'[', encodeHost, false},
1715 {']', encodeHost, false},
1716 {'0', encodeHost, false},
1717 {'9', encodeHost, false},
1718 {'A', encodeHost, false},
1719 {'z', encodeHost, false},
1720 {'_', encodeHost, false},
1721 {'-', encodeHost, false},
1722 {'.', encodeHost, false},
1725 func TestShouldEscape(t *testing.T) {
1726 for _, tt := range shouldEscapeTests {
1727 if shouldEscape(tt.in, tt.mode) != tt.escape {
1728 t.Errorf("shouldEscape(%q, %v) returned %v; expected %v", tt.in, tt.mode, !tt.escape, tt.escape)
1733 type timeoutError struct {
1737 func (e *timeoutError) Error() string { return "timeout error" }
1738 func (e *timeoutError) Timeout() bool { return e.timeout }
1740 type temporaryError struct {
1744 func (e *temporaryError) Error() string { return "temporary error" }
1745 func (e *temporaryError) Temporary() bool { return e.temporary }
1747 type timeoutTemporaryError struct {
1752 func (e *timeoutTemporaryError) Error() string { return "timeout/temporary error" }
1754 var netErrorTests = []struct {
1759 err: &Error{"Get", "http://google.com/", &timeoutError{timeout: true}},
1763 err: &Error{"Get", "http://google.com/", &timeoutError{timeout: false}},
1767 err: &Error{"Get", "http://google.com/", &temporaryError{temporary: true}},
1771 err: &Error{"Get", "http://google.com/", &temporaryError{temporary: false}},
1775 err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: true}, temporaryError{temporary: true}}},
1779 err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: false}, temporaryError{temporary: true}}},
1783 err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: true}, temporaryError{temporary: false}}},
1787 err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: false}, temporaryError{temporary: false}}},
1791 err: &Error{"Get", "http://google.com/", io.EOF},
1796 // Test that url.Error implements net.Error and that it forwards
1797 func TestURLErrorImplementsNetError(t *testing.T) {
1798 for i, tt := range netErrorTests {
1799 err, ok := tt.err.(net.Error)
1801 t.Errorf("%d: %T does not implement net.Error", i+1, tt.err)
1804 if err.Timeout() != tt.timeout {
1805 t.Errorf("%d: err.Timeout(): got %v, want %v", i+1, err.Timeout(), tt.timeout)
1808 if err.Temporary() != tt.temporary {
1809 t.Errorf("%d: err.Temporary(): got %v, want %v", i+1, err.Temporary(), tt.temporary)
1814 func TestURLHostnameAndPort(t *testing.T) {
1816 in string // URL.Host field
1820 {"foo.com:80", "foo.com", "80"},
1821 {"foo.com", "foo.com", ""},
1822 {"foo.com:", "foo.com", ""},
1823 {"FOO.COM", "FOO.COM", ""}, // no canonicalization
1824 {"1.2.3.4", "1.2.3.4", ""},
1825 {"1.2.3.4:80", "1.2.3.4", "80"},
1826 {"[1:2:3:4]", "1:2:3:4", ""},
1827 {"[1:2:3:4]:80", "1:2:3:4", "80"},
1828 {"[::1]:80", "::1", "80"},
1829 {"[::1]", "::1", ""},
1830 {"[::1]:", "::1", ""},
1831 {"localhost", "localhost", ""},
1832 {"localhost:443", "localhost", "443"},
1833 {"some.super.long.domain.example.org:8080", "some.super.long.domain.example.org", "8080"},
1834 {"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "17000"},
1835 {"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", ""},
1837 // Ensure that even when not valid, Host is one of "Hostname",
1838 // "Hostname:Port", "[Hostname]" or "[Hostname]:Port".
1839 // See https://golang.org/issue/29098.
1840 {"[google.com]:80", "google.com", "80"},
1841 {"google.com]:80", "google.com]", "80"},
1842 {"google.com:80_invalid_port", "google.com:80_invalid_port", ""},
1843 {"[::1]extra]:80", "::1]extra", "80"},
1844 {"google.com]extra:extra", "google.com]extra:extra", ""},
1846 for _, tt := range tests {
1847 u := &URL{Host: tt.in}
1848 host, port := u.Hostname(), u.Port()
1849 if host != tt.host {
1850 t.Errorf("Hostname for Host %q = %q; want %q", tt.in, host, tt.host)
1852 if port != tt.port {
1853 t.Errorf("Port for Host %q = %q; want %q", tt.in, port, tt.port)
1858 var _ encodingPkg.BinaryMarshaler = (*URL)(nil)
1859 var _ encodingPkg.BinaryUnmarshaler = (*URL)(nil)
1861 func TestJSON(t *testing.T) {
1862 u, err := Parse("https://www.google.com/x?y=z")
1866 js, err := json.Marshal(u)
1871 // If only we could implement TextMarshaler/TextUnmarshaler,
1874 // if string(js) != strconv.Quote(u.String()) {
1875 // t.Errorf("json encoding: %s\nwant: %s\n", js, strconv.Quote(u.String()))
1879 err = json.Unmarshal(js, u1)
1883 if u1.String() != u.String() {
1884 t.Errorf("json decoded to: %s\nwant: %s\n", u1, u)
1888 func TestGob(t *testing.T) {
1889 u, err := Parse("https://www.google.com/x?y=z")
1894 err = gob.NewEncoder(&w).Encode(u)
1900 err = gob.NewDecoder(&w).Decode(u1)
1904 if u1.String() != u.String() {
1905 t.Errorf("json decoded to: %s\nwant: %s\n", u1, u)
1909 func TestNilUser(t *testing.T) {
1911 if v := recover(); v != nil {
1912 t.Fatalf("unexpected panic: %v", v)
1916 u, err := Parse("http://foo.com/")
1919 t.Fatalf("parse err: %v", err)
1922 if v := u.User.Username(); v != "" {
1923 t.Fatalf("expected empty username, got %s", v)
1926 if v, ok := u.User.Password(); v != "" || ok {
1927 t.Fatalf("expected empty password, got %s (%v)", v, ok)
1930 if v := u.User.String(); v != "" {
1931 t.Fatalf("expected empty string, got %s", v)
1935 func TestInvalidUserPassword(t *testing.T) {
1936 _, err := Parse("http://user^:passwo^rd@foo.com/")
1937 if got, wantsub := fmt.Sprint(err), "net/url: invalid userinfo"; !strings.Contains(got, wantsub) {
1938 t.Errorf("error = %q; want substring %q", got, wantsub)
1942 func TestRejectControlCharacters(t *testing.T) {
1944 "http://foo.com/?foo\nbar",
1945 "http\r://foo.com/",
1946 "http://foo\x7f.com/",
1948 for _, s := range tests {
1950 const wantSub = "net/url: invalid control character in URL"
1951 if got := fmt.Sprint(err); !strings.Contains(got, wantSub) {
1952 t.Errorf("Parse(%q) error = %q; want substring %q", s, got, wantSub)
1956 // But don't reject non-ASCII CTLs, at least for now:
1957 if _, err := Parse("http://foo.com/ctl\x80"); err != nil {
1958 t.Errorf("error parsing URL with non-ASCII control byte: %v", err)
1963 var escapeBenchmarks = []struct {
1969 unescaped: "one two",
1974 unescaped: "Фотки собак",
1975 query: "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8+%D1%81%D0%BE%D0%B1%D0%B0%D0%BA",
1976 path: "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8%20%D1%81%D0%BE%D0%B1%D0%B0%D0%BA",
1980 unescaped: "shortrun(break)shortrun",
1981 query: "shortrun%28break%29shortrun",
1982 path: "shortrun%28break%29shortrun",
1986 unescaped: "longerrunofcharacters(break)anotherlongerrunofcharacters",
1987 query: "longerrunofcharacters%28break%29anotherlongerrunofcharacters",
1988 path: "longerrunofcharacters%28break%29anotherlongerrunofcharacters",
1992 unescaped: strings.Repeat("padded/with+various%characters?that=need$some@escaping+paddedsowebreak/256bytes", 4),
1993 query: strings.Repeat("padded%2Fwith%2Bvarious%25characters%3Fthat%3Dneed%24some%40escaping%2Bpaddedsowebreak%2F256bytes", 4),
1994 path: strings.Repeat("padded%2Fwith+various%25characters%3Fthat=need$some@escaping+paddedsowebreak%2F256bytes", 4),
1998 func BenchmarkQueryEscape(b *testing.B) {
1999 for _, tc := range escapeBenchmarks {
2000 b.Run("", func(b *testing.B) {
2003 for i := 0; i < b.N; i++ {
2004 g = QueryEscape(tc.unescaped)
2008 b.Errorf("QueryEscape(%q) == %q, want %q", tc.unescaped, g, tc.query)
2015 func BenchmarkPathEscape(b *testing.B) {
2016 for _, tc := range escapeBenchmarks {
2017 b.Run("", func(b *testing.B) {
2020 for i := 0; i < b.N; i++ {
2021 g = PathEscape(tc.unescaped)
2025 b.Errorf("PathEscape(%q) == %q, want %q", tc.unescaped, g, tc.path)
2032 func BenchmarkQueryUnescape(b *testing.B) {
2033 for _, tc := range escapeBenchmarks {
2034 b.Run("", func(b *testing.B) {
2037 for i := 0; i < b.N; i++ {
2038 g, _ = QueryUnescape(tc.query)
2041 if g != tc.unescaped {
2042 b.Errorf("QueryUnescape(%q) == %q, want %q", tc.query, g, tc.unescaped)
2049 func BenchmarkPathUnescape(b *testing.B) {
2050 for _, tc := range escapeBenchmarks {
2051 b.Run("", func(b *testing.B) {
2054 for i := 0; i < b.N; i++ {
2055 g, _ = PathUnescape(tc.path)
2058 if g != tc.unescaped {
2059 b.Errorf("PathUnescape(%q) == %q, want %q", tc.path, g, tc.unescaped)
2066 func TestJoinPath(t *testing.T) {
2073 base: "https://go.googlesource.com",
2074 elem: []string{"go"},
2075 out: "https://go.googlesource.com/go",
2078 base: "https://go.googlesource.com/a/b/c",
2079 elem: []string{"../../../go"},
2080 out: "https://go.googlesource.com/go",
2083 base: "https://go.googlesource.com/",
2084 elem: []string{"../go"},
2085 out: "https://go.googlesource.com/go",
2088 base: "https://go.googlesource.com",
2089 elem: []string{"../go"},
2090 out: "https://go.googlesource.com/go",
2093 base: "https://go.googlesource.com",
2094 elem: []string{"../go", "../../go", "../../../go"},
2095 out: "https://go.googlesource.com/go",
2098 base: "https://go.googlesource.com/../go",
2100 out: "https://go.googlesource.com/go",
2103 base: "https://go.googlesource.com/",
2104 elem: []string{"./go"},
2105 out: "https://go.googlesource.com/go",
2108 base: "https://go.googlesource.com//",
2109 elem: []string{"/go"},
2110 out: "https://go.googlesource.com/go",
2113 base: "https://go.googlesource.com//",
2114 elem: []string{"/go", "a", "b", "c"},
2115 out: "https://go.googlesource.com/go/a/b/c",
2118 base: "http://[fe80::1%en0]:8080/",
2119 elem: []string{"/go"},
2122 base: "https://go.googlesource.com",
2123 elem: []string{"go/"},
2124 out: "https://go.googlesource.com/go/",
2127 base: "https://go.googlesource.com",
2128 elem: []string{"go//"},
2129 out: "https://go.googlesource.com/go/",
2132 base: "https://go.googlesource.com",
2134 out: "https://go.googlesource.com/",
2137 base: "https://go.googlesource.com/",
2139 out: "https://go.googlesource.com/",
2142 base: "https://go.googlesource.com/a%2fb",
2143 elem: []string{"c"},
2144 out: "https://go.googlesource.com/a%2fb/c",
2147 base: "https://go.googlesource.com/a%2fb",
2148 elem: []string{"c%2fd"},
2149 out: "https://go.googlesource.com/a%2fb/c%2fd",
2152 base: "https://go.googlesource.com/a/b",
2153 elem: []string{"/go"},
2154 out: "https://go.googlesource.com/a/b/go",
2168 elem: []string{"b"},
2173 elem: []string{"../b"},
2178 elem: []string{"../../b"},
2183 elem: []string{"a"},
2188 elem: []string{"../a"},
2192 for _, tt := range tests {
2195 wantErr = "non-nil error"
2197 if out, err := JoinPath(tt.base, tt.elem...); out != tt.out || (err == nil) != (tt.out != "") {
2198 t.Errorf("JoinPath(%q, %q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)
2201 u, err := Parse(tt.base)
2203 u = u.JoinPath(tt.elem...)
2206 if out != tt.out || (err == nil) != (tt.out != "") {
2207 t.Errorf("Parse(%q).JoinPath(%q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)