get-request-pathname.spec.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {getRequestPathname} from './get-request-pathname.js';
  4. describe('getRequestPathname', function () {
  5. it('requires the argument to be an Object with "url" property', function () {
  6. const throwable = v => () => getRequestPathname(v);
  7. const error = v =>
  8. format(
  9. 'The first argument of "getRequestPathname" should be ' +
  10. 'an instance of IncomingMessage, but %s was given.',
  11. v,
  12. );
  13. expect(throwable('str')).to.throw(error('"str"'));
  14. expect(throwable('')).to.throw(error('""'));
  15. expect(throwable(10)).to.throw(error('10'));
  16. expect(throwable(0)).to.throw(error('0'));
  17. expect(throwable(true)).to.throw(error('true'));
  18. expect(throwable(false)).to.throw(error('false'));
  19. expect(throwable(null)).to.throw(error('null'));
  20. expect(throwable({})).to.throw(error('Object'));
  21. expect(throwable([])).to.throw(error('Array'));
  22. expect(throwable(undefined)).to.throw(error('undefined'));
  23. throwable({url: ''})();
  24. });
  25. it('returns the request path without the query string', function () {
  26. const res = getRequestPathname({url: '/pathname?foo=bar'});
  27. expect(res).to.be.eq('/pathname');
  28. });
  29. });