1'use strict'; 2require('../common'); 3const assert = require('assert'); 4const url = require('url'); 5 6function createWithNoPrototype(properties = []) { 7 const noProto = Object.create(null); 8 properties.forEach((property) => { 9 noProto[property.key] = property.value; 10 }); 11 return noProto; 12} 13 14function check(actual, expected) { 15 assert.notStrictEqual(Object.getPrototypeOf(actual), Object.prototype); 16 assert.deepStrictEqual(Object.keys(actual).sort(), 17 Object.keys(expected).sort()); 18 Object.keys(expected).forEach(function(key) { 19 assert.deepStrictEqual(actual[key], expected[key]); 20 }); 21} 22 23const parseTestsWithQueryString = { 24 '/foo/bar?baz=quux#frag': { 25 href: '/foo/bar?baz=quux#frag', 26 hash: '#frag', 27 search: '?baz=quux', 28 query: createWithNoPrototype([{ key: 'baz', value: 'quux' }]), 29 pathname: '/foo/bar', 30 path: '/foo/bar?baz=quux' 31 }, 32 'http://example.com': { 33 href: 'http://example.com/', 34 protocol: 'http:', 35 slashes: true, 36 host: 'example.com', 37 hostname: 'example.com', 38 query: createWithNoPrototype(), 39 search: null, 40 pathname: '/', 41 path: '/' 42 }, 43 '/example': { 44 protocol: null, 45 slashes: null, 46 auth: undefined, 47 host: null, 48 port: null, 49 hostname: null, 50 hash: null, 51 search: null, 52 query: createWithNoPrototype(), 53 pathname: '/example', 54 path: '/example', 55 href: '/example' 56 }, 57 '/example?query=value': { 58 protocol: null, 59 slashes: null, 60 auth: undefined, 61 host: null, 62 port: null, 63 hostname: null, 64 hash: null, 65 search: '?query=value', 66 query: createWithNoPrototype([{ key: 'query', value: 'value' }]), 67 pathname: '/example', 68 path: '/example?query=value', 69 href: '/example?query=value' 70 } 71}; 72for (const u in parseTestsWithQueryString) { 73 const actual = url.parse(u, true); 74 const expected = Object.assign(new url.Url(), parseTestsWithQueryString[u]); 75 for (const i in actual) { 76 if (actual[i] === null && expected[i] === undefined) { 77 expected[i] = null; 78 } 79 } 80 81 const properties = Object.keys(actual).sort(); 82 assert.deepStrictEqual(properties, Object.keys(expected).sort()); 83 properties.forEach((property) => { 84 if (property === 'query') { 85 check(actual[property], expected[property]); 86 } else { 87 assert.deepStrictEqual(actual[property], expected[property]); 88 } 89 }); 90} 91