ae3b364068d9a32a9c7a81fd9530d90496943d45Dav GlassYUI.add('querystring-tests', function(Y) {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var J = Y.JSON.stringify;
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass // [ wonkyQS, canonicalQS, obj ]
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var qsTestCases = [ //{
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo=bar", "foo=bar", {"foo" : "bar"}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo=bar&foo=quux", "foo%5B%5D=bar&foo%5B%5D=quux", {"foo" : ["bar", "quux"]}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo=1&bar=2", "foo=1&bar=2", {"foo" : 1, "bar" : 2}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["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)?" }],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo%3Dbaz=bar", "foo%3Dbaz=bar", {"foo=baz" : "bar"}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo=baz=bar", "foo=baz%3Dbar", {"foo" : "baz=bar"}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["str=foo&arr[]=1&arr[]=2&arr[]=3&obj[a]=bar&obj[b][]=4&obj[b][]=5&obj[b][]=6&obj[b][]=&obj[c][]=4&obj[c][]=5&obj[c][][somestr]=baz&obj[objobj][objobjstr]=blerg&somenull=&undef=", "str=foo&arr%5B%5D=1&arr%5B%5D=2&arr%5B%5D=3&obj%5Ba%5D=bar&obj%5Bb%5D%5B%5D=4&obj%5Bb%5D%5B%5D=5&obj%5Bb%5D%5B%5D=6&obj%5Bb%5D%5B%5D=&obj%5Bc%5D%5B%5D=4&obj%5Bc%5D%5B%5D=5&obj%5Bc%5D%5B%5D%5Bsomestr%5D=baz&obj%5Bobjobj%5D%5Bobjobjstr%5D=blerg&somenull=&undef=", {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass "str":"foo",
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass "arr":[1,2,3],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass "obj":{
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass "a":"bar",
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass "b":[4,5,6,""],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass "c":[4,5,{"somestr":"baz"}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass "objobj":{"objobjstr":"blerg"}
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass "somenull":"",
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass "undef":""
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass }],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo[bar][bla]=baz&foo[bar][bla]=blo", "foo%5Bbar%5D%5Bbla%5D%5B%5D=baz&foo%5Bbar%5D%5Bbla%5D%5B%5D=blo", {"foo":{"bar":{"bla":["baz","blo"]}}}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo[bar][][bla]=baz&foo[bar][][bla]=blo", "foo%5Bbar%5D%5B%5D%5Bbla%5D=baz&foo%5Bbar%5D%5B%5D%5Bbla%5D=blo", {"foo":{"bar":[{"bla":"baz"},{"bla":"blo"}]}}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo[bar][bla][]=baz&foo[bar][bla][]=blo", "foo%5Bbar%5D%5Bbla%5D%5B%5D=baz&foo%5Bbar%5D%5Bbla%5D%5B%5D=blo", {"foo":{"bar":{"bla":["baz","blo"]}}}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [" foo = bar ", "foo=bar", {"foo":"bar"}]
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ]; //}
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass // [ wonkyQS, canonicalQS, obj ]
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var qsColonTestCases = [ //{
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo:bar", "foo:bar", {"foo":"bar"}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo:bar;foo:quux", "foo%5B%5D:bar;foo%5B%5D:quux", {"foo" : ["bar", "quux"]}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo:1&bar:2;baz:quux", "foo:1%26bar%3A2;baz:quux", {"foo":"1&bar:2", "baz":"quux"}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo%3Abaz:bar", "foo%3Abaz:bar", {"foo:baz":"bar"}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ["foo:baz:bar", "foo:baz%3Abar", {"foo":"baz:bar"}]
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ]; //}
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass // [ wonkyObj, qs, canonicalObj ]
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var extendedFunction = function () {};
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass extendedFunction.prototype = {a:"b"};
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var qsWeirdObjects = [ //{
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [ {regexp:/./g}, "regexp=", {"regexp":""} ],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [ {regexp: new RegExp(".", "g")}, "regexp=", {"regexp":""} ],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [ {fn:function () {}}, "fn=", {"fn":""}],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [ {fn:new Function("")}, "fn=", {"fn":""} ],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [ {math:Math}, "math=", {"math":""} ],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [ {e:extendedFunction}, "e=", {"e":""} ],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [ {d:new Date()}, "d=", {"d":""} ],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [ {d:Date}, "d=", {"d":""} ],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [ {f:new Boolean(false), t:new Boolean(true)}, "f=0&t=1", {"f":0, "t":1} ],
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass [ {f:false, t:true}, "f=0&t=1", {"f":0, "t":1} ]
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass ]; //}
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass // Tests Below...
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var suite = new Y.Test.Suite("Query String Tests");
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass suite.add(new Y.Test.Case({
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass name : "Parse Query Strings",
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testParseBasic : function() {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Array.forEach(qsTestCases, function (testCase) {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(J(testCase[2]), J(Y.QueryString.parse(testCase[0])));
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass });
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testParseColon : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Array.forEach(qsColonTestCases, function (testCase) {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(J(testCase[2]), J(Y.QueryString.parse(testCase[0], ";", ":")));
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass });
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testParseWeird : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Array.forEach(qsWeirdObjects, function (testCase) {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(J(testCase[2]), J(Y.QueryString.parse(testCase[1])));
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass });
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testNested : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var f = Y.QueryString.parse("a=b&q=x%3Dy%26y%3Dz");
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass f.q = Y.QueryString.parse(f.q);
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(J(f), J({ a : "b", q : { x : "y", y : "z" } }));
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testNestedColon : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var f = Y.QueryString.parse("a:b;q:x%3Ay%3By%3Az", ";", ":");
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass f.q = Y.QueryString.parse(f.q, ";", ":");
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(J(f), J({ a : "b", q : { x : "y", y : "z" } }));
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass }
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass }));
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass suite.add(new Y.Test.Case({
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass name : "Query Stringify Objects",
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass _should:{error:{
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testStringifyCyclical : "QueryString.stringify. Cyclical reference"
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass }},
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testStringifyBasic : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Array.forEach(qsTestCases, function (testCase) {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(testCase[1], Y.QueryString.stringify(testCase[2], { arrayKey: true }));
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass });
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testStringifyColon : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Array.forEach(qsColonTestCases, function (testCase) {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(testCase[1], Y.QueryString.stringify(testCase[2], { arrayKey: true, sep: ";", eq: ":" }));
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass });
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testStringifyWeird : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Array.forEach(qsWeirdObjects, function (testCase) {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(testCase[1], Y.QueryString.stringify(testCase[0]), { arrayKey: true });
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass });
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testStringifyCyclical : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var f = {};
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass f.f = f;
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.QueryString.stringify(f);
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testNested : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var f = Y.QueryString.stringify({
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass a : "b",
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass q : Y.QueryString.stringify({
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass x : "y",
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass y : "z"
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass }, { arrayKey: true })
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass });
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(f, "a=b&q=x%3Dy%26y%3Dz");
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testNestedColon : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var f = Y.QueryString.stringify({
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass a : "b",
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass q : Y.QueryString.stringify({
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass x : "y",
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass y : "z"
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass }, { arrayKey: true, sep: ";", eq: ":" })
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass }, { arrayKey: true, sep: ";", eq: ":" });
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(f, "a:b;q:x%3Ay%3By%3Az");
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass },
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass testArrayKey : function () {
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass var f = Y.QueryString.stringify({ foo: ["bar","baz"] });
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Assert.areSame(f, "foo=bar&foo=baz");
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass }
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass }));
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass Y.Test.Runner.add(suite);
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass
ae3b364068d9a32a9c7a81fd9530d90496943d45Dav Glass});