Skip to content

Commit

Permalink
fix(url): ensure toDecode involved in cache match;
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Aug 14, 2021
1 parent 5771c50 commit f64b4bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export function parse(req, toDecode) {
let raw = req.url;
if (raw == null) return;

let prev = req._parsedUrl;
if (prev && prev.raw === raw) return prev;
let prev=req._parsedUrl, encoded=!req._decoded;
if (prev && prev.raw === raw && !toDecode === encoded) return prev;

let pathname=raw, search='', query;

Expand All @@ -37,7 +37,7 @@ export function parse(req, toDecode) {
}
}

if (!!toDecode && !req._decoded) {
if (!!toDecode && encoded) {
req._decoded = true;
if (pathname.indexOf('%') !== -1) {
try { pathname = decodeURIComponent(pathname) }
Expand Down
56 changes: 56 additions & 0 deletions packages/url/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,60 @@ test('url :: decode :: URI malformed', () => {
});
});

test('url :: decode :: cache :: hit', () => {
/** @type any */
let req = { url: '/foo/hell%C3%B6' };
let out1 = parse(req, true);

// @ts-ignore
out1.foobar = 123;

let out2 = parse(req, true);

// @ts-ignore
assert.is(out2.foobar, 123);
assert.is(out1, out2, 'referential');
assert.is(out1.pathname, '/foo/hellö');
});

test('url :: decode :: cache :: miss #1', () => {
/** @type any */
let req = { url: '/foo/hell%C3%B6?fizz=buzz' };
let out1 = parse(req);

assert.is(req._parsedUrl, out1);
assert.is(out1.pathname, '/foo/hell%C3%B6');

// @ts-ignore
out1.foobar = 123;

let out2 = parse(req, true);

// @ts-ignore
assert.is(out2.foobar, undefined);
assert.is.not(req._parsedUrl, out1);
assert.is.not(out1, out2, 'referential');
assert.is(out2.pathname, '/foo/hellö');
});

test('url :: decode :: cache :: miss #2', () => {
/** @type any */
let req = { url: '/foo/hell%C3%B6?fizz=buzz' };
let out1 = parse(req, true);

assert.is(req._parsedUrl, out1);
assert.is(out1.pathname, '/foo/hellö');

// @ts-ignore
out1.foobar = 123;

let out2 = parse(req);

// @ts-ignore
assert.is(out2.foobar, undefined);
assert.is.not(req._parsedUrl, out1);
assert.is.not(out1, out2, 'referential');
assert.is(out2.pathname, '/foo/hell%C3%B6');
});

test.run();

0 comments on commit f64b4bb

Please sign in to comment.