1
0
Fork 0
forked from svrjs/svrjs
This repository has been archived on 2024-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs/tests/utils/urlParser.test.js

81 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

const parseURL = require("../../src/utils/urlParser.js");
2024-08-23 19:58:15 +02:00
describe("URL parser", () => {
2024-08-23 19:58:15 +02:00
test("should parse a simple URL", () => {
const parsedUrl = parseURL("http://example.com");
expect(parsedUrl.protocol).toBe("http:");
expect(parsedUrl.hostname).toBe("example.com");
expect(parsedUrl.pathname).toBe("/");
expect(parsedUrl.path).toBe("/");
expect(parsedUrl.href).toBe("http://example.com/");
});
test("should parse a URL with a path", () => {
const parsedUrl = parseURL("http://example.com/path/to/resource");
expect(parsedUrl.protocol).toBe("http:");
expect(parsedUrl.hostname).toBe("example.com");
expect(parsedUrl.pathname).toBe("/path/to/resource");
expect(parsedUrl.path).toBe("/path/to/resource");
expect(parsedUrl.href).toBe("http://example.com/path/to/resource");
});
test("should parse a URL with a query string", () => {
const parsedUrl = parseURL("http://example.com/path?query=string");
expect(parsedUrl.protocol).toBe("http:");
expect(parsedUrl.hostname).toBe("example.com");
expect(parsedUrl.pathname).toBe("/path");
expect(parsedUrl.search).toBe("?query=string");
expect(parsedUrl.query.query).toBe("string");
expect(parsedUrl.path).toBe("/path?query=string");
expect(parsedUrl.href).toBe("http://example.com/path?query=string");
});
test("should parse a URL with a port", () => {
const parsedUrl = parseURL("http://example.com:8080");
expect(parsedUrl.protocol).toBe("http:");
expect(parsedUrl.hostname).toBe("example.com");
expect(parsedUrl.port).toBe("8080");
expect(parsedUrl.pathname).toBe("/");
expect(parsedUrl.path).toBe("/");
expect(parsedUrl.href).toBe("http://example.com:8080/");
});
test("should parse a URL with a username and password", () => {
const parsedUrl = parseURL("http://user:pass@example.com");
expect(parsedUrl.protocol).toBe("http:");
expect(parsedUrl.auth).toBe("user:pass");
expect(parsedUrl.hostname).toBe("example.com");
expect(parsedUrl.pathname).toBe("/");
expect(parsedUrl.path).toBe("/");
expect(parsedUrl.href).toBe("http://user:pass@example.com/");
});
test("should parse a URL with a fragment", () => {
const parsedUrl = parseURL("http://example.com/path#fragment");
expect(parsedUrl.protocol).toBe("http:");
expect(parsedUrl.hostname).toBe("example.com");
expect(parsedUrl.pathname).toBe("/path");
expect(parsedUrl.hash).toBe("#fragment");
expect(parsedUrl.path).toBe("/path");
expect(parsedUrl.href).toBe("http://example.com/path#fragment");
});
test("should parse a URL with all components", () => {
const parsedUrl = parseURL(
"http://user:pass@example.com:8080/path/to/resource?query=string#fragment"
2024-08-23 19:58:15 +02:00
);
expect(parsedUrl.protocol).toBe("http:");
expect(parsedUrl.auth).toBe("user:pass");
expect(parsedUrl.hostname).toBe("example.com");
expect(parsedUrl.port).toBe("8080");
expect(parsedUrl.pathname).toBe("/path/to/resource");
expect(parsedUrl.search).toBe("?query=string");
expect(parsedUrl.query.query).toBe("string");
expect(parsedUrl.hash).toBe("#fragment");
expect(parsedUrl.path).toBe("/path/to/resource?query=string");
expect(parsedUrl.href).toBe(
"http://user:pass@example.com:8080/path/to/resource?query=string#fragment"
2024-08-23 19:58:15 +02:00
);
});
});