You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
8.3 KiB

'use strict';
require('../common');
14 years ago
var assert = require('assert');
// test using assert
14 years ago
var qs = require('querystring');
function createWithNoPrototype(properties) {
const noProto = Object.create(null);
properties.forEach((property) => {
noProto[property.key] = property.value;
});
return noProto;
}
14 years ago
// folding block, commented to pass gjslint
// {{{
// [ wonkyQS, canonicalQS, obj ]
var qsTestCases = [
['__proto__=1',
'__proto__=1',
createWithNoPrototype([{key: '__proto__', value: '1'}])],
['__defineGetter__=asdf',
'__defineGetter__=asdf',
JSON.parse('{"__defineGetter__":"asdf"}')],
14 years ago
['foo=918854443121279438895193',
'foo=918854443121279438895193',
{'foo': '918854443121279438895193'}],
['foo=bar', 'foo=bar', {'foo': 'bar'}],
['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}],
['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}],
['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F',
'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F',
{'my weird field': 'q1!2"\'w$5&7/z8)?' }],
['foo%3Dbaz=bar', 'foo%3Dbaz=bar', {'foo=baz': 'bar'}],
['foo=baz=bar', 'foo=baz%3Dbar', {'foo': 'baz=bar'}],
['str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
'str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
{ 'str': 'foo',
'arr': ['1', '2', '3'],
'somenull': '',
'undef': ''}],
[' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}],
['foo=%zx', 'foo=%25zx', {'foo': '%zx'}],
['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }],
// See: https://github.com/joyent/node/issues/1707
['hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz',
'hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz',
{ hasOwnProperty: 'x',
toString: 'foo',
valueOf: 'bar',
__defineGetter__: 'baz' }],
// See: https://github.com/joyent/node/issues/3058
['foo&bar=baz', 'foo=&bar=baz', { foo: '', bar: 'baz' }],
[null, '', {}],
[undefined, '', {}]
];
// [ wonkyQS, canonicalQS, obj ]
var qsColonTestCases = [
14 years ago
['foo:bar', 'foo:bar', {'foo': 'bar'}],
['foo:bar;foo:quux', 'foo:bar;foo:quux', {'foo': ['bar', 'quux']}],
['foo:1&bar:2;baz:quux',
'foo:1%26bar%3A2;baz:quux',
{'foo': '1&bar:2', 'baz': 'quux'}],
['foo%3Abaz:bar', 'foo%3Abaz:bar', {'foo:baz': 'bar'}],
['foo:baz:bar', 'foo:baz%3Abar', {'foo': 'baz:bar'}]
];
14 years ago
// [wonkyObj, qs, canonicalObj]
var extendedFunction = function() {};
extendedFunction.prototype = {a: 'b'};
var qsWeirdObjects = [
14 years ago
[{regexp: /./g}, 'regexp=', {'regexp': ''}],
[{regexp: new RegExp('.', 'g')}, 'regexp=', {'regexp': ''}],
[{fn: function() {}}, 'fn=', {'fn': ''}],
[{fn: new Function('')}, 'fn=', {'fn': ''}],
[{math: Math}, 'math=', {'math': ''}],
[{e: extendedFunction}, 'e=', {'e': ''}],
[{d: new Date()}, 'd=', {'d': ''}],
[{d: Date}, 'd=', {'d': ''}],
[{f: new Boolean(false), t: new Boolean(true)}, 'f=&t=', {'f': '', 't': ''}],
14 years ago
[{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}],
[{n: null}, 'n=', {'n': ''}],
[{nan: NaN}, 'nan=', {'nan': ''}],
[{inf: Infinity}, 'inf=', {'inf': ''}],
[{a: [], b: []}, '', {}]
];
14 years ago
// }}}
vm, core, module: re-do vm to fix known issues As documented in #3042 and in [1], the existing vm implementation has many problems. All of these are solved by @brianmcd's [contextify][2] package. This commit uses contextify as a conceptual base and its code core to overhaul the vm module and fix its many edge cases and caveats. Functionally, this fixes #3042. In particular: - A context is now indistinguishable from the object it is based on (the "sandbox"). A context is simply a sandbox that has been marked by the vm module, via `vm.createContext`, with special internal information that allows scripts to be run inside of it. - Consequently, items added to the context from anywhere are immediately visible to all code that can access that context, both inside and outside the virtual machine. This commit also smooths over the API very slightly: - Parameter defaults are now uniformly triggered via `undefined`, per ES6 semantics and previous discussion at [3]. - Several undocumented and problematic features have been removed, e.g. the conflation of `vm.Script` with `vm` itself, and the fact that `Script` instances also had all static `vm` methods. The API is now exactly as documented (although arguably the existence of the `vm.Script` export is not yet documented, just the `Script` class itself). In terms of implementation, this replaces node_script.cc with node_contextify.cc, which is derived originally from [4] (see [5]) but has since undergone extensive modifications and iterations to expose the most useful C++ API and use the coding conventions and utilities of Node core. The bindings exposed by `process.binding('contextify')` (node_contextify.cc) replace those formerly exposed by `process.binding('evals')` (node_script.cc). They are: - ContextifyScript(code, [filename]), with methods: - runInThisContext() - runInContext(sandbox, [timeout]) - makeContext(sandbox) From this, the vm.js file builds the entire documented vm module API. node.js and module.js were modified to use this new native binding, or the vm module itself where possible. This introduces an extra line or two into the stack traces of module compilation (and thus into most stack traces), explaining the changed tests. The tests were also updated slightly, with all vm-related simple tests consolidated as test/simple/test-vm-* (some of them were formerly test/simple/test-script-*). At the same time they switched from `common.debug` to `console.error` and were updated to use `assert.throws` instead of rolling their own error-testing methods. New tests were also added, of course, demonstrating the new capabilities and fixes. [1]: http://nodejs.org/docs/v0.10.16/api/vm.html#vm_caveats [2]: https://github.com/brianmcd/contextify [3]: https://github.com/joyent/node/issues/5323#issuecomment-20250726 [4]: https://github.com/kkoopa/contextify/blob/bf123f3ef960f0943d1e30bda02e3163a004e964/src/contextify.cc [5]: https://gist.github.com/domenic/6068120
12 years ago
var vm = require('vm');
var foreignObject = vm.runInNewContext('({"foo": ["bar", "baz"]})');
var qsNoMungeTestCases = [
14 years ago
['', {}],
['foo=bar&foo=baz', {'foo': ['bar', 'baz']}],
['foo=bar&foo=baz', foreignObject],
['blah=burp', {'blah': 'burp'}],
['gragh=1&gragh=3&goo=2', {'gragh': ['1', '3'], 'goo': '2'}],
['frappucino=muffin&goat%5B%5D=scone&pond=moose',
{'frappucino': 'muffin', 'goat[]': 'scone', 'pond': 'moose'}],
['trololol=yes&lololo=no', {'trololol': 'yes', 'lololo': 'no'}]
];
14 years ago
assert.strictEqual('918854443121279438895193',
qs.parse('id=918854443121279438895193').id);
function check(actual, expected) {
assert(!(actual instanceof Object));
assert.deepStrictEqual(Object.keys(actual).sort(),
Object.keys(expected).sort());
Object.keys(expected).forEach(function(key) {
assert.deepStrictEqual(actual[key], expected[key]);
});
}
// test that the canonical qs is parsed properly.
14 years ago
qsTestCases.forEach(function(testCase) {
check(qs.parse(testCase[0]), testCase[2]);
});
// test that the colon test cases can do the same
14 years ago
qsColonTestCases.forEach(function(testCase) {
check(qs.parse(testCase[0], ';', ':'), testCase[2]);
});
// test the weird objects, that they get parsed properly
14 years ago
qsWeirdObjects.forEach(function(testCase) {
check(qs.parse(testCase[1]), testCase[2]);
});
14 years ago
qsNoMungeTestCases.forEach(function(testCase) {
assert.deepStrictEqual(testCase[0], qs.stringify(testCase[1], '&', '='));
});
// test the nested qs-in-qs case
{
const f = qs.parse('a=b&q=x%3Dy%26y%3Dz');
check(f, createWithNoPrototype([
{ key: 'a', value: 'b'},
{key: 'q', value: 'x=y&y=z'}
]));
14 years ago
f.q = qs.parse(f.q);
const expectedInternal = createWithNoPrototype([
{ key: 'x', value: 'y'},
{key: 'y', value: 'z' }
]);
check(f.q, expectedInternal);
}
// nested in colon
{
const f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':');
check(f, createWithNoPrototype([
{key: 'a', value: 'b'},
{key: 'q', value: 'x:y;y:z'}
]));
14 years ago
f.q = qs.parse(f.q, ';', ':');
const expectedInternal = createWithNoPrototype([
{ key: 'x', value: 'y'},
{key: 'y', value: 'z' }
]);
check(f.q, expectedInternal);
}
// now test stringifying
// basic
14 years ago
qsTestCases.forEach(function(testCase) {
assert.equal(testCase[1], qs.stringify(testCase[2]));
});
14 years ago
qsColonTestCases.forEach(function(testCase) {
assert.equal(testCase[1], qs.stringify(testCase[2], ';', ':'));
});
14 years ago
qsWeirdObjects.forEach(function(testCase) {
assert.equal(testCase[1], qs.stringify(testCase[0]));
});
// invalid surrogate pair throws URIError
assert.throws(function() {
qs.stringify({ foo: '\udc00' });
}, URIError);
// coerce numbers to string
assert.strictEqual('foo=0', qs.stringify({ foo: 0 }));
assert.strictEqual('foo=0', qs.stringify({ foo: -0 }));
assert.strictEqual('foo=3', qs.stringify({ foo: 3 }));
assert.strictEqual('foo=-72.42', qs.stringify({ foo: -72.42 }));
assert.strictEqual('foo=', qs.stringify({ foo: NaN }));
assert.strictEqual('foo=', qs.stringify({ foo: Infinity }));
// nested
{
const f = qs.stringify({
a: 'b',
q: qs.stringify({
x: 'y',
y: 'z'
})
});
assert.equal(f, 'a=b&q=x%3Dy%26y%3Dz');
}
14 years ago
assert.doesNotThrow(function() {
qs.parse(undefined);
});
// nested in colon
{
const f = qs.stringify({
a: 'b',
q: qs.stringify({
x: 'y',
y: 'z'
}, ';', ':')
}, ';', ':');
assert.equal(f, 'a:b;q:x%3Ay%3By%3Az');
}
check(qs.parse(), {});
// Test limiting
assert.equal(
Object.keys(qs.parse('a=1&b=1&c=1', null, null, { maxKeys: 1 })).length,
1);
// Test removing limit
function testUnlimitedKeys() {
const query = {};
for (var i = 0; i < 2000; i++) query[i] = i;
const url = qs.stringify(query);
assert.equal(
Object.keys(qs.parse(url, null, null, { maxKeys: 0 })).length,
2000);
}
testUnlimitedKeys();
14 years ago
var b = qs.unescapeBuffer('%d3%f2Ug%1f6v%24%5e%98%cb' +
'%0d%ac%a2%2f%9d%eb%d8%a2%e6');
// <Buffer d3 f2 55 67 1f 36 76 24 5e 98 cb 0d ac a2 2f 9d eb d8 a2 e6>
assert.equal(0xd3, b[0]);
assert.equal(0xf2, b[1]);
assert.equal(0x55, b[2]);
assert.equal(0x67, b[3]);
assert.equal(0x1f, b[4]);
assert.equal(0x36, b[5]);
assert.equal(0x76, b[6]);
assert.equal(0x24, b[7]);
assert.equal(0x5e, b[8]);
assert.equal(0x98, b[9]);
assert.equal(0xcb, b[10]);
assert.equal(0x0d, b[11]);
assert.equal(0xac, b[12]);
assert.equal(0xa2, b[13]);
assert.equal(0x2f, b[14]);
assert.equal(0x9d, b[15]);
assert.equal(0xeb, b[16]);
assert.equal(0xd8, b[17]);
assert.equal(0xa2, b[18]);
assert.equal(0xe6, b[19]);
// Test custom decode
function demoDecode(str) {
return str + str;
}
check(qs.parse('a=a&b=b&c=c', null, null, { decodeURIComponent: demoDecode }),
{ aa: 'aa', bb: 'bb', cc: 'cc' });
// Test custom encode
function demoEncode(str) {
return str[0];
}
var obj = { aa: 'aa', bb: 'bb', cc: 'cc' };
assert.equal(
qs.stringify(obj, null, null, { encodeURIComponent: demoEncode }),
'a=a&b=b&c=c');
// test overriding .unescape
var prevUnescape = qs.unescape;
qs.unescape = function(str) {
return str.replace(/o/g, '_');
};
check(qs.parse('foo=bor'), createWithNoPrototype([{key: 'f__', value: 'b_r'}]));
qs.unescape = prevUnescape;
// test separator and "equals" parsing order
check(qs.parse('foo&bar', '&', '&'), { foo: '', bar: '' });