From c2c20392ee1072ba3c75d1bf631adbc936914478 Mon Sep 17 00:00:00 2001 From: KaKa Date: Thu, 28 Oct 2021 20:24:01 +0800 Subject: [PATCH] chore: fix linting --- lib/index.js | 144 +++++------ test/index.test.js | 616 ++++++++++++++++++++++----------------------- 2 files changed, 380 insertions(+), 380 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4abb55c..a740952 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,39 +1,39 @@ // we know that the behavior of URL will remove the default port for protocol // https://url.spec.whatwg.org/#url-miscellaneous -const protocols = ["ftp", 21, "http", 80, "https", 443, "ws", 80, "wss", 443]; -const slashProtocols = ["ftp:", "http:", "https:", "ws:", "wss:"]; +const protocols = ['ftp', 21, 'http', 80, 'https', 443, 'ws', 80, 'wss', 443] +const slashProtocols = ['ftp:', 'http:', 'https:', 'ws:', 'wss:'] // check if we should keep port -function checkKeepPort(base) { - if (typeof base !== "string") return -1; +function checkKeepPort (base) { + if (typeof base !== 'string') return -1 for (let i = 0; i < protocols.length; i += 2) { if ( - base.startsWith(protocols[i] + "://") && - base.includes(":" + protocols[i + 1]) + base.startsWith(protocols[i] + '://') && + base.includes(':' + protocols[i + 1]) ) { - return i; + return i } } - return -1; + return -1 } -function sanitizePath(pathname) { - let i = 0; +function sanitizePath (pathname) { + let i = 0 // we detech how many slash before a valid path for (i; i < pathname.length; i++) { - if (pathname[i] !== "/" && pathname[i] !== "\\") break; + if (pathname[i] !== '/' && pathname[i] !== '\\') break } // turns all leading / or \ into a single / - return i ? "/" + pathname.substr(i) : pathname; + return i ? '/' + pathname.substr(i) : pathname } -function isOption(base) { +function isOption (base) { return ( - typeof base === "object" && + typeof base === 'object' && base !== null && !(base instanceof URL) && !(base instanceof SecureURL) - ); + ) } /** @@ -54,78 +54,78 @@ function isOption(base) { * In "insecure" mode, the return is exactly the same when * using `URL` */ -function SecureURL(path, base, option) { +function SecureURL (path, base, option) { if (!(this instanceof SecureURL)) { - return new SecureURL(path, base, option); + return new SecureURL(path, base, option) } option = Object.assign( { - mode: "path", - keepPort: true, + mode: 'path', + keepPort: true }, option - ); + ) if (arguments.length === 2 && isOption(base)) { - option = Object.assign(option, base); - base = undefined; + option = Object.assign(option, base) + base = undefined } - const { mode, keepPort } = option; + const { mode, keepPort } = option if ( - typeof mode !== "string" || - (mode !== "path" && mode !== "relax" && mode !== "insecure") + typeof mode !== 'string' || + (mode !== 'path' && mode !== 'relax' && mode !== 'insecure') ) { throw new Error( `"mode" is expected to be "path", "relax" or "insecure", but recieved "${mode}"` - ); + ) } - if (typeof keepPort !== "boolean") { + if (typeof keepPort !== 'boolean') { throw new Error( `"keepPort" is expected to be boolean, but recieved "${keepPort}"` - ); + ) } // allow base to be either SecureURL or URL - if (base instanceof SecureURL || base instanceof URL) base = base.href; + if (base instanceof SecureURL || base instanceof URL) base = base.href // allow path to be either SecureURL or URL - if (path instanceof SecureURL || path instanceof URL) path = path.href; + if (path instanceof SecureURL || path instanceof URL) path = path.href // in mode "path" or "relax", we do not allow "//" host to be exist - if (mode === "path" || mode === "relax") { - path = sanitizePath(path); + if (mode === 'path' || mode === 'relax') { + path = sanitizePath(path) } // we default a base - const kBase = base || path; + const kBase = base || path // use URL to filter out invalid base - const kHost = new URL(kBase); + const kHost = new URL(kBase) // use URL to filter out invalid path - let kURL = new URL(path, kHost); + let kURL = new URL(path, kHost) /** * In mode `insecure`, we should behave exactly the same as `URL`. */ - if (mode === "insecure") { - kURL = new URL(path, base); + if (mode === 'insecure') { + kURL = new URL(path, base) } - const kBasePort = keepPort ? checkKeepPort(base) : -1; - const kPathPort = keepPort ? checkKeepPort(path) : -1; + const kBasePort = keepPort ? checkKeepPort(base) : -1 + const kPathPort = keepPort ? checkKeepPort(path) : -1 // in mode "path", we do not allow to modify the host - const kUseHost = mode === "path" ? kHost : kURL; - const kUsePort = mode === "path" ? kBasePort : kPathPort; - const isSlashProtocol = slashProtocols.includes(kUseHost.protocol); + const kUseHost = mode === 'path' ? kHost : kURL + const kUsePort = mode === 'path' ? kBasePort : kPathPort + const isSlashProtocol = slashProtocols.includes(kUseHost.protocol) // internal store of const internal = { // computed value - calculate all the value again on set - href: "", + href: '', // computed value - no effect on set - origin: "", - host: "", + origin: '', + host: '', protocol: kUseHost.protocol, username: kUseHost.username, password: kUseHost.password, @@ -134,66 +134,66 @@ function SecureURL(path, base, option) { pathname: kURL.pathname, hash: kURL.hash, search: kURL.search, - searchParams: kURL.searchParams, - }; + searchParams: kURL.searchParams + } - let hasCredential = false; + let hasCredential = false /* istanbul ignore next */ if (internal.protocol) { - internal.href += internal.protocol; - internal.origin += internal.protocol; + internal.href += internal.protocol + internal.origin += internal.protocol } if (isSlashProtocol) { - internal.href += "//"; - internal.origin += "//"; + internal.href += '//' + internal.origin += '//' } if (internal.username) { - internal.href += internal.username; - hasCredential = true; + internal.href += internal.username + hasCredential = true } // password must come with username if (internal.username && internal.password) { - internal.href += ":" + internal.password; - hasCredential = true; + internal.href += ':' + internal.password + hasCredential = true } if (hasCredential) { - internal.href += "@"; + internal.href += '@' } if (internal.hostname) { - internal.href += internal.hostname; - internal.origin += internal.hostname; - internal.host += internal.hostname; + internal.href += internal.hostname + internal.origin += internal.hostname + internal.host += internal.hostname } if (internal.port) { - internal.href += ":" + internal.port; - internal.origin += ":" + internal.port; - internal.host += ":" + internal.port; + internal.href += ':' + internal.port + internal.origin += ':' + internal.port + internal.host += ':' + internal.port } /* istanbul ignore next */ if (internal.pathname) { - internal.href += internal.pathname; + internal.href += internal.pathname } if (internal.search) { - internal.href += internal.search; + internal.href += internal.search } if (internal.hash) { - internal.href += internal.hash; + internal.href += internal.hash } // non-slash protocol to not have origin if (!isSlashProtocol) { - internal.origin = "null"; + internal.origin = 'null' } - Object.assign(this, internal); + Object.assign(this, internal) } // allow const SecureURL = require('url-sanitizer') -module.exports = SecureURL; +module.exports = SecureURL // allow import SecureURL from 'url-sanitizer' -module.exports.default = SecureURL; +module.exports.default = SecureURL // allow import { SecureURL } from 'url-sanitizer' -module.exports.SecureURL = SecureURL; +module.exports.SecureURL = SecureURL // allow import { URL } from 'url-sanitizer' -module.exports.URL = SecureURL; +module.exports.URL = SecureURL diff --git a/test/index.test.js b/test/index.test.js index 9b92c9a..c3fd5c3 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,491 +1,491 @@ -const t = require("tap"); -const SecureURL = require("../lib"); +const t = require('tap') +const SecureURL = require('../lib') -t.plan(9); -t.test("invalid option", function (t) { - t.plan(2); +t.plan(9) +t.test('invalid option', function (t) { + t.plan(2) - t.test("throw for mode", function (t) { - t.plan(2); + t.test('throw for mode', function (t) { + t.plan(2) try { // eslint-disable-next-line new SecureURL("invalid", { mode: "invalid" }); } catch (err) { - t.type(err, "Error"); + t.type(err, 'Error') t.equal( err.message, '"mode" is expected to be "path", "relax" or "insecure", but recieved "invalid"' - ); + ) } - }); + }) - t.test("throw for keepPort", function (t) { - t.plan(2); + t.test('throw for keepPort', function (t) { + t.plan(2) try { // eslint-disable-next-line new SecureURL("invalid", { keepPort: "invalid" }); } catch (err) { - t.type(err, "Error"); + t.type(err, 'Error') t.equal( err.message, '"keepPort" is expected to be boolean, but recieved "invalid"' - ); + ) } - }); -}); + }) +}) -t.test("constructor", function (t) { - t.plan(1); +t.test('constructor', function (t) { + t.plan(1) // eslint-disable-next-line const url = SecureURL("https://localhost.local/"); - t.equal(url instanceof SecureURL, true); -}); + t.equal(url instanceof SecureURL, true) +}) -t.test("SecureURL or URL as base and path", function (t) { +t.test('SecureURL or URL as base and path', function (t) { const cases = [ { - name: "should be allowed", - path: new SecureURL("https://foo.bar/hello/world", { mode: "relax" }), - base: new SecureURL("https://localhost.local/", { mode: "relax" }), - option: { mode: "path", keepPort: false }, + name: 'should be allowed', + path: new SecureURL('https://foo.bar/hello/world', { mode: 'relax' }), + base: new SecureURL('https://localhost.local/', { mode: 'relax' }), + option: { mode: 'path', keepPort: false }, expected: { - href: "https://localhost.local/hello/world", - }, + href: 'https://localhost.local/hello/world' + } }, { - name: "should be allowed", - path: new SecureURL("https://foo.bar/hello/world", { mode: "relax" }), - base: new SecureURL("https://localhost.local/", { mode: "relax" }), - option: { mode: "relax", keepPort: false }, + name: 'should be allowed', + path: new SecureURL('https://foo.bar/hello/world', { mode: 'relax' }), + base: new SecureURL('https://localhost.local/', { mode: 'relax' }), + option: { mode: 'relax', keepPort: false }, expected: { - href: "https://foo.bar/hello/world", - }, + href: 'https://foo.bar/hello/world' + } }, { - name: "should be allowed", - path: new URL("https://foo.bar/hello/world"), - base: new URL("https://localhost.local/"), - option: { mode: "path", keepPort: false }, + name: 'should be allowed', + path: new URL('https://foo.bar/hello/world'), + base: new URL('https://localhost.local/'), + option: { mode: 'path', keepPort: false }, expected: { - href: "https://localhost.local/hello/world", - }, + href: 'https://localhost.local/hello/world' + } }, { - name: "should be allowed", - path: new URL("https://foo.bar/hello/world"), - base: new URL("https://localhost.local/"), - option: { mode: "relax", keepPort: false }, + name: 'should be allowed', + path: new URL('https://foo.bar/hello/world'), + base: new URL('https://localhost.local/'), + option: { mode: 'relax', keepPort: false }, expected: { - href: "https://foo.bar/hello/world", - }, - }, - ]; + href: 'https://foo.bar/hello/world' + } + } + ] - t.plan(cases.length); + t.plan(cases.length) for (let i = 0; i < cases.length; i++) { - const kase = cases[i]; + const kase = cases[i] t.test(kase, function (t) { - const keys = Object.keys(kase.expected); - t.plan(keys.length); - const url = new SecureURL(kase.path, kase.base, kase.option); + const keys = Object.keys(kase.expected) + t.plan(keys.length) + const url = new SecureURL(kase.path, kase.base, kase.option) for (const key in keys) { - t.equal(url[key], kase.expected[key]); + t.equal(url[key], kase.expected[key]) } - }); + }) } -}); +}) -t.test("path mode and not keep port", function (t) { +t.test('path mode and not keep port', function (t) { const cases = [ { - name: "should not update host", - path: "https://foo.bar/hello/world", - base: "https://localhost.local/", - option: { mode: "path", keepPort: false }, + name: 'should not update host', + path: 'https://foo.bar/hello/world', + base: 'https://localhost.local/', + option: { mode: 'path', keepPort: false }, expected: { - href: "https://localhost.local/hello/world", - }, + href: 'https://localhost.local/hello/world' + } }, { - name: "should not update host", - path: "https://foo.bar/hello/world", - base: "https://foo@localhost.local/", - option: { mode: "path", keepPort: false }, + name: 'should not update host', + path: 'https://foo.bar/hello/world', + base: 'https://foo@localhost.local/', + option: { mode: 'path', keepPort: false }, expected: { - href: "https://foo@localhost.local/hello/world", - }, + href: 'https://foo@localhost.local/hello/world' + } }, { - name: "should not update host", - path: "https://foo.bar/hello/world", - base: "https://foo:bar@localhost.local/", - option: { mode: "path", keepPort: false }, + name: 'should not update host', + path: 'https://foo.bar/hello/world', + base: 'https://foo:bar@localhost.local/', + option: { mode: 'path', keepPort: false }, expected: { - href: "https://foo:bar@localhost.local/hello/world", - }, + href: 'https://foo:bar@localhost.local/hello/world' + } }, { - name: "merging search and hash", - path: "https://foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local/", - option: { mode: "path", keepPort: false }, + name: 'merging search and hash', + path: 'https://foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local/', + option: { mode: 'path', keepPort: false }, expected: { - href: "https://localhost.local/hello/world?foo=bar#hash", - }, + href: 'https://localhost.local/hello/world?foo=bar#hash' + } }, { - name: "do not keep port", - path: "https://foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "path", keepPort: false }, + name: 'do not keep port', + path: 'https://foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'path', keepPort: false }, expected: { - href: "https://localhost.local/hello/world?foo=bar#hash", - }, + href: 'https://localhost.local/hello/world?foo=bar#hash' + } }, { - name: "sanitize path", - path: "//foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "path", keepPort: false }, + name: 'sanitize path', + path: '//foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'path', keepPort: false }, expected: { - href: "https://localhost.local/foo.bar/hello/world?foo=bar#hash", - }, + href: 'https://localhost.local/foo.bar/hello/world?foo=bar#hash' + } }, { - name: "sanitize path", - path: "//\\//\\foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "path", keepPort: false }, + name: 'sanitize path', + path: '//\\//\\foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'path', keepPort: false }, expected: { - href: "https://localhost.local/foo.bar/hello/world?foo=bar#hash", - }, - }, - ]; + href: 'https://localhost.local/foo.bar/hello/world?foo=bar#hash' + } + } + ] - t.plan(cases.length); + t.plan(cases.length) for (let i = 0; i < cases.length; i++) { - const kase = cases[i]; + const kase = cases[i] t.test(kase, function (t) { - const keys = Object.keys(kase.expected); - t.plan(keys.length); - const url = new SecureURL(kase.path, kase.base, kase.option); + const keys = Object.keys(kase.expected) + t.plan(keys.length) + const url = new SecureURL(kase.path, kase.base, kase.option) for (const key in keys) { - t.equal(url[key], kase.expected[key]); + t.equal(url[key], kase.expected[key]) } - }); + }) } -}); +}) -t.test("path mode and keep port", function (t) { +t.test('path mode and keep port', function (t) { const cases = [ { - name: "should not update host", - path: "https://foo.bar/hello/world", - base: "https://localhost.local/", - option: { mode: "path", keepPort: true }, + name: 'should not update host', + path: 'https://foo.bar/hello/world', + base: 'https://localhost.local/', + option: { mode: 'path', keepPort: true }, expected: { - href: "https://localhost.local/hello/world", - }, + href: 'https://localhost.local/hello/world' + } }, { - name: "should not update host", - path: "https://foo.bar/hello/world", - base: "https://foo@localhost.local/", - option: { mode: "path", keepPort: true }, + name: 'should not update host', + path: 'https://foo.bar/hello/world', + base: 'https://foo@localhost.local/', + option: { mode: 'path', keepPort: true }, expected: { - href: "https://foo@localhost.local/hello/world", - }, + href: 'https://foo@localhost.local/hello/world' + } }, { - name: "should not update host", - path: "https://foo.bar/hello/world", - base: "https://foo:bar@localhost.local/", - option: { mode: "path", keepPort: true }, + name: 'should not update host', + path: 'https://foo.bar/hello/world', + base: 'https://foo:bar@localhost.local/', + option: { mode: 'path', keepPort: true }, expected: { - href: "https://foo:bar@localhost.local/hello/world", - }, + href: 'https://foo:bar@localhost.local/hello/world' + } }, { - name: "merging search and hash", - path: "https://foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local/", - option: { mode: "path", keepPort: true }, + name: 'merging search and hash', + path: 'https://foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local/', + option: { mode: 'path', keepPort: true }, expected: { - href: "https://localhost.local/hello/world?foo=bar#hash", - }, + href: 'https://localhost.local/hello/world?foo=bar#hash' + } }, { - name: "do keep port", - path: "https://foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "path", keepPort: true }, + name: 'do keep port', + path: 'https://foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'path', keepPort: true }, expected: { - href: "https://localhost.local:443/hello/world?foo=bar#hash", - }, + href: 'https://localhost.local:443/hello/world?foo=bar#hash' + } }, { - name: "sanitize path", - path: "//foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "path", keepPort: true }, + name: 'sanitize path', + path: '//foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'path', keepPort: true }, expected: { - href: "https://localhost.local:443/foo.bar/hello/world?foo=bar#hash", - }, + href: 'https://localhost.local:443/foo.bar/hello/world?foo=bar#hash' + } }, { - name: "sanitize path", - path: "//\\//\\foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "path", keepPort: true }, + name: 'sanitize path', + path: '//\\//\\foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'path', keepPort: true }, expected: { - href: "https://localhost.local:443/foo.bar/hello/world?foo=bar#hash", - }, - }, - ]; + href: 'https://localhost.local:443/foo.bar/hello/world?foo=bar#hash' + } + } + ] - t.plan(cases.length); + t.plan(cases.length) for (let i = 0; i < cases.length; i++) { - const kase = cases[i]; + const kase = cases[i] t.test(kase, function (t) { - const keys = Object.keys(kase.expected); - t.plan(keys.length); - const url = new SecureURL(kase.path, kase.base, kase.option); + const keys = Object.keys(kase.expected) + t.plan(keys.length) + const url = new SecureURL(kase.path, kase.base, kase.option) for (const key in keys) { - t.equal(url[key], kase.expected[key]); + t.equal(url[key], kase.expected[key]) } - }); + }) } -}); +}) -t.test("relax mode and not keep port", function (t) { +t.test('relax mode and not keep port', function (t) { const cases = [ { - name: "should update host", - path: "https://foo.bar/hello/world", - base: "https://localhost.local/", - option: { mode: "relax", keepPort: false }, + name: 'should update host', + path: 'https://foo.bar/hello/world', + base: 'https://localhost.local/', + option: { mode: 'relax', keepPort: false }, expected: { - href: "https://foo.bar/hello/world", - }, + href: 'https://foo.bar/hello/world' + } }, { - name: "should update host", - path: "https://foo.bar/hello/world", - base: "https://foo@localhost.local/", - option: { mode: "relax", keepPort: false }, + name: 'should update host', + path: 'https://foo.bar/hello/world', + base: 'https://foo@localhost.local/', + option: { mode: 'relax', keepPort: false }, expected: { - href: "https://foo.bar/hello/world", - }, + href: 'https://foo.bar/hello/world' + } }, { - name: "should update host", - path: "https://foo.bar/hello/world", - base: "https://foo:bar@localhost.local/", - option: { mode: "relax", keepPort: false }, + name: 'should update host', + path: 'https://foo.bar/hello/world', + base: 'https://foo:bar@localhost.local/', + option: { mode: 'relax', keepPort: false }, expected: { - href: "https://foo.bar/hello/world", - }, + href: 'https://foo.bar/hello/world' + } }, { - name: "merging search and hash", - path: "https://foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local/", - option: { mode: "relax", keepPort: false }, + name: 'merging search and hash', + path: 'https://foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local/', + option: { mode: 'relax', keepPort: false }, expected: { - href: "https://foo.bar/hello/world?foo=bar#hash", - }, + href: 'https://foo.bar/hello/world?foo=bar#hash' + } }, { - name: "do not keep port", - path: "https://foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "relax", keepPort: false }, + name: 'do not keep port', + path: 'https://foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'relax', keepPort: false }, expected: { - href: "https://foo.bar/hello/world?foo=bar#hash", - }, + href: 'https://foo.bar/hello/world?foo=bar#hash' + } }, { - name: "sanitize path", - path: "//foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "relax", keepPort: false }, + name: 'sanitize path', + path: '//foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'relax', keepPort: false }, expected: { - href: "https://localhost.local/foo.bar/hello/world?foo=bar#hash", - }, + href: 'https://localhost.local/foo.bar/hello/world?foo=bar#hash' + } }, { - name: "sanitize path", - path: "//\\//\\foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "relax", keepPort: false }, + name: 'sanitize path', + path: '//\\//\\foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'relax', keepPort: false }, expected: { - href: "https://localhost.local/foo.bar/hello/world?foo=bar#hash", - }, - }, - ]; + href: 'https://localhost.local/foo.bar/hello/world?foo=bar#hash' + } + } + ] - t.plan(cases.length); + t.plan(cases.length) for (let i = 0; i < cases.length; i++) { - const kase = cases[i]; + const kase = cases[i] t.test(kase, function (t) { - const keys = Object.keys(kase.expected); - t.plan(keys.length); - const url = new SecureURL(kase.path, kase.base, kase.option); + const keys = Object.keys(kase.expected) + t.plan(keys.length) + const url = new SecureURL(kase.path, kase.base, kase.option) for (const key in keys) { - t.equal(url[key], kase.expected[key]); + t.equal(url[key], kase.expected[key]) } - }); + }) } -}); +}) -t.test("relax mode and keep port", function (t) { +t.test('relax mode and keep port', function (t) { const cases = [ { - name: "should update host", - path: "https://foo.bar/hello/world", - base: "https://localhost.local/", - option: { mode: "relax", keepPort: true }, + name: 'should update host', + path: 'https://foo.bar/hello/world', + base: 'https://localhost.local/', + option: { mode: 'relax', keepPort: true }, expected: { - href: "https://foo.bar/hello/world", - }, + href: 'https://foo.bar/hello/world' + } }, { - name: "should update host", - path: "https://foo.bar/hello/world", - base: "https://foo@localhost.local/", - option: { mode: "relax", keepPort: true }, + name: 'should update host', + path: 'https://foo.bar/hello/world', + base: 'https://foo@localhost.local/', + option: { mode: 'relax', keepPort: true }, expected: { - href: "https://foo.bar/hello/world", - }, + href: 'https://foo.bar/hello/world' + } }, { - name: "should update host", - path: "https://foo.bar/hello/world", - base: "https://foo:bar@localhost.local/", - option: { mode: "relax", keepPort: true }, + name: 'should update host', + path: 'https://foo.bar/hello/world', + base: 'https://foo:bar@localhost.local/', + option: { mode: 'relax', keepPort: true }, expected: { - href: "https://foo.bar/hello/world", - }, + href: 'https://foo.bar/hello/world' + } }, { - name: "merging search and hash", - path: "https://foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local/", - option: { mode: "relax", keepPort: true }, + name: 'merging search and hash', + path: 'https://foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local/', + option: { mode: 'relax', keepPort: true }, expected: { - href: "https://foo.bar/hello/world?foo=bar#hash", - }, + href: 'https://foo.bar/hello/world?foo=bar#hash' + } }, { - name: "do keep port", - path: "https://foo.bar:443/hello/world?foo=bar#hash", - base: "https://localhost.local/", - option: { mode: "relax", keepPort: true }, + name: 'do keep port', + path: 'https://foo.bar:443/hello/world?foo=bar#hash', + base: 'https://localhost.local/', + option: { mode: 'relax', keepPort: true }, expected: { - href: "https://foo.bar:443/hello/world?foo=bar#hash", - }, + href: 'https://foo.bar:443/hello/world?foo=bar#hash' + } }, { - name: "sanitize path", - path: "//foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "relax", keepPort: true }, + name: 'sanitize path', + path: '//foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'relax', keepPort: true }, expected: { - href: "https://localhost.local:443/foo.bar/hello/world?foo=bar#hash", - }, + href: 'https://localhost.local:443/foo.bar/hello/world?foo=bar#hash' + } }, { - name: "sanitize path", - path: "//\\//\\foo.bar/hello/world?foo=bar#hash", - base: "https://localhost.local:443/", - option: { mode: "relax", keepPort: true }, + name: 'sanitize path', + path: '//\\//\\foo.bar/hello/world?foo=bar#hash', + base: 'https://localhost.local:443/', + option: { mode: 'relax', keepPort: true }, expected: { - href: "https://localhost.local:443/foo.bar/hello/world?foo=bar#hash", - }, - }, - ]; + href: 'https://localhost.local:443/foo.bar/hello/world?foo=bar#hash' + } + } + ] - t.plan(cases.length); + t.plan(cases.length) for (let i = 0; i < cases.length; i++) { - const kase = cases[i]; + const kase = cases[i] t.test(kase, function (t) { - const keys = Object.keys(kase.expected); - t.plan(keys.length); - const url = new SecureURL(kase.path, kase.base, kase.option); + const keys = Object.keys(kase.expected) + t.plan(keys.length) + const url = new SecureURL(kase.path, kase.base, kase.option) for (const key in keys) { - t.equal(url[key], kase.expected[key]); + t.equal(url[key], kase.expected[key]) } - }); + }) } -}); +}) -t.test("insecure mode", function (t) { +t.test('insecure mode', function (t) { const cases = [ { - name: "allow relative path", - path: "//foo.bar/hello/world", - base: "https://localhost.local/", - option: { mode: "insecure" }, + name: 'allow relative path', + path: '//foo.bar/hello/world', + base: 'https://localhost.local/', + option: { mode: 'insecure' }, expected: { - href: "https://foo.bar/hello/world", - }, + href: 'https://foo.bar/hello/world' + } }, { - name: "allow relative path", - path: "//foo.bar/hello/world", - base: "https://localhost.local:443/", - option: { mode: "insecure" }, + name: 'allow relative path', + path: '//foo.bar/hello/world', + base: 'https://localhost.local:443/', + option: { mode: 'insecure' }, expected: { - href: "https://foo.bar/hello/world", - }, - }, - ]; + href: 'https://foo.bar/hello/world' + } + } + ] - t.plan(cases.length); + t.plan(cases.length) for (let i = 0; i < cases.length; i++) { - const kase = cases[i]; + const kase = cases[i] t.test(kase, function (t) { - const keys = Object.keys(kase.expected); - t.plan(keys.length); - const url = new SecureURL(kase.path, kase.base, kase.option); + const keys = Object.keys(kase.expected) + t.plan(keys.length) + const url = new SecureURL(kase.path, kase.base, kase.option) for (const key in keys) { - t.equal(url[key], kase.expected[key]); + t.equal(url[key], kase.expected[key]) } - }); + }) } -}); +}) -t.test("others", function (t) { +t.test('others', function (t) { const cases = [ { - name: "non-slash protocols", - path: "mailto:foo.bar", - base: "mailto:foo.bar", - option: { mode: "relax" }, + name: 'non-slash protocols', + path: 'mailto:foo.bar', + base: 'mailto:foo.bar', + option: { mode: 'relax' }, expected: { - href: "mailto:foo.bar", - origin: "null", - }, - }, - ]; + href: 'mailto:foo.bar', + origin: 'null' + } + } + ] - t.plan(cases.length); + t.plan(cases.length) for (let i = 0; i < cases.length; i++) { - const kase = cases[i]; + const kase = cases[i] t.test(kase, function (t) { - const keys = Object.keys(kase.expected); - t.plan(keys.length); - const url = new SecureURL(kase.path, kase.base, kase.option); + const keys = Object.keys(kase.expected) + t.plan(keys.length) + const url = new SecureURL(kase.path, kase.base, kase.option) for (const key in keys) { - t.equal(url[key], kase.expected[key]); + t.equal(url[key], kase.expected[key]) } - }); + }) } -}); +})