summaryrefslogtreecommitdiff
path: root/test/parallel/test-whatwg-url-properties.js
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2016-12-22 17:16:08 +0800
committerJames M Snell <jasnell@gmail.com>2016-12-29 15:46:59 -0800
commit508d97628b7922123a4159fb54e546580b84b42f (patch)
treebf9e0caa946eca345dad53d3782d5ed7eeea969f /test/parallel/test-whatwg-url-properties.js
parentb7fadf0fa48dee89284e0510ad4de6343636495c (diff)
downloadnode-new-508d97628b7922123a4159fb54e546580b84b42f.tar.gz
url: make WHATWG URL properties spec compliant
* Set exposed attributes of the interface enumerable and configurable, as required by the spec. See: https://heycam.github.io/webidl/#es-attributes * Make sure `URL#searchParams` returns `[[SameObject]]` * Add the missing `URL#href` setter * Reorder the properties to match https://url.spec.whatwg.org/#api * Add tests for the ECMAScript property attributes PR-URL: https://github.com/nodejs/node/pull/10408 Fixes: https://github.com/nodejs/node/issues/10376 Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-whatwg-url-properties.js')
-rw-r--r--test/parallel/test-whatwg-url-properties.js127
1 files changed, 127 insertions, 0 deletions
diff --git a/test/parallel/test-whatwg-url-properties.js b/test/parallel/test-whatwg-url-properties.js
new file mode 100644
index 0000000000..60cf581ad8
--- /dev/null
+++ b/test/parallel/test-whatwg-url-properties.js
@@ -0,0 +1,127 @@
+'use strict';
+
+require('../common');
+
+const URL = require('url').URL;
+const assert = require('assert');
+
+const url = new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test');
+const oldParams = url.searchParams; // for test of [SameObject]
+
+// To retrieve enumerable but not necessarily own properties,
+// we need to use the for-in loop.
+const props = [];
+for (const prop in url) {
+ props.push(prop);
+}
+
+// See: https://url.spec.whatwg.org/#api
+// https://heycam.github.io/webidl/#es-attributes
+// https://heycam.github.io/webidl/#es-stringifier
+const expected = ['toString',
+ 'href', 'origin', 'protocol',
+ 'username', 'password', 'host', 'hostname', 'port',
+ 'pathname', 'search', 'searchParams', 'hash'];
+
+assert.deepStrictEqual(props, expected);
+
+// href is writable (not readonly) and is stringifier
+assert.strictEqual(url.toString(), url.href);
+url.href = 'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test';
+assert.strictEqual(url.href,
+ 'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
+assert.strictEqual(url.toString(), url.href);
+// Return true because it's configurable, but because the properties
+// are defined on the prototype per the spec, the deletion has no effect
+assert.strictEqual((delete url.href), true);
+assert.strictEqual(url.href,
+ 'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
+assert.strictEqual(url.searchParams, oldParams); // [SameObject]
+
+// searchParams is readonly. Under strict mode setting a
+// non-writable property should throw.
+// Note: this error message is subject to change in V8 updates
+assert.throws(() => url.origin = 'http://foo.bar.com:22',
+ new RegExp('TypeError: Cannot set property origin of' +
+ ' \\[object Object\\] which has only a getter'));
+assert.strictEqual(url.origin, 'http://foo.bar.com:21');
+assert.strictEqual(url.toString(),
+ 'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
+assert.strictEqual((delete url.origin), true);
+assert.strictEqual(url.origin, 'http://foo.bar.com:21');
+
+// The following properties should be writable (not readonly)
+url.protocol = 'https:';
+assert.strictEqual(url.protocol, 'https:');
+assert.strictEqual(url.toString(),
+ 'https://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
+assert.strictEqual((delete url.protocol), true);
+assert.strictEqual(url.protocol, 'https:');
+
+url.username = 'user2';
+assert.strictEqual(url.username, 'user2');
+assert.strictEqual(url.toString(),
+ 'https://user2:pass@foo.bar.com:21/aaa/zzz?l=25#test');
+assert.strictEqual((delete url.username), true);
+assert.strictEqual(url.username, 'user2');
+
+url.password = 'pass2';
+assert.strictEqual(url.password, 'pass2');
+assert.strictEqual(url.toString(),
+ 'https://user2:pass2@foo.bar.com:21/aaa/zzz?l=25#test');
+assert.strictEqual((delete url.password), true);
+assert.strictEqual(url.password, 'pass2');
+
+url.host = 'foo.bar.net:22';
+assert.strictEqual(url.host, 'foo.bar.net:22');
+assert.strictEqual(url.toString(),
+ 'https://user2:pass2@foo.bar.net:22/aaa/zzz?l=25#test');
+assert.strictEqual((delete url.host), true);
+assert.strictEqual(url.host, 'foo.bar.net:22');
+
+url.hostname = 'foo.bar.org';
+assert.strictEqual(url.hostname, 'foo.bar.org');
+assert.strictEqual(url.toString(),
+ 'https://user2:pass2@foo.bar.org:22/aaa/zzz?l=25#test');
+assert.strictEqual((delete url.hostname), true);
+assert.strictEqual(url.hostname, 'foo.bar.org');
+
+url.port = '23';
+assert.strictEqual(url.port, '23');
+assert.strictEqual(url.toString(),
+ 'https://user2:pass2@foo.bar.org:23/aaa/zzz?l=25#test');
+assert.strictEqual((delete url.port), true);
+assert.strictEqual(url.port, '23');
+
+url.pathname = '/aaa/bbb';
+assert.strictEqual(url.pathname, '/aaa/bbb');
+assert.strictEqual(url.toString(),
+ 'https://user2:pass2@foo.bar.org:23/aaa/bbb?l=25#test');
+assert.strictEqual((delete url.pathname), true);
+assert.strictEqual(url.pathname, '/aaa/bbb');
+
+url.search = '?k=99';
+assert.strictEqual(url.search, '?k=99');
+assert.strictEqual(url.toString(),
+ 'https://user2:pass2@foo.bar.org:23/aaa/bbb?k=99#test');
+assert.strictEqual((delete url.search), true);
+assert.strictEqual(url.search, '?k=99');
+
+url.hash = '#abcd';
+assert.strictEqual(url.hash, '#abcd');
+assert.strictEqual(url.toString(),
+ 'https://user2:pass2@foo.bar.org:23/aaa/bbb?k=99#abcd');
+assert.strictEqual((delete url.hash), true);
+assert.strictEqual(url.hash, '#abcd');
+
+// searchParams is readonly. Under strict mode setting a
+// non-writable property should throw.
+// Note: this error message is subject to change in V8 updates
+assert.throws(() => url.searchParams = '?k=88',
+ new RegExp('TypeError: Cannot set property searchParams of' +
+ ' \\[object Object\\] which has only a getter'));
+assert.strictEqual(url.searchParams, oldParams);
+assert.strictEqual(url.toString(),
+ 'https://user2:pass2@foo.bar.org:23/aaa/bbb?k=99#abcd');
+assert.strictEqual((delete url.searchParams), true);
+assert.strictEqual(url.searchParams, oldParams);