parse-cookie-string.spec.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {parseCookieString} from './parse-cookie-string.js';
  4. describe('parseCookieString', function () {
  5. it('requires the first parameter to be an IncomingMessage instance', function () {
  6. const throwable = v => () => parseCookieString(v);
  7. const error = v =>
  8. format(
  9. 'The first parameter of "parseCookieString" must be a String, ' +
  10. 'but %s was given.',
  11. v,
  12. );
  13. expect(throwable(10)).to.throw(error('10'));
  14. expect(throwable(0)).to.throw(error('0'));
  15. expect(throwable(true)).to.throw(error('true'));
  16. expect(throwable(false)).to.throw(error('false'));
  17. expect(throwable(null)).to.throw(error('null'));
  18. expect(throwable({})).to.throw(error('Object'));
  19. expect(throwable([])).to.throw(error('Array'));
  20. expect(throwable(undefined)).to.throw(error('undefined'));
  21. throwable('str')();
  22. throwable('')();
  23. });
  24. it('returns cookies as a plain object', function () {
  25. const value = 'pkg=math; equation=E%3Dmc%5E2';
  26. const result = parseCookieString(value);
  27. expect(result).to.have.property('pkg', 'math');
  28. expect(result).to.have.property('equation', 'E=mc^2');
  29. });
  30. it('returns an empty object for an empty string', function () {
  31. const result = parseCookieString('');
  32. expect(result).to.be.eql({});
  33. });
  34. it('parses an empty cookie as an empty string', function () {
  35. const result = parseCookieString('foo=bar; baz');
  36. expect(result).to.be.eql({foo: 'bar', baz: ''});
  37. });
  38. });