react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / qs / test / stringify.js
81146 views/* eslint no-extend-native:0 */1// Load modules23var Code = require('code');4var Lab = require('lab');5var Qs = require('../');678// Declare internals910var internals = {};111213// Test shortcuts1415var lab = exports.lab = Lab.script();16var expect = Code.expect;17var describe = lab.experiment;18var it = lab.test;192021describe('stringify()', function () {2223it('stringifies a querystring object', function (done) {2425expect(Qs.stringify({ a: 'b' })).to.equal('a=b');26expect(Qs.stringify({ a: 1 })).to.equal('a=1');27expect(Qs.stringify({ a: 1, b: 2 })).to.equal('a=1&b=2');28expect(Qs.stringify({ a: 'A_Z' })).to.equal('a=A_Z');29expect(Qs.stringify({ a: '€' })).to.equal('a=%E2%82%AC');30expect(Qs.stringify({ a: '' })).to.equal('a=%EE%80%80');31expect(Qs.stringify({ a: 'א' })).to.equal('a=%D7%90');32expect(Qs.stringify({ a: '𐐷' })).to.equal('a=%F0%90%90%B7');33done();34});3536it('stringifies a nested object', function (done) {3738expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c');39expect(Qs.stringify({ a: { b: { c: { d: 'e' } } } })).to.equal('a%5Bb%5D%5Bc%5D%5Bd%5D=e');40done();41});4243it('stringifies an array value', function (done) {4445expect(Qs.stringify({ a: ['b', 'c', 'd'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d');46done();47});4849it('omits array indices when asked', function (done) {5051expect(Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false })).to.equal('a=b&a=c&a=d');52done();53});5455it('stringifies a nested array value', function (done) {5657expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');58done();59});6061it('stringifies an object inside an array', function (done) {6263expect(Qs.stringify({ a: [{ b: 'c' }] })).to.equal('a%5B0%5D%5Bb%5D=c');64expect(Qs.stringify({ a: [{ b: { c: [1] } }] })).to.equal('a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1');65done();66});6768it('does not omit object keys when indices = false', function (done) {6970expect(Qs.stringify({ a: [{ b: 'c' }] }, { indices: false })).to.equal('a%5Bb%5D=c');71done();72});7374it('uses indices notation for arrays when indices=true', function (done) {7576expect(Qs.stringify({ a: ['b', 'c'] }, { indices: true })).to.equal('a%5B0%5D=b&a%5B1%5D=c');77done();78});7980it('uses indices notation for arrays when no arrayFormat is specified', function (done) {8182expect(Qs.stringify({ a: ['b', 'c'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c');83done();84});8586it('uses indices notation for arrays when no arrayFormat=indices', function (done) {8788expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })).to.equal('a%5B0%5D=b&a%5B1%5D=c');89done();90});9192it('uses repeat notation for arrays when no arrayFormat=repeat', function (done) {9394expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })).to.equal('a=b&a=c');95done();96});9798it('uses brackets notation for arrays when no arrayFormat=brackets', function (done) {99100expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })).to.equal('a%5B%5D=b&a%5B%5D=c');101done();102});103104it('stringifies a complicated object', function (done) {105106expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e');107done();108});109110it('stringifies an empty value', function (done) {111112expect(Qs.stringify({ a: '' })).to.equal('a=');113expect(Qs.stringify({ a: null }, {strictNullHandling: true})).to.equal('a');114115expect(Qs.stringify({ a: '', b: '' })).to.equal('a=&b=');116expect(Qs.stringify({ a: null, b: '' }, {strictNullHandling: true})).to.equal('a&b=');117118expect(Qs.stringify({ a: { b: '' } })).to.equal('a%5Bb%5D=');119expect(Qs.stringify({ a: { b: null } }, {strictNullHandling: true})).to.equal('a%5Bb%5D');120expect(Qs.stringify({ a: { b: null } }, {strictNullHandling: false})).to.equal('a%5Bb%5D=');121122done();123});124125it('stringifies an empty object', function (done) {126127var obj = Object.create(null);128obj.a = 'b';129expect(Qs.stringify(obj)).to.equal('a=b');130done();131});132133it('returns an empty string for invalid input', function (done) {134135expect(Qs.stringify(undefined)).to.equal('');136expect(Qs.stringify(false)).to.equal('');137expect(Qs.stringify(null)).to.equal('');138expect(Qs.stringify('')).to.equal('');139done();140});141142it('stringifies an object with an empty object as a child', function (done) {143144var obj = {145a: Object.create(null)146};147148obj.a.b = 'c';149expect(Qs.stringify(obj)).to.equal('a%5Bb%5D=c');150done();151});152153it('drops keys with a value of undefined', function (done) {154155expect(Qs.stringify({ a: undefined })).to.equal('');156157expect(Qs.stringify({ a: { b: undefined, c: null } }, {strictNullHandling: true})).to.equal('a%5Bc%5D');158expect(Qs.stringify({ a: { b: undefined, c: null } }, {strictNullHandling: false})).to.equal('a%5Bc%5D=');159expect(Qs.stringify({ a: { b: undefined, c: '' } })).to.equal('a%5Bc%5D=');160done();161});162163it('url encodes values', function (done) {164165expect(Qs.stringify({ a: 'b c' })).to.equal('a=b%20c');166done();167});168169it('stringifies a date', function (done) {170171var now = new Date();172var str = 'a=' + encodeURIComponent(now.toISOString());173expect(Qs.stringify({ a: now })).to.equal(str);174done();175});176177it('stringifies the weird object from qs', function (done) {178179expect(Qs.stringify({ 'my weird field': '~q1!2"\'w$5&7/z8)?' })).to.equal('my%20weird%20field=~q1%212%22%27w%245%267%2Fz8%29%3F');180done();181});182183it('skips properties that are part of the object prototype', function (done) {184185Object.prototype.crash = 'test';186expect(Qs.stringify({ a: 'b'})).to.equal('a=b');187expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c');188delete Object.prototype.crash;189done();190});191192it('stringifies boolean values', function (done) {193194expect(Qs.stringify({ a: true })).to.equal('a=true');195expect(Qs.stringify({ a: { b: true } })).to.equal('a%5Bb%5D=true');196expect(Qs.stringify({ b: false })).to.equal('b=false');197expect(Qs.stringify({ b: { c: false } })).to.equal('b%5Bc%5D=false');198done();199});200201it('stringifies buffer values', function (done) {202203expect(Qs.stringify({ a: new Buffer('test') })).to.equal('a=test');204expect(Qs.stringify({ a: { b: new Buffer('test') } })).to.equal('a%5Bb%5D=test');205done();206});207208it('stringifies an object using an alternative delimiter', function (done) {209210expect(Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' })).to.equal('a=b;c=d');211done();212});213214it('doesn\'t blow up when Buffer global is missing', function (done) {215216var tempBuffer = global.Buffer;217delete global.Buffer;218expect(Qs.stringify({ a: 'b', c: 'd' })).to.equal('a=b&c=d');219global.Buffer = tempBuffer;220done();221});222223it('selects properties when filter=array', function (done) {224225expect(Qs.stringify({ a: 'b' }, { filter: ['a'] })).to.equal('a=b');226expect(Qs.stringify({ a: 1}, { filter: [] })).to.equal('');227expect(Qs.stringify({ a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' }, { filter: ['a', 'b', 0, 2]})).to.equal('a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3');228done();229230});231232it('supports custom representations when filter=function', function (done) {233234var calls = 0;235var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };236var filterFunc = function (prefix, value) {237238calls++;239if (calls === 1) {240expect(prefix).to.be.empty();241expect(value).to.equal(obj);242}243else if (prefix === 'c') {244return;245}246else if (value instanceof Date) {247expect(prefix).to.equal('e[f]');248return value.getTime();249}250return value;251};252253expect(Qs.stringify(obj, { filter: filterFunc })).to.equal('a=b&e%5Bf%5D=1257894000000');254expect(calls).to.equal(5);255done();256257});258});259260261