|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tape') |
| 4 | +const fastURI = require('..') |
| 5 | + |
| 6 | +const ABSOLUTE_BASES = [ |
| 7 | + 'http://h/p/q/r', |
| 8 | + 'http://h/', |
| 9 | + 'foo://h/a/b/', |
| 10 | + 'http://h/p/q/' |
| 11 | +] |
| 12 | + |
| 13 | +test('normalize keeps the meaning of relative-path references', (t) => { |
| 14 | + const cases = [ |
| 15 | + ['a/../b', 'b'], |
| 16 | + ['x/..', './'], |
| 17 | + ['../x', '../x'], |
| 18 | + ['./../x', '../x'], |
| 19 | + ['a/b/../../..', '..'], |
| 20 | + ['a/./b', 'a/b'], |
| 21 | + ['a/b/..', 'a/'], |
| 22 | + ['.', './'], |
| 23 | + ['..', '..'], |
| 24 | + ['../', '../'], |
| 25 | + ['../../a/./b/../c', '../../a/c'], |
| 26 | + ['a//..', 'a/'], |
| 27 | + ['a/../b?q#f', 'b?q#f'] |
| 28 | + ] |
| 29 | + for (const [input, expected] of cases) { |
| 30 | + t.equal(fastURI.normalize(input), expected, `${input} -> ${expected}`) |
| 31 | + } |
| 32 | + t.end() |
| 33 | +}) |
| 34 | + |
| 35 | +test('normalize never turns a relative-path reference into a root or network path', (t) => { |
| 36 | + t.equal(fastURI.normalize('.//x'), './/x', 'leading empty segment is kept behind "."') |
| 37 | + t.equal(fastURI.normalize('a/..//x'), './/x', 'collapsed prefix does not expose "//"') |
| 38 | + t.equal(fastURI.parse(fastURI.normalize('a/..//x')).host, undefined, 'no authority is introduced') |
| 39 | + t.equal(fastURI.normalize('./a:b'), 'a%3Ab', 'exposed colon is still escaped') |
| 40 | + t.equal(fastURI.parse(fastURI.normalize('./a:b')).scheme, undefined, 'no scheme is introduced') |
| 41 | + t.end() |
| 42 | +}) |
| 43 | + |
| 44 | +test('normalize keeps the existing behavior for root-relative and absolute paths', (t) => { |
| 45 | + t.equal(fastURI.normalize('/a/../b'), '/b') |
| 46 | + t.equal(fastURI.normalize('/../x'), '/x') |
| 47 | + t.equal(fastURI.normalize('http://h/a/../../b'), 'http://h/b') |
| 48 | + t.end() |
| 49 | +}) |
| 50 | + |
| 51 | +test('equal does not alias relative references with different targets', (t) => { |
| 52 | + t.equal(fastURI.equal('../x', 'x'), false) |
| 53 | + t.equal(fastURI.equal('a/../b', '/b'), false) |
| 54 | + t.equal(fastURI.equal('x/..', '/'), false) |
| 55 | + t.equal(fastURI.equal('a/../b', 'b'), true) |
| 56 | + t.end() |
| 57 | +}) |
| 58 | + |
| 59 | +test('resolving a normalized relative reference matches resolving the original', (t) => { |
| 60 | + for (const base of ABSOLUTE_BASES) { |
| 61 | + for (const ref of ['a/../b', 'x/..', '../x', './../x', 'a/b/../../..', '.', '..', './/x']) { |
| 62 | + t.equal( |
| 63 | + fastURI.resolve(base, fastURI.normalize(ref)), |
| 64 | + fastURI.resolve(base, ref), |
| 65 | + `${base} + ${ref}` |
| 66 | + ) |
| 67 | + } |
| 68 | + } |
| 69 | + t.equal(fastURI.resolve('http://h/p/q/r', fastURI.normalize('a/../b')), 'http://h/p/q/b') |
| 70 | + t.equal(fastURI.resolve('http://h/p/q/r', fastURI.normalize('../x')), 'http://h/p/x') |
| 71 | + t.end() |
| 72 | +}) |
| 73 | + |
| 74 | +test('property: normalization of relative references is resolution-preserving and idempotent', (t) => { |
| 75 | + const segments = ['a', 'b', '.', '..', ''] |
| 76 | + // Deterministic linear congruential generator so failures are reproducible. |
| 77 | + let seed = 0x5ec02 |
| 78 | + const next = (n) => { |
| 79 | + seed = (seed * 1103515245 + 12345) % 2147483648 |
| 80 | + return Math.floor((seed / 2147483648) * n) |
| 81 | + } |
| 82 | + |
| 83 | + let failures = 0 |
| 84 | + let checked = 0 |
| 85 | + for (let i = 0; i < 4000; i++) { |
| 86 | + const length = 1 + next(6) |
| 87 | + const parts = [] |
| 88 | + for (let j = 0; j < length; j++) parts.push(segments[next(segments.length)]) |
| 89 | + const ref = parts.join('/') |
| 90 | + // Only relative-path references: empty refs and refs starting with "/" |
| 91 | + // are not affected by this change ("//" would also introduce an authority). |
| 92 | + if (ref === '' || ref[0] === '/') continue |
| 93 | + |
| 94 | + checked++ |
| 95 | + const normalized = fastURI.normalize(ref) |
| 96 | + if (fastURI.normalize(normalized) !== normalized) { |
| 97 | + failures++ |
| 98 | + t.fail(`not idempotent: ${ref} -> ${normalized} -> ${fastURI.normalize(normalized)}`) |
| 99 | + } |
| 100 | + // 3.x's removeDotSegments (used by resolve) collapses a trailing "//" to |
| 101 | + // "/" (fixed in v4 by #185), so resolve() itself is inconsistent for |
| 102 | + // these paths regardless of normalization. |
| 103 | + if (normalized.endsWith('//')) continue |
| 104 | + for (const base of ABSOLUTE_BASES) { |
| 105 | + const expected = fastURI.resolve(base, ref) |
| 106 | + const actual = fastURI.resolve(base, normalized) |
| 107 | + if (actual !== expected) { |
| 108 | + failures++ |
| 109 | + t.fail(`${base} + ${ref}: normalized ${normalized} resolves to ${actual}, expected ${expected}`) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + t.ok(checked > 1000, `checked ${checked} relative references`) |
| 115 | + t.equal(failures, 0, 'no relative reference changed meaning') |
| 116 | + t.end() |
| 117 | +}) |
0 commit comments