2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 11:45:18 -05:00
|
|
|
import { assert, assertEquals } from "./test_util.ts";
|
2024-07-25 01:30:28 -04:00
|
|
|
import { assertType, IsExact } from "@std/testing/types";
|
2021-09-08 05:14:29 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlPatternFromString() {
|
2021-09-08 05:14:29 -04:00
|
|
|
const pattern = new URLPattern("https://deno.land/foo/:bar");
|
|
|
|
assertEquals(pattern.protocol, "https");
|
|
|
|
assertEquals(pattern.hostname, "deno.land");
|
|
|
|
assertEquals(pattern.pathname, "/foo/:bar");
|
|
|
|
|
|
|
|
assert(pattern.test("https://deno.land/foo/x"));
|
|
|
|
assert(!pattern.test("https://deno.com/foo/x"));
|
|
|
|
const match = pattern.exec("https://deno.land/foo/x");
|
|
|
|
assert(match);
|
|
|
|
assertEquals(match.pathname.input, "/foo/x");
|
|
|
|
assertEquals(match.pathname.groups, { bar: "x" });
|
2023-04-26 19:15:25 -04:00
|
|
|
|
|
|
|
// group values should be nullable
|
|
|
|
const val = match.pathname.groups.val;
|
|
|
|
assertType<IsExact<typeof val, string | undefined>>(true);
|
2021-09-08 05:14:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlPatternFromStringWithBase() {
|
2021-09-08 05:14:29 -04:00
|
|
|
const pattern = new URLPattern("/foo/:bar", "https://deno.land");
|
|
|
|
assertEquals(pattern.protocol, "https");
|
|
|
|
assertEquals(pattern.hostname, "deno.land");
|
|
|
|
assertEquals(pattern.pathname, "/foo/:bar");
|
|
|
|
|
|
|
|
assert(pattern.test("https://deno.land/foo/x"));
|
|
|
|
assert(!pattern.test("https://deno.com/foo/x"));
|
|
|
|
const match = pattern.exec("https://deno.land/foo/x");
|
|
|
|
assert(match);
|
|
|
|
assertEquals(match.pathname.input, "/foo/x");
|
|
|
|
assertEquals(match.pathname.groups, { bar: "x" });
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function urlPatternFromInit() {
|
2021-09-08 05:14:29 -04:00
|
|
|
const pattern = new URLPattern({
|
|
|
|
pathname: "/foo/:bar",
|
|
|
|
});
|
|
|
|
assertEquals(pattern.protocol, "*");
|
|
|
|
assertEquals(pattern.hostname, "*");
|
|
|
|
assertEquals(pattern.pathname, "/foo/:bar");
|
|
|
|
|
|
|
|
assert(pattern.test("https://deno.land/foo/x"));
|
|
|
|
assert(pattern.test("https://deno.com/foo/x"));
|
|
|
|
assert(!pattern.test("https://deno.com/bar/x"));
|
|
|
|
|
|
|
|
assert(pattern.test({ pathname: "/foo/x" }));
|
|
|
|
});
|
2023-02-28 18:14:16 -05:00
|
|
|
|
|
|
|
Deno.test(function urlPatternWithPrototypePollution() {
|
|
|
|
const originalExec = RegExp.prototype.exec;
|
|
|
|
try {
|
|
|
|
RegExp.prototype.exec = () => {
|
|
|
|
throw Error();
|
|
|
|
};
|
|
|
|
const pattern = new URLPattern({
|
|
|
|
pathname: "/foo/:bar",
|
|
|
|
});
|
|
|
|
assert(pattern.test("https://deno.land/foo/x"));
|
|
|
|
} finally {
|
|
|
|
RegExp.prototype.exec = originalExec;
|
|
|
|
}
|
|
|
|
});
|
2024-08-22 08:44:09 -04:00
|
|
|
|
|
|
|
Deno.test(function urlPatternEmptyFallback() {
|
|
|
|
const p = new URLPattern({ pathname: "/foo/bar{/:qaz}?" });
|
|
|
|
const match = p.exec("https://example.com/foo/bar");
|
|
|
|
assertEquals(match!.pathname.groups.qaz, "");
|
|
|
|
});
|