PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/gofrontend/libgo/go/net/url/url_test.go

http://github.com/axw/llgo
Go | 1231 lines | 1083 code | 59 blank | 89 comment | 117 complexity | ade4c903cabceec5627b68281772a7ed MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  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. package url
  5. import (
  6. "fmt"
  7. "reflect"
  8. "strings"
  9. "testing"
  10. )
  11. type URLTest struct {
  12. in string
  13. out *URL // expected parse; RawPath="" means same as Path
  14. roundtrip string // expected result of reserializing the URL; empty means same as "in".
  15. }
  16. var urltests = []URLTest{
  17. // no path
  18. {
  19. "http://www.google.com",
  20. &URL{
  21. Scheme: "http",
  22. Host: "www.google.com",
  23. },
  24. "",
  25. },
  26. // path
  27. {
  28. "http://www.google.com/",
  29. &URL{
  30. Scheme: "http",
  31. Host: "www.google.com",
  32. Path: "/",
  33. },
  34. "",
  35. },
  36. // path with hex escaping
  37. {
  38. "http://www.google.com/file%20one%26two",
  39. &URL{
  40. Scheme: "http",
  41. Host: "www.google.com",
  42. Path: "/file one&two",
  43. RawPath: "/file%20one%26two",
  44. },
  45. "",
  46. },
  47. // user
  48. {
  49. "ftp://webmaster@www.google.com/",
  50. &URL{
  51. Scheme: "ftp",
  52. User: User("webmaster"),
  53. Host: "www.google.com",
  54. Path: "/",
  55. },
  56. "",
  57. },
  58. // escape sequence in username
  59. {
  60. "ftp://john%20doe@www.google.com/",
  61. &URL{
  62. Scheme: "ftp",
  63. User: User("john doe"),
  64. Host: "www.google.com",
  65. Path: "/",
  66. },
  67. "ftp://john%20doe@www.google.com/",
  68. },
  69. // query
  70. {
  71. "http://www.google.com/?q=go+language",
  72. &URL{
  73. Scheme: "http",
  74. Host: "www.google.com",
  75. Path: "/",
  76. RawQuery: "q=go+language",
  77. },
  78. "",
  79. },
  80. // query with hex escaping: NOT parsed
  81. {
  82. "http://www.google.com/?q=go%20language",
  83. &URL{
  84. Scheme: "http",
  85. Host: "www.google.com",
  86. Path: "/",
  87. RawQuery: "q=go%20language",
  88. },
  89. "",
  90. },
  91. // %20 outside query
  92. {
  93. "http://www.google.com/a%20b?q=c+d",
  94. &URL{
  95. Scheme: "http",
  96. Host: "www.google.com",
  97. Path: "/a b",
  98. RawQuery: "q=c+d",
  99. },
  100. "",
  101. },
  102. // path without leading /, so no parsing
  103. {
  104. "http:www.google.com/?q=go+language",
  105. &URL{
  106. Scheme: "http",
  107. Opaque: "www.google.com/",
  108. RawQuery: "q=go+language",
  109. },
  110. "http:www.google.com/?q=go+language",
  111. },
  112. // path without leading /, so no parsing
  113. {
  114. "http:%2f%2fwww.google.com/?q=go+language",
  115. &URL{
  116. Scheme: "http",
  117. Opaque: "%2f%2fwww.google.com/",
  118. RawQuery: "q=go+language",
  119. },
  120. "http:%2f%2fwww.google.com/?q=go+language",
  121. },
  122. // non-authority with path
  123. {
  124. "mailto:/webmaster@golang.org",
  125. &URL{
  126. Scheme: "mailto",
  127. Path: "/webmaster@golang.org",
  128. },
  129. "mailto:///webmaster@golang.org", // unfortunate compromise
  130. },
  131. // non-authority
  132. {
  133. "mailto:webmaster@golang.org",
  134. &URL{
  135. Scheme: "mailto",
  136. Opaque: "webmaster@golang.org",
  137. },
  138. "",
  139. },
  140. // unescaped :// in query should not create a scheme
  141. {
  142. "/foo?query=http://bad",
  143. &URL{
  144. Path: "/foo",
  145. RawQuery: "query=http://bad",
  146. },
  147. "",
  148. },
  149. // leading // without scheme should create an authority
  150. {
  151. "//foo",
  152. &URL{
  153. Host: "foo",
  154. },
  155. "",
  156. },
  157. // leading // without scheme, with userinfo, path, and query
  158. {
  159. "//user@foo/path?a=b",
  160. &URL{
  161. User: User("user"),
  162. Host: "foo",
  163. Path: "/path",
  164. RawQuery: "a=b",
  165. },
  166. "",
  167. },
  168. // Three leading slashes isn't an authority, but doesn't return an error.
  169. // (We can't return an error, as this code is also used via
  170. // ServeHTTP -> ReadRequest -> Parse, which is arguably a
  171. // different URL parsing context, but currently shares the
  172. // same codepath)
  173. {
  174. "///threeslashes",
  175. &URL{
  176. Path: "///threeslashes",
  177. },
  178. "",
  179. },
  180. {
  181. "http://user:password@google.com",
  182. &URL{
  183. Scheme: "http",
  184. User: UserPassword("user", "password"),
  185. Host: "google.com",
  186. },
  187. "http://user:password@google.com",
  188. },
  189. // unescaped @ in username should not confuse host
  190. {
  191. "http://j@ne:password@google.com",
  192. &URL{
  193. Scheme: "http",
  194. User: UserPassword("j@ne", "password"),
  195. Host: "google.com",
  196. },
  197. "http://j%40ne:password@google.com",
  198. },
  199. // unescaped @ in password should not confuse host
  200. {
  201. "http://jane:p@ssword@google.com",
  202. &URL{
  203. Scheme: "http",
  204. User: UserPassword("jane", "p@ssword"),
  205. Host: "google.com",
  206. },
  207. "http://jane:p%40ssword@google.com",
  208. },
  209. {
  210. "http://j@ne:password@google.com/p@th?q=@go",
  211. &URL{
  212. Scheme: "http",
  213. User: UserPassword("j@ne", "password"),
  214. Host: "google.com",
  215. Path: "/p@th",
  216. RawQuery: "q=@go",
  217. },
  218. "http://j%40ne:password@google.com/p@th?q=@go",
  219. },
  220. {
  221. "http://www.google.com/?q=go+language#foo",
  222. &URL{
  223. Scheme: "http",
  224. Host: "www.google.com",
  225. Path: "/",
  226. RawQuery: "q=go+language",
  227. Fragment: "foo",
  228. },
  229. "",
  230. },
  231. {
  232. "http://www.google.com/?q=go+language#foo%26bar",
  233. &URL{
  234. Scheme: "http",
  235. Host: "www.google.com",
  236. Path: "/",
  237. RawQuery: "q=go+language",
  238. Fragment: "foo&bar",
  239. },
  240. "http://www.google.com/?q=go+language#foo&bar",
  241. },
  242. {
  243. "file:///home/adg/rabbits",
  244. &URL{
  245. Scheme: "file",
  246. Host: "",
  247. Path: "/home/adg/rabbits",
  248. },
  249. "file:///home/adg/rabbits",
  250. },
  251. // "Windows" paths are no exception to the rule.
  252. // See golang.org/issue/6027, especially comment #9.
  253. {
  254. "file:///C:/FooBar/Baz.txt",
  255. &URL{
  256. Scheme: "file",
  257. Host: "",
  258. Path: "/C:/FooBar/Baz.txt",
  259. },
  260. "file:///C:/FooBar/Baz.txt",
  261. },
  262. // case-insensitive scheme
  263. {
  264. "MaIlTo:webmaster@golang.org",
  265. &URL{
  266. Scheme: "mailto",
  267. Opaque: "webmaster@golang.org",
  268. },
  269. "mailto:webmaster@golang.org",
  270. },
  271. // Relative path
  272. {
  273. "a/b/c",
  274. &URL{
  275. Path: "a/b/c",
  276. },
  277. "a/b/c",
  278. },
  279. // escaped '?' in username and password
  280. {
  281. "http://%3Fam:pa%3Fsword@google.com",
  282. &URL{
  283. Scheme: "http",
  284. User: UserPassword("?am", "pa?sword"),
  285. Host: "google.com",
  286. },
  287. "",
  288. },
  289. // host subcomponent; IPv4 address in RFC 3986
  290. {
  291. "http://192.168.0.1/",
  292. &URL{
  293. Scheme: "http",
  294. Host: "192.168.0.1",
  295. Path: "/",
  296. },
  297. "",
  298. },
  299. // host and port subcomponents; IPv4 address in RFC 3986
  300. {
  301. "http://192.168.0.1:8080/",
  302. &URL{
  303. Scheme: "http",
  304. Host: "192.168.0.1:8080",
  305. Path: "/",
  306. },
  307. "",
  308. },
  309. // host subcomponent; IPv6 address in RFC 3986
  310. {
  311. "http://[fe80::1]/",
  312. &URL{
  313. Scheme: "http",
  314. Host: "[fe80::1]",
  315. Path: "/",
  316. },
  317. "",
  318. },
  319. // host and port subcomponents; IPv6 address in RFC 3986
  320. {
  321. "http://[fe80::1]:8080/",
  322. &URL{
  323. Scheme: "http",
  324. Host: "[fe80::1]:8080",
  325. Path: "/",
  326. },
  327. "",
  328. },
  329. // host subcomponent; IPv6 address with zone identifier in RFC 6847
  330. {
  331. "http://[fe80::1%25en0]/", // alphanum zone identifier
  332. &URL{
  333. Scheme: "http",
  334. Host: "[fe80::1%en0]",
  335. Path: "/",
  336. },
  337. "",
  338. },
  339. // host and port subcomponents; IPv6 address with zone identifier in RFC 6847
  340. {
  341. "http://[fe80::1%25en0]:8080/", // alphanum zone identifier
  342. &URL{
  343. Scheme: "http",
  344. Host: "[fe80::1%en0]:8080",
  345. Path: "/",
  346. },
  347. "",
  348. },
  349. // host subcomponent; IPv6 address with zone identifier in RFC 6847
  350. {
  351. "http://[fe80::1%25%65%6e%301-._~]/", // percent-encoded+unreserved zone identifier
  352. &URL{
  353. Scheme: "http",
  354. Host: "[fe80::1%en01-._~]",
  355. Path: "/",
  356. },
  357. "http://[fe80::1%25en01-._~]/",
  358. },
  359. // host and port subcomponents; IPv6 address with zone identifier in RFC 6847
  360. {
  361. "http://[fe80::1%25%65%6e%301-._~]:8080/", // percent-encoded+unreserved zone identifier
  362. &URL{
  363. Scheme: "http",
  364. Host: "[fe80::1%en01-._~]:8080",
  365. Path: "/",
  366. },
  367. "http://[fe80::1%25en01-._~]:8080/",
  368. },
  369. // alternate escapings of path survive round trip
  370. {
  371. "http://rest.rsc.io/foo%2fbar/baz%2Fquux?alt=media",
  372. &URL{
  373. Scheme: "http",
  374. Host: "rest.rsc.io",
  375. Path: "/foo/bar/baz/quux",
  376. RawPath: "/foo%2fbar/baz%2Fquux",
  377. RawQuery: "alt=media",
  378. },
  379. "",
  380. },
  381. // issue 12036
  382. {
  383. "mysql://a,b,c/bar",
  384. &URL{
  385. Scheme: "mysql",
  386. Host: "a,b,c",
  387. Path: "/bar",
  388. },
  389. "",
  390. },
  391. // worst case host, still round trips
  392. {
  393. "scheme://!$&'()*+,;=hello!:port/path",
  394. &URL{
  395. Scheme: "scheme",
  396. Host: "!$&'()*+,;=hello!:port",
  397. Path: "/path",
  398. },
  399. "",
  400. },
  401. // worst case path, still round trips
  402. {
  403. "http://host/!$&'()*+,;=:@[hello]",
  404. &URL{
  405. Scheme: "http",
  406. Host: "host",
  407. Path: "/!$&'()*+,;=:@[hello]",
  408. RawPath: "/!$&'()*+,;=:@[hello]",
  409. },
  410. "",
  411. },
  412. // golang.org/issue/5684
  413. {
  414. "http://example.com/oid/[order_id]",
  415. &URL{
  416. Scheme: "http",
  417. Host: "example.com",
  418. Path: "/oid/[order_id]",
  419. RawPath: "/oid/[order_id]",
  420. },
  421. "",
  422. },
  423. }
  424. // more useful string for debugging than fmt's struct printer
  425. func ufmt(u *URL) string {
  426. var user, pass interface{}
  427. if u.User != nil {
  428. user = u.User.Username()
  429. if p, ok := u.User.Password(); ok {
  430. pass = p
  431. }
  432. }
  433. return fmt.Sprintf("opaque=%q, scheme=%q, user=%#v, pass=%#v, host=%q, path=%q, rawpath=%q, rawq=%q, frag=%q",
  434. u.Opaque, u.Scheme, user, pass, u.Host, u.Path, u.RawPath, u.RawQuery, u.Fragment)
  435. }
  436. func DoTest(t *testing.T, parse func(string) (*URL, error), name string, tests []URLTest) {
  437. for _, tt := range tests {
  438. u, err := parse(tt.in)
  439. if err != nil {
  440. t.Errorf("%s(%q) returned error %s", name, tt.in, err)
  441. continue
  442. }
  443. if !reflect.DeepEqual(u, tt.out) {
  444. t.Errorf("%s(%q):\n\thave %v\n\twant %v\n",
  445. name, tt.in, ufmt(u), ufmt(tt.out))
  446. }
  447. }
  448. }
  449. func BenchmarkString(b *testing.B) {
  450. b.StopTimer()
  451. b.ReportAllocs()
  452. for _, tt := range urltests {
  453. u, err := Parse(tt.in)
  454. if err != nil {
  455. b.Errorf("Parse(%q) returned error %s", tt.in, err)
  456. continue
  457. }
  458. if tt.roundtrip == "" {
  459. continue
  460. }
  461. b.StartTimer()
  462. var g string
  463. for i := 0; i < b.N; i++ {
  464. g = u.String()
  465. }
  466. b.StopTimer()
  467. if w := tt.roundtrip; g != w {
  468. b.Errorf("Parse(%q).String() == %q, want %q", tt.in, g, w)
  469. }
  470. }
  471. }
  472. func TestParse(t *testing.T) {
  473. DoTest(t, Parse, "Parse", urltests)
  474. }
  475. const pathThatLooksSchemeRelative = "//not.a.user@not.a.host/just/a/path"
  476. var parseRequestURLTests = []struct {
  477. url string
  478. expectedValid bool
  479. }{
  480. {"http://foo.com", true},
  481. {"http://foo.com/", true},
  482. {"http://foo.com/path", true},
  483. {"/", true},
  484. {pathThatLooksSchemeRelative, true},
  485. {"//not.a.user@%66%6f%6f.com/just/a/path/also", true},
  486. {"*", true},
  487. {"http://192.168.0.1/", true},
  488. {"http://192.168.0.1:8080/", true},
  489. {"http://[fe80::1]/", true},
  490. {"http://[fe80::1]:8080/", true},
  491. // Tests exercising RFC 6874 compliance:
  492. {"http://[fe80::1%25en0]/", true}, // with alphanum zone identifier
  493. {"http://[fe80::1%25en0]:8080/", true}, // with alphanum zone identifier
  494. {"http://[fe80::1%25%65%6e%301-._~]/", true}, // with percent-encoded+unreserved zone identifier
  495. {"http://[fe80::1%25%65%6e%301-._~]:8080/", true}, // with percent-encoded+unreserved zone identifier
  496. {"foo.html", false},
  497. {"../dir/", false},
  498. {"http://192.168.0.%31/", false},
  499. {"http://192.168.0.%31:8080/", false},
  500. {"http://[fe80::%31]/", false},
  501. {"http://[fe80::%31]:8080/", false},
  502. {"http://[fe80::%31%25en0]/", false},
  503. {"http://[fe80::%31%25en0]:8080/", false},
  504. // These two cases are valid as textual representations as
  505. // described in RFC 4007, but are not valid as address
  506. // literals with IPv6 zone identifiers in URIs as described in
  507. // RFC 6874.
  508. {"http://[fe80::1%en0]/", false},
  509. {"http://[fe80::1%en0]:8080/", false},
  510. }
  511. func TestParseRequestURI(t *testing.T) {
  512. for _, test := range parseRequestURLTests {
  513. _, err := ParseRequestURI(test.url)
  514. valid := err == nil
  515. if valid != test.expectedValid {
  516. t.Errorf("Expected valid=%v for %q; got %v", test.expectedValid, test.url, valid)
  517. }
  518. }
  519. url, err := ParseRequestURI(pathThatLooksSchemeRelative)
  520. if err != nil {
  521. t.Fatalf("Unexpected error %v", err)
  522. }
  523. if url.Path != pathThatLooksSchemeRelative {
  524. t.Errorf("Expected path %q; got %q", pathThatLooksSchemeRelative, url.Path)
  525. }
  526. }
  527. func DoTestString(t *testing.T, parse func(string) (*URL, error), name string, tests []URLTest) {
  528. for _, tt := range tests {
  529. u, err := parse(tt.in)
  530. if err != nil {
  531. t.Errorf("%s(%q) returned error %s", name, tt.in, err)
  532. continue
  533. }
  534. expected := tt.in
  535. if len(tt.roundtrip) > 0 {
  536. expected = tt.roundtrip
  537. }
  538. s := u.String()
  539. if s != expected {
  540. t.Errorf("%s(%q).String() == %q (expected %q)", name, tt.in, s, expected)
  541. }
  542. }
  543. }
  544. func TestURLString(t *testing.T) {
  545. DoTestString(t, Parse, "Parse", urltests)
  546. // no leading slash on path should prepend
  547. // slash on String() call
  548. noslash := URLTest{
  549. "http://www.google.com/search",
  550. &URL{
  551. Scheme: "http",
  552. Host: "www.google.com",
  553. Path: "search",
  554. },
  555. "",
  556. }
  557. s := noslash.out.String()
  558. if s != noslash.in {
  559. t.Errorf("Expected %s; go %s", noslash.in, s)
  560. }
  561. }
  562. type EscapeTest struct {
  563. in string
  564. out string
  565. err error
  566. }
  567. var unescapeTests = []EscapeTest{
  568. {
  569. "",
  570. "",
  571. nil,
  572. },
  573. {
  574. "abc",
  575. "abc",
  576. nil,
  577. },
  578. {
  579. "1%41",
  580. "1A",
  581. nil,
  582. },
  583. {
  584. "1%41%42%43",
  585. "1ABC",
  586. nil,
  587. },
  588. {
  589. "%4a",
  590. "J",
  591. nil,
  592. },
  593. {
  594. "%6F",
  595. "o",
  596. nil,
  597. },
  598. {
  599. "%", // not enough characters after %
  600. "",
  601. EscapeError("%"),
  602. },
  603. {
  604. "%a", // not enough characters after %
  605. "",
  606. EscapeError("%a"),
  607. },
  608. {
  609. "%1", // not enough characters after %
  610. "",
  611. EscapeError("%1"),
  612. },
  613. {
  614. "123%45%6", // not enough characters after %
  615. "",
  616. EscapeError("%6"),
  617. },
  618. {
  619. "%zzzzz", // invalid hex digits
  620. "",
  621. EscapeError("%zz"),
  622. },
  623. }
  624. func TestUnescape(t *testing.T) {
  625. for _, tt := range unescapeTests {
  626. actual, err := QueryUnescape(tt.in)
  627. if actual != tt.out || (err != nil) != (tt.err != nil) {
  628. t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", tt.in, actual, err, tt.out, tt.err)
  629. }
  630. }
  631. }
  632. var escapeTests = []EscapeTest{
  633. {
  634. "",
  635. "",
  636. nil,
  637. },
  638. {
  639. "abc",
  640. "abc",
  641. nil,
  642. },
  643. {
  644. "one two",
  645. "one+two",
  646. nil,
  647. },
  648. {
  649. "10%",
  650. "10%25",
  651. nil,
  652. },
  653. {
  654. " ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;",
  655. "+%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",
  656. nil,
  657. },
  658. }
  659. func TestEscape(t *testing.T) {
  660. for _, tt := range escapeTests {
  661. actual := QueryEscape(tt.in)
  662. if tt.out != actual {
  663. t.Errorf("QueryEscape(%q) = %q, want %q", tt.in, actual, tt.out)
  664. }
  665. // for bonus points, verify that escape:unescape is an identity.
  666. roundtrip, err := QueryUnescape(actual)
  667. if roundtrip != tt.in || err != nil {
  668. t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]")
  669. }
  670. }
  671. }
  672. //var userinfoTests = []UserinfoTest{
  673. // {"user", "password", "user:password"},
  674. // {"foo:bar", "~!@#$%^&*()_+{}|[]\\-=`:;'\"<>?,./",
  675. // "foo%3Abar:~!%40%23$%25%5E&*()_+%7B%7D%7C%5B%5D%5C-=%60%3A;'%22%3C%3E?,.%2F"},
  676. //}
  677. type EncodeQueryTest struct {
  678. m Values
  679. expected string
  680. }
  681. var encodeQueryTests = []EncodeQueryTest{
  682. {nil, ""},
  683. {Values{"q": {"puppies"}, "oe": {"utf8"}}, "oe=utf8&q=puppies"},
  684. {Values{"q": {"dogs", "&", "7"}}, "q=dogs&q=%26&q=7"},
  685. {Values{
  686. "a": {"a1", "a2", "a3"},
  687. "b": {"b1", "b2", "b3"},
  688. "c": {"c1", "c2", "c3"},
  689. }, "a=a1&a=a2&a=a3&b=b1&b=b2&b=b3&c=c1&c=c2&c=c3"},
  690. }
  691. func TestEncodeQuery(t *testing.T) {
  692. for _, tt := range encodeQueryTests {
  693. if q := tt.m.Encode(); q != tt.expected {
  694. t.Errorf(`EncodeQuery(%+v) = %q, want %q`, tt.m, q, tt.expected)
  695. }
  696. }
  697. }
  698. var resolvePathTests = []struct {
  699. base, ref, expected string
  700. }{
  701. {"a/b", ".", "/a/"},
  702. {"a/b", "c", "/a/c"},
  703. {"a/b", "..", "/"},
  704. {"a/", "..", "/"},
  705. {"a/", "../..", "/"},
  706. {"a/b/c", "..", "/a/"},
  707. {"a/b/c", "../d", "/a/d"},
  708. {"a/b/c", ".././d", "/a/d"},
  709. {"a/b", "./..", "/"},
  710. {"a/./b", ".", "/a/"},
  711. {"a/../", ".", "/"},
  712. {"a/.././b", "c", "/c"},
  713. }
  714. func TestResolvePath(t *testing.T) {
  715. for _, test := range resolvePathTests {
  716. got := resolvePath(test.base, test.ref)
  717. if got != test.expected {
  718. t.Errorf("For %q + %q got %q; expected %q", test.base, test.ref, got, test.expected)
  719. }
  720. }
  721. }
  722. var resolveReferenceTests = []struct {
  723. base, rel, expected string
  724. }{
  725. // Absolute URL references
  726. {"http://foo.com?a=b", "https://bar.com/", "https://bar.com/"},
  727. {"http://foo.com/", "https://bar.com/?a=b", "https://bar.com/?a=b"},
  728. {"http://foo.com/bar", "mailto:foo@example.com", "mailto:foo@example.com"},
  729. // Path-absolute references
  730. {"http://foo.com/bar", "/baz", "http://foo.com/baz"},
  731. {"http://foo.com/bar?a=b#f", "/baz", "http://foo.com/baz"},
  732. {"http://foo.com/bar?a=b", "/baz?c=d", "http://foo.com/baz?c=d"},
  733. // Scheme-relative
  734. {"https://foo.com/bar?a=b", "//bar.com/quux", "https://bar.com/quux"},
  735. // Path-relative references:
  736. // ... current directory
  737. {"http://foo.com", ".", "http://foo.com/"},
  738. {"http://foo.com/bar", ".", "http://foo.com/"},
  739. {"http://foo.com/bar/", ".", "http://foo.com/bar/"},
  740. // ... going down
  741. {"http://foo.com", "bar", "http://foo.com/bar"},
  742. {"http://foo.com/", "bar", "http://foo.com/bar"},
  743. {"http://foo.com/bar/baz", "quux", "http://foo.com/bar/quux"},
  744. // ... going up
  745. {"http://foo.com/bar/baz", "../quux", "http://foo.com/quux"},
  746. {"http://foo.com/bar/baz", "../../../../../quux", "http://foo.com/quux"},
  747. {"http://foo.com/bar", "..", "http://foo.com/"},
  748. {"http://foo.com/bar/baz", "./..", "http://foo.com/"},
  749. // ".." in the middle (issue 3560)
  750. {"http://foo.com/bar/baz", "quux/dotdot/../tail", "http://foo.com/bar/quux/tail"},
  751. {"http://foo.com/bar/baz", "quux/./dotdot/../tail", "http://foo.com/bar/quux/tail"},
  752. {"http://foo.com/bar/baz", "quux/./dotdot/.././tail", "http://foo.com/bar/quux/tail"},
  753. {"http://foo.com/bar/baz", "quux/./dotdot/./../tail", "http://foo.com/bar/quux/tail"},
  754. {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/././../../tail", "http://foo.com/bar/quux/tail"},
  755. {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/./.././../tail", "http://foo.com/bar/quux/tail"},
  756. {"http://foo.com/bar/baz", "quux/./dotdot/dotdot/dotdot/./../../.././././tail", "http://foo.com/bar/quux/tail"},
  757. {"http://foo.com/bar/baz", "quux/./dotdot/../dotdot/../dot/./tail/..", "http://foo.com/bar/quux/dot/"},
  758. // Remove any dot-segments prior to forming the target URI.
  759. // http://tools.ietf.org/html/rfc3986#section-5.2.4
  760. {"http://foo.com/dot/./dotdot/../foo/bar", "../baz", "http://foo.com/dot/baz"},
  761. // Triple dot isn't special
  762. {"http://foo.com/bar", "...", "http://foo.com/..."},
  763. // Fragment
  764. {"http://foo.com/bar", ".#frag", "http://foo.com/#frag"},
  765. // RFC 3986: Normal Examples
  766. // http://tools.ietf.org/html/rfc3986#section-5.4.1
  767. {"http://a/b/c/d;p?q", "g:h", "g:h"},
  768. {"http://a/b/c/d;p?q", "g", "http://a/b/c/g"},
  769. {"http://a/b/c/d;p?q", "./g", "http://a/b/c/g"},
  770. {"http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"},
  771. {"http://a/b/c/d;p?q", "/g", "http://a/g"},
  772. {"http://a/b/c/d;p?q", "//g", "http://g"},
  773. {"http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y"},
  774. {"http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"},
  775. {"http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s"},
  776. {"http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"},
  777. {"http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"},
  778. {"http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"},
  779. {"http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"},
  780. {"http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"},
  781. {"http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q"},
  782. {"http://a/b/c/d;p?q", ".", "http://a/b/c/"},
  783. {"http://a/b/c/d;p?q", "./", "http://a/b/c/"},
  784. {"http://a/b/c/d;p?q", "..", "http://a/b/"},
  785. {"http://a/b/c/d;p?q", "../", "http://a/b/"},
  786. {"http://a/b/c/d;p?q", "../g", "http://a/b/g"},
  787. {"http://a/b/c/d;p?q", "../..", "http://a/"},
  788. {"http://a/b/c/d;p?q", "../../", "http://a/"},
  789. {"http://a/b/c/d;p?q", "../../g", "http://a/g"},
  790. // RFC 3986: Abnormal Examples
  791. // http://tools.ietf.org/html/rfc3986#section-5.4.2
  792. {"http://a/b/c/d;p?q", "../../../g", "http://a/g"},
  793. {"http://a/b/c/d;p?q", "../../../../g", "http://a/g"},
  794. {"http://a/b/c/d;p?q", "/./g", "http://a/g"},
  795. {"http://a/b/c/d;p?q", "/../g", "http://a/g"},
  796. {"http://a/b/c/d;p?q", "g.", "http://a/b/c/g."},
  797. {"http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"},
  798. {"http://a/b/c/d;p?q", "g..", "http://a/b/c/g.."},
  799. {"http://a/b/c/d;p?q", "..g", "http://a/b/c/..g"},
  800. {"http://a/b/c/d;p?q", "./../g", "http://a/b/g"},
  801. {"http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"},
  802. {"http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"},
  803. {"http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h"},
  804. {"http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"},
  805. {"http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y"},
  806. {"http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x"},
  807. {"http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x"},
  808. {"http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x"},
  809. {"http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x"},
  810. // Extras.
  811. {"https://a/b/c/d;p?q", "//g?q", "https://g?q"},
  812. {"https://a/b/c/d;p?q", "//g#s", "https://g#s"},
  813. {"https://a/b/c/d;p?q", "//g/d/e/f?y#s", "https://g/d/e/f?y#s"},
  814. {"https://a/b/c/d;p#s", "?y", "https://a/b/c/d;p?y"},
  815. {"https://a/b/c/d;p?q#s", "?y", "https://a/b/c/d;p?y"},
  816. }
  817. func TestResolveReference(t *testing.T) {
  818. mustParse := func(url string) *URL {
  819. u, err := Parse(url)
  820. if err != nil {
  821. t.Fatalf("Expected URL to parse: %q, got error: %v", url, err)
  822. }
  823. return u
  824. }
  825. opaque := &URL{Scheme: "scheme", Opaque: "opaque"}
  826. for _, test := range resolveReferenceTests {
  827. base := mustParse(test.base)
  828. rel := mustParse(test.rel)
  829. url := base.ResolveReference(rel)
  830. if url.String() != test.expected {
  831. t.Errorf("URL(%q).ResolveReference(%q) == %q, got %q", test.base, test.rel, test.expected, url.String())
  832. }
  833. // Ensure that new instances are returned.
  834. if base == url {
  835. t.Errorf("Expected URL.ResolveReference to return new URL instance.")
  836. }
  837. // Test the convenience wrapper too.
  838. url, err := base.Parse(test.rel)
  839. if err != nil {
  840. t.Errorf("URL(%q).Parse(%q) failed: %v", test.base, test.rel, err)
  841. } else if url.String() != test.expected {
  842. t.Errorf("URL(%q).Parse(%q) == %q, got %q", test.base, test.rel, test.expected, url.String())
  843. } else if base == url {
  844. // Ensure that new instances are returned for the wrapper too.
  845. t.Errorf("Expected URL.Parse to return new URL instance.")
  846. }
  847. // Ensure Opaque resets the URL.
  848. url = base.ResolveReference(opaque)
  849. if *url != *opaque {
  850. t.Errorf("ResolveReference failed to resolve opaque URL: want %#v, got %#v", url, opaque)
  851. }
  852. // Test the convenience wrapper with an opaque URL too.
  853. url, err = base.Parse("scheme:opaque")
  854. if err != nil {
  855. t.Errorf(`URL(%q).Parse("scheme:opaque") failed: %v`, test.base, err)
  856. } else if *url != *opaque {
  857. t.Errorf("Parse failed to resolve opaque URL: want %#v, got %#v", url, opaque)
  858. } else if base == url {
  859. // Ensure that new instances are returned, again.
  860. t.Errorf("Expected URL.Parse to return new URL instance.")
  861. }
  862. }
  863. }
  864. func TestQueryValues(t *testing.T) {
  865. u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2")
  866. v := u.Query()
  867. if len(v) != 2 {
  868. t.Errorf("got %d keys in Query values, want 2", len(v))
  869. }
  870. if g, e := v.Get("foo"), "bar"; g != e {
  871. t.Errorf("Get(foo) = %q, want %q", g, e)
  872. }
  873. // Case sensitive:
  874. if g, e := v.Get("Foo"), ""; g != e {
  875. t.Errorf("Get(Foo) = %q, want %q", g, e)
  876. }
  877. if g, e := v.Get("bar"), "1"; g != e {
  878. t.Errorf("Get(bar) = %q, want %q", g, e)
  879. }
  880. if g, e := v.Get("baz"), ""; g != e {
  881. t.Errorf("Get(baz) = %q, want %q", g, e)
  882. }
  883. v.Del("bar")
  884. if g, e := v.Get("bar"), ""; g != e {
  885. t.Errorf("second Get(bar) = %q, want %q", g, e)
  886. }
  887. }
  888. type parseTest struct {
  889. query string
  890. out Values
  891. }
  892. var parseTests = []parseTest{
  893. {
  894. query: "a=1&b=2",
  895. out: Values{"a": []string{"1"}, "b": []string{"2"}},
  896. },
  897. {
  898. query: "a=1&a=2&a=banana",
  899. out: Values{"a": []string{"1", "2", "banana"}},
  900. },
  901. {
  902. query: "ascii=%3Ckey%3A+0x90%3E",
  903. out: Values{"ascii": []string{"<key: 0x90>"}},
  904. },
  905. {
  906. query: "a=1;b=2",
  907. out: Values{"a": []string{"1"}, "b": []string{"2"}},
  908. },
  909. {
  910. query: "a=1&a=2;a=banana",
  911. out: Values{"a": []string{"1", "2", "banana"}},
  912. },
  913. }
  914. func TestParseQuery(t *testing.T) {
  915. for i, test := range parseTests {
  916. form, err := ParseQuery(test.query)
  917. if err != nil {
  918. t.Errorf("test %d: Unexpected error: %v", i, err)
  919. continue
  920. }
  921. if len(form) != len(test.out) {
  922. t.Errorf("test %d: len(form) = %d, want %d", i, len(form), len(test.out))
  923. }
  924. for k, evs := range test.out {
  925. vs, ok := form[k]
  926. if !ok {
  927. t.Errorf("test %d: Missing key %q", i, k)
  928. continue
  929. }
  930. if len(vs) != len(evs) {
  931. t.Errorf("test %d: len(form[%q]) = %d, want %d", i, k, len(vs), len(evs))
  932. continue
  933. }
  934. for j, ev := range evs {
  935. if v := vs[j]; v != ev {
  936. t.Errorf("test %d: form[%q][%d] = %q, want %q", i, k, j, v, ev)
  937. }
  938. }
  939. }
  940. }
  941. }
  942. type RequestURITest struct {
  943. url *URL
  944. out string
  945. }
  946. var requritests = []RequestURITest{
  947. {
  948. &URL{
  949. Scheme: "http",
  950. Host: "example.com",
  951. Path: "",
  952. },
  953. "/",
  954. },
  955. {
  956. &URL{
  957. Scheme: "http",
  958. Host: "example.com",
  959. Path: "/a b",
  960. },
  961. "/a%20b",
  962. },
  963. // golang.org/issue/4860 variant 1
  964. {
  965. &URL{
  966. Scheme: "http",
  967. Host: "example.com",
  968. Opaque: "/%2F/%2F/",
  969. },
  970. "/%2F/%2F/",
  971. },
  972. // golang.org/issue/4860 variant 2
  973. {
  974. &URL{
  975. Scheme: "http",
  976. Host: "example.com",
  977. Opaque: "//other.example.com/%2F/%2F/",
  978. },
  979. "http://other.example.com/%2F/%2F/",
  980. },
  981. // better fix for issue 4860
  982. {
  983. &URL{
  984. Scheme: "http",
  985. Host: "example.com",
  986. Path: "/////",
  987. RawPath: "/%2F/%2F/",
  988. },
  989. "/%2F/%2F/",
  990. },
  991. {
  992. &URL{
  993. Scheme: "http",
  994. Host: "example.com",
  995. Path: "/////",
  996. RawPath: "/WRONG/", // ignored because doesn't match Path
  997. },
  998. "/////",
  999. },
  1000. {
  1001. &URL{
  1002. Scheme: "http",
  1003. Host: "example.com",
  1004. Path: "/a b",
  1005. RawQuery: "q=go+language",
  1006. },
  1007. "/a%20b?q=go+language",
  1008. },
  1009. {
  1010. &URL{
  1011. Scheme: "http",
  1012. Host: "example.com",
  1013. Path: "/a b",
  1014. RawPath: "/a b", // ignored because invalid
  1015. RawQuery: "q=go+language",
  1016. },
  1017. "/a%20b?q=go+language",
  1018. },
  1019. {
  1020. &URL{
  1021. Scheme: "http",
  1022. Host: "example.com",
  1023. Path: "/a?b",
  1024. RawPath: "/a?b", // ignored because invalid
  1025. RawQuery: "q=go+language",
  1026. },
  1027. "/a%3Fb?q=go+language",
  1028. },
  1029. {
  1030. &URL{
  1031. Scheme: "myschema",
  1032. Opaque: "opaque",
  1033. },
  1034. "opaque",
  1035. },
  1036. {
  1037. &URL{
  1038. Scheme: "myschema",
  1039. Opaque: "opaque",
  1040. RawQuery: "q=go+language",
  1041. },
  1042. "opaque?q=go+language",
  1043. },
  1044. }
  1045. func TestRequestURI(t *testing.T) {
  1046. for _, tt := range requritests {
  1047. s := tt.url.RequestURI()
  1048. if s != tt.out {
  1049. t.Errorf("%#v.RequestURI() == %q (expected %q)", tt.url, s, tt.out)
  1050. }
  1051. }
  1052. }
  1053. func TestParseFailure(t *testing.T) {
  1054. // Test that the first parse error is returned.
  1055. const url = "%gh&%ij"
  1056. _, err := ParseQuery(url)
  1057. errStr := fmt.Sprint(err)
  1058. if !strings.Contains(errStr, "%gh") {
  1059. t.Errorf(`ParseQuery(%q) returned error %q, want something containing %q"`, url, errStr, "%gh")
  1060. }
  1061. }
  1062. func TestParseAuthority(t *testing.T) {
  1063. tests := []struct {
  1064. in string
  1065. wantErr bool
  1066. }{
  1067. {"http://[::1]", false},
  1068. {"http://[::1]:80", false},
  1069. {"http://[::1]:namedport", true}, // rfc3986 3.2.3
  1070. {"http://[::1]/", false},
  1071. {"http://[::1]a", true},
  1072. {"http://[::1]%23", true},
  1073. {"http://[::1%25en0]", false}, // valid zone id
  1074. {"http://[::1]:", true}, // colon, but no port
  1075. {"http://[::1]:%38%30", true}, // no hex in port
  1076. {"http://[::1%25%10]", false}, // TODO: reject the %10 after the valid zone %25 separator?
  1077. {"http://[%10::1]", true}, // no %xx escapes in IP address
  1078. {"http://[::1]/%48", false}, // %xx in path is fine
  1079. {"http://%41:8080/", true}, // TODO: arguably we should accept reg-name with %xx
  1080. {"mysql://x@y(z:123)/foo", false}, // golang.org/issue/12023
  1081. {"mysql://x@y(1.2.3.4:123)/foo", false},
  1082. {"mysql://x@y([2001:db8::1]:123)/foo", false},
  1083. {"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
  1084. }
  1085. for _, tt := range tests {
  1086. u, err := Parse(tt.in)
  1087. if tt.wantErr {
  1088. if err == nil {
  1089. t.Errorf("Parse(%q) = %#v; want an error", tt.in, u)
  1090. }
  1091. continue
  1092. }
  1093. if err != nil {
  1094. t.Logf("Parse(%q) = %v; want no error", tt.in, err)
  1095. }
  1096. }
  1097. }
  1098. // Issue 11202
  1099. func TestStarRequest(t *testing.T) {
  1100. u, err := Parse("*")
  1101. if err != nil {
  1102. t.Fatal(err)
  1103. }
  1104. if got, want := u.RequestURI(), "*"; got != want {
  1105. t.Errorf("RequestURI = %q; want %q", got, want)
  1106. }
  1107. }
  1108. type shouldEscapeTest struct {
  1109. in byte
  1110. mode encoding
  1111. escape bool
  1112. }
  1113. var shouldEscapeTests = []shouldEscapeTest{
  1114. // Unreserved characters (§2.3)
  1115. {'a', encodePath, false},
  1116. {'a', encodeUserPassword, false},
  1117. {'a', encodeQueryComponent, false},
  1118. {'a', encodeFragment, false},
  1119. {'a', encodeHost, false},
  1120. {'z', encodePath, false},
  1121. {'A', encodePath, false},
  1122. {'Z', encodePath, false},
  1123. {'0', encodePath, false},
  1124. {'9', encodePath, false},
  1125. {'-', encodePath, false},
  1126. {'-', encodeUserPassword, false},
  1127. {'-', encodeQueryComponent, false},
  1128. {'-', encodeFragment, false},
  1129. {'.', encodePath, false},
  1130. {'_', encodePath, false},
  1131. {'~', encodePath, false},
  1132. // User information (§3.2.1)
  1133. {':', encodeUserPassword, true},
  1134. {'/', encodeUserPassword, true},
  1135. {'?', encodeUserPassword, true},
  1136. {'@', encodeUserPassword, true},
  1137. {'$', encodeUserPassword, false},
  1138. {'&', encodeUserPassword, false},
  1139. {'+', encodeUserPassword, false},
  1140. {',', encodeUserPassword, false},
  1141. {';', encodeUserPassword, false},
  1142. {'=', encodeUserPassword, false},
  1143. // Host (IP address, IPv6 address, registered name, port suffix; §3.2.2)
  1144. {'!', encodeHost, false},
  1145. {'$', encodeHost, false},
  1146. {'&', encodeHost, false},
  1147. {'\'', encodeHost, false},
  1148. {'(', encodeHost, false},
  1149. {')', encodeHost, false},
  1150. {'*', encodeHost, false},
  1151. {'+', encodeHost, false},
  1152. {',', encodeHost, false},
  1153. {';', encodeHost, false},
  1154. {'=', encodeHost, false},
  1155. {':', encodeHost, false},
  1156. {'[', encodeHost, false},
  1157. {']', encodeHost, false},
  1158. {'0', encodeHost, false},
  1159. {'9', encodeHost, false},
  1160. {'A', encodeHost, false},
  1161. {'z', encodeHost, false},
  1162. {'_', encodeHost, false},
  1163. {'-', encodeHost, false},
  1164. {'.', encodeHost, false},
  1165. }
  1166. func TestShouldEscape(t *testing.T) {
  1167. for _, tt := range shouldEscapeTests {
  1168. if shouldEscape(tt.in, tt.mode) != tt.escape {
  1169. t.Errorf("shouldEscape(%q, %v) returned %v; expected %v", tt.in, tt.mode, !tt.escape, tt.escape)
  1170. }
  1171. }
  1172. }