Files
bun.sh/test/js/node/url/url.test.ts
Jarred Sumner f5c8914c8a Re-sync URL from WebKit + set ERR_MISSING_ARGS (#10129)
* Update URL from WebKit

* Set `ERR_MISSING_ARGS` code on all Error objects from C++

* Fix the `code`

* [autofix.ci] apply automated fixes

* Micro optimize URL

* [autofix.ci] apply automated fixes

* Update url.mjs

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2024-04-09 23:21:11 -07:00

82 lines
1.9 KiB
TypeScript

import { parse } from "url";
describe("Url.prototype.parse", () => {
it("parses URL correctly", () => {
const url = parse("https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat");
expect(url.hash).toEqual("#qat");
expect(url.host).toEqual("baz.qat:8000");
expect(url.hostname).toEqual("baz.qat");
expect(url.href).toEqual("https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat");
expect(url.pathname).toEqual("/qux/quux");
expect(url.port).toEqual("8000");
expect(url.protocol).toEqual("https:");
expect(url.search).toEqual("?foo=bar&baz=12");
});
it("accepts empty host", () => {
expect(() => parse("http://")).not.toThrow();
});
it("accepts ipv6 host", () => {
expect(parse("http://[::1]")).toEqual({
protocol: "http:",
slashes: true,
auth: null,
host: "[::1]",
port: null,
hostname: "::1",
hash: null,
search: null,
query: null,
pathname: "/",
path: "/",
href: "http://[::1]/",
});
});
it("handles punycode", () => {
expect(parse("http://xn--xample-hva.com")).toEqual({
protocol: "http:",
slashes: true,
auth: null,
host: "xn--xample-hva.com",
port: null,
hostname: "xn--xample-hva.com",
hash: null,
search: null,
query: null,
pathname: "/",
path: "/",
href: "http://xn--xample-hva.com/",
});
expect(parse("http://💥.net")).toEqual({
protocol: "http:",
slashes: true,
auth: null,
host: "xn--hs8h.net",
port: null,
hostname: "xn--hs8h.net",
hash: null,
search: null,
query: null,
pathname: "/",
path: "/",
href: "http://xn--hs8h.net/",
});
});
});
it("URL constructor throws ERR_MISSING_ARGS", () => {
var err;
try {
// @ts-expect-error
new URL();
} catch (e) {
err = e;
}
// @ts-expect-error
expect(err?.code).toEqual("ERR_MISSING_ARGS");
});