parse-content-type.spec.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {expect} from 'chai';
  2. import {parseContentType} from './parse-content-type.js';
  3. import {format} from '@e22m4u/js-format';
  4. describe('parseContentType', function () {
  5. it('requires the first parameter to be a string', function () {
  6. const throwable = v => () => parseContentType(v);
  7. const error = s =>
  8. format(
  9. 'The first parameter of `parseContentType` ' +
  10. 'must be a String, but %s was given.',
  11. s,
  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([])).to.throw(error('Array'));
  18. expect(throwable({})).to.throw(error('Object'));
  19. expect(throwable(undefined)).to.throw(error('undefined'));
  20. expect(throwable(null)).to.throw(error('null'));
  21. expect(throwable(() => undefined)).to.throw(error('Function'));
  22. throwable('text/html')();
  23. });
  24. it('returns an object with specific properties', function () {
  25. const res = parseContentType('');
  26. expect(res).to.be.eql({
  27. mediaType: undefined,
  28. charset: undefined,
  29. boundary: undefined,
  30. });
  31. });
  32. it('parses media type', function () {
  33. const res1 = parseContentType('text/html');
  34. expect(res1).to.be.eql({
  35. mediaType: 'text/html',
  36. charset: undefined,
  37. boundary: undefined,
  38. });
  39. const res2 = parseContentType('text/html;');
  40. expect(res2).to.be.eql({
  41. mediaType: 'text/html',
  42. charset: undefined,
  43. boundary: undefined,
  44. });
  45. });
  46. it('parses media type with charset', function () {
  47. const res1 = parseContentType('text/html; charset=utf-8');
  48. expect(res1).to.be.eql({
  49. mediaType: 'text/html',
  50. charset: 'utf-8',
  51. boundary: undefined,
  52. });
  53. const res2 = parseContentType('text/html; charset=utf-8;');
  54. expect(res2).to.be.eql({
  55. mediaType: 'text/html',
  56. charset: 'utf-8',
  57. boundary: undefined,
  58. });
  59. });
  60. it('parses media type with boundary', function () {
  61. const res1 = parseContentType(
  62. 'multipart/form-data; boundary=---WebKitFormBoundary7MA4YWxkTrZu0gW',
  63. );
  64. expect(res1).to.be.eql({
  65. mediaType: 'multipart/form-data',
  66. charset: undefined,
  67. boundary: '---WebKitFormBoundary7MA4YWxkTrZu0gW',
  68. });
  69. const res2 = parseContentType(
  70. 'multipart/form-data; boundary=---WebKitFormBoundary7MA4YWxkTrZu0gW;',
  71. );
  72. expect(res2).to.be.eql({
  73. mediaType: 'multipart/form-data',
  74. charset: undefined,
  75. boundary: '---WebKitFormBoundary7MA4YWxkTrZu0gW',
  76. });
  77. });
  78. });