-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test new URLSearchParams constructor features #4523
Conversation
The main thing here is new tests for https://github.com/whatwg/url/pull/175/files but I also found issues with some existing tests that I’m attempting to fix at the same time. The current tests assumed the constructor argument was mandatory, but it’s in fact optional. The other issue was that the current test assume stringifying DOMException.prototype throws, but I don’t think that’s the case.
Notifying @Sebmaster, @domenic, @mikewest, @rubys, @sideshowbarker, @smola, @tomalec, @xiaojunwu, and @zcorpan. (Learn how reviewing works.) |
ChromeTesting revision df3a098 All results/url/urlsearchparams-constructor.html
|
FirefoxTesting revision df3a098 All results/url/urlsearchparams-constructor.html
|
assert_true(params != null, 'constructor returned non-null value.'); | ||
assert_equals(params.__proto__, URLSearchParams.prototype, 'expected URLSearchParams.prototype as prototype.'); | ||
params = new URLSearchParams({}); | ||
assert_equals(params + '', '%5Bobject+Object%5D='); | ||
}, 'URLSearchParams constructor, empty.'); | ||
}, 'URLSearchParams constructor, empty and unusual input.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate tests per input will make failures clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done this as well.
let params3 = new URLSearchParams(params2) | ||
assert_equals(params3.get("a"), params1.get("a")) | ||
assert_equals(params3.get("a"), "b") | ||
}, "Evil constructor test") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this works as expected. params1[Symbol.iterator] === params2[Symbol.iterator] === URLSearchParams.prototype[Symbol.iterator]
. The assignment is a no-op. The iterator will look up the private data on the instance it's invoked on, so iterating over params2 will always give no params, so params3 should be empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, shit. I based that on #4240 which I also wrote. How would I create a custom iterator? Would assigning an array be sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The easiest way is to use a generator:
params3[Symbol.iterator] = function *() {
yield [a, b];
yield [c, d];
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, used.
Per feedback in #4523 the original test would never be able to pass.
Per feedback in #4523 the original test would never be able to pass.
assert_true(params != null, 'constructor returned non-null value.'); | ||
assert_equals(params.__proto__, URLSearchParams.prototype, 'expected URLSearchParams.prototype as prototype.'); | ||
}, "URLSearchParams constructor, empty string as argument") | ||
|
||
test(() => { | ||
params = new URLSearchParams({}); | ||
assert_equals(params + '', '%5Bobject+Object%5D='); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should now be equivalent to no arguments per record semantics. So just empty string.
} | ||
let params2 = new URLSearchParams(params) | ||
assert_equals(params2.get("a"), "b") | ||
}, "Evil constructor test") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "overridden iterator test" is now more accurate.
Thanks! |
|
||
test(() => { | ||
params = new URLSearchParams(DOMException.prototype); | ||
assert_equals(params.toString(), "Error=") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify why this is the expected result here. My interpretation of the Web IDL spec [1] is that we should go to step 11.1 and treat the input as a sequence (not a String).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but method is undefined so it then continues on with the rest of the steps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about 11.4 ? We do have a record in there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah you're right. I think this is indeed incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4581 has the fix. I also commented on the WebKit bug.
@@ -124,6 +134,30 @@ | |||
params = new URLSearchParams('a%f0%9f%92%a9b=c'); | |||
assert_equals(params.get('a\uD83D\uDCA9b'), 'c'); | |||
}, 'Parse %f0%9f%92%a9'); // Unicode Character 'PILE OF POO' (U+1F4A9) | |||
|
|||
;[ | |||
{ "input": {"+": "%C2"}, "output": [[" ", "\uFFFD"]], "name": "object with +" }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@annevk, I'm in the process of implementing the URL spec change in Node.js. Where does it say in the URL Standard that keys and values of a record must be unescaped?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question, it doesn't and I'm not sure it should. I filed whatwg/url#220.
PR-URL: #11060 Fixes: #10635 Ref: whatwg/url#175 Ref: web-platform-tests/wpt#4523 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: nodejs#11060 Fixes: nodejs#10635 Ref: whatwg/url#175 Ref: web-platform-tests/wpt#4523 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
The main thing here is new tests for
whatwg/url#175 but I also found issues
with some existing tests that I’m attempting to fix at the same time.
The current tests assumed the constructor argument was mandatory, but
it’s in fact optional. The other issue was that the current test assume
stringifying DOMException.prototype throws, but I don’t think that’s
the case.