url-test.c++ 21.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Copyright (c) 2017 Cloudflare, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "url.h"
#include <kj/debug.h>
#include <kj/test.h>

namespace kj {
namespace {

29 30
Url parseAndCheck(kj::StringPtr originalText, kj::StringPtr expectedRestringified = nullptr,
                  Url::Options options = {}) {
31
  if (expectedRestringified == nullptr) expectedRestringified = originalText;
32
  auto url = Url::parse(originalText, Url::REMOTE_HREF, options);
33
  KJ_EXPECT(kj::str(url) == expectedRestringified, url, originalText, expectedRestringified);
34 35 36
  // Make sure clones also restringify to the expected string.
  auto clone = url.clone();
  KJ_EXPECT(kj::str(clone) == expectedRestringified, clone, originalText, expectedRestringified);
37 38 39
  return url;
}

40 41
static constexpr Url::Options NO_DECODE {
  false,  // percentDecode
42 43 44 45 46 47
  false,  // allowEmpty
};

static constexpr Url::Options ALLOW_EMPTY {
  true,    // percentDecode
  true,    // allowEmpty
48 49
};

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
KJ_TEST("parse / stringify URL") {
  {
    auto url = parseAndCheck("https://capnproto.org");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path == nullptr);
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(url.fragment == nullptr);
  }

  {
    auto url = parseAndCheck("https://capnproto.org:80");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org:80");
    KJ_EXPECT(url.path == nullptr);
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(url.fragment == nullptr);
  }

  {
    auto url = parseAndCheck("https://capnproto.org/");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path == nullptr);
    KJ_EXPECT(url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(url.fragment == nullptr);
  }

  {
    auto url = parseAndCheck("https://capnproto.org/foo/bar");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(url.fragment == nullptr);
  }

  {
    auto url = parseAndCheck("https://capnproto.org/foo/bar/");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(url.fragment == nullptr);
  }

  {
    auto url = parseAndCheck("https://capnproto.org/foo/bar?baz=qux&corge#garply");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_ASSERT(url.query.size() == 2);
    KJ_EXPECT(url.query[0].name == "baz");
    KJ_EXPECT(url.query[0].value == "qux");
    KJ_EXPECT(url.query[1].name == "corge");
    KJ_EXPECT(url.query[1].value == nullptr);
    KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply");
  }

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  {
    auto url = parseAndCheck("https://capnproto.org/foo/bar?baz=qux&corge=#garply");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_ASSERT(url.query.size() == 2);
    KJ_EXPECT(url.query[0].name == "baz");
    KJ_EXPECT(url.query[0].value == "qux");
    KJ_EXPECT(url.query[1].name == "corge");
    KJ_EXPECT(url.query[1].value == nullptr);
    KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply");
  }

  {
    auto url = parseAndCheck("https://capnproto.org/foo/bar?baz=&corge=grault#garply");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_ASSERT(url.query.size() == 2);
    KJ_EXPECT(url.query[0].name == "baz");
    KJ_EXPECT(url.query[0].value == "");
    KJ_EXPECT(url.query[1].name == "corge");
    KJ_EXPECT(url.query[1].value == "grault");
    KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply");
  }

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  {
    auto url = parseAndCheck("https://capnproto.org/foo/bar/?baz=qux&corge=grault#garply");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(url.hasTrailingSlash);
    KJ_ASSERT(url.query.size() == 2);
    KJ_EXPECT(url.query[0].name == "baz");
    KJ_EXPECT(url.query[0].value == "qux");
    KJ_EXPECT(url.query[1].name == "corge");
    KJ_EXPECT(url.query[1].value == "grault");
    KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply");
  }

  {
    auto url = parseAndCheck("https://capnproto.org/foo/bar?baz=qux#garply");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_ASSERT(url.query.size() == 1);
    KJ_EXPECT(url.query[0].name == "baz");
    KJ_EXPECT(url.query[0].value == "qux");
    KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply");
  }

179 180 181 182 183 184 185 186
  {
    auto url = parseAndCheck("https://capnproto.org/foo?bar%20baz=qux+quux",
                             "https://capnproto.org/foo?bar+baz=qux+quux");
    KJ_ASSERT(url.query.size() == 1);
    KJ_EXPECT(url.query[0].name == "bar baz");
    KJ_EXPECT(url.query[0].value == "qux quux");
  }

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  {
    auto url = parseAndCheck("https://capnproto.org/foo/bar#garply");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply");
  }

  {
    auto url = parseAndCheck("https://capnproto.org/foo/bar/#garply");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply");
  }

  {
    auto url = parseAndCheck("https://capnproto.org#garply");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path == nullptr);
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply");
  }

  {
    auto url = parseAndCheck("https://capnproto.org/#garply");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path == nullptr);
    KJ_EXPECT(url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply");
  }

  {
    auto url = parseAndCheck("https://foo@capnproto.org");
    KJ_EXPECT(url.scheme == "https");
    auto& user = KJ_ASSERT_NONNULL(url.userInfo);
    KJ_EXPECT(user.username == "foo");
    KJ_EXPECT(user.password == nullptr);
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path == nullptr);
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(url.fragment == nullptr);
  }

  {
245
    auto url = parseAndCheck("https://$foo&:12+,34@capnproto.org");
246 247
    KJ_EXPECT(url.scheme == "https");
    auto& user = KJ_ASSERT_NONNULL(url.userInfo);
248 249
    KJ_EXPECT(user.username == "$foo&");
    KJ_EXPECT(KJ_ASSERT_NONNULL(user.password) == "12+,34");
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path == nullptr);
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(url.fragment == nullptr);
  }

  {
    auto url = parseAndCheck("https://[2001:db8::1234]:80/foo");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.userInfo == nullptr);
    KJ_EXPECT(url.host == "[2001:db8::1234]:80");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo"}));
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(url.fragment == nullptr);
  }

268 269 270 271 272
  {
    auto url = parseAndCheck("https://capnproto.org/foo%2Fbar/baz");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo/bar", "baz"}));
  }

273 274 275 276 277 278
  parseAndCheck("https://capnproto.org/foo/bar?", "https://capnproto.org/foo/bar");
  parseAndCheck("https://capnproto.org/foo/bar?#", "https://capnproto.org/foo/bar#");
  parseAndCheck("https://capnproto.org/foo/bar#");

  // Scheme and host are forced to lower-case.
  parseAndCheck("hTtP://capNprotO.org/fOo/bAr", "http://capnproto.org/fOo/bAr");
279 280 281 282

  // URLs with underscores in their hostnames are allowed, but you probably shouldn't use them. They
  // are not valid domain names.
  parseAndCheck("https://bad_domain.capnproto.org/");
283 284 285 286 287 288 289

  // Make sure URLs with %-encoded '%' signs in their userinfo, path, query, and fragment components
  // get correctly re-encoded.
  parseAndCheck("https://foo%25bar:baz%25qux@capnproto.org/");
  parseAndCheck("https://capnproto.org/foo%25bar");
  parseAndCheck("https://capnproto.org/?foo%25bar=baz%25qux");
  parseAndCheck("https://capnproto.org/#foo%25bar");
290 291 292 293 294 295 296

  // Make sure redundant /'s and &'s aren't collapsed when options.removeEmpty is false.
  parseAndCheck("https://capnproto.org/foo//bar///test//?foo=bar&&baz=qux&", nullptr, ALLOW_EMPTY);

  // "." and ".." are still processed, though.
  parseAndCheck("https://capnproto.org/foo//../bar/.",
                "https://capnproto.org/foo/bar/", ALLOW_EMPTY);
297 298 299 300 301 302 303 304 305 306 307 308

  {
    auto url = parseAndCheck("https://foo/", nullptr, ALLOW_EMPTY);
    KJ_EXPECT(url.path.size() == 0);
    KJ_EXPECT(url.hasTrailingSlash);
  }

  {
    auto url = parseAndCheck("https://foo/bar/", nullptr, ALLOW_EMPTY);
    KJ_EXPECT(url.path.size() == 1);
    KJ_EXPECT(url.hasTrailingSlash);
  }
309 310 311 312 313 314 315 316 317 318 319 320 321
}

KJ_TEST("URL percent encoding") {
  parseAndCheck(
      "https://b%6fb:%61bcd@capnpr%6fto.org/f%6fo?b%61r=b%61z#q%75x",
      "https://bob:abcd@capnproto.org/foo?bar=baz#qux");

  parseAndCheck(
      "https://b\001b:\001bcd@capnproto.org/f\001o?b\001r=b\001z#q\001x",
      "https://b%01b:%01bcd@capnproto.org/f%01o?b%01r=b%01z#q%01x");

  parseAndCheck(
      "https://b b: bcd@capnproto.org/f o?b r=b z#q x",
322
      "https://b%20b:%20bcd@capnproto.org/f%20o?b+r=b+z#q%20x");
323 324 325 326 327 328 329 330 331

  parseAndCheck(
      "https://capnproto.org/foo?bar=baz#@?#^[\\]{|}",
      "https://capnproto.org/foo?bar=baz#@?#^[\\]{|}");

  // All permissible non-alphanumeric, non-separator path characters.
  parseAndCheck(
      "https://capnproto.org/!$&'()*+,-.:;=@[]^_|~",
      "https://capnproto.org/!$&'()*+,-.:;=@[]^_|~");
332 333
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
KJ_TEST("parse / stringify URL w/o decoding") {
  {
    auto url = parseAndCheck("https://capnproto.org/foo%2Fbar/baz", nullptr, NO_DECODE);
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo%2Fbar", "baz"}));
  }

  {
    // This case would throw an exception without NO_DECODE.
    Url url = parseAndCheck("https://capnproto.org/R%20%26%20S?%foo=%QQ", nullptr, NO_DECODE);
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"R%20%26%20S"}));
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_ASSERT(url.query.size() == 1);
    KJ_EXPECT(url.query[0].name == "%foo");
    KJ_EXPECT(url.query[0].value == "%QQ");
  }
}

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
KJ_TEST("URL relative paths") {
  parseAndCheck(
      "https://capnproto.org/foo//bar",
      "https://capnproto.org/foo/bar");

  parseAndCheck(
      "https://capnproto.org/foo/./bar",
      "https://capnproto.org/foo/bar");

  parseAndCheck(
      "https://capnproto.org/foo/bar//",
      "https://capnproto.org/foo/bar/");

  parseAndCheck(
      "https://capnproto.org/foo/bar/.",
      "https://capnproto.org/foo/bar/");

  parseAndCheck(
      "https://capnproto.org/foo/baz/../bar",
      "https://capnproto.org/foo/bar");

  parseAndCheck(
      "https://capnproto.org/foo/bar/baz/..",
      "https://capnproto.org/foo/bar/");

  parseAndCheck(
      "https://capnproto.org/..",
      "https://capnproto.org/");

  parseAndCheck(
      "https://capnproto.org/foo/../..",
      "https://capnproto.org/");
}

KJ_TEST("URL for HTTP request") {
  {
    Url url = Url::parse("https://bob:1234@capnproto.org/foo/bar?baz=qux#corge");
390 391
    KJ_EXPECT(url.toString(Url::REMOTE_HREF) ==
        "https://bob:1234@capnproto.org/foo/bar?baz=qux#corge");
392 393 394 395 396 397
    KJ_EXPECT(url.toString(Url::HTTP_PROXY_REQUEST) == "https://capnproto.org/foo/bar?baz=qux");
    KJ_EXPECT(url.toString(Url::HTTP_REQUEST) == "/foo/bar?baz=qux");
  }

  {
    Url url = Url::parse("https://capnproto.org");
398
    KJ_EXPECT(url.toString(Url::REMOTE_HREF) == "https://capnproto.org");
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    KJ_EXPECT(url.toString(Url::HTTP_PROXY_REQUEST) == "https://capnproto.org");
    KJ_EXPECT(url.toString(Url::HTTP_REQUEST) == "/");
  }

  {
    Url url = Url::parse("/foo/bar?baz=qux&corge", Url::HTTP_REQUEST);
    KJ_EXPECT(url.scheme == nullptr);
    KJ_EXPECT(url.host == nullptr);
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_ASSERT(url.query.size() == 2);
    KJ_EXPECT(url.query[0].name == "baz");
    KJ_EXPECT(url.query[0].value == "qux");
    KJ_EXPECT(url.query[1].name == "corge");
    KJ_EXPECT(url.query[1].value == nullptr);
  }

  {
    Url url = Url::parse("https://capnproto.org/foo/bar?baz=qux&corge", Url::HTTP_PROXY_REQUEST);
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>({"foo", "bar"}));
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_ASSERT(url.query.size() == 2);
    KJ_EXPECT(url.query[0].name == "baz");
    KJ_EXPECT(url.query[0].value == "qux");
    KJ_EXPECT(url.query[1].name == "corge");
    KJ_EXPECT(url.query[1].value == nullptr);
  }
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

  {
    // '#' is allowed in path components in the HTTP_REQUEST context.
    Url url = Url::parse("/foo#bar", Url::HTTP_REQUEST);
    KJ_EXPECT(url.toString(Url::HTTP_REQUEST) == "/foo%23bar");
    KJ_EXPECT(url.scheme == nullptr);
    KJ_EXPECT(url.host == nullptr);
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>{"foo#bar"});
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(url.fragment == nullptr);
  }

  {
    // '#' is allowed in path components in the HTTP_PROXY_REQUEST context.
    Url url = Url::parse("https://capnproto.org/foo#bar", Url::HTTP_PROXY_REQUEST);
    KJ_EXPECT(url.toString(Url::HTTP_PROXY_REQUEST) == "https://capnproto.org/foo%23bar");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr<const StringPtr>{"foo#bar"});
    KJ_EXPECT(!url.hasTrailingSlash);
    KJ_EXPECT(url.query == nullptr);
    KJ_EXPECT(url.fragment == nullptr);
  }

  {
    // '#' is allowed in query components in the HTTP_REQUEST context.
    Url url = Url::parse("/?foo=bar#123", Url::HTTP_REQUEST);
    KJ_EXPECT(url.toString(Url::HTTP_REQUEST) == "/?foo=bar%23123");
    KJ_EXPECT(url.scheme == nullptr);
    KJ_EXPECT(url.host == nullptr);
    KJ_EXPECT(url.path == nullptr);
    KJ_EXPECT(url.hasTrailingSlash);
    KJ_ASSERT(url.query.size() == 1);
    KJ_EXPECT(url.query[0].name == "foo");
    KJ_EXPECT(url.query[0].value == "bar#123");
    KJ_EXPECT(url.fragment == nullptr);
  }

  {
    // '#' is allowed in query components in the HTTP_PROXY_REQUEST context.
    Url url = Url::parse("https://capnproto.org/?foo=bar#123", Url::HTTP_PROXY_REQUEST);
    KJ_EXPECT(url.toString(Url::HTTP_PROXY_REQUEST) == "https://capnproto.org/?foo=bar%23123");
    KJ_EXPECT(url.scheme == "https");
    KJ_EXPECT(url.host == "capnproto.org");
    KJ_EXPECT(url.path == nullptr);
    KJ_EXPECT(url.hasTrailingSlash);
    KJ_ASSERT(url.query.size() == 1);
    KJ_EXPECT(url.query[0].name == "foo");
    KJ_EXPECT(url.query[0].value == "bar#123");
    KJ_EXPECT(url.fragment == nullptr);
  }
480 481 482 483 484 485 486 487 488 489 490 491 492 493
}

KJ_TEST("parse URL failure") {
  KJ_EXPECT(Url::tryParse("ht/tps://capnproto.org") == nullptr);
  KJ_EXPECT(Url::tryParse("capnproto.org") == nullptr);
  KJ_EXPECT(Url::tryParse("https:foo") == nullptr);

  // percent-decode errors
  KJ_EXPECT(Url::tryParse("https://capnproto.org/f%nno") == nullptr);
  KJ_EXPECT(Url::tryParse("https://capnproto.org/foo?b%nnr=baz") == nullptr);

  // components not valid in context
  KJ_EXPECT(Url::tryParse("https://capnproto.org/foo", Url::HTTP_REQUEST) == nullptr);
  KJ_EXPECT(Url::tryParse("https://bob:123@capnproto.org/foo", Url::HTTP_PROXY_REQUEST) == nullptr);
494
  KJ_EXPECT(Url::tryParse("https://capnproto.org#/foo", Url::HTTP_PROXY_REQUEST) == nullptr);
495 496
}

497 498 499
void parseAndCheckRelative(kj::StringPtr base, kj::StringPtr relative, kj::StringPtr expected,
                           Url::Options options = {}) {
  auto parsed = Url::parse(base, Url::REMOTE_HREF, options).parseRelative(relative);
500
  KJ_EXPECT(kj::str(parsed) == expected, parsed, expected);
501 502
  auto clone = parsed.clone();
  KJ_EXPECT(kj::str(clone) == expected, clone, expected);
503 504 505 506 507 508
}

KJ_TEST("parse relative URL") {
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge",
                        "#grault",
                        "https://capnproto.org/foo/bar?baz=qux#grault");
509 510 511 512 513 514
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz#corge",
                        "#grault",
                        "https://capnproto.org/foo/bar?baz#grault");
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=#corge",
                        "#grault",
                        "https://capnproto.org/foo/bar?baz=#grault");
515 516 517
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge",
                        "?grault",
                        "https://capnproto.org/foo/bar?grault");
518 519 520
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge",
                        "?grault=",
                        "https://capnproto.org/foo/bar?grault=");
521 522 523
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge",
                        "?grault+garply=waldo",
                        "https://capnproto.org/foo/bar?grault+garply=waldo");
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge",
                        "grault",
                        "https://capnproto.org/foo/grault");
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge",
                        "/grault",
                        "https://capnproto.org/grault");
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge",
                        "//grault",
                        "https://grault");
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge",
                        "//grault/garply",
                        "https://grault/garply");
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge",
                        "http:/grault",
                        "http://capnproto.org/grault");
  parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge",
                        "/http:/grault",
541
                        "https://capnproto.org/http:/grault");
542 543 544
  parseAndCheckRelative("https://capnproto.org/",
                        "/foo/../bar",
                        "https://capnproto.org/bar");
545 546
}

547 548 549 550 551 552 553
KJ_TEST("parse relative URL w/o decoding") {
  // This case would throw an exception without NO_DECODE.
  parseAndCheckRelative("https://capnproto.org/R%20%26%20S?%foo=%QQ",
                        "%ANOTH%ERBAD%URL",
                        "https://capnproto.org/%ANOTH%ERBAD%URL", NO_DECODE);
}

554 555 556 557 558
KJ_TEST("parse relative URL failure") {
  auto base = Url::parse("https://example.com/");
  KJ_EXPECT(base.tryParseRelative("https://[not a host]") == nullptr);
}

559 560
}  // namespace
}  // namespace kj