| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package url |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
encodingPkg "encoding" |
| 10 |
"encoding/gob" |
| 11 |
"encoding/json" |
| 12 |
"fmt" |
| 13 |
"io" |
| 14 |
"net" |
| 15 |
"reflect" |
| 16 |
"strconv" |
| 17 |
"strings" |
| 18 |
"testing" |
| 19 |
) |
| 20 |
|
| 21 |
type URLTest struct { |
| 22 |
in string |
| 23 |
out *URL // expected parse |
| 24 |
roundtrip string // expected result of reserializing the URL; empty means same as "in". |
| 25 |
} |
| 26 |
|
| 27 |
var urltests = []URLTest{ |
| 28 |
// no path |
| 29 |
{ |
| 30 |
"http://www.google.com", |
| 31 |
&URL{ |
| 32 |
Scheme: "http", |
| 33 |
Host: "www.google.com", |
| 34 |
}, |
| 35 |
"", |
| 36 |
}, |
| 37 |
// path |
| 38 |
{ |
| 39 |
"http://www.google.com/", |
| 40 |
&URL{ |
| 41 |
Scheme: "http", |
| 42 |
Host: "www.google.com", |
| 43 |
Path: "/", |
| 44 |
}, |
| 45 |
"", |
| 46 |
}, |
| 47 |
// path with hex escaping |
| 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 |
// fragment with hex escaping |
| 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 |
// user |
| 71 |
{ |
| 72 |
"ftp://[email protected]/", |
| 73 |
&URL{ |
| 74 |
Scheme: "ftp", |
| 75 |
User: User("webmaster"), |
| 76 |
Host: "www.google.com", |
| 77 |
Path: "/", |
| 78 |
}, |
| 79 |
"", |
| 80 |
}, |
| 81 |
// escape sequence in username |
| 82 |
{ |
| 83 |
"ftp://john%[email protected]/", |
| 84 |
&URL{ |
| 85 |
Scheme: "ftp", |
| 86 |
User: User("john doe"), |
| 87 |
Host: "www.google.com", |
| 88 |
Path: "/", |
| 89 |
}, |
| 90 |
"ftp://john%[email protected]/", |
| 91 |
}, |
| 92 |
// empty query |
| 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 |
// query ending in question mark (Issue 14573) |
| 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 |
// query |
| 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 |
// query with hex escaping: NOT parsed |
| 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 |
// %20 outside query |
| 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 |
// path without leading /, so no parsing |
| 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 |
// path without leading /, so no parsing |
| 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 |
// non-authority with path; see golang.org/issue/46059 |
| 168 |
{ |
| 169 |
"mailto:/[email protected]", |
| 170 |
&URL{ |
| 171 |
Scheme: "mailto", |
| 172 |
Path: "/[email protected]", |
| 173 |
OmitHost: true, |
| 174 |
}, |
| 175 |
"", |
| 176 |
}, |
| 177 |
// non-authority |
| 178 |
{ |
| 179 |
"mailto:[email protected]", |
| 180 |
&URL{ |
| 181 |
Scheme: "mailto", |
| 182 |
Opaque: "[email protected]", |
| 183 |
}, |
| 184 |
"", |
| 185 |
}, |
| 186 |
// unescaped :// in query should not create a scheme |
| 187 |
{ |
| 188 |
"/foo?query=http://bad", |
| 189 |
&URL{ |
| 190 |
Path: "/foo", |
| 191 |
RawQuery: "query=http://bad", |
| 192 |
}, |
| 193 |
"", |
| 194 |
}, |
| 195 |
// leading // without scheme should create an authority |
| 196 |
{ |
| 197 |
"//foo", |
| 198 |
&URL{ |
| 199 |
Host: "foo", |
| 200 |
}, |
| 201 |
"", |
| 202 |
}, |
| 203 |
// leading // without scheme, with userinfo, path, and query |
| 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 |
// Three leading slashes isn't an authority, but doesn't return an error. |
| 215 |
// (We can't return an error, as this code is also used via |
| 216 |
// ServeHTTP -> ReadRequest -> Parse, which is arguably a |
| 217 |
// different URL parsing context, but currently shares the |
| 218 |
// same codepath) |
| 219 |
{ |
| 220 |
"///threeslashes", |
| 221 |
&URL{ |
| 222 |
Path: "///threeslashes", |
| 223 |
}, |
| 224 |
"", |
| 225 |
}, |
| 226 |
{ |
| 227 |
"http://user:[email protected]", |
| 228 |
&URL{ |
| 229 |
Scheme: "http", |
| 230 |
User: UserPassword("user", "password"), |
| 231 |
Host: "google.com", |
| 232 |
}, |
| 233 |
"http://user:[email protected]", |
| 234 |
}, |
| 235 |
// unescaped @ in username should not confuse host |
| 236 |
{ |
| 237 |
"http://j@ne:[email protected]", |
| 238 |
&URL{ |
| 239 |
Scheme: "http", |
| 240 |
User: UserPassword("j@ne", "password"), |
| 241 |
Host: "google.com", |
| 242 |
}, |
| 243 |
"http://j%40ne:[email protected]", |
| 244 |
}, |
| 245 |
// unescaped @ in password should not confuse host |
| 246 |
{ |
| 247 |
"http://jane:p@[email protected]", |
| 248 |
&URL{ |
| 249 |
Scheme: "http", |
| 250 |
User: UserPassword("jane", "p@ssword"), |
| 251 |
Host: "google.com", |
| 252 |
}, |
| 253 |
"http://jane:p%[email protected]", |
| 254 |
}, |
| 255 |
{ |
| 256 |
"http://j@ne:[email protected]/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:[email protected]/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 |
// "Windows" paths are no exception to the rule. |
| 310 |
// See golang.org/issue/6027, especially comment #9. |
| 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 |
// case-insensitive scheme |
| 321 |
{ |
| 322 |
"MaIlTo:[email protected]", |
| 323 |
&URL{ |
| 324 |
Scheme: "mailto", |
| 325 |
Opaque: "[email protected]", |
| 326 |
}, |
| 327 |
"mailto:[email protected]", |
| 328 |
}, |
| 329 |
// Relative path |
| 330 |
{ |
| 331 |
"a/b/c", |
| 332 |
&URL{ |
| 333 |
Path: "a/b/c", |
| 334 |
}, |
| 335 |
"a/b/c", |
| 336 |
}, |
| 337 |
// escaped '?' in username and password |
| 338 |
{ |
| 339 |
"http://%3Fam:pa%[email protected]", |
| 340 |
&URL{ |
| 341 |
Scheme: "http", |
| 342 |
User: UserPassword("?am", "pa?sword"), |
| 343 |
Host: "google.com", |
| 344 |
}, |
| 345 |
"", |
| 346 |
}, |
| 347 |
// host subcomponent; IPv4 address in RFC 3986 |
| 348 |
{ |
| 349 |
"http://192.168.0.1/", |
| 350 |
&URL{ |
| 351 |
Scheme: "http", |
| 352 |
Host: "192.168.0.1", |
| 353 |
Path: "/", |
| 354 |
}, |
| 355 |
"", |
| 356 |
}, |
| 357 |
// host and port subcomponents; IPv4 address in RFC 3986 |
| 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 |
// host subcomponent; IPv6 address in RFC 3986 |
| 368 |
{ |
| 369 |
"http://[fe80::1]/", |
| 370 |
&URL{ |
| 371 |
Scheme: "http", |
| 372 |
Host: "[fe80::1]", |
| 373 |
Path: "/", |
| 374 |
}, |
| 375 |
"", |
| 376 |
}, |
| 377 |
// host and port subcomponents; IPv6 address in RFC 3986 |
| 378 |
{ |
| 379 |
"http://[fe80::1]:8080/", |
| 380 |
&URL{ |
| 381 |
Scheme: "http", |
| 382 |
Host: "[fe80::1]:8080", |
| 383 |
Path: "/", |
| 384 |
}, |
| 385 |
"", |
| 386 |
}, |
| 387 |
// valid IPv6 host with port and path |
| 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 |
// host subcomponent; IPv6 address with zone identifier in RFC 6874 |
| 398 |
{ |
| 399 |
"http://[fe80::1%25en0]/", // alphanum zone identifier |
| 400 |
&URL{ |
| 401 |
Scheme: "http", |
| 402 |
Host: "[fe80::1%en0]", |
| 403 |
Path: "/", |
| 404 |
}, |
| 405 |
"", |
| 406 |
}, |
| 407 |
// host and port subcomponents; IPv6 address with zone identifier in RFC 6874 |
| 408 |
{ |
| 409 |
"http://[fe80::1%25en0]:8080/", // alphanum zone identifier |
| 410 |
&URL{ |
| 411 |
Scheme: "http", |
| 412 |
Host: "[fe80::1%en0]:8080", |
| 413 |
Path: "/", |
| 414 |
}, |
| 415 |
"", |
| 416 |
}, |
| 417 |
// host subcomponent; IPv6 address with zone identifier in RFC 6874 |
| 418 |
{ |
| 419 |
"http://[fe80::1%25%65%6e%301-._~]/", // percent-encoded+unreserved zone identifier |
| 420 |
&URL{ |
| 421 |
Scheme: "http", |
| 422 |
Host: "[fe80::1%en01-._~]", |
| 423 |
Path: "/", |
| 424 |
}, |
| 425 |
"http://[fe80::1%25en01-._~]/", |
| 426 |
}, |
| 427 |
// host and port subcomponents; IPv6 address with zone identifier in RFC 6874 |
| 428 |
{ |
| 429 |
"http://[fe80::1%25%65%6e%301-._~]:8080/", // percent-encoded+unreserved zone identifier |
| 430 |
&URL{ |
| 431 |
Scheme: "http", |
| 432 |
Host: "[fe80::1%en01-._~]:8080", |
| 433 |
Path: "/", |
| 434 |
}, |
| 435 |
"http://[fe80::1%25en01-._~]:8080/", |
| 436 |
}, |
| 437 |
// alternate escapings of path survive round trip |
| 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 |
// issue 12036 |
| 450 |
{ |
| 451 |
"mysql://a,b,c/bar", |
| 452 |
&URL{ |
| 453 |
Scheme: "mysql", |
| 454 |
Host: "a,b,c", |
| 455 |
Path: "/bar", |
| 456 |
}, |
| 457 |
"", |
| 458 |
}, |
| 459 |
// worst case host, still round trips |
| 460 |
{ |
| 461 |
"scheme://!$&'()*+,;=hello!:1/path", |
| 462 |
&URL{ |
| 463 |
Scheme: "scheme", |
| 464 |
Host: "!$&'()*+,;=hello!:1", |
| 465 |
Path: "/path", |
| 466 |
}, |
| 467 |
"", |
| 468 |
}, |
| 469 |
// worst case path, still round trips |
| 470 |
{ |
| 471 |
"http://host/!$&'()*+,;=:@[hello]", |
| 472 |
&URL{ |
| 473 |
Scheme: "http", |
| 474 |
Host: "host", |
| 475 |
Path: "/!$&'()*+,;=:@[hello]", |
| 476 |
RawPath: "/!$&'()*+,;=:@[hello]", |
| 477 |
}, |
| 478 |
"", |
| 479 |
}, |
| 480 |
// golang.org/issue/5684 |
| 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 |
// golang.org/issue/12200 (colon with empty port) |
| 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 |
// golang.org/issue/7991 and golang.org/issue/12719 (non-ascii %-encoded in host) |
| 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 |
// golang.org/issue/10433 (path beginning with //) |
| 557 |
{ |
| 558 |
"http://example.com//foo", |
| 559 |
&URL{ |
| 560 |
Scheme: "http", |
| 561 |
Host: "example.com", |
| 562 |
Path: "//foo", |
| 563 |
}, |
| 564 |
"", |
| 565 |
}, |
| 566 |
// test that we can reparse the host names we accept. |
| 567 |
{ |
| 568 |
"myscheme://authority<\"hi\">/foo", |
| 569 |
&URL{ |
| 570 |
Scheme: "myscheme", |
| 571 |
Host: "authority<\"hi\">", |
| 572 |
Path: "/foo", |
| 573 |
}, |
| 574 |
"", |
| 575 |
}, |
| 576 |
// spaces in hosts are disallowed but escaped spaces in IPv6 scope IDs are grudgingly OK. |
| 577 |
// This happens on Windows. |
| 578 |
// golang.org/issue/14002 |
| 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 |
// test we can roundtrip magnet url |
| 588 |
// fix issue https://golang.org/issue/20054 |
| 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 |
// PostgreSQL URLs can include a comma-separated list of host:post hosts. |
| 610 |
// https://go.dev/issue/75859 |
| 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 |
// Mongodb URLs can include a comma-separated list of host:post hosts. |
| 630 |
{ |
| 631 |
"mongodb://user:password@host1:1,host2:2,host3:3", |
| 632 |
&URL{ |
| 633 |
Scheme: "mongodb", |
| 634 |
User: UserPassword("user", "password"), |
| 635 |
Host: "host1:1,host2:2,host3:3", |
| 636 |
Path: "", |
| 637 |
}, |
| 638 |
"", |
| 639 |
}, |
| 640 |
{ |
| 641 |
"mongodb+srv://user:password@host1:1,host2:2,host3:3", |
| 642 |
&URL{ |
| 643 |
Scheme: "mongodb+srv", |
| 644 |
User: UserPassword("user", "password"), |
| 645 |
Host: "host1:1,host2:2,host3:3", |
| 646 |
Path: "", |
| 647 |
}, |
| 648 |
"", |
| 649 |
}, |
| 650 |
} |
| 651 |
|
| 652 |
// more useful string for debugging than fmt's struct printer |
| 653 |
func ufmt(u *URL) string { |
| 654 |
var user, pass any |
| 655 |
if u.User != nil { |
| 656 |
user = u.User.Username() |
| 657 |
if p, ok := u.User.Password(); ok { |
| 658 |
pass = p |
| 659 |
} |
| 660 |
} |
| 661 |
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", |
| 662 |
u.Opaque, u.Scheme, user, pass, u.Host, u.Path, u.RawPath, u.RawQuery, u.Fragment, u.RawFragment, u.ForceQuery, u.OmitHost) |
| 663 |
} |
| 664 |
|
| 665 |
func BenchmarkString(b *testing.B) { |
| 666 |
b.StopTimer() |
| 667 |
b.ReportAllocs() |
| 668 |
for _, tt := range urltests { |
| 669 |
u, err := Parse(tt.in) |
| 670 |
if err != nil { |
| 671 |
b.Errorf("Parse(%q) returned error %s", tt.in, err) |
| 672 |
continue |
| 673 |
} |
| 674 |
if tt.roundtrip == "" { |
| 675 |
continue |
| 676 |
} |
| 677 |
b.StartTimer() |
| 678 |
var g string |
| 679 |
for i := 0; i < b.N; i++ { |
| 680 |
g = u.String() |
| 681 |
} |
| 682 |
b.StopTimer() |
| 683 |
if w := tt.roundtrip; b.N > 0 && g != w { |
| 684 |
b.Errorf("Parse(%q).String() == %q, want %q", tt.in, g, w) |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
func TestParse(t *testing.T) { |
| 690 |
for _, tt := range urltests { |
| 691 |
u, err := Parse(tt.in) |
| 692 |
if err != nil { |
| 693 |
t.Errorf("Parse(%q) returned error %v", tt.in, err) |
| 694 |
continue |
| 695 |
} |
| 696 |
if !reflect.DeepEqual(u, tt.out) { |
| 697 |
t.Errorf("Parse(%q):\n\tgot %v\n\twant %v\n", tt.in, ufmt(u), ufmt(tt.out)) |
| 698 |
} |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
const pathThatLooksSchemeRelative = "//[email protected]/just/a/path" |
| 703 |
|
| 704 |
var parseRequestURLTests = []struct { |
| 705 |
url string |
| 706 |
expectedValid bool |
| 707 |
}{ |
| 708 |
{"http://foo.com", true}, |
| 709 |
{"http://foo.com/", true}, |
| 710 |
{"http://foo.com/path", true}, |
| 711 |
{"/", true}, |
| 712 |
{pathThatLooksSchemeRelative, true}, |
| 713 |
{"//not.a.user@%66%6f%6f.com/just/a/path/also", true}, |
| 714 |
{"*", true}, |
| 715 |
{"http://192.168.0.1/", true}, |
| 716 |
{"http://192.168.0.1:8080/", true}, |
| 717 |
{"http://[fe80::1]/", true}, |
| 718 |
{"http://[fe80::1]:8080/", true}, |
| 719 |
|
| 720 |
// Tests exercising RFC 6874 compliance: |
| 721 |
{"http://[fe80::1%25en0]/", true}, // with alphanum zone identifier |
| 722 |
{"http://[fe80::1%25en0]:8080/", true}, // with alphanum zone identifier |
| 723 |
{"http://[fe80::1%25%65%6e%301-._~]/", true}, // with percent-encoded+unreserved zone identifier |
| 724 |
{"http://[fe80::1%25%65%6e%301-._~]:8080/", true}, // with percent-encoded+unreserved zone identifier |
| 725 |
|
| 726 |
{"foo.html", false}, |
| 727 |
{"../dir/", false}, |
| 728 |
{" http://foo.com", false}, |
| 729 |
{"http://192.168.0.%31/", false}, |
| 730 |
{"http://192.168.0.%31:8080/", false}, |
| 731 |
{"http://[fe80::%31]/", false}, |
| 732 |
{"http://[fe80::%31]:8080/", false}, |
| 733 |
{"http://[fe80::%31%25en0]/", false}, |
| 734 |
{"http://[fe80::%31%25en0]:8080/", false}, |
| 735 |
|
| 736 |
// These two cases are valid as textual representations as |
| 737 |
// described in RFC 4007, but are not valid as address |
| 738 |
// literals with IPv6 zone identifiers in URIs as described in |
| 739 |
// RFC 6874. |
| 740 |
{"http://[fe80::1%en0]/", false}, |
| 741 |
{"http://[fe80::1%en0]:8080/", false}, |
| 742 |
|
| 743 |
// Tests exercising RFC 3986 compliance |
| 744 |
{"https://[1:2:3:4:5:6:7:8]", true}, // full IPv6 address |
| 745 |
{"https://[2001:db8::a:b:c:d]", true}, // compressed IPv6 address |
| 746 |
{"https://[fe80::1%25eth0]", true}, // link-local address with zone ID (interface name) |
| 747 |
{"https://[fe80::abc:def%254]", true}, // link-local address with zone ID (interface index) |
| 748 |
{"https://[2001:db8::1]/path", true}, // compressed IPv6 address with path |
| 749 |
{"https://[fe80::1%25eth0]/path?query=1", true}, // link-local with zone, path, and query |
| 750 |
|
| 751 |
{"https://[::ffff:192.0.2.1]", true}, |
| 752 |
{"https://[:1] ", false}, |
| 753 |
{"https://[1:2:3:4:5:6:7:8:9]", false}, |
| 754 |
{"https://[1::1::1]", false}, |
| 755 |
{"https://[1:2:3:]", false}, |
| 756 |
{"https://[ffff::127.0.0.4000]", false}, |
| 757 |
{"https://[0:0::test.com]:80", false}, |
| 758 |
{"https://[2001:db8::test.com]", false}, |
| 759 |
{"https://[test.com]", false}, |
| 760 |
{"https://1:2:3:4:5:6:7:8", false}, |
| 761 |
{"https://1:2:3:4:5:6:7:8:80", false}, |
| 762 |
{"https://example.com:80:", false}, |
| 763 |
} |
| 764 |
|
| 765 |
func TestParseRequestURI(t *testing.T) { |
| 766 |
for _, test := range parseRequestURLTests { |
| 767 |
_, err := ParseRequestURI(test.url) |
| 768 |
if test.expectedValid && err != nil { |
| 769 |
t.Errorf("ParseRequestURI(%q) gave err %v; want no error", test.url, err) |
| 770 |
} else if !test.expectedValid && err == nil { |
| 771 |
t.Errorf("ParseRequestURI(%q) gave nil error; want some error", test.url) |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
url, err := ParseRequestURI(pathThatLooksSchemeRelative) |
| 776 |
if err != nil { |
| 777 |
t.Fatalf("Unexpected error %v", err) |
| 778 |
} |
| 779 |
if url.Path != pathThatLooksSchemeRelative { |
| 780 |
t.Errorf("ParseRequestURI path:\ngot %q\nwant %q", url.Path, pathThatLooksSchemeRelative) |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
var stringURLTests = []struct { |
| 785 |
url URL |
| 786 |
want string |
| 787 |
}{ |
| 788 |
// No leading slash on path should prepend slash on String() call |
| 789 |
{ |
| 790 |
url: URL{ |
| 791 |
Scheme: "http", |
| 792 |
Host: "www.google.com", |
| 793 |
Path: "search", |
| 794 |
}, |
| 795 |
want: "http://www.google.com/search", |
| 796 |
}, |
| 797 |
// Relative path with first element containing ":" should be prepended with "./", golang.org/issue/17184 |
| 798 |
{ |
| 799 |
url: URL{ |
| 800 |
Path: "this:that", |
| 801 |
}, |
| 802 |
want: "./this:that", |
| 803 |
}, |
| 804 |
// Relative path with second element containing ":" should not be prepended with "./" |
| 805 |
{ |
| 806 |
url: URL{ |
| 807 |
Path: "here/this:that", |
| 808 |
}, |
| 809 |
want: "here/this:that", |
| 810 |
}, |
| 811 |
// Non-relative path with first element containing ":" should not be prepended with "./" |
| 812 |
{ |
| 813 |
url: URL{ |
| 814 |
Scheme: "http", |
| 815 |
Host: "www.google.com", |
| 816 |
Path: "this:that", |
| 817 |
}, |
| 818 |
want: "http://www.google.com/this:that", |
| 819 |
}, |
| 820 |
} |
| 821 |
|
| 822 |
func TestURLString(t *testing.T) { |
| 823 |
for _, tt := range urltests { |
| 824 |
u, err := Parse(tt.in) |
| 825 |
if err != nil { |
| 826 |
t.Errorf("Parse(%q) returned error %s", tt.in, err) |
| 827 |
continue |
| 828 |
} |
| 829 |
expected := tt.in |
| 830 |
if tt.roundtrip != "" { |
| 831 |
expected = tt.roundtrip |
| 832 |
} |
| 833 |
s := u.String() |
| 834 |
if s != expected { |
| 835 |
t.Errorf("Parse(%q).String() == %q (expected %q)", tt.in, s, expected) |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
for _, tt := range stringURLTests { |
| 840 |
if got := tt.url.String(); got != tt.want { |
| 841 |
t.Errorf("%+v.String() = %q; want %q", tt.url, got, tt.want) |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
func TestURLRedacted(t *testing.T) { |
| 847 |
cases := []struct { |
| 848 |
name string |
| 849 |
url *URL |
| 850 |
want string |
| 851 |
}{ |
| 852 |
{ |
| 853 |
name: "non-blank Password", |
| 854 |
url: &URL{ |
| 855 |
Scheme: "http", |
| 856 |
Host: "host.tld", |
| 857 |
Path: "this:that", |
| 858 |
User: UserPassword("user", "password"), |
| 859 |
}, |
| 860 |
want: "http://user:[email protected]/this:that", |
| 861 |
}, |
| 862 |
{ |
| 863 |
name: "blank Password", |
| 864 |
url: &URL{ |
| 865 |
Scheme: "http", |
| 866 |
Host: "host.tld", |
| 867 |
Path: "this:that", |
| 868 |
User: User("user"), |
| 869 |
}, |
| 870 |
want: "http://[email protected]/this:that", |
| 871 |
}, |
| 872 |
{ |
| 873 |
name: "nil User", |
| 874 |
url: &URL{ |
| 875 |
Scheme: "http", |
| 876 |
Host: "host.tld", |
| 877 |
Path: "this:that", |
| 878 |
User: UserPassword("", "password"), |
| 879 |
}, |
| 880 |
want: "http://:[email protected]/this:that", |
| 881 |
}, |
| 882 |
{ |
| 883 |
name: "blank Username, blank Password", |
| 884 |
url: &URL{ |
| 885 |
Scheme: "http", |
| 886 |
Host: "host.tld", |
| 887 |
Path: "this:that", |
| 888 |
}, |
| 889 |
want: "http://host.tld/this:that", |
| 890 |
}, |
| 891 |
{ |
| 892 |
name: "empty URL", |
| 893 |
url: &URL{}, |
| 894 |
want: "", |
| 895 |
}, |
| 896 |
{ |
| 897 |
name: "nil URL", |
| 898 |
url: nil, |
| 899 |
want: "", |
| 900 |
}, |
| 901 |
} |
| 902 |
|
| 903 |
for _, tt := range cases { |
| 904 |
t.Run(tt.name, func(t *testing.T) { |
| 905 |
if g, w := tt.url.Redacted(), tt.want; g != w { |
| 906 |
t.Fatalf("got: %q\nwant: %q", g, w) |
| 907 |
} |
| 908 |
}) |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
type EscapeTest struct { |
| 913 |
in string |
| 914 |
out string |
| 915 |
err error |
| 916 |
} |
| 917 |
|
| 918 |
var unescapeTests = []EscapeTest{ |
| 919 |
{ |
| 920 |
"", |
| 921 |
"", |
| 922 |
nil, |
| 923 |
}, |
| 924 |
{ |
| 925 |
"abc", |
| 926 |
"abc", |
| 927 |
nil, |
| 928 |
}, |
| 929 |
{ |
| 930 |
"1%41", |
| 931 |
"1A", |
| 932 |
nil, |
| 933 |
}, |
| 934 |
{ |
| 935 |
"1%41%42%43", |
| 936 |
"1ABC", |
| 937 |
nil, |
| 938 |
}, |
| 939 |
{ |
| 940 |
"%4a", |
| 941 |
"J", |
| 942 |
nil, |
| 943 |
}, |
| 944 |
{ |
| 945 |
"%6F", |
| 946 |
"o", |
| 947 |
nil, |
| 948 |
}, |
| 949 |
{ |
| 950 |
"%", // not enough characters after % |
| 951 |
"", |
| 952 |
EscapeError("%"), |
| 953 |
}, |
| 954 |
{ |
| 955 |
"%a", // not enough characters after % |
| 956 |
"", |
| 957 |
EscapeError("%a"), |
| 958 |
}, |
| 959 |
{ |
| 960 |
"%1", // not enough characters after % |
| 961 |
"", |
| 962 |
EscapeError("%1"), |
| 963 |
}, |
| 964 |
{ |
| 965 |
"123%45%6", // not enough characters after % |
| 966 |
"", |
| 967 |
EscapeError("%6"), |
| 968 |
}, |
| 969 |
{ |
| 970 |
"%zzzzz", // invalid hex digits |
| 971 |
"", |
| 972 |
EscapeError("%zz"), |
| 973 |
}, |
| 974 |
{ |
| 975 |
"a+b", |
| 976 |
"a b", |
| 977 |
nil, |
| 978 |
}, |
| 979 |
{ |
| 980 |
"a%20b", |
| 981 |
"a b", |
| 982 |
nil, |
| 983 |
}, |
| 984 |
} |
| 985 |
|
| 986 |
func TestUnescape(t *testing.T) { |
| 987 |
for _, tt := range unescapeTests { |
| 988 |
actual, err := QueryUnescape(tt.in) |
| 989 |
if actual != tt.out || (err != nil) != (tt.err != nil) { |
| 990 |
t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", tt.in, actual, err, tt.out, tt.err) |
| 991 |
} |
| 992 |
|
| 993 |
in := tt.in |
| 994 |
out := tt.out |
| 995 |
if strings.Contains(tt.in, "+") { |
| 996 |
in = strings.ReplaceAll(tt.in, "+", "%20") |
| 997 |
actual, err := PathUnescape(in) |
| 998 |
if actual != tt.out || (err != nil) != (tt.err != nil) { |
| 999 |
t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", in, actual, err, tt.out, tt.err) |
| 1000 |
} |
| 1001 |
if tt.err == nil { |
| 1002 |
s, err := QueryUnescape(strings.ReplaceAll(tt.in, "+", "XXX")) |
| 1003 |
if err != nil { |
| 1004 |
continue |
| 1005 |
} |
| 1006 |
in = tt.in |
| 1007 |
out = strings.ReplaceAll(s, "XXX", "+") |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
actual, err = PathUnescape(in) |
| 1012 |
if actual != out || (err != nil) != (tt.err != nil) { |
| 1013 |
t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", in, actual, err, out, tt.err) |
| 1014 |
} |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
var queryEscapeTests = []EscapeTest{ |
| 1019 |
{ |
| 1020 |
"", |
| 1021 |
"", |
| 1022 |
nil, |
| 1023 |
}, |
| 1024 |
{ |
| 1025 |
"abc", |
| 1026 |
"abc", |
| 1027 |
nil, |
| 1028 |
}, |
| 1029 |
{ |
| 1030 |
"one two", |
| 1031 |
"one+two", |
| 1032 |
nil, |
| 1033 |
}, |
| 1034 |
{ |
| 1035 |
"10%", |
| 1036 |
"10%25", |
| 1037 |
nil, |
| 1038 |
}, |
| 1039 |
{ |
| 1040 |
" ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;", |
| 1041 |
"+%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", |
| 1042 |
nil, |
| 1043 |
}, |
| 1044 |
} |
| 1045 |
|
| 1046 |
func TestQueryEscape(t *testing.T) { |
| 1047 |
for _, tt := range queryEscapeTests { |
| 1048 |
actual := QueryEscape(tt.in) |
| 1049 |
if tt.out != actual { |
| 1050 |
t.Errorf("QueryEscape(%q) = %q, want %q", tt.in, actual, tt.out) |
| 1051 |
} |
| 1052 |
|
| 1053 |
// for bonus points, verify that escape:unescape is an identity. |
| 1054 |
roundtrip, err := QueryUnescape(actual) |
| 1055 |
if roundtrip != tt.in || err != nil { |
| 1056 |
t.Errorf("QueryUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]") |
| 1057 |
} |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
var pathEscapeTests = []EscapeTest{ |
| 1062 |
{ |
| 1063 |
"", |
| 1064 |
"", |
| 1065 |
nil, |
| 1066 |
}, |
| 1067 |
{ |
| 1068 |
"abc", |
| 1069 |
"abc", |
| 1070 |
nil, |
| 1071 |
}, |
| 1072 |
{ |
| 1073 |
"abc+def", |
| 1074 |
"abc+def", |
| 1075 |
nil, |
| 1076 |
}, |
| 1077 |
{ |
| 1078 |
"a/b", |
| 1079 |
"a%2Fb", |
| 1080 |
nil, |
| 1081 |
}, |
| 1082 |
{ |
| 1083 |
"one two", |
| 1084 |
"one%20two", |
| 1085 |
nil, |
| 1086 |
}, |
| 1087 |
{ |
| 1088 |
"10%", |
| 1089 |
"10%25", |
| 1090 |
nil, |
| 1091 |
}, |
| 1092 |
{ |
| 1093 |
" ?&=#+%!<>#\"{}|\\^[]`☺\t:/@$'()*,;", |
| 1094 |
"%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", |
| 1095 |
nil, |
| 1096 |
}, |
| 1097 |
} |
| 1098 |
|
| 1099 |
func TestPathEscape(t *testing.T) { |
| 1100 |
for _, tt := range pathEscapeTests { |
| 1101 |
actual := PathEscape(tt.in) |
| 1102 |
if tt.out != actual { |
| 1103 |
t.Errorf("PathEscape(%q) = %q, want %q", tt.in, actual, tt.out) |
| 1104 |
} |
| 1105 |
|
| 1106 |
// for bonus points, verify that escape:unescape is an identity. |
| 1107 |
roundtrip, err := PathUnescape(actual) |
| 1108 |
if roundtrip != tt.in || err != nil { |
| 1109 |
t.Errorf("PathUnescape(%q) = %q, %s; want %q, %s", actual, roundtrip, err, tt.in, "[no error]") |
| 1110 |
} |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
//var userinfoTests = []UserinfoTest{ |
| 1115 |
// {"user", "password", "user:password"}, |
| 1116 |
// {"foo:bar", "~!@#$%^&*()_+{}|[]\\-=`:;'\"<>?,./", |
| 1117 |
// "foo%3Abar:~!%40%23$%25%5E&*()_+%7B%7D%7C%5B%5D%5C-=%60%3A;'%22%3C%3E?,.%2F"}, |
| 1118 |
//} |
| 1119 |
|
| 1120 |
type EncodeQueryTest struct { |
| 1121 |
m Values |
| 1122 |
expected string |
| 1123 |
} |
| 1124 |
|
| 1125 |
var encodeQueryTests = []EncodeQueryTest{ |
| 1126 |
{nil, ""}, |
| 1127 |
{Values{}, ""}, |
| 1128 |
{Values{"q": {"puppies"}, "oe": {"utf8"}}, "oe=utf8&q=puppies"}, |
| 1129 |
{Values{"q": {"dogs", "&", "7"}}, "q=dogs&q=%26&q=7"}, |
| 1130 |
{Values{ |
| 1131 |
"a": {"a1", "a2", "a3"}, |
| 1132 |
"b": {"b1", "b2", "b3"}, |
| 1133 |
"c": {"c1", "c2", "c3"}, |
| 1134 |
}, "a=a1&a=a2&a=a3&b=b1&b=b2&b=b3&c=c1&c=c2&c=c3"}, |
| 1135 |
{Values{ |
| 1136 |
"a": {"a"}, |
| 1137 |
"b": {"b"}, |
| 1138 |
"c": {"c"}, |
| 1139 |
"d": {"d"}, |
| 1140 |
"e": {"e"}, |
| 1141 |
"f": {"f"}, |
| 1142 |
"g": {"g"}, |
| 1143 |
"h": {"h"}, |
| 1144 |
"i": {"i"}, |
| 1145 |
}, "a=a&b=b&c=c&d=d&e=e&f=f&g=g&h=h&i=i"}, |
| 1146 |
} |
| 1147 |
|
| 1148 |
func TestEncodeQuery(t *testing.T) { |
| 1149 |
for _, tt := range encodeQueryTests { |
| 1150 |
if q := tt.m.Encode(); q != tt.expected { |
| 1151 |
t.Errorf(`EncodeQuery(%+v) = %q, want %q`, tt.m, q, tt.expected) |
| 1152 |
} |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
func BenchmarkEncodeQuery(b *testing.B) { |
| 1157 |
for _, tt := range encodeQueryTests { |
| 1158 |
b.Run(tt.expected, func(b *testing.B) { |
| 1159 |
b.ReportAllocs() |
| 1160 |
for b.Loop() { |
| 1161 |
tt.m.Encode() |
| 1162 |
} |
| 1163 |
}) |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
var resolvePathTests = []struct { |
| 1168 |
base, ref, expected string |
| 1169 |
}{ |
| 1170 |
{"a/b", ".", "/a/"}, |
| 1171 |
{"a/b", "c", "/a/c"}, |
| 1172 |
{"a/b", "..", "/"}, |
| 1173 |
{"a/", "..", "/"}, |
| 1174 |
{"a/", "../..", "/"}, |
| 1175 |
{"a/b/c", "..", "/a/"}, |
| 1176 |
{"a/b/c", "../d", "/a/d"}, |
| 1177 |
{"a/b/c", ".././d", "/a/d"}, |
| 1178 |
{"a/b", "./..", "/"}, |
| 1179 |
{"a/./b", ".", "/a/"}, |
| 1180 |
{"a/../", ".", "/"}, |
| 1181 |
{"a/.././b", "c", "/c"}, |
| 1182 |
} |
| 1183 |
|
| 1184 |
func TestResolvePath(t *testing.T) { |
| 1185 |
for _, test := range resolvePathTests { |
| 1186 |
got := resolvePath(test.base, test.ref) |
| 1187 |
if got != test.expected { |
| 1188 |
t.Errorf("For %q + %q got %q; expected %q", test.base, test.ref, got, test.expected) |
| 1189 |
} |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
func BenchmarkResolvePath(b *testing.B) { |
| 1194 |
b.Run("Simple", func(b *testing.B) { |
| 1195 |
for i := 0; i < b.N; i++ { |
| 1196 |
resolvePath("a/b/c", ".././d") |
| 1197 |
} |
| 1198 |
}) |
| 1199 |
b.Run("Deep", func(b *testing.B) { |
| 1200 |
base := strings.Repeat("a/", 100) + "b" |
| 1201 |
ref := "c" |
| 1202 |
b.ResetTimer() |
| 1203 |
for b.Loop() { |
| 1204 |
resolvePath(base, ref) |
| 1205 |
} |
| 1206 |
}) |
| 1207 |
b.Run("Backtrack", func(b *testing.B) { |
| 1208 |
base := strings.Repeat("a/", 100) + "b" |
| 1209 |
ref := strings.Repeat("../", 50) + "c" |
| 1210 |
b.ResetTimer() |
| 1211 |
for b.Loop() { |
| 1212 |
resolvePath(base, ref) |
| 1213 |
} |
| 1214 |
}) |
| 1215 |
} |
| 1216 |
|
| 1217 |
var resolveReferenceTests = []struct { |
| 1218 |
base, rel, expected string |
| 1219 |
}{ |
| 1220 |
// Absolute URL references |
| 1221 |
{"http://foo.com?a=b", "https://bar.com/", "https://bar.com/"}, |
| 1222 |
{"http://foo.com/", "https://bar.com/?a=b", "https://bar.com/?a=b"}, |
| 1223 |
{"http://foo.com/", "https://bar.com/?", "https://bar.com/?"}, |
| 1224 |
{"http://foo.com/bar", "mailto:[email protected]", "mailto:[email protected]"}, |
| 1225 |
|
| 1226 |
// Path-absolute references |
| 1227 |
{"http://foo.com/bar", "/baz", "http://foo.com/baz"}, |
| 1228 |
{"http://foo.com/bar?a=b#f", "/baz", "http://foo.com/baz"}, |
| 1229 |
{"http://foo.com/bar?a=b", "/baz?", "http://foo.com/baz?"}, |
| 1230 |
{"http://foo.com/bar?a=b", "/baz?c=d", "http://foo.com/baz?c=d"}, |
| 1231 |
|
| 1232 |
// Multiple slashes |
| 1233 |
{"http://foo.com/bar", "http://foo.com//baz", "http://foo.com//baz"}, |
| 1234 |
{"http://foo.com/bar", "http://foo.com///baz/quux", "http://foo.com///baz/quux"}, |
| 1235 |
|
| 1236 |
// Scheme-relative |
| 1237 |
{"https://foo.com/bar?a=b", "//bar.com/quux", "https://bar.com/quux"}, |
| 1238 |
|
| 1239 |
// Path-relative references: |
| 1240 |
|
| 1241 |
// ... current directory |
| 1242 |
{"http://foo.com", ".", "http://foo.com/"}, |
| 1243 |
{"http://foo.com/bar", ".", "http://foo.com/"}, |
| 1244 |
{"http://foo.com/bar/", ".", "http://foo.com/bar/"}, |
| 1245 |
|
| 1246 |
// ... going down |
| 1247 |
{"http://foo.com", "bar", "http://foo.com/bar"}, |
| 1248 |
{"http://foo.com/", "bar", "http://foo.com/bar"}, |
| 1249 |
{"http://foo.com/bar/baz", "quux", "http://foo.com/bar/quux"}, |
| 1250 |
|
| 1251 |
// ... going up |
| 1252 |
{"http://foo.com/bar/baz", "../quux", "http://foo.com/quux"}, |
| 1253 |
{"http://foo.com/bar/baz", "../../../../../quux", "http://foo.com/quux"}, |
| 1254 |
{"http://foo.com/bar", "..", "http://foo.com/"}, |
| 1255 |
{"http://foo.com/bar/baz", "./..", "http://foo.com/"}, |
| 1256 |
// ".." in the middle (issue 3560) |
| 1257 |
{"http://foo.com/bar/baz", "quux/dotdot/../tail", "http://foo.com/bar/quux/tail"}, |
| 1258 |
{"http://foo.com/bar/baz", "quux/./dotdot/../tail", "http://foo.com/bar/quux/tail"}, |
| 1259 |
{"http://foo.com/bar/baz", "quux/./dotdot/.././tail", "http://foo.com/bar/quux/tail"}, |
| 1260 |
{"http://foo.com/bar/baz", "quux/./dotdot/./../tail", "http://foo.com/bar/quux/tail"}, |
| 1261 |
{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/././../../tail", "http://foo.com/bar/quux/tail"}, |
| 1262 |
{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/./.././../tail", "http://foo.com/bar/quux/tail"}, |
| 1263 |
{"http://foo.com/bar/baz", "quux/./dotdot/dotdot/dotdot/./../../.././././tail", "http://foo.com/bar/quux/tail"}, |
| 1264 |
{"http://foo.com/bar/baz", "quux/./dotdot/../dotdot/../dot/./tail/..", "http://foo.com/bar/quux/dot/"}, |
| 1265 |
|
| 1266 |
// Remove any dot-segments prior to forming the target URI. |
| 1267 |
// https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4 |
| 1268 |
{"http://foo.com/dot/./dotdot/../foo/bar", "../baz", "http://foo.com/dot/baz"}, |
| 1269 |
|
| 1270 |
// Triple dot isn't special |
| 1271 |
{"http://foo.com/bar", "...", "http://foo.com/..."}, |
| 1272 |
|
| 1273 |
// Fragment |
| 1274 |
{"http://foo.com/bar", ".#frag", "http://foo.com/#frag"}, |
| 1275 |
{"http://example.org/", "#!$&%27()*+,;=", "http://example.org/#!$&%27()*+,;="}, |
| 1276 |
|
| 1277 |
// Paths with escaping (issue 16947). |
| 1278 |
{"http://foo.com/foo%2fbar/", "../baz", "http://foo.com/baz"}, |
| 1279 |
{"http://foo.com/1/2%2f/3%2f4/5", "../../a/b/c", "http://foo.com/1/a/b/c"}, |
| 1280 |
{"http://foo.com/1/2/3", "./a%2f../../b/..%2fc", "http://foo.com/1/2/b/..%2fc"}, |
| 1281 |
{"http://foo.com/1/2%2f/3%2f4/5", "./a%2f../b/../c", "http://foo.com/1/2%2f/3%2f4/a%2f../c"}, |
| 1282 |
{"http://foo.com/foo%20bar/", "../baz", "http://foo.com/baz"}, |
| 1283 |
{"http://foo.com/foo", "../bar%2fbaz", "http://foo.com/bar%2fbaz"}, |
| 1284 |
{"http://foo.com/foo%2dbar/", "./baz-quux", "http://foo.com/foo%2dbar/baz-quux"}, |
| 1285 |
|
| 1286 |
// RFC 3986: Normal Examples |
| 1287 |
// https://datatracker.ietf.org/doc/html/rfc3986#section-5.4.1 |
| 1288 |
{"http://a/b/c/d;p?q", "g:h", "g:h"}, |
| 1289 |
{"http://a/b/c/d;p?q", "g", "http://a/b/c/g"}, |
| 1290 |
{"http://a/b/c/d;p?q", "./g", "http://a/b/c/g"}, |
| 1291 |
{"http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"}, |
| 1292 |
{"http://a/b/c/d;p?q", "/g", "http://a/g"}, |
| 1293 |
{"http://a/b/c/d;p?q", "//g", "http://g"}, |
| 1294 |
{"http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y"}, |
| 1295 |
{"http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"}, |
| 1296 |
{"http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s"}, |
| 1297 |
{"http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"}, |
| 1298 |
{"http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"}, |
| 1299 |
{"http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"}, |
| 1300 |
{"http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"}, |
| 1301 |
{"http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"}, |
| 1302 |
{"http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q"}, |
| 1303 |
{"http://a/b/c/d;p?q", ".", "http://a/b/c/"}, |
| 1304 |
{"http://a/b/c/d;p?q", "./", "http://a/b/c/"}, |
| 1305 |
{"http://a/b/c/d;p?q", "..", "http://a/b/"}, |
| 1306 |
{"http://a/b/c/d;p?q", "../", "http://a/b/"}, |
| 1307 |
{"http://a/b/c/d;p?q", "../g", "http://a/b/g"}, |
| 1308 |
{"http://a/b/c/d;p?q", "../..", "http://a/"}, |
| 1309 |
{"http://a/b/c/d;p?q", "../../", "http://a/"}, |
| 1310 |
{"http://a/b/c/d;p?q", "../../g", "http://a/g"}, |
| 1311 |
|
| 1312 |
// RFC 3986: Abnormal Examples |
| 1313 |
// https://datatracker.ietf.org/doc/html/rfc3986#section-5.4.2 |
| 1314 |
{"http://a/b/c/d;p?q", "../../../g", "http://a/g"}, |
| 1315 |
{"http://a/b/c/d;p?q", "../../../../g", "http://a/g"}, |
| 1316 |
{"http://a/b/c/d;p?q", "/./g", "http://a/g"}, |
| 1317 |
{"http://a/b/c/d;p?q", "/../g", "http://a/g"}, |
| 1318 |
{"http://a/b/c/d;p?q", "g.", "http://a/b/c/g."}, |
| 1319 |
{"http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"}, |
| 1320 |
{"http://a/b/c/d;p?q", "g..", "http://a/b/c/g.."}, |
| 1321 |
{"http://a/b/c/d;p?q", "..g", "http://a/b/c/..g"}, |
| 1322 |
{"http://a/b/c/d;p?q", "./../g", "http://a/b/g"}, |
| 1323 |
{"http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"}, |
| 1324 |
{"http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"}, |
| 1325 |
{"http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h"}, |
| 1326 |
{"http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"}, |
| 1327 |
{"http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y"}, |
| 1328 |
{"http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x"}, |
| 1329 |
{"http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x"}, |
| 1330 |
{"http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x"}, |
| 1331 |
{"http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x"}, |
| 1332 |
|
| 1333 |
// Extras. |
| 1334 |
{"https://a/b/c/d;p?q", "//g?q", "https://g?q"}, |
| 1335 |
{"https://a/b/c/d;p?q", "//g#s", "https://g#s"}, |
| 1336 |
{"https://a/b/c/d;p?q", "//g/d/e/f?y#s", "https://g/d/e/f?y#s"}, |
| 1337 |
{"https://a/b/c/d;p#s", "?y", "https://a/b/c/d;p?y"}, |
| 1338 |
{"https://a/b/c/d;p?q#s", "?y", "https://a/b/c/d;p?y"}, |
| 1339 |
|
| 1340 |
// Empty path and query but with ForceQuery (issue 46033). |
| 1341 |
{"https://a/b/c/d;p?q#s", "?", "https://a/b/c/d;p?"}, |
| 1342 |
|
| 1343 |
// Opaque URLs (issue 66084). |
| 1344 |
{"https://foo.com/bar?a=b", "http:opaque", "http:opaque"}, |
| 1345 |
{"http:opaque?x=y#zzz", "https:/foo?a=b#frag", "https:/foo?a=b#frag"}, |
| 1346 |
{"http:opaque?x=y#zzz", "https:foo:bar", "https:foo:bar"}, |
| 1347 |
{"http:opaque?x=y#zzz", "https:bar/baz?a=b#frag", "https:bar/baz?a=b#frag"}, |
| 1348 |
{"http:opaque?x=y#zzz", "https://user@host:1234?a=b#frag", "https://user@host:1234?a=b#frag"}, |
| 1349 |
{"http:opaque?x=y#zzz", "?a=b#frag", "http:opaque?a=b#frag"}, |
| 1350 |
} |
| 1351 |
|
| 1352 |
func TestResolveReference(t *testing.T) { |
| 1353 |
mustParse := func(url string) *URL { |
| 1354 |
u, err := Parse(url) |
| 1355 |
if err != nil { |
| 1356 |
t.Fatalf("Parse(%q) got err %v", url, err) |
| 1357 |
} |
| 1358 |
return u |
| 1359 |
} |
| 1360 |
opaque := &URL{Scheme: "scheme", Opaque: "opaque"} |
| 1361 |
for _, test := range resolveReferenceTests { |
| 1362 |
base := mustParse(test.base) |
| 1363 |
rel := mustParse(test.rel) |
| 1364 |
url := base.ResolveReference(rel) |
| 1365 |
if got := url.String(); got != test.expected { |
| 1366 |
t.Errorf("URL(%q).ResolveReference(%q)\ngot %q\nwant %q", test.base, test.rel, got, test.expected) |
| 1367 |
} |
| 1368 |
// Ensure that new instances are returned. |
| 1369 |
if base == url { |
| 1370 |
t.Errorf("Expected URL.ResolveReference to return new URL instance.") |
| 1371 |
} |
| 1372 |
// Test the convenience wrapper too. |
| 1373 |
url, err := base.Parse(test.rel) |
| 1374 |
if err != nil { |
| 1375 |
t.Errorf("URL(%q).Parse(%q) failed: %v", test.base, test.rel, err) |
| 1376 |
} else if got := url.String(); got != test.expected { |
| 1377 |
t.Errorf("URL(%q).Parse(%q)\ngot %q\nwant %q", test.base, test.rel, got, test.expected) |
| 1378 |
} else if base == url { |
| 1379 |
// Ensure that new instances are returned for the wrapper too. |
| 1380 |
t.Errorf("Expected URL.Parse to return new URL instance.") |
| 1381 |
} |
| 1382 |
// Ensure Opaque resets the URL. |
| 1383 |
url = base.ResolveReference(opaque) |
| 1384 |
if *url != *opaque { |
| 1385 |
t.Errorf("ResolveReference failed to resolve opaque URL:\ngot %#v\nwant %#v", url, opaque) |
| 1386 |
} |
| 1387 |
// Test the convenience wrapper with an opaque URL too. |
| 1388 |
url, err = base.Parse("scheme:opaque") |
| 1389 |
if err != nil { |
| 1390 |
t.Errorf(`URL(%q).Parse("scheme:opaque") failed: %v`, test.base, err) |
| 1391 |
} else if *url != *opaque { |
| 1392 |
t.Errorf("Parse failed to resolve opaque URL:\ngot %#v\nwant %#v", opaque, url) |
| 1393 |
} else if base == url { |
| 1394 |
// Ensure that new instances are returned, again. |
| 1395 |
t.Errorf("Expected URL.Parse to return new URL instance.") |
| 1396 |
} |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
func TestQueryValues(t *testing.T) { |
| 1401 |
u, _ := Parse("http://x.com?foo=bar&bar=1&bar=2&baz") |
| 1402 |
v := u.Query() |
| 1403 |
if len(v) != 3 { |
| 1404 |
t.Errorf("got %d keys in Query values, want 3", len(v)) |
| 1405 |
} |
| 1406 |
if g, e := v.Get("foo"), "bar"; g != e { |
| 1407 |
t.Errorf("Get(foo) = %q, want %q", g, e) |
| 1408 |
} |
| 1409 |
// Case sensitive: |
| 1410 |
if g, e := v.Get("Foo"), ""; g != e { |
| 1411 |
t.Errorf("Get(Foo) = %q, want %q", g, e) |
| 1412 |
} |
| 1413 |
if g, e := v.Get("bar"), "1"; g != e { |
| 1414 |
t.Errorf("Get(bar) = %q, want %q", g, e) |
| 1415 |
} |
| 1416 |
if g, e := v.Get("baz"), ""; g != e { |
| 1417 |
t.Errorf("Get(baz) = %q, want %q", g, e) |
| 1418 |
} |
| 1419 |
if h, e := v.Has("foo"), true; h != e { |
| 1420 |
t.Errorf("Has(foo) = %t, want %t", h, e) |
| 1421 |
} |
| 1422 |
if h, e := v.Has("bar"), true; h != e { |
| 1423 |
t.Errorf("Has(bar) = %t, want %t", h, e) |
| 1424 |
} |
| 1425 |
if h, e := v.Has("baz"), true; h != e { |
| 1426 |
t.Errorf("Has(baz) = %t, want %t", h, e) |
| 1427 |
} |
| 1428 |
if h, e := v.Has("noexist"), false; h != e { |
| 1429 |
t.Errorf("Has(noexist) = %t, want %t", h, e) |
| 1430 |
} |
| 1431 |
v.Del("bar") |
| 1432 |
if g, e := v.Get("bar"), ""; g != e { |
| 1433 |
t.Errorf("second Get(bar) = %q, want %q", g, e) |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
type parseTest struct { |
| 1438 |
query string |
| 1439 |
out Values |
| 1440 |
ok bool |
| 1441 |
} |
| 1442 |
|
| 1443 |
var parseTests = []parseTest{ |
| 1444 |
{ |
| 1445 |
query: "a=1", |
| 1446 |
out: Values{"a": []string{"1"}}, |
| 1447 |
ok: true, |
| 1448 |
}, |
| 1449 |
{ |
| 1450 |
query: "a=1&b=2", |
| 1451 |
out: Values{"a": []string{"1"}, "b": []string{"2"}}, |
| 1452 |
ok: true, |
| 1453 |
}, |
| 1454 |
{ |
| 1455 |
query: "a=1&a=2&a=banana", |
| 1456 |
out: Values{"a": []string{"1", "2", "banana"}}, |
| 1457 |
ok: true, |
| 1458 |
}, |
| 1459 |
{ |
| 1460 |
query: "ascii=%3Ckey%3A+0x90%3E", |
| 1461 |
out: Values{"ascii": []string{"<key: 0x90>"}}, |
| 1462 |
ok: true, |
| 1463 |
}, { |
| 1464 |
query: "a=1;b=2", |
| 1465 |
out: Values{}, |
| 1466 |
ok: false, |
| 1467 |
}, { |
| 1468 |
query: "a;b=1", |
| 1469 |
out: Values{}, |
| 1470 |
ok: false, |
| 1471 |
}, { |
| 1472 |
query: "a=%3B", // hex encoding for semicolon |
| 1473 |
out: Values{"a": []string{";"}}, |
| 1474 |
ok: true, |
| 1475 |
}, |
| 1476 |
{ |
| 1477 |
query: "a%3Bb=1", |
| 1478 |
out: Values{"a;b": []string{"1"}}, |
| 1479 |
ok: true, |
| 1480 |
}, |
| 1481 |
{ |
| 1482 |
query: "a=1&a=2;a=banana", |
| 1483 |
out: Values{"a": []string{"1"}}, |
| 1484 |
ok: false, |
| 1485 |
}, |
| 1486 |
{ |
| 1487 |
query: "a;b&c=1", |
| 1488 |
out: Values{"c": []string{"1"}}, |
| 1489 |
ok: false, |
| 1490 |
}, |
| 1491 |
{ |
| 1492 |
query: "a=1&b=2;a=3&c=4", |
| 1493 |
out: Values{"a": []string{"1"}, "c": []string{"4"}}, |
| 1494 |
ok: false, |
| 1495 |
}, |
| 1496 |
{ |
| 1497 |
query: "a=1&b=2;c=3", |
| 1498 |
out: Values{"a": []string{"1"}}, |
| 1499 |
ok: false, |
| 1500 |
}, |
| 1501 |
{ |
| 1502 |
query: ";", |
| 1503 |
out: Values{}, |
| 1504 |
ok: false, |
| 1505 |
}, |
| 1506 |
{ |
| 1507 |
query: "a=1;", |
| 1508 |
out: Values{}, |
| 1509 |
ok: false, |
| 1510 |
}, |
| 1511 |
{ |
| 1512 |
query: "a=1&;", |
| 1513 |
out: Values{"a": []string{"1"}}, |
| 1514 |
ok: false, |
| 1515 |
}, |
| 1516 |
{ |
| 1517 |
query: ";a=1&b=2", |
| 1518 |
out: Values{"b": []string{"2"}}, |
| 1519 |
ok: false, |
| 1520 |
}, |
| 1521 |
{ |
| 1522 |
query: "a=1&b=2;", |
| 1523 |
out: Values{"a": []string{"1"}}, |
| 1524 |
ok: false, |
| 1525 |
}, |
| 1526 |
} |
| 1527 |
|
| 1528 |
func TestParseQuery(t *testing.T) { |
| 1529 |
for _, test := range parseTests { |
| 1530 |
t.Run(test.query, func(t *testing.T) { |
| 1531 |
form, err := ParseQuery(test.query) |
| 1532 |
if test.ok != (err == nil) { |
| 1533 |
want := "<error>" |
| 1534 |
if test.ok { |
| 1535 |
want = "<nil>" |
| 1536 |
} |
| 1537 |
t.Errorf("Unexpected error: %v, want %v", err, want) |
| 1538 |
} |
| 1539 |
if len(form) != len(test.out) { |
| 1540 |
t.Errorf("len(form) = %d, want %d", len(form), len(test.out)) |
| 1541 |
} |
| 1542 |
for k, evs := range test.out { |
| 1543 |
vs, ok := form[k] |
| 1544 |
if !ok { |
| 1545 |
t.Errorf("Missing key %q", k) |
| 1546 |
continue |
| 1547 |
} |
| 1548 |
if len(vs) != len(evs) { |
| 1549 |
t.Errorf("len(form[%q]) = %d, want %d", k, len(vs), len(evs)) |
| 1550 |
continue |
| 1551 |
} |
| 1552 |
for j, ev := range evs { |
| 1553 |
if v := vs[j]; v != ev { |
| 1554 |
t.Errorf("form[%q][%d] = %q, want %q", k, j, v, ev) |
| 1555 |
} |
| 1556 |
} |
| 1557 |
} |
| 1558 |
}) |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
func TestParseQueryLimits(t *testing.T) { |
| 1563 |
for _, test := range []struct { |
| 1564 |
params int |
| 1565 |
godebug string |
| 1566 |
wantErr bool |
| 1567 |
}{{ |
| 1568 |
params: 10, |
| 1569 |
wantErr: false, |
| 1570 |
}, { |
| 1571 |
params: defaultMaxParams, |
| 1572 |
wantErr: false, |
| 1573 |
}, { |
| 1574 |
params: defaultMaxParams + 1, |
| 1575 |
wantErr: true, |
| 1576 |
}, { |
| 1577 |
params: 10, |
| 1578 |
godebug: "urlmaxqueryparams=9", |
| 1579 |
wantErr: true, |
| 1580 |
}, { |
| 1581 |
params: defaultMaxParams + 1, |
| 1582 |
godebug: "urlmaxqueryparams=0", |
| 1583 |
wantErr: false, |
| 1584 |
}} { |
| 1585 |
t.Setenv("GODEBUG", test.godebug) |
| 1586 |
want := Values{} |
| 1587 |
var b strings.Builder |
| 1588 |
for i := range test.params { |
| 1589 |
if i > 0 { |
| 1590 |
b.WriteString("&") |
| 1591 |
} |
| 1592 |
p := fmt.Sprintf("p%v", i) |
| 1593 |
b.WriteString(p) |
| 1594 |
want[p] = []string{""} |
| 1595 |
} |
| 1596 |
query := b.String() |
| 1597 |
got, err := ParseQuery(query) |
| 1598 |
if gotErr, wantErr := err != nil, test.wantErr; gotErr != wantErr { |
| 1599 |
t.Errorf("GODEBUG=%v ParseQuery(%v params) = %v, want error: %v", test.godebug, test.params, err, wantErr) |
| 1600 |
} |
| 1601 |
if err != nil { |
| 1602 |
continue |
| 1603 |
} |
| 1604 |
if got, want := len(got), test.params; got != want { |
| 1605 |
t.Errorf("GODEBUG=%v ParseQuery(%v params): got %v params, want %v", test.godebug, test.params, got, want) |
| 1606 |
} |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
type RequestURITest struct { |
| 1611 |
url *URL |
| 1612 |
out string |
| 1613 |
} |
| 1614 |
|
| 1615 |
var requritests = []RequestURITest{ |
| 1616 |
{ |
| 1617 |
&URL{ |
| 1618 |
Scheme: "http", |
| 1619 |
Host: "example.com", |
| 1620 |
Path: "", |
| 1621 |
}, |
| 1622 |
"/", |
| 1623 |
}, |
| 1624 |
{ |
| 1625 |
&URL{ |
| 1626 |
Scheme: "http", |
| 1627 |
Host: "example.com", |
| 1628 |
Path: "/a b", |
| 1629 |
}, |
| 1630 |
"/a%20b", |
| 1631 |
}, |
| 1632 |
// golang.org/issue/4860 variant 1 |
| 1633 |
{ |
| 1634 |
&URL{ |
| 1635 |
Scheme: "http", |
| 1636 |
Host: "example.com", |
| 1637 |
Opaque: "/%2F/%2F/", |
| 1638 |
}, |
| 1639 |
"/%2F/%2F/", |
| 1640 |
}, |
| 1641 |
// golang.org/issue/4860 variant 2 |
| 1642 |
{ |
| 1643 |
&URL{ |
| 1644 |
Scheme: "http", |
| 1645 |
Host: "example.com", |
| 1646 |
Opaque: "//other.example.com/%2F/%2F/", |
| 1647 |
}, |
| 1648 |
"http://other.example.com/%2F/%2F/", |
| 1649 |
}, |
| 1650 |
// better fix for issue 4860 |
| 1651 |
{ |
| 1652 |
&URL{ |
| 1653 |
Scheme: "http", |
| 1654 |
Host: "example.com", |
| 1655 |
Path: "/////", |
| 1656 |
RawPath: "/%2F/%2F/", |
| 1657 |
}, |
| 1658 |
"/%2F/%2F/", |
| 1659 |
}, |
| 1660 |
{ |
| 1661 |
&URL{ |
| 1662 |
Scheme: "http", |
| 1663 |
Host: "example.com", |
| 1664 |
Path: "/////", |
| 1665 |
RawPath: "/WRONG/", // ignored because doesn't match Path |
| 1666 |
}, |
| 1667 |
"/////", |
| 1668 |
}, |
| 1669 |
{ |
| 1670 |
&URL{ |
| 1671 |
Scheme: "http", |
| 1672 |
Host: "example.com", |
| 1673 |
Path: "/a b", |
| 1674 |
RawQuery: "q=go+language", |
| 1675 |
}, |
| 1676 |
"/a%20b?q=go+language", |
| 1677 |
}, |
| 1678 |
{ |
| 1679 |
&URL{ |
| 1680 |
Scheme: "http", |
| 1681 |
Host: "example.com", |
| 1682 |
Path: "/a b", |
| 1683 |
RawPath: "/a b", // ignored because invalid |
| 1684 |
RawQuery: "q=go+language", |
| 1685 |
}, |
| 1686 |
"/a%20b?q=go+language", |
| 1687 |
}, |
| 1688 |
{ |
| 1689 |
&URL{ |
| 1690 |
Scheme: "http", |
| 1691 |
Host: "example.com", |
| 1692 |
Path: "/a?b", |
| 1693 |
RawPath: "/a?b", // ignored because invalid |
| 1694 |
RawQuery: "q=go+language", |
| 1695 |
}, |
| 1696 |
"/a%3Fb?q=go+language", |
| 1697 |
}, |
| 1698 |
{ |
| 1699 |
&URL{ |
| 1700 |
Scheme: "myschema", |
| 1701 |
Opaque: "opaque", |
| 1702 |
}, |
| 1703 |
"opaque", |
| 1704 |
}, |
| 1705 |
{ |
| 1706 |
&URL{ |
| 1707 |
Scheme: "myschema", |
| 1708 |
Opaque: "opaque", |
| 1709 |
RawQuery: "q=go+language", |
| 1710 |
}, |
| 1711 |
"opaque?q=go+language", |
| 1712 |
}, |
| 1713 |
{ |
| 1714 |
&URL{ |
| 1715 |
Scheme: "http", |
| 1716 |
Host: "example.com", |
| 1717 |
Path: "//foo", |
| 1718 |
}, |
| 1719 |
"//foo", |
| 1720 |
}, |
| 1721 |
{ |
| 1722 |
&URL{ |
| 1723 |
Scheme: "http", |
| 1724 |
Host: "example.com", |
| 1725 |
Path: "/foo", |
| 1726 |
ForceQuery: true, |
| 1727 |
}, |
| 1728 |
"/foo?", |
| 1729 |
}, |
| 1730 |
} |
| 1731 |
|
| 1732 |
func TestRequestURI(t *testing.T) { |
| 1733 |
for _, tt := range requritests { |
| 1734 |
s := tt.url.RequestURI() |
| 1735 |
if s != tt.out { |
| 1736 |
t.Errorf("%#v.RequestURI() == %q (expected %q)", tt.url, s, tt.out) |
| 1737 |
} |
| 1738 |
} |
| 1739 |
} |
| 1740 |
|
| 1741 |
func TestParseFailure(t *testing.T) { |
| 1742 |
// Test that the first parse error is returned. |
| 1743 |
const url = "%gh&%ij" |
| 1744 |
_, err := ParseQuery(url) |
| 1745 |
errStr := fmt.Sprint(err) |
| 1746 |
if !strings.Contains(errStr, "%gh") { |
| 1747 |
t.Errorf(`ParseQuery(%q) returned error %q, want something containing %q"`, url, errStr, "%gh") |
| 1748 |
} |
| 1749 |
} |
| 1750 |
|
| 1751 |
func TestParseErrors(t *testing.T) { |
| 1752 |
tests := []struct { |
| 1753 |
in string |
| 1754 |
wantErr bool |
| 1755 |
}{ |
| 1756 |
{"http://[::1]", false}, |
| 1757 |
{"http://[::1]:80", false}, |
| 1758 |
{"http://[::1]:namedport", true}, // rfc3986 3.2.3 |
| 1759 |
{"http://x:namedport", true}, // rfc3986 3.2.3 |
| 1760 |
{"http://[::1]/", false}, |
| 1761 |
{"http://[::1]a", true}, |
| 1762 |
{"http://[::1]%23", true}, |
| 1763 |
{"http://[::1%25en0]", false}, // valid zone id |
| 1764 |
{"http://[::1]:", false}, // colon, but no port OK |
| 1765 |
{"http://x:", false}, // colon, but no port OK |
| 1766 |
{"http://[::1]:%38%30", true}, // not allowed: % encoding only for non-ASCII |
| 1767 |
{"http://[::1%25%41]", false}, // RFC 6874 allows over-escaping in zone |
| 1768 |
{"http://[%10::1]", true}, // no %xx escapes in IP address |
| 1769 |
{"http://[::1]/%48", false}, // %xx in path is fine |
| 1770 |
{"http://%41:8080/", true}, // not allowed: % encoding only for non-ASCII |
| 1771 |
{"mysql://x@y(z:123)/foo", true}, // not well-formed per RFC 3986, golang.org/issue/33646 |
| 1772 |
{"mysql://x@y(1.2.3.4:123)/foo", true}, |
| 1773 |
|
| 1774 |
{" http://foo.com", true}, // invalid character in schema |
| 1775 |
{"ht tp://foo.com", true}, // invalid character in schema |
| 1776 |
{"ahttp://foo.com", false}, // valid schema characters |
| 1777 |
{"1http://foo.com", true}, // invalid character in schema |
| 1778 |
|
| 1779 |
{"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 |
| 1780 |
{"http://a b.com/", true}, // no space in host name please |
| 1781 |
{"cache_object://foo", true}, // scheme cannot have _, relative path cannot have : in first segment |
| 1782 |
{"cache_object:foo", true}, |
| 1783 |
{"cache_object:foo/bar", true}, |
| 1784 |
{"cache_object/:foo/bar", false}, |
| 1785 |
|
| 1786 |
{"http://[192.168.0.1]/", true}, // IPv4 in brackets |
| 1787 |
{"http://[192.168.0.1]:8080/", true}, // IPv4 in brackets with port |
| 1788 |
{"http://[::ffff:192.168.0.1]/", false}, // IPv4-mapped IPv6 in brackets |
| 1789 |
{"http://[::ffff:192.168.0.1000]/", true}, // Out of range IPv4-mapped IPv6 in brackets |
| 1790 |
{"http://[::ffff:192.168.0.1]:8080/", false}, // IPv4-mapped IPv6 in brackets with port |
| 1791 |
{"http://[::ffff:c0a8:1]/", false}, // IPv4-mapped IPv6 in brackets (hex) |
| 1792 |
{"http://[not-an-ip]/", true}, // invalid IP string in brackets |
| 1793 |
{"http://[fe80::1%foo]/", true}, // invalid zone format in brackets |
| 1794 |
{"http://[fe80::1", true}, // missing closing bracket |
| 1795 |
{"http://fe80::1]/", true}, // missing opening bracket |
| 1796 |
{"http://[test.com]/", true}, // domain name in brackets |
| 1797 |
{"http://example.com[::1]", true}, // IPv6 literal doesn't start with '[' |
| 1798 |
{"http://example.com[::1", true}, |
| 1799 |
{"http://[::1", true}, |
| 1800 |
{"http://.[::1]", true}, |
| 1801 |
{"http:// [::1]", true}, |
| 1802 |
{"hxxp://mathepqo[.]serveftp(.)com:9059", true}, |
| 1803 |
} |
| 1804 |
for _, tt := range tests { |
| 1805 |
u, err := Parse(tt.in) |
| 1806 |
if tt.wantErr { |
| 1807 |
if err == nil { |
| 1808 |
t.Errorf("Parse(%q) = %#v; want an error", tt.in, u) |
| 1809 |
} |
| 1810 |
continue |
| 1811 |
} |
| 1812 |
if err != nil { |
| 1813 |
t.Errorf("Parse(%q) = %v; want no error", tt.in, err) |
| 1814 |
} |
| 1815 |
} |
| 1816 |
} |
| 1817 |
|
| 1818 |
// Issue 11202 |
| 1819 |
func TestStarRequest(t *testing.T) { |
| 1820 |
u, err := Parse("*") |
| 1821 |
if err != nil { |
| 1822 |
t.Fatal(err) |
| 1823 |
} |
| 1824 |
if got, want := u.RequestURI(), "*"; got != want { |
| 1825 |
t.Errorf("RequestURI = %q; want %q", got, want) |
| 1826 |
} |
| 1827 |
} |
| 1828 |
|
| 1829 |
type shouldEscapeTest struct { |
| 1830 |
in byte |
| 1831 |
mode encoding |
| 1832 |
escape bool |
| 1833 |
} |
| 1834 |
|
| 1835 |
var shouldEscapeTests = []shouldEscapeTest{ |
| 1836 |
// Unreserved characters (§2.3) |
| 1837 |
{'a', encodePath, false}, |
| 1838 |
{'a', encodeUserPassword, false}, |
| 1839 |
{'a', encodeQueryComponent, false}, |
| 1840 |
{'a', encodeFragment, false}, |
| 1841 |
{'a', encodeHost, false}, |
| 1842 |
{'z', encodePath, false}, |
| 1843 |
{'A', encodePath, false}, |
| 1844 |
{'Z', encodePath, false}, |
| 1845 |
{'0', encodePath, false}, |
| 1846 |
{'9', encodePath, false}, |
| 1847 |
{'-', encodePath, false}, |
| 1848 |
{'-', encodeUserPassword, false}, |
| 1849 |
{'-', encodeQueryComponent, false}, |
| 1850 |
{'-', encodeFragment, false}, |
| 1851 |
{'.', encodePath, false}, |
| 1852 |
{'_', encodePath, false}, |
| 1853 |
{'~', encodePath, false}, |
| 1854 |
|
| 1855 |
// User information (§3.2.1) |
| 1856 |
{':', encodeUserPassword, true}, |
| 1857 |
{'/', encodeUserPassword, true}, |
| 1858 |
{'?', encodeUserPassword, true}, |
| 1859 |
{'@', encodeUserPassword, true}, |
| 1860 |
{'$', encodeUserPassword, false}, |
| 1861 |
{'&', encodeUserPassword, false}, |
| 1862 |
{'+', encodeUserPassword, false}, |
| 1863 |
{',', encodeUserPassword, false}, |
| 1864 |
{';', encodeUserPassword, false}, |
| 1865 |
{'=', encodeUserPassword, false}, |
| 1866 |
|
| 1867 |
// Host (IP address, IPv6 address, registered name, port suffix; §3.2.2) |
| 1868 |
{'!', encodeHost, false}, |
| 1869 |
{'$', encodeHost, false}, |
| 1870 |
{'&', encodeHost, false}, |
| 1871 |
{'\'', encodeHost, false}, |
| 1872 |
{'(', encodeHost, false}, |
| 1873 |
{')', encodeHost, false}, |
| 1874 |
{'*', encodeHost, false}, |
| 1875 |
{'+', encodeHost, false}, |
| 1876 |
{',', encodeHost, false}, |
| 1877 |
{';', encodeHost, false}, |
| 1878 |
{'=', encodeHost, false}, |
| 1879 |
{':', encodeHost, false}, |
| 1880 |
{'[', encodeHost, false}, |
| 1881 |
{']', encodeHost, false}, |
| 1882 |
{'0', encodeHost, false}, |
| 1883 |
{'9', encodeHost, false}, |
| 1884 |
{'A', encodeHost, false}, |
| 1885 |
{'z', encodeHost, false}, |
| 1886 |
{'_', encodeHost, false}, |
| 1887 |
{'-', encodeHost, false}, |
| 1888 |
{'.', encodeHost, false}, |
| 1889 |
} |
| 1890 |
|
| 1891 |
func TestShouldEscape(t *testing.T) { |
| 1892 |
for _, tt := range shouldEscapeTests { |
| 1893 |
if shouldEscape(tt.in, tt.mode) != tt.escape { |
| 1894 |
t.Errorf("shouldEscape(%q, %v) returned %v; expected %v", tt.in, tt.mode, !tt.escape, tt.escape) |
| 1895 |
} |
| 1896 |
} |
| 1897 |
} |
| 1898 |
|
| 1899 |
type timeoutError struct { |
| 1900 |
timeout bool |
| 1901 |
} |
| 1902 |
|
| 1903 |
func (e *timeoutError) Error() string { return "timeout error" } |
| 1904 |
func (e *timeoutError) Timeout() bool { return e.timeout } |
| 1905 |
|
| 1906 |
type temporaryError struct { |
| 1907 |
temporary bool |
| 1908 |
} |
| 1909 |
|
| 1910 |
func (e *temporaryError) Error() string { return "temporary error" } |
| 1911 |
func (e *temporaryError) Temporary() bool { return e.temporary } |
| 1912 |
|
| 1913 |
type timeoutTemporaryError struct { |
| 1914 |
timeoutError |
| 1915 |
temporaryError |
| 1916 |
} |
| 1917 |
|
| 1918 |
func (e *timeoutTemporaryError) Error() string { return "timeout/temporary error" } |
| 1919 |
|
| 1920 |
var netErrorTests = []struct { |
| 1921 |
err error |
| 1922 |
timeout bool |
| 1923 |
temporary bool |
| 1924 |
}{{ |
| 1925 |
err: &Error{"Get", "http://google.com/", &timeoutError{timeout: true}}, |
| 1926 |
timeout: true, |
| 1927 |
temporary: false, |
| 1928 |
}, { |
| 1929 |
err: &Error{"Get", "http://google.com/", &timeoutError{timeout: false}}, |
| 1930 |
timeout: false, |
| 1931 |
temporary: false, |
| 1932 |
}, { |
| 1933 |
err: &Error{"Get", "http://google.com/", &temporaryError{temporary: true}}, |
| 1934 |
timeout: false, |
| 1935 |
temporary: true, |
| 1936 |
}, { |
| 1937 |
err: &Error{"Get", "http://google.com/", &temporaryError{temporary: false}}, |
| 1938 |
timeout: false, |
| 1939 |
temporary: false, |
| 1940 |
}, { |
| 1941 |
err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: true}, temporaryError{temporary: true}}}, |
| 1942 |
timeout: true, |
| 1943 |
temporary: true, |
| 1944 |
}, { |
| 1945 |
err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: false}, temporaryError{temporary: true}}}, |
| 1946 |
timeout: false, |
| 1947 |
temporary: true, |
| 1948 |
}, { |
| 1949 |
err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: true}, temporaryError{temporary: false}}}, |
| 1950 |
timeout: true, |
| 1951 |
temporary: false, |
| 1952 |
}, { |
| 1953 |
err: &Error{"Get", "http://google.com/", &timeoutTemporaryError{timeoutError{timeout: false}, temporaryError{temporary: false}}}, |
| 1954 |
timeout: false, |
| 1955 |
temporary: false, |
| 1956 |
}, { |
| 1957 |
err: &Error{"Get", "http://google.com/", io.EOF}, |
| 1958 |
timeout: false, |
| 1959 |
temporary: false, |
| 1960 |
}} |
| 1961 |
|
| 1962 |
// Test that url.Error implements net.Error and that it forwards |
| 1963 |
func TestURLErrorImplementsNetError(t *testing.T) { |
| 1964 |
for i, tt := range netErrorTests { |
| 1965 |
err, ok := tt.err.(net.Error) |
| 1966 |
if !ok { |
| 1967 |
t.Errorf("%d: %T does not implement net.Error", i+1, tt.err) |
| 1968 |
continue |
| 1969 |
} |
| 1970 |
if err.Timeout() != tt.timeout { |
| 1971 |
t.Errorf("%d: err.Timeout(): got %v, want %v", i+1, err.Timeout(), tt.timeout) |
| 1972 |
continue |
| 1973 |
} |
| 1974 |
if err.Temporary() != tt.temporary { |
| 1975 |
t.Errorf("%d: err.Temporary(): got %v, want %v", i+1, err.Temporary(), tt.temporary) |
| 1976 |
} |
| 1977 |
} |
| 1978 |
} |
| 1979 |
|
| 1980 |
func TestURLHostnameAndPort(t *testing.T) { |
| 1981 |
tests := []struct { |
| 1982 |
in string // URL.Host field |
| 1983 |
host string |
| 1984 |
port string |
| 1985 |
}{ |
| 1986 |
{"foo.com:80", "foo.com", "80"}, |
| 1987 |
{"foo.com", "foo.com", ""}, |
| 1988 |
{"foo.com:", "foo.com", ""}, |
| 1989 |
{"FOO.COM", "FOO.COM", ""}, // no canonicalization |
| 1990 |
{"1.2.3.4", "1.2.3.4", ""}, |
| 1991 |
{"1.2.3.4:80", "1.2.3.4", "80"}, |
| 1992 |
{"[1:2:3:4]", "1:2:3:4", ""}, |
| 1993 |
{"[1:2:3:4]:80", "1:2:3:4", "80"}, |
| 1994 |
{"[::1]:80", "::1", "80"}, |
| 1995 |
{"[::1]", "::1", ""}, |
| 1996 |
{"[::1]:", "::1", ""}, |
| 1997 |
{"localhost", "localhost", ""}, |
| 1998 |
{"localhost:443", "localhost", "443"}, |
| 1999 |
{"some.super.long.domain.example.org:8080", "some.super.long.domain.example.org", "8080"}, |
| 2000 |
{"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "17000"}, |
| 2001 |
{"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", ""}, |
| 2002 |
|
| 2003 |
// Ensure that even when not valid, Host is one of "Hostname", |
| 2004 |
// "Hostname:Port", "[Hostname]" or "[Hostname]:Port". |
| 2005 |
// See https://golang.org/issue/29098. |
| 2006 |
{"[google.com]:80", "google.com", "80"}, |
| 2007 |
{"google.com]:80", "google.com]", "80"}, |
| 2008 |
{"google.com:80_invalid_port", "google.com:80_invalid_port", ""}, |
| 2009 |
{"[::1]extra]:80", "::1]extra", "80"}, |
| 2010 |
{"google.com]extra:extra", "google.com]extra:extra", ""}, |
| 2011 |
} |
| 2012 |
for _, tt := range tests { |
| 2013 |
u := &URL{Host: tt.in} |
| 2014 |
host, port := u.Hostname(), u.Port() |
| 2015 |
if host != tt.host { |
| 2016 |
t.Errorf("Hostname for Host %q = %q; want %q", tt.in, host, tt.host) |
| 2017 |
} |
| 2018 |
if port != tt.port { |
| 2019 |
t.Errorf("Port for Host %q = %q; want %q", tt.in, port, tt.port) |
| 2020 |
} |
| 2021 |
} |
| 2022 |
} |
| 2023 |
|
| 2024 |
var _ encodingPkg.BinaryMarshaler = (*URL)(nil) |
| 2025 |
var _ encodingPkg.BinaryUnmarshaler = (*URL)(nil) |
| 2026 |
var _ encodingPkg.BinaryAppender = (*URL)(nil) |
| 2027 |
|
| 2028 |
func TestJSON(t *testing.T) { |
| 2029 |
u, err := Parse("https://www.google.com/x?y=z") |
| 2030 |
if err != nil { |
| 2031 |
t.Fatal(err) |
| 2032 |
} |
| 2033 |
js, err := json.Marshal(u) |
| 2034 |
if err != nil { |
| 2035 |
t.Fatal(err) |
| 2036 |
} |
| 2037 |
|
| 2038 |
// If only we could implement TextMarshaler/TextUnmarshaler, |
| 2039 |
// this would work: |
| 2040 |
// |
| 2041 |
// if string(js) != strconv.Quote(u.String()) { |
| 2042 |
// t.Errorf("json encoding: %s\nwant: %s\n", js, strconv.Quote(u.String())) |
| 2043 |
// } |
| 2044 |
|
| 2045 |
u1 := new(URL) |
| 2046 |
err = json.Unmarshal(js, u1) |
| 2047 |
if err != nil { |
| 2048 |
t.Fatal(err) |
| 2049 |
} |
| 2050 |
if u1.String() != u.String() { |
| 2051 |
t.Errorf("json decoded to: %s\nwant: %s\n", u1, u) |
| 2052 |
} |
| 2053 |
} |
| 2054 |
|
| 2055 |
func TestGob(t *testing.T) { |
| 2056 |
u, err := Parse("https://www.google.com/x?y=z") |
| 2057 |
if err != nil { |
| 2058 |
t.Fatal(err) |
| 2059 |
} |
| 2060 |
var w bytes.Buffer |
| 2061 |
err = gob.NewEncoder(&w).Encode(u) |
| 2062 |
if err != nil { |
| 2063 |
t.Fatal(err) |
| 2064 |
} |
| 2065 |
|
| 2066 |
u1 := new(URL) |
| 2067 |
err = gob.NewDecoder(&w).Decode(u1) |
| 2068 |
if err != nil { |
| 2069 |
t.Fatal(err) |
| 2070 |
} |
| 2071 |
if u1.String() != u.String() { |
| 2072 |
t.Errorf("json decoded to: %s\nwant: %s\n", u1, u) |
| 2073 |
} |
| 2074 |
} |
| 2075 |
|
| 2076 |
func TestNilUser(t *testing.T) { |
| 2077 |
defer func() { |
| 2078 |
if v := recover(); v != nil { |
| 2079 |
t.Fatalf("unexpected panic: %v", v) |
| 2080 |
} |
| 2081 |
}() |
| 2082 |
|
| 2083 |
u, err := Parse("http://foo.com/") |
| 2084 |
|
| 2085 |
if err != nil { |
| 2086 |
t.Fatalf("parse err: %v", err) |
| 2087 |
} |
| 2088 |
|
| 2089 |
if v := u.User.Username(); v != "" { |
| 2090 |
t.Fatalf("expected empty username, got %s", v) |
| 2091 |
} |
| 2092 |
|
| 2093 |
if v, ok := u.User.Password(); v != "" || ok { |
| 2094 |
t.Fatalf("expected empty password, got %s (%v)", v, ok) |
| 2095 |
} |
| 2096 |
|
| 2097 |
if v := u.User.String(); v != "" { |
| 2098 |
t.Fatalf("expected empty string, got %s", v) |
| 2099 |
} |
| 2100 |
} |
| 2101 |
|
| 2102 |
func TestInvalidUserPassword(t *testing.T) { |
| 2103 |
_, err := Parse("http://user^:passwo^[email protected]/") |
| 2104 |
if got, wantsub := fmt.Sprint(err), "net/url: invalid userinfo"; !strings.Contains(got, wantsub) { |
| 2105 |
t.Errorf("error = %q; want substring %q", got, wantsub) |
| 2106 |
} |
| 2107 |
} |
| 2108 |
|
| 2109 |
func TestRejectControlCharacters(t *testing.T) { |
| 2110 |
tests := []string{ |
| 2111 |
"http://foo.com/?foo\nbar", |
| 2112 |
"http\r://foo.com/", |
| 2113 |
"http://foo\x7f.com/", |
| 2114 |
} |
| 2115 |
for _, s := range tests { |
| 2116 |
_, err := Parse(s) |
| 2117 |
const wantSub = "net/url: invalid control character in URL" |
| 2118 |
if got := fmt.Sprint(err); !strings.Contains(got, wantSub) { |
| 2119 |
t.Errorf("Parse(%q) error = %q; want substring %q", s, got, wantSub) |
| 2120 |
} |
| 2121 |
} |
| 2122 |
|
| 2123 |
// But don't reject non-ASCII CTLs, at least for now: |
| 2124 |
if _, err := Parse("http://foo.com/ctl\x80"); err != nil { |
| 2125 |
t.Errorf("error parsing URL with non-ASCII control byte: %v", err) |
| 2126 |
} |
| 2127 |
|
| 2128 |
} |
| 2129 |
|
| 2130 |
var escapeBenchmarks = []struct { |
| 2131 |
unescaped string |
| 2132 |
query string |
| 2133 |
path string |
| 2134 |
}{ |
| 2135 |
{ |
| 2136 |
unescaped: "one two", |
| 2137 |
query: "one+two", |
| 2138 |
path: "one%20two", |
| 2139 |
}, |
| 2140 |
{ |
| 2141 |
unescaped: "Фотки собак", |
| 2142 |
query: "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8+%D1%81%D0%BE%D0%B1%D0%B0%D0%BA", |
| 2143 |
path: "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8%20%D1%81%D0%BE%D0%B1%D0%B0%D0%BA", |
| 2144 |
}, |
| 2145 |
|
| 2146 |
{ |
| 2147 |
unescaped: "shortrun(break)shortrun", |
| 2148 |
query: "shortrun%28break%29shortrun", |
| 2149 |
path: "shortrun%28break%29shortrun", |
| 2150 |
}, |
| 2151 |
|
| 2152 |
{ |
| 2153 |
unescaped: "longerrunofcharacters(break)anotherlongerrunofcharacters", |
| 2154 |
query: "longerrunofcharacters%28break%29anotherlongerrunofcharacters", |
| 2155 |
path: "longerrunofcharacters%28break%29anotherlongerrunofcharacters", |
| 2156 |
}, |
| 2157 |
|
| 2158 |
{ |
| 2159 |
unescaped: strings.Repeat("padded/with+various%characters?that=need$some@escaping+paddedsowebreak/256bytes", 4), |
| 2160 |
query: strings.Repeat("padded%2Fwith%2Bvarious%25characters%3Fthat%3Dneed%24some%40escaping%2Bpaddedsowebreak%2F256bytes", 4), |
| 2161 |
path: strings.Repeat("padded%2Fwith+various%25characters%3Fthat=need$some@escaping+paddedsowebreak%2F256bytes", 4), |
| 2162 |
}, |
| 2163 |
} |
| 2164 |
|
| 2165 |
func BenchmarkQueryEscape(b *testing.B) { |
| 2166 |
for _, tc := range escapeBenchmarks { |
| 2167 |
b.Run("", func(b *testing.B) { |
| 2168 |
b.ReportAllocs() |
| 2169 |
var g string |
| 2170 |
for i := 0; i < b.N; i++ { |
| 2171 |
g = QueryEscape(tc.unescaped) |
| 2172 |
} |
| 2173 |
b.StopTimer() |
| 2174 |
if g != tc.query { |
| 2175 |
b.Errorf("QueryEscape(%q) == %q, want %q", tc.unescaped, g, tc.query) |
| 2176 |
} |
| 2177 |
|
| 2178 |
}) |
| 2179 |
} |
| 2180 |
} |
| 2181 |
|
| 2182 |
func BenchmarkPathEscape(b *testing.B) { |
| 2183 |
for _, tc := range escapeBenchmarks { |
| 2184 |
b.Run("", func(b *testing.B) { |
| 2185 |
b.ReportAllocs() |
| 2186 |
var g string |
| 2187 |
for i := 0; i < b.N; i++ { |
| 2188 |
g = PathEscape(tc.unescaped) |
| 2189 |
} |
| 2190 |
b.StopTimer() |
| 2191 |
if g != tc.path { |
| 2192 |
b.Errorf("PathEscape(%q) == %q, want %q", tc.unescaped, g, tc.path) |
| 2193 |
} |
| 2194 |
|
| 2195 |
}) |
| 2196 |
} |
| 2197 |
} |
| 2198 |
|
| 2199 |
func BenchmarkQueryUnescape(b *testing.B) { |
| 2200 |
for _, tc := range escapeBenchmarks { |
| 2201 |
b.Run("", func(b *testing.B) { |
| 2202 |
b.ReportAllocs() |
| 2203 |
var g string |
| 2204 |
for i := 0; i < b.N; i++ { |
| 2205 |
g, _ = QueryUnescape(tc.query) |
| 2206 |
} |
| 2207 |
b.StopTimer() |
| 2208 |
if g != tc.unescaped { |
| 2209 |
b.Errorf("QueryUnescape(%q) == %q, want %q", tc.query, g, tc.unescaped) |
| 2210 |
} |
| 2211 |
|
| 2212 |
}) |
| 2213 |
} |
| 2214 |
} |
| 2215 |
|
| 2216 |
func BenchmarkPathUnescape(b *testing.B) { |
| 2217 |
for _, tc := range escapeBenchmarks { |
| 2218 |
b.Run("", func(b *testing.B) { |
| 2219 |
b.ReportAllocs() |
| 2220 |
var g string |
| 2221 |
for i := 0; i < b.N; i++ { |
| 2222 |
g, _ = PathUnescape(tc.path) |
| 2223 |
} |
| 2224 |
b.StopTimer() |
| 2225 |
if g != tc.unescaped { |
| 2226 |
b.Errorf("PathUnescape(%q) == %q, want %q", tc.path, g, tc.unescaped) |
| 2227 |
} |
| 2228 |
|
| 2229 |
}) |
| 2230 |
} |
| 2231 |
} |
| 2232 |
|
| 2233 |
func TestJoinPath(t *testing.T) { |
| 2234 |
tests := []struct { |
| 2235 |
base string |
| 2236 |
elem []string |
| 2237 |
out string |
| 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/a/b/c", |
| 2246 |
elem: []string{"../../../go"}, |
| 2247 |
out: "https://go.googlesource.com/go", |
| 2248 |
}, |
| 2249 |
{ |
| 2250 |
base: "https://go.googlesource.com/", |
| 2251 |
elem: []string{"../go"}, |
| 2252 |
out: "https://go.googlesource.com/go", |
| 2253 |
}, |
| 2254 |
{ |
| 2255 |
base: "https://go.googlesource.com", |
| 2256 |
elem: []string{"../go", "../../go", "../../../go"}, |
| 2257 |
out: "https://go.googlesource.com/go", |
| 2258 |
}, |
| 2259 |
{ |
| 2260 |
base: "https://go.googlesource.com/../go", |
| 2261 |
elem: nil, |
| 2262 |
out: "https://go.googlesource.com/go", |
| 2263 |
}, |
| 2264 |
{ |
| 2265 |
base: "https://go.googlesource.com/", |
| 2266 |
elem: []string{"./go"}, |
| 2267 |
out: "https://go.googlesource.com/go", |
| 2268 |
}, |
| 2269 |
{ |
| 2270 |
base: "https://go.googlesource.com//", |
| 2271 |
elem: []string{"/go"}, |
| 2272 |
out: "https://go.googlesource.com/go", |
| 2273 |
}, |
| 2274 |
{ |
| 2275 |
base: "https://go.googlesource.com//", |
| 2276 |
elem: []string{"/go", "a", "b", "c"}, |
| 2277 |
out: "https://go.googlesource.com/go/a/b/c", |
| 2278 |
}, |
| 2279 |
{ |
| 2280 |
base: "http://[fe80::1%en0]:8080/", |
| 2281 |
elem: []string{"/go"}, |
| 2282 |
}, |
| 2283 |
{ |
| 2284 |
base: "https://go.googlesource.com", |
| 2285 |
elem: []string{"go/"}, |
| 2286 |
out: "https://go.googlesource.com/go/", |
| 2287 |
}, |
| 2288 |
{ |
| 2289 |
base: "https://go.googlesource.com", |
| 2290 |
elem: []string{"go//"}, |
| 2291 |
out: "https://go.googlesource.com/go/", |
| 2292 |
}, |
| 2293 |
{ |
| 2294 |
base: "https://go.googlesource.com", |
| 2295 |
elem: nil, |
| 2296 |
out: "https://go.googlesource.com/", |
| 2297 |
}, |
| 2298 |
{ |
| 2299 |
base: "https://go.googlesource.com/", |
| 2300 |
elem: nil, |
| 2301 |
out: "https://go.googlesource.com/", |
| 2302 |
}, |
| 2303 |
{ |
| 2304 |
base: "https://go.googlesource.com/a%2fb", |
| 2305 |
elem: []string{"c"}, |
| 2306 |
out: "https://go.googlesource.com/a%2fb/c", |
| 2307 |
}, |
| 2308 |
{ |
| 2309 |
base: "https://go.googlesource.com/a%2fb", |
| 2310 |
elem: []string{"c%2fd"}, |
| 2311 |
out: "https://go.googlesource.com/a%2fb/c%2fd", |
| 2312 |
}, |
| 2313 |
{ |
| 2314 |
base: "https://go.googlesource.com/a/b", |
| 2315 |
elem: []string{"/go"}, |
| 2316 |
out: "https://go.googlesource.com/a/b/go", |
| 2317 |
}, |
| 2318 |
{ |
| 2319 |
base: "https://go.googlesource.com/", |
| 2320 |
elem: []string{"100%"}, |
| 2321 |
}, |
| 2322 |
{ |
| 2323 |
base: "/", |
| 2324 |
elem: nil, |
| 2325 |
out: "/", |
| 2326 |
}, |
| 2327 |
{ |
| 2328 |
base: "a", |
| 2329 |
elem: nil, |
| 2330 |
out: "a", |
| 2331 |
}, |
| 2332 |
{ |
| 2333 |
base: "a", |
| 2334 |
elem: []string{"b"}, |
| 2335 |
out: "a/b", |
| 2336 |
}, |
| 2337 |
{ |
| 2338 |
base: "a", |
| 2339 |
elem: []string{"../b"}, |
| 2340 |
out: "b", |
| 2341 |
}, |
| 2342 |
{ |
| 2343 |
base: "a", |
| 2344 |
elem: []string{"../../b"}, |
| 2345 |
out: "b", |
| 2346 |
}, |
| 2347 |
{ |
| 2348 |
base: "", |
| 2349 |
elem: []string{"a"}, |
| 2350 |
out: "a", |
| 2351 |
}, |
| 2352 |
{ |
| 2353 |
base: "", |
| 2354 |
elem: []string{"../a"}, |
| 2355 |
out: "a", |
| 2356 |
}, |
| 2357 |
} |
| 2358 |
for _, tt := range tests { |
| 2359 |
wantErr := "nil" |
| 2360 |
if tt.out == "" { |
| 2361 |
wantErr = "non-nil error" |
| 2362 |
} |
| 2363 |
out, err := JoinPath(tt.base, tt.elem...) |
| 2364 |
if out != tt.out || (err == nil) != (tt.out != "") { |
| 2365 |
t.Errorf("JoinPath(%q, %q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr) |
| 2366 |
} |
| 2367 |
|
| 2368 |
u, err := Parse(tt.base) |
| 2369 |
if err != nil { |
| 2370 |
if tt.out != "" { |
| 2371 |
t.Errorf("Parse(%q) = %v", tt.base, err) |
| 2372 |
} |
| 2373 |
continue |
| 2374 |
} |
| 2375 |
if tt.out == "" { |
| 2376 |
// URL.JoinPath doesn't return an error, so leave it unchanged |
| 2377 |
tt.out = tt.base |
| 2378 |
} |
| 2379 |
out = u.JoinPath(tt.elem...).String() |
| 2380 |
if out != tt.out { |
| 2381 |
t.Errorf("Parse(%q).JoinPath(%q) = %q, want %q", tt.base, tt.elem, out, tt.out) |
| 2382 |
} |
| 2383 |
} |
| 2384 |
} |
| 2385 |
|
| 2386 |
func TestParseStrictIpv6(t *testing.T) { |
| 2387 |
t.Setenv("GODEBUG", "urlstrictcolons=0") |
| 2388 |
|
| 2389 |
tests := []struct { |
| 2390 |
url string |
| 2391 |
}{ |
| 2392 |
// Malformed URLs that used to parse. |
| 2393 |
{"https://1:2:3:4:5:6:7:8"}, |
| 2394 |
{"https://1:2:3:4:5:6:7:8:80"}, |
| 2395 |
{"https://example.com:80:"}, |
| 2396 |
} |
| 2397 |
for i, tc := range tests { |
| 2398 |
t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 2399 |
_, err := Parse(tc.url) |
| 2400 |
if err != nil { |
| 2401 |
t.Errorf("Parse(%q) error = %v, want nil", tc.url, err) |
| 2402 |
} |
| 2403 |
}) |
| 2404 |
} |
| 2405 |
|
| 2406 |
} |