Source file
src/net/url/url_test.go
1
2
3
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 "strconv"
17 "strings"
18 "testing"
19 )
20
21 type URLTest struct {
22 in string
23 out *URL
24 roundtrip string
25 }
26
27 var urltests = []URLTest{
28
29 {
30 "http://www.google.com",
31 &URL{
32 Scheme: "http",
33 Host: "www.google.com",
34 },
35 "",
36 },
37
38 {
39 "http://www.google.com/",
40 &URL{
41 Scheme: "http",
42 Host: "www.google.com",
43 Path: "/",
44 },
45 "",
46 },
47
48 {
49 "http://www.google.com/file%20one%26two",
50 &URL{
51 Scheme: "http",
52 Host: "www.google.com",
53 Path: "/file one&two",
54 RawPath: "/file%20one%26two",
55 },
56 "",
57 },
58
59 {
60 "http://www.google.com/#file%20one%26two",
61 &URL{
62 Scheme: "http",
63 Host: "www.google.com",
64 Path: "/",
65 Fragment: "file one&two",
66 RawFragment: "file%20one%26two",
67 },
68 "",
69 },
70
71 {
72 "ftp://webmaster@www.google.com/",
73 &URL{
74 Scheme: "ftp",
75 User: User("webmaster"),
76 Host: "www.google.com",
77 Path: "/",
78 },
79 "",
80 },
81
82 {
83 "ftp://john%20doe@www.google.com/",
84 &URL{
85 Scheme: "ftp",
86 User: User("john doe"),
87 Host: "www.google.com",
88 Path: "/",
89 },
90 "ftp://john%20doe@www.google.com/",
91 },
92
93 {
94 "http://www.google.com/?",
95 &URL{
96 Scheme: "http",
97 Host: "www.google.com",
98 Path: "/",
99 ForceQuery: true,
100 },
101 "",
102 },
103
104 {
105 "http://www.google.com/?foo=bar?",
106 &URL{
107 Scheme: "http",
108 Host: "www.google.com",
109 Path: "/",
110 RawQuery: "foo=bar?",
111 },
112 "",
113 },
114
115 {
116 "http://www.google.com/?q=go+language",
117 &URL{
118 Scheme: "http",
119 Host: "www.google.com",
120 Path: "/",
121 RawQuery: "q=go+language",
122 },
123 "",
124 },
125
126 {
127 "http://www.google.com/?q=go%20language",
128 &URL{
129 Scheme: "http",
130 Host: "www.google.com",
131 Path: "/",
132 RawQuery: "q=go%20language",
133 },
134 "",
135 },
136
137 {
138 "http://www.google.com/a%20b?q=c+d",
139 &URL{
140 Scheme: "http",
141 Host: "www.google.com",
142 Path: "/a b",
143 RawQuery: "q=c+d",
144 },
145 "",
146 },
147
148 {
149 "http:www.google.com/?q=go+language",
150 &URL{
151 Scheme: "http",
152 Opaque: "www.google.com/",
153 RawQuery: "q=go+language",
154 },
155 "http:www.google.com/?q=go+language",
156 },
157
158 {
159 "http:%2f%2fwww.google.com/?q=go+language",
160 &URL{
161 Scheme: "http",
162 Opaque: "%2f%2fwww.google.com/",
163 RawQuery: "q=go+language",
164 },
165 "http:%2f%2fwww.google.com/?q=go+language",
166 },
167
168 {
169 "mailto:/webmaster@golang.org",
170 &URL{
171 Scheme: "mailto",
172 Path: "/webmaster@golang.org",
173 OmitHost: true,
174 },
175 "",
176 },
177
178 {
179 "mailto:webmaster@golang.org",
180 &URL{
181 Scheme: "mailto",
182 Opaque: "webmaster@golang.org",
183 },
184 "",
185 },
186
187 {
188 "/foo?query=http://bad",
189 &URL{
190 Path: "/foo",
191 RawQuery: "query=http://bad",
192 },
193 "",
194 },
195
196 {
197 "//foo",
198 &URL{
199 Host: "foo",
200 },
201 "",
202 },
203
204 {
205 "//user@foo/path?a=b",
206 &URL{
207 User: User("user"),
208 Host: "foo",
209 Path: "/path",
210 RawQuery: "a=b",
211 },
212 "",
213 },
214
215
216
217
218
219 {
220 "///threeslashes",
221 &URL{
222 Path: "///threeslashes",
223 },
224 "",
225 },
226 {
227 "http://user:password@google.com",
228 &URL{
229 Scheme: "http",
230 User: UserPassword("user", "password"),
231 Host: "google.com",
232 },
233 "http://user:password@google.com",
234 },
235
236 {
237 "http://j@ne:password@google.com",
238 &URL{
239 Scheme: "http",
240 User: UserPassword("j@ne", "password"),
241 Host: "google.com",
242 },
243 "http://j%40ne:password@google.com",
244 },
245
246 {
247 "http://jane:p@ssword@google.com",
248 &URL{
249 Scheme: "http",
250 User: UserPassword("jane", "p@ssword"),
251 Host: "google.com",
252 },
253 "http://jane:p%40ssword@google.com",
254 },
255 {
256 "http://j@ne:password@google.com/p@th?q=@go",
257 &URL{
258 Scheme: "http",
259 User: UserPassword("j@ne", "password"),
260 Host: "google.com",
261 Path: "/p@th",
262 RawQuery: "q=@go",
263 },
264 "http://j%40ne:password@google.com/p@th?q=@go",
265 },
266 {
267 "http://www.google.com/?q=go+language#foo",
268 &URL{
269 Scheme: "http",
270 Host: "www.google.com",
271 Path: "/",
272 RawQuery: "q=go+language",
273 Fragment: "foo",
274 },
275 "",
276 },
277 {
278 "http://www.google.com/?q=go+language#foo&bar",
279 &URL{
280 Scheme: "http",
281 Host: "www.google.com",
282 Path: "/",
283 RawQuery: "q=go+language",
284 Fragment: "foo&bar",
285 },
286 "http://www.google.com/?q=go+language#foo&bar",
287 },
288 {
289 "http://www.google.com/?q=go+language#foo%26bar",
290 &URL{
291 Scheme: "http",
292 Host: "www.google.com",
293 Path: "/",
294 RawQuery: "q=go+language",
295 Fragment: "foo&bar",
296 RawFragment: "foo%26bar",
297 },
298 "http://www.google.com/?q=go+language#foo%26bar",
299 },
300 {
301 "file:///home/adg/rabbits",
302 &URL{
303 Scheme: "file",
304 Host: "",
305 Path: "/home/adg/rabbits",
306 },
307 "file:///home/adg/rabbits",
308 },
309
310
311 {
312 "file:///C:/FooBar/Baz.txt",
313 &URL{
314 Scheme: "file",
315 Host: "",
316 Path: "/C:/FooBar/Baz.txt",
317 },
318 "file:///C:/FooBar/Baz.txt",
319 },
320
321 {
322 "MaIlTo:webmaster@golang.org",
323 &URL{
324 Scheme: "mailto",
325 Opaque: "webmaster@golang.org",
326 },
327 "mailto:webmaster@golang.org",
328 },
329
330 {
331 "a/b/c",
332 &URL{
333 Path: "a/b/c",
334 },
335 "a/b/c",
336 },
337
338 {
339 "http://%3Fam:pa%3Fsword@google.com",
340 &URL{
341 Scheme: "http",
342 User: UserPassword("?am", "pa?sword"),
343 Host: "google.com",
344 },
345 "",
346 },
347
348 {
349 "http://192.168.0.1/",
350 &URL{
351 Scheme: "http",
352 Host: "192.168.0.1",
353 Path: "/",
354 },
355 "",
356 },
357
358 {
359 "http://192.168.0.1:8080/",
360 &URL{
361 Scheme: "http",
362 Host: "192.168.0.1:8080",
363 Path: "/",
364 },
365 "",
366 },
367
368 {
369 "http://[fe80::1]/",
370 &URL{
371 Scheme: "http",
372 Host: "[fe80::1]",
373 Path: "/",
374 },
375 "",
376 },
377
378 {
379 "http://[fe80::1]:8080/",
380 &URL{
381 Scheme: "http",
382 Host: "[fe80::1]:8080",
383 Path: "/",
384 },
385 "",
386 },
387
388 {
389 "https://[2001:db8::1]:8443/test/path",
390 &URL{
391 Scheme: "https",
392 Host: "[2001:db8::1]:8443",
393 Path: "/test/path",
394 },
395 "",
396 },
397
398 {
399 "http://[fe80::1%25en0]/",
400 &URL{
401 Scheme: "http",
402 Host: "[fe80::1%en0]",
403 Path: "/",
404 },
405 "",
406 },
407
408 {
409 "http://[fe80::1%25en0]:8080/",
410 &URL{
411 Scheme: "http",
412 Host: "[fe80::1%en0]:8080",
413 Path: "/",
414 },
415 "",
416 },
417
418 {
419 "http://[fe80::1%25%65%6e%301-._~]/",
420 &URL{
421 Scheme: "http",
422 Host: "[fe80::1%en01-._~]",
423 Path: "/",
424 },
425 "http://[fe80::1%25en01-._~]/",
426 },
427
428 {
429 "http://[fe80::1%25%65%6e%301-._~]:8080/",
430 &URL{
431 Scheme: "http",
432 Host: "[fe80::1%en01-._~]:8080",
433 Path: "/",
434 },
435 "http://[fe80::1%25en01-._~]:8080/",
436 },
437
438 {
439 "http://rest.rsc.io/foo%2fbar/baz%2Fquux?alt=media",
440 &URL{
441 Scheme: "http",
442 Host: "rest.rsc.io",
443 Path: "/foo/bar/baz/quux",
444 RawPath: "/foo%2fbar/baz%2Fquux",
445 RawQuery: "alt=media",
446 },
447 "",
448 },
449
450 {
451 "mysql://a,b,c/bar",
452 &URL{
453 Scheme: "mysql",
454 Host: "a,b,c",
455 Path: "/bar",
456 },
457 "",
458 },
459
460 {
461 "scheme://!$&'()*+,;=hello!:1/path",
462 &URL{
463 Scheme: "scheme",
464 Host: "!$&'()*+,;=hello!:1",
465 Path: "/path",
466 },
467 "",
468 },
469
470 {
471 "http://host/!$&'()*+,;=:@[hello]",
472 &URL{
473 Scheme: "http",
474 Host: "host",
475 Path: "/!$&'()*+,;=:@[hello]",
476 RawPath: "/!$&'()*+,;=:@[hello]",
477 },
478 "",
479 },
480
481 {
482 "http://example.com/oid/[order_id]",
483 &URL{
484 Scheme: "http",
485 Host: "example.com",
486 Path: "/oid/[order_id]",
487 RawPath: "/oid/[order_id]",
488 },
489 "",
490 },
491
492 {
493 "http://192.168.0.2:8080/foo",
494 &URL{
495 Scheme: "http",
496 Host: "192.168.0.2:8080",
497 Path: "/foo",
498 },
499 "",
500 },
501 {
502 "http://192.168.0.2:/foo",
503 &URL{
504 Scheme: "http",
505 Host: "192.168.0.2:",
506 Path: "/foo",
507 },
508 "",
509 },
510 {
511 "http://[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:8080/foo",
512 &URL{
513 Scheme: "http",
514 Host: "[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:8080",
515 Path: "/foo",
516 },
517 "",
518 },
519 {
520 "http://[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:/foo",
521 &URL{
522 Scheme: "http",
523 Host: "[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:",
524 Path: "/foo",
525 },
526 "",
527 },
528
529 {
530 "http://hello.世界.com/foo",
531 &URL{
532 Scheme: "http",
533 Host: "hello.世界.com",
534 Path: "/foo",
535 },
536 "http://hello.%E4%B8%96%E7%95%8C.com/foo",
537 },
538 {
539 "http://hello.%e4%b8%96%e7%95%8c.com/foo",
540 &URL{
541 Scheme: "http",
542 Host: "hello.世界.com",
543 Path: "/foo",
544 },
545 "http://hello.%E4%B8%96%E7%95%8C.com/foo",
546 },
547 {
548 "http://hello.%E4%B8%96%E7%95%8C.com/foo",
549 &URL{
550 Scheme: "http",
551 Host: "hello.世界.com",
552 Path: "/foo",
553 },
554 "",
555 },
556
557 {
558 "http://example.com//foo",
559 &URL{
560 Scheme: "http",
561 Host: "example.com",
562 Path: "//foo",
563 },
564 "",
565 },
566
567 {
568 "myscheme://authority<\"hi\">/foo",
569 &URL{
570 Scheme: "myscheme",
571 Host: "authority<\"hi\">",
572 Path: "/foo",
573 },
574 "",
575 },
576
577
578
579 {
580 "tcp://[2020::2020:20:2020:2020%25Windows%20Loves%20Spaces]:2020",
581 &URL{
582 Scheme: "tcp",
583 Host: "[2020::2020:20:2020:2020%Windows Loves Spaces]:2020",
584 },
585 "",
586 },
587
588
589 {
590 "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
591 &URL{
592 Scheme: "magnet",
593 Host: "",
594 Path: "",
595 RawQuery: "xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
596 },
597 "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
598 },
599 {
600 "mailto:?subject=hi",
601 &URL{
602 Scheme: "mailto",
603 Host: "",
604 Path: "",
605 RawQuery: "subject=hi",
606 },
607 "mailto:?subject=hi",
608 },
609
610
611 {
612 "postgres://host1:1,host2:2,host3:3",
613 &URL{
614 Scheme: "postgres",
615 Host: "host1:1,host2:2,host3:3",
616 Path: "",
617 },
618 "postgres://host1:1,host2:2,host3:3",
619 },
620 {
621 "postgresql://host1:1,host2:2,host3:3",
622 &URL{
623 Scheme: "postgresql",
624 Host: "host1:1,host2:2,host3:3",
625 Path: "",
626 },
627 "postgresql://host1:1,host2:2,host3:3",
628 },
629 }
630
631
632 func ufmt(u *URL) string {
633 var user, pass any
634 if u.User != nil {
635 user = u.User.Username()
636 if p, ok := u.User.Password(); ok {
637 pass = p
638 }
639 }
640 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",
641 u.Opaque, u.Scheme, user, pass, u.Host, u.Path, u.RawPath, u.RawQuery, u.Fragment, u.RawFragment, u.ForceQuery, u.OmitHost)
642 }
643
644 func BenchmarkString(b *testing.B) {
645 b.StopTimer()
646 b.ReportAllocs()
647 for _, tt := range urltests {
648 u, err := Parse(tt.in)
649 if err != nil {
650 b.Errorf("Parse(%q) returned error %s", tt.in, err)
651 continue
652 }
653 if tt.roundtrip == "" {
654 continue
655 }
656 b.StartTimer()
657 var g string
658 for i := 0; i < b.N; i++ {
659 g = u.String()
660 }
661 b.StopTimer()
662 if w := tt.roundtrip; b.N > 0 && g != w {
663 b.Errorf("Parse(%q).String() == %q, want %q", tt.in, g, w)
664 }
665 }
666 }
667
668 func TestParse(t *testing.T) {
669 for _, tt := range urltests {
670 u, err := Parse(tt.in)
671 if err != nil {
672 t.Errorf("Parse(%q) returned error %v", tt.in, err)
673 continue
674 }
675 if !reflect.DeepEqual(u, tt.out) {
676 t.Errorf("Parse(%q):\n\tgot %v\n\twant %v\n", tt.in, ufmt(u), ufmt(tt.out))
677 }
678 }
679 }
680
681 const pathThatLooksSchemeRelative = "//not.a.user@not.a.host/just/a/path"
682
683 var parseRequestURLTests = []struct {
684 url string
685 expectedValid bool
686 }{
687 {"http://foo.com", true},
688 {"http://foo.com/", true},
689 {"http://foo.com/path", true},
690 {"/", true},
691 {pathThatLooksSchemeRelative, true},
692 {"//not.a.user@%66%6f%6f.com/just/a/path/also", true},
693 {"*", true},
694 {"http://192.168.0.1/", true},
695 {"http://192.168.0.1:8080/", true},
696 {"http://[fe80::1]/", true},
697 {"http://[fe80::1]:8080/", true},
698
699
700 {"http://[fe80::1%25en0]/", true},
701 {"http://[fe80::1%25en0]:8080/", true},
702 {"http://[fe80::1%25%65%6e%301-._~]/", true},
703 {"http://[fe80::1%25%65%6e%301-._~]:8080/", true},
704
705 {"foo.html", false},
706 {"../dir/", false},
707 {" http://foo.com", false},
708 {"http://192.168.0.%31/", false},
709 {"http://192.168.0.%31:8080/", false},
710 {"http://[fe80::%31]/", false},
711 {"http://[fe80::%31]:8080/", false},
712 {"http://[fe80::%31%25en0]/", false},
713 {"http://[fe80::%31%25en0]:8080/", false},
714
715
716
717
718
719 {"http://[fe80::1%en0]/", false},
720 {"http://[fe80::1%en0]:8080/", false},
721
722
723 {"https://[1:2:3:4:5:6:7:8]", true},
724 {"https://[2001:db8::a:b:c:d]", true},
725 {"https://[fe80::1%25eth0]", true},
726 {"https://[fe80::abc:def%254]", true},
727 {"https://[2001:db8::1]/path", true},
728 {"https://[fe80::1%25eth0]/path?query=1", true},
729
730 {"https://[::ffff:192.0.2.1]", true},
731 {"https://[:1] ", false},
732 {"https://[1:2:3:4:5:6:7:8:9]", false},
733 {"https://[1::1::1]", false},
734 {"https://[1:2:3:]", false},
735 {"https://[ffff::127.0.0.4000]", false},
736 {"https://[0:0::test.com]:80", false},
737 {"https://[2001:db8::test.com]", false},
738 {"https://[test.com]", false},
739 {"https://1:2:3:4:5:6:7:8", false},
740 {"https://1:2:3:4:5:6:7:8:80", false},
741 {"https://example.com:80:", false},
742 }
743
744 func TestParseRequestURI(t *testing.T) {
745 for _, test := range parseRequestURLTests {
746 _, err := ParseRequestURI(test.url)
747 if test.expectedValid && err != nil {
748 t.Errorf("ParseRequestURI(%q) gave err %v; want no error", test.url, err)
749 } else if !test.expectedValid && err == nil {
750 t.Errorf("ParseRequestURI(%q) gave nil error; want some error", test.url)
751 }
752 }
753
754 url, err := ParseRequestURI(pathThatLooksSchemeRelative)
755 if err != nil {
756 t.Fatalf("Unexpected error %v", err)
757 }
758 if url.Path != pathThatLooksSchemeRelative {
759 t.Errorf("ParseRequestURI path:\ngot %q\nwant %q", url.Path, pathThatLooksSchemeRelative)
760 }
761 }
762
763 var stringURLTests = []struct {
764 url URL
765 want string
766 }{
767
768 {
769 url: URL{
770 Scheme: "http",
771 Host: "www.google.com",
772 Path: "search",
773 },
774 want: "http://www.google.com/search",
775 },
776
777 {
778 url: URL{
779 Path: "this:that",
780 },
781 want: "./this:that",
782 },
783
784 {
785 url: URL{
786 Path: "here/this:that",
787 },
788 want: "here/this:that",
789 },
790
791 {
792 url: URL{
793 Scheme: "http",
794 Host: "www.google.com",
795 Path: "this:that",
796 },
797 want: "http://www.google.com/this:that",
798 },
799 }
800
801 func TestURLString(t *testing.T) {
802 for _, tt := range urltests {
803 u, err := Parse(tt.in)
804 if err != nil {
805 t.Errorf("Parse(%q) returned error %s", tt.in, err)
806 continue
807 }
808 expected := tt.in
809 if tt.roundtrip != "" {
810 expected = tt.roundtrip
811 }
812 s := u.String()
813 if s != expected {
814 t.Errorf("Parse(%q).String() == %q (expected %q)", tt.in, s, expected)
815 }
816 }
817
818 for _, tt := range stringURLTests {
819 if got := tt.url.String(); got != tt.want {
820 t.Errorf("%+v.String() = %q; want %q", tt.url, got, tt.want)
821 }
822 }
823 }
824
825 func TestURLRedacted(t *testing.T) {
826 cases := []struct {
827 name string
828 url *URL
829 want string
830 }{
831 {
832 name: "non-blank Password",
833 url: &URL{
834 Scheme: "http",
835 Host: "host.tld",
836 Path: "this:that",
837 User: UserPassword("user", "password"),
838 },
839 want: "http://user:xxxxx@host.tld/this:that",
840 },
841 {
842 name: "blank Password",
843 url: &URL{
844 Scheme: "http",
845 Host: "host.tld",
846 Path: "this:that",
847 User: User("user"),
848 },
849 want: "http://user@host.tld/this:that",
850 },
851 {
852 name: "nil User",
853 url: &URL{
854 Scheme: "http",
855 Host: "host.tld",
856 Path: "this:that",
857 User: UserPassword("", "password"),
858 },
859 want: "http://:xxxxx@host.tld/this:that",
860 },
861 {
862 name: "blank Username, blank Password",
863 url: &URL{
864 Scheme: "http",
865 Host: "host.tld",
866 Path: "this:that",
867 },
868 want: "http://host.tld/this:that",
869 },
870 {
871 name: "empty URL",
872 url: &URL{},
873 want: "",
874 },
875 {
876 name: "nil URL",
877 url: nil,
878 want: "",
879 },
880 }
881
882 for _, tt := range cases {
883 t.Run(tt.name, func(t *testing.T) {
884 if g, w := tt.url.Redacted(), tt.want; g != w {
885 t.Fatalf("got: %q\nwant: %q", g, w)
886 }
887 })
888 }
889 }
890
891 type EscapeTest struct {
892 in string
893 out string
894 err error
895 }
896
897 var unescapeTests = []EscapeTest{
898 {
899 "",
900 "",
901 nil,
902 },
903 {
904 "abc",
905 "abc",
906 nil,
907 },
908 {
909 "1%41",
910 "1A",
911 nil,
912 },
913 {
914 "1%41%42%43",
915 "1ABC",
916 nil,
917 },
918 {
919 "%4a",
920 "J",
921 nil,
922 },
923 {
924 "%6F",
925 "o",
926 nil,
927 },
928 {
929 "%",
930 "",
931 EscapeError("%"),
932 },
933 {
934 "%a",
935 "",
936 EscapeError("%a"),
937 },
938 {
939 "%1",
940 "",
941 EscapeError("%1"),
942 },
943 {
944 "123%45%6",
945 "",
946 EscapeError("%6"),
947 },
948 {
949 "%zzzzz",
950 "",
951 EscapeError("%zz"),
952 },
953 {
954 "a+b",
955 "a b",
956 nil,
957 },
958 {
959 "a%20b",
960 "a b",
961 nil,
962 },
963 }
964
965 func TestUnescape(t *testing.T) {
966 for _, tt := range unescapeTests {
967 actual, err := QueryUnescape(tt.in)
968 if actual != tt.out || (err != nil) != (tt.err != nil) {
969 t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", tt.in, actual, err, tt.out, tt.err)
970 }
971
972 in := tt.in
973 out := tt.out
974 if strings.Contains(tt.in, "+") {
975 in = strings.ReplaceAll(tt.in, "+", "%20")
976 actual, err := PathUnescape(in)
977 if actual != tt.out || (err != nil) != (tt.err != nil) {
978 t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", in, actual, err, tt.out, tt.err)
979 }
980 if tt.err == nil {
981 s, err := QueryUnescape(strings.ReplaceAll(tt.in, "+", "XXX"))
982 if err != nil {
983 continue
984 }
985 in = tt.in
986 out = strings.ReplaceAll(s, "XXX", "+")
987 }
988 }
989
990 actual, err = PathUnescape(in)
991 if actual != out || (err != nil) != (tt.err != nil) {
992 t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", in, actual, err, out, tt.err)
993 }
994 }
995 }
996
997 var queryEscapeTests = []EscapeTest{
998 {
999 "",
1000 "",
1001 nil,
1002 },
1003 {
1004 "abc",
1005 "abc",
1006 nil,
1007 },
1008 {
1009 "one two",
1010 "one+two",
1011 nil,
1012 },
1013 {
1014 "10%",
1015 "10%25",
1016 nil,
1017 },
1018 {
1019 " ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
1020 "+%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",
1021 nil,
1022 },
1023 }
1024
1025 func TestQueryEscape(t *testing.T) {
1026 for _, tt := range queryEscapeTests {
1027 actual := QueryEscape(tt.in)
1028 if tt.out != actual {
1029 t.Errorf("QueryEscape(%q) = %q, want %q", tt.in, actual, tt.out)
1030 }
1031
1032
1033 roundtrip, err := QueryUnescape(actual)
1034 if roundtrip != tt.in || err != nil {
1035 t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]")
1036 }
1037 }
1038 }
1039
1040 var pathEscapeTests = []EscapeTest{
1041 {
1042 "",
1043 "",
1044 nil,
1045 },
1046 {
1047 "abc",
1048 "abc",
1049 nil,
1050 },
1051 {
1052 "abc+def",
1053 "abc+def",
1054 nil,
1055 },
1056 {
1057 "a/b",
1058 "a%2Fb",
1059 nil,
1060 },
1061 {
1062 "one two",
1063 "one%20two",
1064 nil,
1065 },
1066 {
1067 "10%",
1068 "10%25",
1069 nil,
1070 },
1071 {
1072 " ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
1073 "%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",
1074 nil,
1075 },
1076 }
1077
1078 func TestPathEscape(t *testing.T) {
1079 for _, tt := range pathEscapeTests {
1080 actual := PathEscape(tt.in)
1081 if tt.out != actual {
1082 t.Errorf("PathEscape(%q) = %q, want %q", tt.in, actual, tt.out)
1083 }
1084
1085
1086 roundtrip, err := PathUnescape(actual)
1087 if roundtrip != tt.in || err != nil {
1088 t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]")
1089 }
1090 }
1091 }
1092
1093
1094
1095
1096
1097
1098
1099 type EncodeQueryTest struct {
1100 m Values
1101 expected string
1102 }
1103
1104 var encodeQueryTests = []EncodeQueryTest{
1105 {nil, ""},
1106 {Values{}, ""},
1107 {Values{"q": {"puppies"}, "oe": {"utf8"}}, "oe=utf8&q=puppies"},
1108 {Values{"q": {"dogs", "&", "7"}}, "q=dogs&q=%26&q=7"},
1109 {Values{
1110 "a": {"a1", "a2", "a3"},
1111 "b": {"b1", "b2", "b3"},
1112 "c": {"c1", "c2", "c3"},
1113 }, "a=a1&a=a2&a=a3&b=b1&b=b2&b=b3&c=c1&c=c2&c=c3"},
1114 {Values{
1115 "a": {"a"},
1116 "b": {"b"},
1117 "c": {"c"},
1118 "d": {"d"},
1119 "e": {"e"},
1120 "f": {"f"},
1121 "g": {"g"},
1122 "h": {"h"},
1123 "i": {"i"},
1124 }, "a=a&b=b&c=c&d=d&e=e&f=f&g=g&h=h&i=i"},
1125 }
1126
1127 func TestEncodeQuery(t *testing.T) {
1128 for _, tt := range encodeQueryTests {
1129 if q := tt.m.Encode(); q != tt.expected {
1130 t.Errorf(`EncodeQuery(%+v) = %q, want %q`, tt.m, q, tt.expected)
1131 }
1132 }
1133 }
1134
1135 func BenchmarkEncodeQuery(b *testing.B) {
1136 for _, tt := range encodeQueryTests {
1137 b.Run(tt.expected, func(b *testing.B) {
1138 b.ReportAllocs()
1139 for b.Loop() {
1140 tt.m.Encode()
1141 }
1142 })
1143 }
1144 }
1145
1146 var resolvePathTests = []struct {
1147 base, ref, expected string
1148 }{
1149 {"a/b", ".", "/a/"},
1150 {"a/b", "c", "/a/c"},
1151 {"a/b", "..", "/"},
1152 {"a/", "..", "/"},
1153 {"a/", "../..", "/"},
1154 {"a/b/c", "..", "/a/"},
1155 {"a/b/c", "../d", "/a/d"},
1156 {"a/b/c", ".././d", "/a/d"},
1157 {"a/b", "./..", "/"},
1158 {"a/./b", ".", "/a/"},
1159 {"a/../", ".", "/"},
1160 {"a/.././b", "c", "/c"},
1161 }
1162
1163 func TestResolvePath(t *testing.T) {
1164 for _, test := range resolvePathTests {
1165 got := resolvePath(test.base, test.ref)
1166 if got != test.expected {
1167 t.Errorf("For %q + %q got %q; expected %q", test.base, test.ref, got, test.expected)
1168 }
1169 }
1170 }
1171
1172 func BenchmarkResolvePath(b *testing.B) {
1173 b.ReportAllocs()
1174 for i := 0; i < b.N; i++ {
1175 resolvePath("a/b/c", ".././d")
1176 }
1177 }
1178
1179 var resolveReferenceTests = []struct {
1180 base, rel, expected string
1181 }{
1182
1183 {"http://foo.com?a=b", "https://bar.com/", "https://bar.com/"},
1184 {"http://foo.com/", "https://bar.com/?a=b", "https://bar.com/?a=b"},
1185 {"http://foo.com/", "https://bar.com/?", "https://bar.com/?"},
1186 {"http://foo.com/bar", "mailto:foo@example.com", "mailto:foo@example.com"},
1187
1188
1189 {"http://foo.com/bar", "/baz", "http://foo.com/baz"},
1190 {"http://foo.com/bar?a=b#f", "/baz", "http://foo.com/baz"},
1191 {"http://foo.com/bar?a=b", "/baz?", "http://foo.com/baz?"},
1192 {"http://foo.com/bar?a=b", "/baz?c=d", "http://foo.com/baz?c=d"},
1193
1194
1195 {"http://foo.com/bar", "http://foo.com//baz", "http://foo.com//baz"},
1196 {"http://foo.com/bar", "http://foo.com///baz/quux", "http://foo.com///baz/quux"},
1197
1198
1199 {"https://foo.com/bar?a=b", "//bar.com/quux", "https://bar.com/quux"},
1200
1201
1202
1203
1204 {"http://foo.com", ".", "http://foo.com/"},
1205 {"http://foo.com/bar", ".", "http://foo.com/"},
1206 {"http://foo.com/bar/", ".", "http://foo.com/bar/"},
1207
1208
1209 {"http://foo.com", "bar", "http://foo.com/bar"},
1210 {"http://foo.com/", "bar", "http://foo.com/bar"},
1211 {"http://foo.com/bar/baz", "quux", "http://foo.com/bar/quux"},
1212
1213
1214 {"http://foo.com/bar/baz", "../quux", "http://foo.com/quux"},
1215 {"http://foo.com/bar/baz", "../../../../../quux", "http://foo.com/quux"},
1216 {"http://foo.com/bar", "..", "http://foo.com/"},
1217 {"http://foo.com/bar/baz", "./..", "http://foo.com/"},
1218
1219 {"http://foo.com/bar/baz", "quux/dotdot/../tail", "http://foo.com/bar/quux/tail"},
1220 {"http://foo.com/bar/baz", "quux/./dotdot/../tail", "http://foo.com/bar/quux/tail"},
1221 {"http://foo.com/bar/baz", "quux/./dotdot/.././tail", "http://foo.com/bar/quux/tail"},
1222 {"http://foo.com/bar/baz", "quux/./dotdot/./../tail", "http://foo.com/bar/quux/tail"},
1223 {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/././../../tail", "http://foo.com/bar/quux/tail"},
1224 {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/./.././../tail", "http://foo.com/bar/quux/tail"},
1225 {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/dotdot/./../../.././././tail", "http://foo.com/bar/quux/tail"},
1226 {"http://foo.com/bar/baz", "quux/./dotdot/../dotdot/../dot/./tail/..", "http://foo.com/bar/quux/dot/"},
1227
1228
1229
1230 {"http://foo.com/dot/./dotdot/../foo/bar", "../baz", "http://foo.com/dot/baz"},
1231
1232
1233 {"http://foo.com/bar", "...", "http://foo.com/..."},
1234
1235
1236 {"http://foo.com/bar", ".#frag", "http://foo.com/#frag"},
1237 {"http://example.org/", "#!$&%27()*+,;=", "http://example.org/#!$&%27()*+,;="},
1238
1239
1240 {"http://foo.com/foo%2fbar/", "../baz", "http://foo.com/baz"},
1241 {"http://foo.com/1/2%2f/3%2f4/5", "../../a/b/c", "http://foo.com/1/a/b/c"},
1242 {"http://foo.com/1/2/3", "./a%2f../../b/..%2fc", "http://foo.com/1/2/b/..%2fc"},
1243 {"http://foo.com/1/2%2f/3%2f4/5", "./a%2f../b/../c", "http://foo.com/1/2%2f/3%2f4/a%2f../c"},
1244 {"http://foo.com/foo%20bar/", "../baz", "http://foo.com/baz"},
1245 {"http://foo.com/foo", "../bar%2fbaz", "http://foo.com/bar%2fbaz"},
1246 {"http://foo.com/foo%2dbar/", "./baz-quux", "http://foo.com/foo%2dbar/baz-quux"},
1247
1248
1249
1250 {"http://a/b/c/d;p?q", "g:h", "g:h"},
1251 {"http://a/b/c/d;p?q", "g", "http://a/b/c/g"},
1252 {"http://a/b/c/d;p?q", "./g", "http://a/b/c/g"},
1253 {"http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"},
1254 {"http://a/b/c/d;p?q", "/g", "http://a/g"},
1255 {"http://a/b/c/d;p?q", "//g", "http://g"},
1256 {"http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y"},
1257 {"http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"},
1258 {"http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s"},
1259 {"http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"},
1260 {"http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"},
1261 {"http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"},
1262 {"http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"},
1263 {"http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"},
1264 {"http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q"},
1265 {"http://a/b/c/d;p?q", ".", "http://a/b/c/"},
1266 {"http://a/b/c/d;p?q", "./", "http://a/b/c/"},
1267 {"http://a/b/c/d;p?q", "..", "http://a/b/"},
1268 {"http://a/b/c/d;p?q", "../", "http://a/b/"},
1269 {"http://a/b/c/d;p?q", "../g", "http://a/b/g"},
1270 {"http://a/b/c/d;p?q", "../..", "http://a/"},
1271 {"http://a/b/c/d;p?q", "../../", "http://a/"},
1272 {"http://a/b/c/d;p?q", "../../g", "http://a/g"},
1273
1274
1275
1276 {"http://a/b/c/d;p?q", "../../../g", "http://a/g"},
1277 {"http://a/b/c/d;p?q", "../../../../g", "http://a/g"},
1278 {"http://a/b/c/d;p?q", "/./g", "http://a/g"},
1279 {"http://a/b/c/d;p?q", "/../g", "http://a/g"},
1280 {"http://a/b/c/d;p?q", "g.", "http://a/b/c/g."},
1281 {"http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"},
1282 {"http://a/b/c/d;p?q", "g..", "http://a/b/c/g.."},
1283 {"http://a/b/c/d;p?q", "..g", "http://a/b/c/..g"},
1284 {"http://a/b/c/d;p?q", "./../g", "http://a/b/g"},
1285 {"http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"},
1286 {"http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"},
1287 {"http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h"},
1288 {"http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"},
1289 {"http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y"},
1290 {"http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x"},
1291 {"http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x"},
1292 {"http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x"},
1293 {"http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x"},
1294
1295
1296 {"https://a/b/c/d;p?q", "//g?q", "https://g?q"},
1297 {"https://a/b/c/d;p?q", "//g#s", "https://g#s"},
1298 {"https://a/b/c/d;p?q", "//g/d/e/f?y#s", "https://g/d/e/f?y#s"},
1299 {"https://a/b/c/d;p#s", "?y", "https://a/b/c/d;p?y"},
1300 {"https://a/b/c/d;p?q#s", "?y", "https://a/b/c/d;p?y"},
1301
1302
1303 {"https://a/b/c/d;p?q#s", "?", "https://a/b/c/d;p?"},
1304
1305
1306 {"https://foo.com/bar?a=b", "http:opaque", "http:opaque"},
1307 {"http:opaque?x=y#zzz", "https:/foo?a=b#frag", "https:/foo?a=b#frag"},
1308 {"http:opaque?x=y#zzz", "https:foo:bar", "https:foo:bar"},
1309 {"http:opaque?x=y#zzz", "https:bar/baz?a=b#frag", "https:bar/baz?a=b#frag"},
1310 {"http:opaque?x=y#zzz", "https://user@host:1234?a=b#frag", "https://user@host:1234?a=b#frag"},
1311 {"http:opaque?x=y#zzz", "?a=b#frag", "http:opaque?a=b#frag"},
1312 }
1313
1314 func TestResolveReference(t *testing.T) {
1315 mustParse := func(url string) *URL {
1316 u, err := Parse(url)
1317 if err != nil {
1318 t.Fatalf("Parse(%q) got err %v", url, err)
1319 }
1320 return u
1321 }
1322 opaque := &URL{Scheme: "scheme", Opaque: "opaque"}
1323 for _, test := range resolveReferenceTests {
1324 base := mustParse(test.base)
1325 rel := mustParse(test.rel)
1326 url := base.ResolveReference(rel)
1327 if got := url.String(); got != test.expected {
1328 t.Errorf("URL(%q).ResolveReference(%q)\ngot %q\nwant %q", test.base, test.rel, got, test.expected)
1329 }
1330
1331 if base == url {
1332 t.Errorf("Expected URL.ResolveReference to return new URL instance.")
1333 }
1334
1335 url, err := base.Parse(test.rel)
1336 if err != nil {
1337 t.Errorf("URL(%q).Parse(%q) failed: %v", test.base, test.rel, err)
1338 } else if got := url.String(); got != test.expected {
1339 t.Errorf("URL(%q).Parse(%q)\ngot %q\nwant %q", test.base, test.rel, got, test.expected)
1340 } else if base == url {
1341
1342 t.Errorf("Expected URL.Parse to return new URL instance.")
1343 }
1344
1345 url = base.ResolveReference(opaque)
1346 if *url != *opaque {
1347 t.Errorf("ResolveReference failed to resolve opaque URL:\ngot %#v\nwant %#v", url, opaque)
1348 }
1349
1350 url, err = base.Parse("scheme:opaque")
1351 if err != nil {
1352 t.Errorf(`URL(%q).Parse("scheme:opaque") failed: %v`, test.base, err)
1353 } else if *url != *opaque {
1354 t.Errorf("Parse failed to resolve opaque URL:\ngot %#v\nwant %#v", opaque, url)
1355 } else if base == url {
1356
1357 t.Errorf("Expected URL.Parse to return new URL instance.")
1358 }
1359 }
1360 }
1361
1362 func TestQueryValues(t *testing.T) {
1363 u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2&baz")
1364 v := u.Query()
1365 if len(v) != 3 {
1366 t.Errorf("got %d keys in Query values, want 3", len(v))
1367 }
1368 if g, e := v.Get("foo"), "bar"; g != e {
1369 t.Errorf("Get(foo) = %q, want %q", g, e)
1370 }
1371
1372 if g, e := v.Get("Foo"), ""; g != e {
1373 t.Errorf("Get(Foo) = %q, want %q", g, e)
1374 }
1375 if g, e := v.Get("bar"), "1"; g != e {
1376 t.Errorf("Get(bar) = %q, want %q", g, e)
1377 }
1378 if g, e := v.Get("baz"), ""; g != e {
1379 t.Errorf("Get(baz) = %q, want %q", g, e)
1380 }
1381 if h, e := v.Has("foo"), true; h != e {
1382 t.Errorf("Has(foo) = %t, want %t", h, e)
1383 }
1384 if h, e := v.Has("bar"), true; h != e {
1385 t.Errorf("Has(bar) = %t, want %t", h, e)
1386 }
1387 if h, e := v.Has("baz"), true; h != e {
1388 t.Errorf("Has(baz) = %t, want %t", h, e)
1389 }
1390 if h, e := v.Has("noexist"), false; h != e {
1391 t.Errorf("Has(noexist) = %t, want %t", h, e)
1392 }
1393 v.Del("bar")
1394 if g, e := v.Get("bar"), ""; g != e {
1395 t.Errorf("second Get(bar) = %q, want %q", g, e)
1396 }
1397 }
1398
1399 type parseTest struct {
1400 query string
1401 out Values
1402 ok bool
1403 }
1404
1405 var parseTests = []parseTest{
1406 {
1407 query: "a=1",
1408 out: Values{"a": []string{"1"}},
1409 ok: true,
1410 },
1411 {
1412 query: "a=1&b=2",
1413 out: Values{"a": []string{"1"}, "b": []string{"2"}},
1414 ok: true,
1415 },
1416 {
1417 query: "a=1&a=2&a=banana",
1418 out: Values{"a": []string{"1", "2", "banana"}},
1419 ok: true,
1420 },
1421 {
1422 query: "ascii=%3Ckey%3A+0x90%3E",
1423 out: Values{"ascii": []string{"<key: 0x90>"}},
1424 ok: true,
1425 }, {
1426 query: "a=1;b=2",
1427 out: Values{},
1428 ok: false,
1429 }, {
1430 query: "a;b=1",
1431 out: Values{},
1432 ok: false,
1433 }, {
1434 query: "a=%3B",
1435 out: Values{"a": []string{";"}},
1436 ok: true,
1437 },
1438 {
1439 query: "a%3Bb=1",
1440 out: Values{"a;b": []string{"1"}},
1441 ok: true,
1442 },
1443 {
1444 query: "a=1&a=2;a=banana",
1445 out: Values{"a": []string{"1"}},
1446 ok: false,
1447 },
1448 {
1449 query: "a;b&c=1",
1450 out: Values{"c": []string{"1"}},
1451 ok: false,
1452 },
1453 {
1454 query: "a=1&b=2;a=3&c=4",
1455 out: Values{"a": []string{"1"}, "c": []string{"4"}},
1456 ok: false,
1457 },
1458 {
1459 query: "a=1&b=2;c=3",
1460 out: Values{"a": []string{"1"}},
1461 ok: false,
1462 },
1463 {
1464 query: ";",
1465 out: Values{},
1466 ok: false,
1467 },
1468 {
1469 query: "a=1;",
1470 out: Values{},
1471 ok: false,
1472 },
1473 {
1474 query: "a=1&;",
1475 out: Values{"a": []string{"1"}},
1476 ok: false,
1477 },
1478 {
1479 query: ";a=1&b=2",
1480 out: Values{"b": []string{"2"}},
1481 ok: false,
1482 },
1483 {
1484 query: "a=1&b=2;",
1485 out: Values{"a": []string{"1"}},
1486 ok: false,
1487 },
1488 }
1489
1490 func TestParseQuery(t *testing.T) {
1491 for _, test := range parseTests {
1492 t.Run(test.query, func(t *testing.T) {
1493 form, err := ParseQuery(test.query)
1494 if test.ok != (err == nil) {
1495 want := "<error>"
1496 if test.ok {
1497 want = "<nil>"
1498 }
1499 t.Errorf("Unexpected error: %v, want %v", err, want)
1500 }
1501 if len(form) != len(test.out) {
1502 t.Errorf("len(form) = %d, want %d", len(form), len(test.out))
1503 }
1504 for k, evs := range test.out {
1505 vs, ok := form[k]
1506 if !ok {
1507 t.Errorf("Missing key %q", k)
1508 continue
1509 }
1510 if len(vs) != len(evs) {
1511 t.Errorf("len(form[%q]) = %d, want %d", k, len(vs), len(evs))
1512 continue
1513 }
1514 for j, ev := range evs {
1515 if v := vs[j]; v != ev {
1516 t.Errorf("form[%q][%d] = %q, want %q", k, j, v, ev)
1517 }
1518 }
1519 }
1520 })
1521 }
1522 }
1523
1524 func TestParseQueryLimits(t *testing.T) {
1525 for _, test := range []struct {
1526 params int
1527 godebug string
1528 wantErr bool
1529 }{{
1530 params: 10,
1531 wantErr: false,
1532 }, {
1533 params: defaultMaxParams,
1534 wantErr: false,
1535 }, {
1536 params: defaultMaxParams + 1,
1537 wantErr: true,
1538 }, {
1539 params: 10,
1540 godebug: "urlmaxqueryparams=9",
1541 wantErr: true,
1542 }, {
1543 params: defaultMaxParams + 1,
1544 godebug: "urlmaxqueryparams=0",
1545 wantErr: false,
1546 }} {
1547 t.Setenv("GODEBUG", test.godebug)
1548 want := Values{}
1549 var b strings.Builder
1550 for i := range test.params {
1551 if i > 0 {
1552 b.WriteString("&")
1553 }
1554 p := fmt.Sprintf("p%v", i)
1555 b.WriteString(p)
1556 want[p] = []string{""}
1557 }
1558 query := b.String()
1559 got, err := ParseQuery(query)
1560 if gotErr, wantErr := err != nil, test.wantErr; gotErr != wantErr {
1561 t.Errorf("GODEBUG=%v ParseQuery(%v params) = %v, want error: %v", test.godebug, test.params, err, wantErr)
1562 }
1563 if err != nil {
1564 continue
1565 }
1566 if got, want := len(got), test.params; got != want {
1567 t.Errorf("GODEBUG=%v ParseQuery(%v params): got %v params, want %v", test.godebug, test.params, got, want)
1568 }
1569 }
1570 }
1571
1572 type RequestURITest struct {
1573 url *URL
1574 out string
1575 }
1576
1577 var requritests = []RequestURITest{
1578 {
1579 &URL{
1580 Scheme: "http",
1581 Host: "example.com",
1582 Path: "",
1583 },
1584 "/",
1585 },
1586 {
1587 &URL{
1588 Scheme: "http",
1589 Host: "example.com",
1590 Path: "/a b",
1591 },
1592 "/a%20b",
1593 },
1594
1595 {
1596 &URL{
1597 Scheme: "http",
1598 Host: "example.com",
1599 Opaque: "/%2F/%2F/",
1600 },
1601 "/%2F/%2F/",
1602 },
1603
1604 {
1605 &URL{
1606 Scheme: "http",
1607 Host: "example.com",
1608 Opaque: "//other.example.com/%2F/%2F/",
1609 },
1610 "http://other.example.com/%2F/%2F/",
1611 },
1612
1613 {
1614 &URL{
1615 Scheme: "http",
1616 Host: "example.com",
1617 Path: "/////",
1618 RawPath: "/%2F/%2F/",
1619 },
1620 "/%2F/%2F/",
1621 },
1622 {
1623 &URL{
1624 Scheme: "http",
1625 Host: "example.com",
1626 Path: "/////",
1627 RawPath: "/WRONG/",
1628 },
1629 "/////",
1630 },
1631 {
1632 &URL{
1633 Scheme: "http",
1634 Host: "example.com",
1635 Path: "/a b",
1636 RawQuery: "q=go+language",
1637 },
1638 "/a%20b?q=go+language",
1639 },
1640 {
1641 &URL{
1642 Scheme: "http",
1643 Host: "example.com",
1644 Path: "/a b",
1645 RawPath: "/a b",
1646 RawQuery: "q=go+language",
1647 },
1648 "/a%20b?q=go+language",
1649 },
1650 {
1651 &URL{
1652 Scheme: "http",
1653 Host: "example.com",
1654 Path: "/a?b",
1655 RawPath: "/a?b",
1656 RawQuery: "q=go+language",
1657 },
1658 "/a%3Fb?q=go+language",
1659 },
1660 {
1661 &URL{
1662 Scheme: "myschema",
1663 Opaque: "opaque",
1664 },
1665 "opaque",
1666 },
1667 {
1668 &URL{
1669 Scheme: "myschema",
1670 Opaque: "opaque",
1671 RawQuery: "q=go+language",
1672 },
1673 "opaque?q=go+language",
1674 },
1675 {
1676 &URL{
1677 Scheme: "http",
1678 Host: "example.com",
1679 Path: "//foo",
1680 },
1681 "//foo",
1682 },
1683 {
1684 &URL{
1685 Scheme: "http",
1686 Host: "example.com",
1687 Path: "/foo",
1688 ForceQuery: true,
1689 },
1690 "/foo?",
1691 },
1692 }
1693
1694 func TestRequestURI(t *testing.T) {
1695 for _, tt := range requritests {
1696 s := tt.url.RequestURI()
1697 if s != tt.out {
1698 t.Errorf("%#v.RequestURI() == %q (expected %q)", tt.url, s, tt.out)
1699 }
1700 }
1701 }
1702
1703 func TestParseFailure(t *testing.T) {
1704
1705 const url = "%gh&%ij"
1706 _, err := ParseQuery(url)
1707 errStr := fmt.Sprint(err)
1708 if !strings.Contains(errStr, "%gh") {
1709 t.Errorf(`ParseQuery(%q) returned error %q, want something containing %q"`, url, errStr, "%gh")
1710 }
1711 }
1712
1713 func TestParseErrors(t *testing.T) {
1714 tests := []struct {
1715 in string
1716 wantErr bool
1717 }{
1718 {"http://[::1]", false},
1719 {"http://[::1]:80", false},
1720 {"http://[::1]:namedport", true},
1721 {"http://x:namedport", true},
1722 {"http://[::1]/", false},
1723 {"http://[::1]a", true},
1724 {"http://[::1]%23", true},
1725 {"http://[::1%25en0]", false},
1726 {"http://[::1]:", false},
1727 {"http://x:", false},
1728 {"http://[::1]:%38%30", true},
1729 {"http://[::1%25%41]", false},
1730 {"http://[%10::1]", true},
1731 {"http://[::1]/%48", false},
1732 {"http://%41:8080/", true},
1733 {"mysql://x@y(z:123)/foo", true},
1734 {"mysql://x@y(1.2.3.4:123)/foo", true},
1735
1736 {" http://foo.com", true},
1737 {"ht tp://foo.com", true},
1738 {"ahttp://foo.com", false},
1739 {"1http://foo.com", true},
1740
1741 {"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},
1742 {"http://a b.com/", true},
1743 {"cache_object://foo", true},
1744 {"cache_object:foo", true},
1745 {"cache_object:foo/bar", true},
1746 {"cache_object/:foo/bar", false},
1747
1748 {"http://[192.168.0.1]/", true},
1749 {"http://[192.168.0.1]:8080/", true},
1750 {"http://[::ffff:192.168.0.1]/", false},
1751 {"http://[::ffff:192.168.0.1000]/", true},
1752 {"http://[::ffff:192.168.0.1]:8080/", false},
1753 {"http://[::ffff:c0a8:1]/", false},
1754 {"http://[not-an-ip]/", true},
1755 {"http://[fe80::1%foo]/", true},
1756 {"http://[fe80::1", true},
1757 {"http://fe80::1]/", true},
1758 {"http://[test.com]/", true},
1759 }
1760 for _, tt := range tests {
1761 u, err := Parse(tt.in)
1762 if tt.wantErr {
1763 if err == nil {
1764 t.Errorf("Parse(%q) = %#v; want an error", tt.in, u)
1765 }
1766 continue
1767 }
1768 if err != nil {
1769 t.Errorf("Parse(%q) = %v; want no error", tt.in, err)
1770 }
1771 }
1772 }
1773
1774
1775 func TestStarRequest(t *testing.T) {
1776 u, err := Parse("*")
1777 if err != nil {
1778 t.Fatal(err)
1779 }
1780 if got, want := u.RequestURI(), "*"; got != want {
1781 t.Errorf("RequestURI = %q; want %q", got, want)
1782 }
1783 }
1784
1785 type shouldEscapeTest struct {
1786 in byte
1787 mode encoding
1788 escape bool
1789 }
1790
1791 var shouldEscapeTests = []shouldEscapeTest{
1792
1793 {'a', encodePath, false},
1794 {'a', encodeUserPassword, false},
1795 {'a', encodeQueryComponent, false},
1796 {'a', encodeFragment, false},
1797 {'a', encodeHost, false},
1798 {'z', encodePath, false},
1799 {'A', encodePath, false},
1800 {'Z', encodePath, false},
1801 {'0', encodePath, false},
1802 {'9', encodePath, false},
1803 {'-', encodePath, false},
1804 {'-', encodeUserPassword, false},
1805 {'-', encodeQueryComponent, false},
1806 {'-', encodeFragment, false},
1807 {'.', encodePath, false},
1808 {'_', encodePath, false},
1809 {'~', encodePath, false},
1810
1811
1812 {':', encodeUserPassword, true},
1813 {'/', encodeUserPassword, true},
1814 {'?', encodeUserPassword, true},
1815 {'@', encodeUserPassword, true},
1816 {'$', encodeUserPassword, false},
1817 {'&', encodeUserPassword, false},
1818 {'+', encodeUserPassword, false},
1819 {',', encodeUserPassword, false},
1820 {';', encodeUserPassword, false},
1821 {'=', encodeUserPassword, false},
1822
1823
1824 {'!', encodeHost, false},
1825 {'$', encodeHost, false},
1826 {'&', encodeHost, false},
1827 {'\'', encodeHost, false},
1828 {'(', encodeHost, false},
1829 {')', encodeHost, false},
1830 {'*', encodeHost, false},
1831 {'+', encodeHost, false},
1832 {',', encodeHost, false},
1833 {';', encodeHost, false},
1834 {'=', encodeHost, false},
1835 {':', encodeHost, false},
1836 {'[', encodeHost, false},
1837 {']', encodeHost, false},
1838 {'0', encodeHost, false},
1839 {'9', encodeHost, false},
1840 {'A', encodeHost, false},
1841 {'z', encodeHost, false},
1842 {'_', encodeHost, false},
1843 {'-', encodeHost, false},
1844 {'.', encodeHost, false},
1845 }
1846
1847 func TestShouldEscape(t *testing.T) {
1848 for _, tt := range shouldEscapeTests {
1849 if shouldEscape(tt.in, tt.mode) != tt.escape {
1850 t.Errorf("shouldEscape(%q, %v) returned %v; expected %v", tt.in, tt.mode, !tt.escape, tt.escape)
1851 }
1852 }
1853 }
1854
1855 type timeoutError struct {
1856 timeout bool
1857 }
1858
1859 func (e *timeoutError) Error() string { return "timeout error" }
1860 func (e *timeoutError) Timeout() bool { return e.timeout }
1861
1862 type temporaryError struct {
1863 temporary bool
1864 }
1865
1866 func (e *temporaryError) Error() string { return "temporary error" }
1867 func (e *temporaryError) Temporary() bool { return e.temporary }
1868
1869 type timeoutTemporaryError struct {
1870 timeoutError
1871 temporaryError
1872 }
1873
1874 func (e *timeoutTemporaryError) Error() string { return "timeout/temporary error" }
1875
1876 var netErrorTests = []struct {
1877 err error
1878 timeout bool
1879 temporary bool
1880 }{{
1881 err: &Error{"Get", "http://google.com/", &timeoutError{timeout: true}},
1882 timeout: true,
1883 temporary: false,
1884 }, {
1885 err: &Error{"Get", "http://google.com/", &timeoutError{timeout: false}},
1886 timeout: false,
1887 temporary: false,
1888 }, {
1889 err: &Error{"Get", "http://google.com/", &temporaryError{temporary: true}},
1890 timeout: false,
1891 temporary: true,
1892 }, {
1893 err: &Error{"Get", "http://google.com/", &temporaryError{temporary: false}},
1894 timeout: false,
1895 temporary: false,
1896 }, {
1897 err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: true}, temporaryError{temporary: true}}},
1898 timeout: true,
1899 temporary: true,
1900 }, {
1901 err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: false}, temporaryError{temporary: true}}},
1902 timeout: false,
1903 temporary: true,
1904 }, {
1905 err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: true}, temporaryError{temporary: false}}},
1906 timeout: true,
1907 temporary: false,
1908 }, {
1909 err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: false}, temporaryError{temporary: false}}},
1910 timeout: false,
1911 temporary: false,
1912 }, {
1913 err: &Error{"Get", "http://google.com/", io.EOF},
1914 timeout: false,
1915 temporary: false,
1916 }}
1917
1918
1919 func TestURLErrorImplementsNetError(t *testing.T) {
1920 for i, tt := range netErrorTests {
1921 err, ok := tt.err.(net.Error)
1922 if !ok {
1923 t.Errorf("%d: %T does not implement net.Error", i+1, tt.err)
1924 continue
1925 }
1926 if err.Timeout() != tt.timeout {
1927 t.Errorf("%d: err.Timeout(): got %v, want %v", i+1, err.Timeout(), tt.timeout)
1928 continue
1929 }
1930 if err.Temporary() != tt.temporary {
1931 t.Errorf("%d: err.Temporary(): got %v, want %v", i+1, err.Temporary(), tt.temporary)
1932 }
1933 }
1934 }
1935
1936 func TestURLHostnameAndPort(t *testing.T) {
1937 tests := []struct {
1938 in string
1939 host string
1940 port string
1941 }{
1942 {"foo.com:80", "foo.com", "80"},
1943 {"foo.com", "foo.com", ""},
1944 {"foo.com:", "foo.com", ""},
1945 {"FOO.COM", "FOO.COM", ""},
1946 {"1.2.3.4", "1.2.3.4", ""},
1947 {"1.2.3.4:80", "1.2.3.4", "80"},
1948 {"[1:2:3:4]", "1:2:3:4", ""},
1949 {"[1:2:3:4]:80", "1:2:3:4", "80"},
1950 {"[::1]:80", "::1", "80"},
1951 {"[::1]", "::1", ""},
1952 {"[::1]:", "::1", ""},
1953 {"localhost", "localhost", ""},
1954 {"localhost:443", "localhost", "443"},
1955 {"some.super.long.domain.example.org:8080", "some.super.long.domain.example.org", "8080"},
1956 {"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "17000"},
1957 {"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", ""},
1958
1959
1960
1961
1962 {"[google.com]:80", "google.com", "80"},
1963 {"google.com]:80", "google.com]", "80"},
1964 {"google.com:80_invalid_port", "google.com:80_invalid_port", ""},
1965 {"[::1]extra]:80", "::1]extra", "80"},
1966 {"google.com]extra:extra", "google.com]extra:extra", ""},
1967 }
1968 for _, tt := range tests {
1969 u := &URL{Host: tt.in}
1970 host, port := u.Hostname(), u.Port()
1971 if host != tt.host {
1972 t.Errorf("Hostname for Host %q = %q; want %q", tt.in, host, tt.host)
1973 }
1974 if port != tt.port {
1975 t.Errorf("Port for Host %q = %q; want %q", tt.in, port, tt.port)
1976 }
1977 }
1978 }
1979
1980 var _ encodingPkg.BinaryMarshaler = (*URL)(nil)
1981 var _ encodingPkg.BinaryUnmarshaler = (*URL)(nil)
1982 var _ encodingPkg.BinaryAppender = (*URL)(nil)
1983
1984 func TestJSON(t *testing.T) {
1985 u, err := Parse("https://www.google.com/x?y=z")
1986 if err != nil {
1987 t.Fatal(err)
1988 }
1989 js, err := json.Marshal(u)
1990 if err != nil {
1991 t.Fatal(err)
1992 }
1993
1994
1995
1996
1997
1998
1999
2000
2001 u1 := new(URL)
2002 err = json.Unmarshal(js, u1)
2003 if err != nil {
2004 t.Fatal(err)
2005 }
2006 if u1.String() != u.String() {
2007 t.Errorf("json decoded to: %s\nwant: %s\n", u1, u)
2008 }
2009 }
2010
2011 func TestGob(t *testing.T) {
2012 u, err := Parse("https://www.google.com/x?y=z")
2013 if err != nil {
2014 t.Fatal(err)
2015 }
2016 var w bytes.Buffer
2017 err = gob.NewEncoder(&w).Encode(u)
2018 if err != nil {
2019 t.Fatal(err)
2020 }
2021
2022 u1 := new(URL)
2023 err = gob.NewDecoder(&w).Decode(u1)
2024 if err != nil {
2025 t.Fatal(err)
2026 }
2027 if u1.String() != u.String() {
2028 t.Errorf("json decoded to: %s\nwant: %s\n", u1, u)
2029 }
2030 }
2031
2032 func TestNilUser(t *testing.T) {
2033 defer func() {
2034 if v := recover(); v != nil {
2035 t.Fatalf("unexpected panic: %v", v)
2036 }
2037 }()
2038
2039 u, err := Parse("http://foo.com/")
2040
2041 if err != nil {
2042 t.Fatalf("parse err: %v", err)
2043 }
2044
2045 if v := u.User.Username(); v != "" {
2046 t.Fatalf("expected empty username, got %s", v)
2047 }
2048
2049 if v, ok := u.User.Password(); v != "" || ok {
2050 t.Fatalf("expected empty password, got %s (%v)", v, ok)
2051 }
2052
2053 if v := u.User.String(); v != "" {
2054 t.Fatalf("expected empty string, got %s", v)
2055 }
2056 }
2057
2058 func TestInvalidUserPassword(t *testing.T) {
2059 _, err := Parse("http://user^:passwo^rd@foo.com/")
2060 if got, wantsub := fmt.Sprint(err), "net/url: invalid userinfo"; !strings.Contains(got, wantsub) {
2061 t.Errorf("error = %q; want substring %q", got, wantsub)
2062 }
2063 }
2064
2065 func TestRejectControlCharacters(t *testing.T) {
2066 tests := []string{
2067 "http://foo.com/?foo\nbar",
2068 "http\r://foo.com/",
2069 "http://foo\x7f.com/",
2070 }
2071 for _, s := range tests {
2072 _, err := Parse(s)
2073 const wantSub = "net/url: invalid control character in URL"
2074 if got := fmt.Sprint(err); !strings.Contains(got, wantSub) {
2075 t.Errorf("Parse(%q) error = %q; want substring %q", s, got, wantSub)
2076 }
2077 }
2078
2079
2080 if _, err := Parse("http://foo.com/ctl\x80"); err != nil {
2081 t.Errorf("error parsing URL with non-ASCII control byte: %v", err)
2082 }
2083
2084 }
2085
2086 var escapeBenchmarks = []struct {
2087 unescaped string
2088 query string
2089 path string
2090 }{
2091 {
2092 unescaped: "one two",
2093 query: "one+two",
2094 path: "one%20two",
2095 },
2096 {
2097 unescaped: "Фотки собак",
2098 query: "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8+%D1%81%D0%BE%D0%B1%D0%B0%D0%BA",
2099 path: "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8%20%D1%81%D0%BE%D0%B1%D0%B0%D0%BA",
2100 },
2101
2102 {
2103 unescaped: "shortrun(break)shortrun",
2104 query: "shortrun%28break%29shortrun",
2105 path: "shortrun%28break%29shortrun",
2106 },
2107
2108 {
2109 unescaped: "longerrunofcharacters(break)anotherlongerrunofcharacters",
2110 query: "longerrunofcharacters%28break%29anotherlongerrunofcharacters",
2111 path: "longerrunofcharacters%28break%29anotherlongerrunofcharacters",
2112 },
2113
2114 {
2115 unescaped: strings.Repeat("padded/with+various%characters?that=need$some@escaping+paddedsowebreak/256bytes", 4),
2116 query: strings.Repeat("padded%2Fwith%2Bvarious%25characters%3Fthat%3Dneed%24some%40escaping%2Bpaddedsowebreak%2F256bytes", 4),
2117 path: strings.Repeat("padded%2Fwith+various%25characters%3Fthat=need$some@escaping+paddedsowebreak%2F256bytes", 4),
2118 },
2119 }
2120
2121 func BenchmarkQueryEscape(b *testing.B) {
2122 for _, tc := range escapeBenchmarks {
2123 b.Run("", func(b *testing.B) {
2124 b.ReportAllocs()
2125 var g string
2126 for i := 0; i < b.N; i++ {
2127 g = QueryEscape(tc.unescaped)
2128 }
2129 b.StopTimer()
2130 if g != tc.query {
2131 b.Errorf("QueryEscape(%q) == %q, want %q", tc.unescaped, g, tc.query)
2132 }
2133
2134 })
2135 }
2136 }
2137
2138 func BenchmarkPathEscape(b *testing.B) {
2139 for _, tc := range escapeBenchmarks {
2140 b.Run("", func(b *testing.B) {
2141 b.ReportAllocs()
2142 var g string
2143 for i := 0; i < b.N; i++ {
2144 g = PathEscape(tc.unescaped)
2145 }
2146 b.StopTimer()
2147 if g != tc.path {
2148 b.Errorf("PathEscape(%q) == %q, want %q", tc.unescaped, g, tc.path)
2149 }
2150
2151 })
2152 }
2153 }
2154
2155 func BenchmarkQueryUnescape(b *testing.B) {
2156 for _, tc := range escapeBenchmarks {
2157 b.Run("", func(b *testing.B) {
2158 b.ReportAllocs()
2159 var g string
2160 for i := 0; i < b.N; i++ {
2161 g, _ = QueryUnescape(tc.query)
2162 }
2163 b.StopTimer()
2164 if g != tc.unescaped {
2165 b.Errorf("QueryUnescape(%q) == %q, want %q", tc.query, g, tc.unescaped)
2166 }
2167
2168 })
2169 }
2170 }
2171
2172 func BenchmarkPathUnescape(b *testing.B) {
2173 for _, tc := range escapeBenchmarks {
2174 b.Run("", func(b *testing.B) {
2175 b.ReportAllocs()
2176 var g string
2177 for i := 0; i < b.N; i++ {
2178 g, _ = PathUnescape(tc.path)
2179 }
2180 b.StopTimer()
2181 if g != tc.unescaped {
2182 b.Errorf("PathUnescape(%q) == %q, want %q", tc.path, g, tc.unescaped)
2183 }
2184
2185 })
2186 }
2187 }
2188
2189 func TestJoinPath(t *testing.T) {
2190 tests := []struct {
2191 base string
2192 elem []string
2193 out string
2194 }{
2195 {
2196 base: "https://go.googlesource.com",
2197 elem: []string{"go"},
2198 out: "https://go.googlesource.com/go",
2199 },
2200 {
2201 base: "https://go.googlesource.com/a/b/c",
2202 elem: []string{"../../../go"},
2203 out: "https://go.googlesource.com/go",
2204 },
2205 {
2206 base: "https://go.googlesource.com/",
2207 elem: []string{"../go"},
2208 out: "https://go.googlesource.com/go",
2209 },
2210 {
2211 base: "https://go.googlesource.com",
2212 elem: []string{"../go", "../../go", "../../../go"},
2213 out: "https://go.googlesource.com/go",
2214 },
2215 {
2216 base: "https://go.googlesource.com/../go",
2217 elem: nil,
2218 out: "https://go.googlesource.com/go",
2219 },
2220 {
2221 base: "https://go.googlesource.com/",
2222 elem: []string{"./go"},
2223 out: "https://go.googlesource.com/go",
2224 },
2225 {
2226 base: "https://go.googlesource.com//",
2227 elem: []string{"/go"},
2228 out: "https://go.googlesource.com/go",
2229 },
2230 {
2231 base: "https://go.googlesource.com//",
2232 elem: []string{"/go", "a", "b", "c"},
2233 out: "https://go.googlesource.com/go/a/b/c",
2234 },
2235 {
2236 base: "http://[fe80::1%en0]:8080/",
2237 elem: []string{"/go"},
2238 },
2239 {
2240 base: "https://go.googlesource.com",
2241 elem: []string{"go/"},
2242 out: "https://go.googlesource.com/go/",
2243 },
2244 {
2245 base: "https://go.googlesource.com",
2246 elem: []string{"go//"},
2247 out: "https://go.googlesource.com/go/",
2248 },
2249 {
2250 base: "https://go.googlesource.com",
2251 elem: nil,
2252 out: "https://go.googlesource.com/",
2253 },
2254 {
2255 base: "https://go.googlesource.com/",
2256 elem: nil,
2257 out: "https://go.googlesource.com/",
2258 },
2259 {
2260 base: "https://go.googlesource.com/a%2fb",
2261 elem: []string{"c"},
2262 out: "https://go.googlesource.com/a%2fb/c",
2263 },
2264 {
2265 base: "https://go.googlesource.com/a%2fb",
2266 elem: []string{"c%2fd"},
2267 out: "https://go.googlesource.com/a%2fb/c%2fd",
2268 },
2269 {
2270 base: "https://go.googlesource.com/a/b",
2271 elem: []string{"/go"},
2272 out: "https://go.googlesource.com/a/b/go",
2273 },
2274 {
2275 base: "https://go.googlesource.com/",
2276 elem: []string{"100%"},
2277 },
2278 {
2279 base: "/",
2280 elem: nil,
2281 out: "/",
2282 },
2283 {
2284 base: "a",
2285 elem: nil,
2286 out: "a",
2287 },
2288 {
2289 base: "a",
2290 elem: []string{"b"},
2291 out: "a/b",
2292 },
2293 {
2294 base: "a",
2295 elem: []string{"../b"},
2296 out: "b",
2297 },
2298 {
2299 base: "a",
2300 elem: []string{"../../b"},
2301 out: "b",
2302 },
2303 {
2304 base: "",
2305 elem: []string{"a"},
2306 out: "a",
2307 },
2308 {
2309 base: "",
2310 elem: []string{"../a"},
2311 out: "a",
2312 },
2313 }
2314 for _, tt := range tests {
2315 wantErr := "nil"
2316 if tt.out == "" {
2317 wantErr = "non-nil error"
2318 }
2319 out, err := JoinPath(tt.base, tt.elem...)
2320 if out != tt.out || (err == nil) != (tt.out != "") {
2321 t.Errorf("JoinPath(%q, %q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)
2322 }
2323
2324 u, err := Parse(tt.base)
2325 if err != nil {
2326 if tt.out != "" {
2327 t.Errorf("Parse(%q) = %v", tt.base, err)
2328 }
2329 continue
2330 }
2331 if tt.out == "" {
2332
2333 tt.out = tt.base
2334 }
2335 out = u.JoinPath(tt.elem...).String()
2336 if out != tt.out {
2337 t.Errorf("Parse(%q).JoinPath(%q) = %q, want %q", tt.base, tt.elem, out, tt.out)
2338 }
2339 }
2340 }
2341
2342 func TestParseStrictIpv6(t *testing.T) {
2343 t.Setenv("GODEBUG", "urlstrictcolons=0")
2344
2345 tests := []struct {
2346 url string
2347 }{
2348
2349 {"https://1:2:3:4:5:6:7:8"},
2350 {"https://1:2:3:4:5:6:7:8:80"},
2351 {"https://example.com:80:"},
2352 }
2353 for i, tc := range tests {
2354 t.Run(strconv.Itoa(i), func(t *testing.T) {
2355 _, err := Parse(tc.url)
2356 if err != nil {
2357 t.Errorf("Parse(%q) error = %v, want nil", tc.url, err)
2358 }
2359 })
2360 }
2361
2362 }
2363
View as plain text