github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/net/url/url_test.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package url 6 7 import ( 8 "fmt" 9 "reflect" 10 "strings" 11 "testing" 12 ) 13 14 type URLTest struct { 15 in string 16 out *URL 17 roundtrip string // expected result of reserializing the URL; empty means same as "in". 18 } 19 20 var urltests = []URLTest{ 21 // no path 22 { 23 "http://www.google.com", 24 &URL{ 25 Scheme: "http", 26 Host: "www.google.com", 27 }, 28 "", 29 }, 30 // path 31 { 32 "http://www.google.com/", 33 &URL{ 34 Scheme: "http", 35 Host: "www.google.com", 36 Path: "/", 37 }, 38 "", 39 }, 40 // path with hex escaping 41 { 42 "http://www.google.com/file%20one%26two", 43 &URL{ 44 Scheme: "http", 45 Host: "www.google.com", 46 Path: "/file one&two", 47 }, 48 "http://www.google.com/file%20one&two", 49 }, 50 // user 51 { 52 "ftp://webmaster@www.google.com/", 53 &URL{ 54 Scheme: "ftp", 55 User: User("webmaster"), 56 Host: "www.google.com", 57 Path: "/", 58 }, 59 "", 60 }, 61 // escape sequence in username 62 { 63 "ftp://john%20doe@www.google.com/", 64 &URL{ 65 Scheme: "ftp", 66 User: User("john doe"), 67 Host: "www.google.com", 68 Path: "/", 69 }, 70 "ftp://john%20doe@www.google.com/", 71 }, 72 // query 73 { 74 "http://www.google.com/?q=go+language", 75 &URL{ 76 Scheme: "http", 77 Host: "www.google.com", 78 Path: "/", 79 RawQuery: "q=go+language", 80 }, 81 "", 82 }, 83 // query with hex escaping: NOT parsed 84 { 85 "http://www.google.com/?q=go%20language", 86 &URL{ 87 Scheme: "http", 88 Host: "www.google.com", 89 Path: "/", 90 RawQuery: "q=go%20language", 91 }, 92 "", 93 }, 94 // %20 outside query 95 { 96 "http://www.google.com/a%20b?q=c+d", 97 &URL{ 98 Scheme: "http", 99 Host: "www.google.com", 100 Path: "/a b", 101 RawQuery: "q=c+d", 102 }, 103 "", 104 }, 105 // path without leading /, so no parsing 106 { 107 "http:www.google.com/?q=go+language", 108 &URL{ 109 Scheme: "http", 110 Opaque: "www.google.com/", 111 RawQuery: "q=go+language", 112 }, 113 "http:www.google.com/?q=go+language", 114 }, 115 // path without leading /, so no parsing 116 { 117 "http:%2f%2fwww.google.com/?q=go+language", 118 &URL{ 119 Scheme: "http", 120 Opaque: "%2f%2fwww.google.com/", 121 RawQuery: "q=go+language", 122 }, 123 "http:%2f%2fwww.google.com/?q=go+language", 124 }, 125 // non-authority with path 126 { 127 "mailto:/webmaster@golang.org", 128 &URL{ 129 Scheme: "mailto", 130 Path: "/webmaster@golang.org", 131 }, 132 "mailto:///webmaster@golang.org", // unfortunate compromise 133 }, 134 // non-authority 135 { 136 "mailto:webmaster@golang.org", 137 &URL{ 138 Scheme: "mailto", 139 Opaque: "webmaster@golang.org", 140 }, 141 "", 142 }, 143 // unescaped :// in query should not create a scheme 144 { 145 "/foo?query=http://bad", 146 &URL{ 147 Path: "/foo", 148 RawQuery: "query=http://bad", 149 }, 150 "", 151 }, 152 // leading // without scheme should create an authority 153 { 154 "//foo", 155 &URL{ 156 Host: "foo", 157 }, 158 "", 159 }, 160 // leading // without scheme, with userinfo, path, and query 161 { 162 "//user@foo/path?a=b", 163 &URL{ 164 User: User("user"), 165 Host: "foo", 166 Path: "/path", 167 RawQuery: "a=b", 168 }, 169 "", 170 }, 171 // Three leading slashes isn't an authority, but doesn't return an error. 172 // (We can't return an error, as this code is also used via 173 // ServeHTTP -> ReadRequest -> Parse, which is arguably a 174 // different URL parsing context, but currently shares the 175 // same codepath) 176 { 177 "///threeslashes", 178 &URL{ 179 Path: "///threeslashes", 180 }, 181 "", 182 }, 183 { 184 "http://user:password@google.com", 185 &URL{ 186 Scheme: "http", 187 User: UserPassword("user", "password"), 188 Host: "google.com", 189 }, 190 "http://user:password@google.com", 191 }, 192 // unescaped @ in username should not confuse host 193 { 194 "http://j@ne:password@google.com", 195 &URL{ 196 Scheme: "http", 197 User: UserPassword("j@ne", "password"), 198 Host: "google.com", 199 }, 200 "http://j%40ne:password@google.com", 201 }, 202 // unescaped @ in password should not confuse host 203 { 204 "http://jane:p@ssword@google.com", 205 &URL{ 206 Scheme: "http", 207 User: UserPassword("jane", "p@ssword"), 208 Host: "google.com", 209 }, 210 "http://jane:p%40ssword@google.com", 211 }, 212 { 213 "http://j@ne:password@google.com/p@th?q=@go", 214 &URL{ 215 Scheme: "http", 216 User: UserPassword("j@ne", "password"), 217 Host: "google.com", 218 Path: "/p@th", 219 RawQuery: "q=@go", 220 }, 221 "http://j%40ne:password@google.com/p@th?q=@go", 222 }, 223 { 224 "http://www.google.com/?q=go+language#foo", 225 &URL{ 226 Scheme: "http", 227 Host: "www.google.com", 228 Path: "/", 229 RawQuery: "q=go+language", 230 Fragment: "foo", 231 }, 232 "", 233 }, 234 { 235 "http://www.google.com/?q=go+language#foo%26bar", 236 &URL{ 237 Scheme: "http", 238 Host: "www.google.com", 239 Path: "/", 240 RawQuery: "q=go+language", 241 Fragment: "foo&bar", 242 }, 243 "http://www.google.com/?q=go+language#foo&bar", 244 }, 245 { 246 "file:///home/adg/rabbits", 247 &URL{ 248 Scheme: "file", 249 Host: "", 250 Path: "/home/adg/rabbits", 251 }, 252 "file:///home/adg/rabbits", 253 }, 254 // case-insensitive scheme 255 { 256 "MaIlTo:webmaster@golang.org", 257 &URL{ 258 Scheme: "mailto", 259 Opaque: "webmaster@golang.org", 260 }, 261 "mailto:webmaster@golang.org", 262 }, 263 } 264 265 // more useful string for debugging than fmt's struct printer 266 func ufmt(u *URL) string { 267 var user, pass interface{} 268 if u.User != nil { 269 user = u.User.Username() 270 if p, ok := u.User.Password(); ok { 271 pass = p 272 } 273 } 274 return fmt.Sprintf("opaque=%q, scheme=%q, user=%#v, pass=%#v, host=%q, path=%q, rawq=%q, frag=%q", 275 u.Opaque, u.Scheme, user, pass, u.Host, u.Path, u.RawQuery, u.Fragment) 276 } 277 278 func DoTest(t *testing.T, parse func(string) (*URL, error), name string, tests []URLTest) { 279 for _, tt := range tests { 280 u, err := parse(tt.in) 281 if err != nil { 282 t.Errorf("%s(%q) returned error %s", name, tt.in, err) 283 continue 284 } 285 if !reflect.DeepEqual(u, tt.out) { 286 t.Errorf("%s(%q):\n\thave %v\n\twant %v\n", 287 name, tt.in, ufmt(u), ufmt(tt.out)) 288 } 289 } 290 } 291 292 func BenchmarkString(b *testing.B) { 293 b.StopTimer() 294 b.ReportAllocs() 295 for _, tt := range urltests { 296 u, err := Parse(tt.in) 297 if err != nil { 298 b.Errorf("Parse(%q) returned error %s", tt.in, err) 299 continue 300 } 301 if tt.roundtrip == "" { 302 continue 303 } 304 b.StartTimer() 305 var g string 306 for i := 0; i < b.N; i++ { 307 g = u.String() 308 } 309 b.StopTimer() 310 if w := tt.roundtrip; g != w { 311 b.Errorf("Parse(%q).String() == %q, want %q", tt.in, g, w) 312 } 313 } 314 } 315 316 func TestParse(t *testing.T) { 317 DoTest(t, Parse, "Parse", urltests) 318 } 319 320 const pathThatLooksSchemeRelative = "//not.a.user@not.a.host/just/a/path" 321 322 var parseRequestURLTests = []struct { 323 url string 324 expectedValid bool 325 }{ 326 {"http://foo.com", true}, 327 {"http://foo.com/", true}, 328 {"http://foo.com/path", true}, 329 {"/", true}, 330 {pathThatLooksSchemeRelative, true}, 331 {"//not.a.user@%66%6f%6f.com/just/a/path/also", true}, 332 {"foo.html", false}, 333 {"../dir/", false}, 334 {"*", true}, 335 } 336 337 func TestParseRequestURI(t *testing.T) { 338 for _, test := range parseRequestURLTests { 339 _, err := ParseRequestURI(test.url) 340 valid := err == nil 341 if valid != test.expectedValid { 342 t.Errorf("Expected valid=%v for %q; got %v", test.expectedValid, test.url, valid) 343 } 344 } 345 346 url, err := ParseRequestURI(pathThatLooksSchemeRelative) 347 if err != nil { 348 t.Fatalf("Unexpected error %v", err) 349 } 350 if url.Path != pathThatLooksSchemeRelative { 351 t.Errorf("Expected path %q; got %q", pathThatLooksSchemeRelative, url.Path) 352 } 353 } 354 355 func DoTestString(t *testing.T, parse func(string) (*URL, error), name string, tests []URLTest) { 356 for _, tt := range tests { 357 u, err := parse(tt.in) 358 if err != nil { 359 t.Errorf("%s(%q) returned error %s", name, tt.in, err) 360 continue 361 } 362 expected := tt.in 363 if len(tt.roundtrip) > 0 { 364 expected = tt.roundtrip 365 } 366 s := u.String() 367 if s != expected { 368 t.Errorf("%s(%q).String() == %q (expected %q)", name, tt.in, s, expected) 369 } 370 } 371 } 372 373 func TestURLString(t *testing.T) { 374 DoTestString(t, Parse, "Parse", urltests) 375 } 376 377 type EscapeTest struct { 378 in string 379 out string 380 err error 381 } 382 383 var unescapeTests = []EscapeTest{ 384 { 385 "", 386 "", 387 nil, 388 }, 389 { 390 "abc", 391 "abc", 392 nil, 393 }, 394 { 395 "1%41", 396 "1A", 397 nil, 398 }, 399 { 400 "1%41%42%43", 401 "1ABC", 402 nil, 403 }, 404 { 405 "%4a", 406 "J", 407 nil, 408 }, 409 { 410 "%6F", 411 "o", 412 nil, 413 }, 414 { 415 "%", // not enough characters after % 416 "", 417 EscapeError("%"), 418 }, 419 { 420 "%a", // not enough characters after % 421 "", 422 EscapeError("%a"), 423 }, 424 { 425 "%1", // not enough characters after % 426 "", 427 EscapeError("%1"), 428 }, 429 { 430 "123%45%6", // not enough characters after % 431 "", 432 EscapeError("%6"), 433 }, 434 { 435 "%zzzzz", // invalid hex digits 436 "", 437 EscapeError("%zz"), 438 }, 439 } 440 441 func TestUnescape(t *testing.T) { 442 for _, tt := range unescapeTests { 443 actual, err := QueryUnescape(tt.in) 444 if actual != tt.out || (err != nil) != (tt.err != nil) { 445 t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", tt.in, actual, err, tt.out, tt.err) 446 } 447 } 448 } 449 450 var escapeTests = []EscapeTest{ 451 { 452 "", 453 "", 454 nil, 455 }, 456 { 457 "abc", 458 "abc", 459 nil, 460 }, 461 { 462 "one two", 463 "one+two", 464 nil, 465 }, 466 { 467 "10%", 468 "10%25", 469 nil, 470 }, 471 { 472 " ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;", 473 "+%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", 474 nil, 475 }, 476 } 477 478 func TestEscape(t *testing.T) { 479 for _, tt := range escapeTests { 480 actual := QueryEscape(tt.in) 481 if tt.out != actual { 482 t.Errorf("QueryEscape(%q) = %q, want %q", tt.in, actual, tt.out) 483 } 484 485 // for bonus points, verify that escape:unescape is an identity. 486 roundtrip, err := QueryUnescape(actual) 487 if roundtrip != tt.in || err != nil { 488 t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]") 489 } 490 } 491 } 492 493 //var userinfoTests = []UserinfoTest{ 494 // {"user", "password", "user:password"}, 495 // {"foo:bar", "~!@#$%^&*()_+{}|[]\\-=`:;'\"<>?,./", 496 // "foo%3Abar:~!%40%23$%25%5E&*()_+%7B%7D%7C%5B%5D%5C-=%60%3A;'%22%3C%3E?,.%2F"}, 497 //} 498 499 type EncodeQueryTest struct { 500 m Values 501 expected string 502 } 503 504 var encodeQueryTests = []EncodeQueryTest{ 505 {nil, ""}, 506 {Values{"q": {"puppies"}, "oe": {"utf8"}}, "oe=utf8&q=puppies"}, 507 {Values{"q": {"dogs", "&", "7"}}, "q=dogs&q=%26&q=7"}, 508 {Values{ 509 "a": {"a1", "a2", "a3"}, 510 "b": {"b1", "b2", "b3"}, 511 "c": {"c1", "c2", "c3"}, 512 }, "a=a1&a=a2&a=a3&b=b1&b=b2&b=b3&c=c1&c=c2&c=c3"}, 513 } 514 515 func TestEncodeQuery(t *testing.T) { 516 for _, tt := range encodeQueryTests { 517 if q := tt.m.Encode(); q != tt.expected { 518 t.Errorf(`EncodeQuery(%+v) = %q, want %q`, tt.m, q, tt.expected) 519 } 520 } 521 } 522 523 var resolvePathTests = []struct { 524 base, ref, expected string 525 }{ 526 {"a/b", ".", "/a/"}, 527 {"a/b", "c", "/a/c"}, 528 {"a/b", "..", "/"}, 529 {"a/", "..", "/"}, 530 {"a/", "../..", "/"}, 531 {"a/b/c", "..", "/a/"}, 532 {"a/b/c", "../d", "/a/d"}, 533 {"a/b/c", ".././d", "/a/d"}, 534 {"a/b", "./..", "/"}, 535 {"a/./b", ".", "/a/"}, 536 {"a/../", ".", "/"}, 537 {"a/.././b", "c", "/c"}, 538 } 539 540 func TestResolvePath(t *testing.T) { 541 for _, test := range resolvePathTests { 542 got := resolvePath(test.base, test.ref) 543 if got != test.expected { 544 t.Errorf("For %q + %q got %q; expected %q", test.base, test.ref, got, test.expected) 545 } 546 } 547 } 548 549 var resolveReferenceTests = []struct { 550 base, rel, expected string 551 }{ 552 // Absolute URL references 553 {"http://foo.com?a=b", "https://bar.com/", "https://bar.com/"}, 554 {"http://foo.com/", "https://bar.com/?a=b", "https://bar.com/?a=b"}, 555 {"http://foo.com/bar", "mailto:foo@example.com", "mailto:foo@example.com"}, 556 557 // Path-absolute references 558 {"http://foo.com/bar", "/baz", "http://foo.com/baz"}, 559 {"http://foo.com/bar?a=b#f", "/baz", "http://foo.com/baz"}, 560 {"http://foo.com/bar?a=b", "/baz?c=d", "http://foo.com/baz?c=d"}, 561 562 // Scheme-relative 563 {"https://foo.com/bar?a=b", "//bar.com/quux", "https://bar.com/quux"}, 564 565 // Path-relative references: 566 567 // ... current directory 568 {"http://foo.com", ".", "http://foo.com/"}, 569 {"http://foo.com/bar", ".", "http://foo.com/"}, 570 {"http://foo.com/bar/", ".", "http://foo.com/bar/"}, 571 572 // ... going down 573 {"http://foo.com", "bar", "http://foo.com/bar"}, 574 {"http://foo.com/", "bar", "http://foo.com/bar"}, 575 {"http://foo.com/bar/baz", "quux", "http://foo.com/bar/quux"}, 576 577 // ... going up 578 {"http://foo.com/bar/baz", "../quux", "http://foo.com/quux"}, 579 {"http://foo.com/bar/baz", "../../../../../quux", "http://foo.com/quux"}, 580 {"http://foo.com/bar", "..", "http://foo.com/"}, 581 {"http://foo.com/bar/baz", "./..", "http://foo.com/"}, 582 // ".." in the middle (issue 3560) 583 {"http://foo.com/bar/baz", "quux/dotdot/../tail", "http://foo.com/bar/quux/tail"}, 584 {"http://foo.com/bar/baz", "quux/./dotdot/../tail", "http://foo.com/bar/quux/tail"}, 585 {"http://foo.com/bar/baz", "quux/./dotdot/.././tail", "http://foo.com/bar/quux/tail"}, 586 {"http://foo.com/bar/baz", "quux/./dotdot/./../tail", "http://foo.com/bar/quux/tail"}, 587 {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/././../../tail", "http://foo.com/bar/quux/tail"}, 588 {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/./.././../tail", "http://foo.com/bar/quux/tail"}, 589 {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/dotdot/./../../.././././tail", "http://foo.com/bar/quux/tail"}, 590 {"http://foo.com/bar/baz", "quux/./dotdot/../dotdot/../dot/./tail/..", "http://foo.com/bar/quux/dot/"}, 591 592 // Remove any dot-segments prior to forming the target URI. 593 // http://tools.ietf.org/html/rfc3986#section-5.2.4 594 {"http://foo.com/dot/./dotdot/../foo/bar", "../baz", "http://foo.com/dot/baz"}, 595 596 // Triple dot isn't special 597 {"http://foo.com/bar", "...", "http://foo.com/..."}, 598 599 // Fragment 600 {"http://foo.com/bar", ".#frag", "http://foo.com/#frag"}, 601 602 // RFC 3986: Normal Examples 603 // http://tools.ietf.org/html/rfc3986#section-5.4.1 604 {"http://a/b/c/d;p?q", "g:h", "g:h"}, 605 {"http://a/b/c/d;p?q", "g", "http://a/b/c/g"}, 606 {"http://a/b/c/d;p?q", "./g", "http://a/b/c/g"}, 607 {"http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"}, 608 {"http://a/b/c/d;p?q", "/g", "http://a/g"}, 609 {"http://a/b/c/d;p?q", "//g", "http://g"}, 610 {"http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y"}, 611 {"http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"}, 612 {"http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s"}, 613 {"http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"}, 614 {"http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"}, 615 {"http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"}, 616 {"http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"}, 617 {"http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"}, 618 {"http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q"}, 619 {"http://a/b/c/d;p?q", ".", "http://a/b/c/"}, 620 {"http://a/b/c/d;p?q", "./", "http://a/b/c/"}, 621 {"http://a/b/c/d;p?q", "..", "http://a/b/"}, 622 {"http://a/b/c/d;p?q", "../", "http://a/b/"}, 623 {"http://a/b/c/d;p?q", "../g", "http://a/b/g"}, 624 {"http://a/b/c/d;p?q", "../..", "http://a/"}, 625 {"http://a/b/c/d;p?q", "../../", "http://a/"}, 626 {"http://a/b/c/d;p?q", "../../g", "http://a/g"}, 627 628 // RFC 3986: Abnormal Examples 629 // http://tools.ietf.org/html/rfc3986#section-5.4.2 630 {"http://a/b/c/d;p?q", "../../../g", "http://a/g"}, 631 {"http://a/b/c/d;p?q", "../../../../g", "http://a/g"}, 632 {"http://a/b/c/d;p?q", "/./g", "http://a/g"}, 633 {"http://a/b/c/d;p?q", "/../g", "http://a/g"}, 634 {"http://a/b/c/d;p?q", "g.", "http://a/b/c/g."}, 635 {"http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"}, 636 {"http://a/b/c/d;p?q", "g..", "http://a/b/c/g.."}, 637 {"http://a/b/c/d;p?q", "..g", "http://a/b/c/..g"}, 638 {"http://a/b/c/d;p?q", "./../g", "http://a/b/g"}, 639 {"http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"}, 640 {"http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"}, 641 {"http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h"}, 642 {"http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"}, 643 {"http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y"}, 644 {"http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x"}, 645 {"http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x"}, 646 {"http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x"}, 647 {"http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x"}, 648 649 // Extras. 650 {"https://a/b/c/d;p?q", "//g?q", "https://g?q"}, 651 {"https://a/b/c/d;p?q", "//g#s", "https://g#s"}, 652 {"https://a/b/c/d;p?q", "//g/d/e/f?y#s", "https://g/d/e/f?y#s"}, 653 {"https://a/b/c/d;p#s", "?y", "https://a/b/c/d;p?y"}, 654 {"https://a/b/c/d;p?q#s", "?y", "https://a/b/c/d;p?y"}, 655 } 656 657 func TestResolveReference(t *testing.T) { 658 mustParse := func(url string) *URL { 659 u, err := Parse(url) 660 if err != nil { 661 t.Fatalf("Expected URL to parse: %q, got error: %v", url, err) 662 } 663 return u 664 } 665 opaque := &URL{Scheme: "scheme", Opaque: "opaque"} 666 for _, test := range resolveReferenceTests { 667 base := mustParse(test.base) 668 rel := mustParse(test.rel) 669 url := base.ResolveReference(rel) 670 if url.String() != test.expected { 671 t.Errorf("URL(%q).ResolveReference(%q) == %q, got %q", test.base, test.rel, test.expected, url.String()) 672 } 673 // Ensure that new instances are returned. 674 if base == url { 675 t.Errorf("Expected URL.ResolveReference to return new URL instance.") 676 } 677 // Test the convenience wrapper too. 678 url, err := base.Parse(test.rel) 679 if err != nil { 680 t.Errorf("URL(%q).Parse(%q) failed: %v", test.base, test.rel, err) 681 } else if url.String() != test.expected { 682 t.Errorf("URL(%q).Parse(%q) == %q, got %q", test.base, test.rel, test.expected, url.String()) 683 } else if base == url { 684 // Ensure that new instances are returned for the wrapper too. 685 t.Errorf("Expected URL.Parse to return new URL instance.") 686 } 687 // Ensure Opaque resets the URL. 688 url = base.ResolveReference(opaque) 689 if *url != *opaque { 690 t.Errorf("ResolveReference failed to resolve opaque URL: want %#v, got %#v", url, opaque) 691 } 692 // Test the convenience wrapper with an opaque URL too. 693 url, err = base.Parse("scheme:opaque") 694 if err != nil { 695 t.Errorf(`URL(%q).Parse("scheme:opaque") failed: %v`, test.base, err) 696 } else if *url != *opaque { 697 t.Errorf("Parse failed to resolve opaque URL: want %#v, got %#v", url, opaque) 698 } else if base == url { 699 // Ensure that new instances are returned, again. 700 t.Errorf("Expected URL.Parse to return new URL instance.") 701 } 702 } 703 } 704 705 func TestQueryValues(t *testing.T) { 706 u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2") 707 v := u.Query() 708 if len(v) != 2 { 709 t.Errorf("got %d keys in Query values, want 2", len(v)) 710 } 711 if g, e := v.Get("foo"), "bar"; g != e { 712 t.Errorf("Get(foo) = %q, want %q", g, e) 713 } 714 // Case sensitive: 715 if g, e := v.Get("Foo"), ""; g != e { 716 t.Errorf("Get(Foo) = %q, want %q", g, e) 717 } 718 if g, e := v.Get("bar"), "1"; g != e { 719 t.Errorf("Get(bar) = %q, want %q", g, e) 720 } 721 if g, e := v.Get("baz"), ""; g != e { 722 t.Errorf("Get(baz) = %q, want %q", g, e) 723 } 724 v.Del("bar") 725 if g, e := v.Get("bar"), ""; g != e { 726 t.Errorf("second Get(bar) = %q, want %q", g, e) 727 } 728 } 729 730 type parseTest struct { 731 query string 732 out Values 733 } 734 735 var parseTests = []parseTest{ 736 { 737 query: "a=1&b=2", 738 out: Values{"a": []string{"1"}, "b": []string{"2"}}, 739 }, 740 { 741 query: "a=1&a=2&a=banana", 742 out: Values{"a": []string{"1", "2", "banana"}}, 743 }, 744 { 745 query: "ascii=%3Ckey%3A+0x90%3E", 746 out: Values{"ascii": []string{"<key: 0x90>"}}, 747 }, 748 { 749 query: "a=1;b=2", 750 out: Values{"a": []string{"1"}, "b": []string{"2"}}, 751 }, 752 { 753 query: "a=1&a=2;a=banana", 754 out: Values{"a": []string{"1", "2", "banana"}}, 755 }, 756 } 757 758 func TestParseQuery(t *testing.T) { 759 for i, test := range parseTests { 760 form, err := ParseQuery(test.query) 761 if err != nil { 762 t.Errorf("test %d: Unexpected error: %v", i, err) 763 continue 764 } 765 if len(form) != len(test.out) { 766 t.Errorf("test %d: len(form) = %d, want %d", i, len(form), len(test.out)) 767 } 768 for k, evs := range test.out { 769 vs, ok := form[k] 770 if !ok { 771 t.Errorf("test %d: Missing key %q", i, k) 772 continue 773 } 774 if len(vs) != len(evs) { 775 t.Errorf("test %d: len(form[%q]) = %d, want %d", i, k, len(vs), len(evs)) 776 continue 777 } 778 for j, ev := range evs { 779 if v := vs[j]; v != ev { 780 t.Errorf("test %d: form[%q][%d] = %q, want %q", i, k, j, v, ev) 781 } 782 } 783 } 784 } 785 } 786 787 type RequestURITest struct { 788 url *URL 789 out string 790 } 791 792 var requritests = []RequestURITest{ 793 { 794 &URL{ 795 Scheme: "http", 796 Host: "example.com", 797 Path: "", 798 }, 799 "/", 800 }, 801 { 802 &URL{ 803 Scheme: "http", 804 Host: "example.com", 805 Path: "/a b", 806 }, 807 "/a%20b", 808 }, 809 // golang.org/issue/4860 variant 1 810 { 811 &URL{ 812 Scheme: "http", 813 Host: "example.com", 814 Opaque: "/%2F/%2F/", 815 }, 816 "/%2F/%2F/", 817 }, 818 // golang.org/issue/4860 variant 2 819 { 820 &URL{ 821 Scheme: "http", 822 Host: "example.com", 823 Opaque: "//other.example.com/%2F/%2F/", 824 }, 825 "http://other.example.com/%2F/%2F/", 826 }, 827 { 828 &URL{ 829 Scheme: "http", 830 Host: "example.com", 831 Path: "/a b", 832 RawQuery: "q=go+language", 833 }, 834 "/a%20b?q=go+language", 835 }, 836 { 837 &URL{ 838 Scheme: "myschema", 839 Opaque: "opaque", 840 }, 841 "opaque", 842 }, 843 { 844 &URL{ 845 Scheme: "myschema", 846 Opaque: "opaque", 847 RawQuery: "q=go+language", 848 }, 849 "opaque?q=go+language", 850 }, 851 } 852 853 func TestRequestURI(t *testing.T) { 854 for _, tt := range requritests { 855 s := tt.url.RequestURI() 856 if s != tt.out { 857 t.Errorf("%#v.RequestURI() == %q (expected %q)", tt.url, s, tt.out) 858 } 859 } 860 } 861 862 func TestParseFailure(t *testing.T) { 863 // Test that the first parse error is returned. 864 const url = "%gh&%ij" 865 _, err := ParseQuery(url) 866 errStr := fmt.Sprint(err) 867 if !strings.Contains(errStr, "%gh") { 868 t.Errorf(`ParseQuery(%q) returned error %q, want something containing %q"`, url, errStr, "%gh") 869 } 870 }