Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict';
require('../common');
const assert = require('assert');
const inspect = require('util').inspect;
const url = require('url');
// When source is false
assert.strictEqual(url.resolveObject('', 'foo'), 'foo');
/*
[from, path, expected]
*/
const relativeTests = [
['/foo/bar/baz', 'quux', '/foo/bar/quux'],
['/foo/bar/baz', 'quux/asdf', '/foo/bar/quux/asdf'],
['/foo/bar/baz', 'quux/baz', '/foo/bar/quux/baz'],
['/foo/bar/baz', '../quux/baz', '/foo/quux/baz'],
['/foo/bar/baz', '/bar', '/bar'],
['/foo/bar/baz/', 'quux', '/foo/bar/baz/quux'],
['/foo/bar/baz/', 'quux/baz', '/foo/bar/baz/quux/baz'],
['/foo/bar/baz', '../../../../../../../../quux/baz', '/quux/baz'],
['/foo/bar/baz', '../../../../../../../quux/baz', '/quux/baz'],
['/foo', '.', '/'],
['/foo', '..', '/'],
route = function(request, response, chain) {
try {
var url = parse(request.url, true);
url = resolveObject(url, url); // strange syntax (url passed twice) but this resolves ../../ paths in the URL
url = parse(request.url, true);
if (url.pathname.indexOf('/data/') == 0) {
service(restModule, request, response, url);
}
else if (url.pathname.indexOf('/app/') == 0) {
service(appModule, request, response, url);
}
else if (url.pathname.indexOf('/favicon.png') == 0 || url.pathname.indexOf('/favicon.ico') == 0) {
service(faviconModule, request, response, url);
}
else if (url.pathname.indexOf('/config/') == 0) {
service(configModule, request, response, url);
}
else if (request.method == 'GET') {
defaults.fileNotFound(response);
relativeTests.forEach(function(relativeTest) {
var actual = url.resolveObject(url.parse(relativeTest[0]), relativeTest[1]),
expected = url.parse(relativeTest[2]);
t.deepEqual(objectify(actual), objectify(expected));
expected = relativeTest[2];
actual = url.format(actual);
t.equal(actual, expected,
'format(' + actual + ') == ' + expected + '\nactual:' + actual);
});
relativeTests2.forEach(function(relativeTest) {
var actual = url.resolveObject(url.parse(relativeTest[1]), relativeTest[0]),
expected = url.parse(relativeTest[2]);
assert.deepEqual(actual, expected);
var expected = relativeTest[2],
actual = url.format(actual);
assert.equal(actual, expected,
'format(' + relativeTest[1] + ') == ' + expected +
'\nactual:' + actual);
});
relativeTests.forEach(function(relativeTest) {
let actual = url.resolveObject(url.parse(relativeTest[0]), relativeTest[1]);
let expected = url.parse(relativeTest[2]);
assert.deepStrictEqual(actual, expected);
expected = relativeTest[2];
actual = url.format(actual);
assert.strictEqual(actual, expected,
`format(${actual}) == ${expected}\n` +
`actual: ${actual}`);
});
relativeTests2.forEach(function(relativeTest) {
var actual = url.resolveObject(url.parse(relativeTest[1]), relativeTest[0]),
expected = url.parse(relativeTest[2]);
//because of evaluation order
//resolveObject(parse(x), y) == parse(resolve(x, y)) will differ by
//false-ish values. remove all except host and hostname
for (var i in actual) {
if (actual[i] === undefined ||
(!emptyIsImportant.hasOwnProperty(i) && !actual[i])) {
delete actual[i];
}
}
assert.deepEqual(actual, expected);
var expected = relativeTest[2],
actual = url.format(actual);
relativeTests2.forEach(function(relativeTest) {
let actual = url.resolveObject(url.parse(relativeTest[1]), relativeTest[0]);
let expected = url.parse(relativeTest[2]);
assert.deepStrictEqual(
actual,
expected,
`expected ${inspect(expected)} but got ${inspect(actual)}`
);
expected = url.format(relativeTest[2]);
actual = url.format(actual);
assert.strictEqual(actual, expected,
`format(${relativeTest[1]}) == ${expected}\n` +
`actual: ${actual}`);
});