0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

url: fix URL query update if searchParams changes

If searchParams becomes empty, query must be set to null.
Add missing update of context flags.

Fixes: https://github.com/nodejs/node/issues/10480
PR-URL: https://github.com/nodejs/node/pull/10486
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Michaël Zasso 2016-12-28 10:11:26 +01:00
parent db18dd8356
commit b465cd07fe
2 changed files with 21 additions and 1 deletions

View File

@ -593,7 +593,15 @@ function update(url, params) {
if (!url)
return;
url[context].query = params.toString();
const ctx = url[context];
const serializedParams = params.toString();
if (serializedParams) {
ctx.query = serializedParams;
ctx.flags |= binding.URL_FLAGS_HAS_QUERY;
} else {
ctx.query = null;
ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY;
}
}
function getSearchParamPairs(target) {

View File

@ -42,3 +42,15 @@ params.append('first', 10);
params.delete('first');
assert.strictEqual(false, params.has('first'),
'Search params object has no "first" name');
// https://github.com/nodejs/node/issues/10480
// Emptying searchParams should correctly update url's query
{
const url = new URL('http://domain?var=1&var=2&var=3');
for (const param of url.searchParams.keys()) {
url.searchParams.delete(param);
}
assert.strictEqual(url.searchParams.toString(), '');
assert.strictEqual(url.search, '');
assert.strictEqual(url.href, 'http://domain/');
}