UNPKG

@abalmus/request

Version:

Core utils, fetch wrapper, querystring utils

46 lines (39 loc) 1.6 kB
import { decode, encode } from '../utils/index'; import { expect, assert } from 'chai'; import 'mocha'; const testCases = [ ['', {}], ['foo=bar&foo=baz', {'foo': ['bar', 'baz']}], ['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'}] ]; describe('Querystring', () => { it('should decode canonical query string properly', () => { testCases.forEach(testCase => { expect(decode(testCase[0])).to.deep.equal(testCase[1]); }); }); it('should encode objects', () => { testCases.forEach(testCase => { expect(encode(testCase[1])).to.deep.equal(testCase[0]); }); }); it('should throw an error', () => { expect(decode(null)).to.be.an('object'); expect(decode(undefined)).to.be.an('object'); }); it('should be able to set decode options', () => { expect(decode('foo:bar;foo:quux', ';', ':', {maxKeys: 10})) .to.deep.equal({'foo': ['bar', 'quux']}); }); it('should be able to encode URI component', () => { expect(encode('foo:bar;foo:quux', ';', ':', 'test')) .to.equal('test:foo%3Abar%3Bfoo%3Aquux'); expect(encode('foo:bar;foo:quux', ';', ':', true)) .to.equal('true:foo%3Abar%3Bfoo%3Aquux'); expect(encode('foo:bar;foo:quux', ';', ':', null)) .to.equal(''); }); });