request-parser.spec.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {expect} from '../chai.js';
  2. import {HTTP_METHOD} from '../route.js';
  3. import {format} from '@e22m4u/js-format';
  4. import {RequestParser} from './request-parser.js';
  5. import {createRequestMock} from '../utils/create-request-mock.js';
  6. describe('RequestParser', function () {
  7. describe('parse', function () {
  8. it('requires the first argument to be an instance of IncomingMessage', function () {
  9. const s = new RequestParser();
  10. const throwable = v => () => s.parse(v);
  11. const error = v =>
  12. format(
  13. 'The first argument of RequestParser.parse should be ' +
  14. 'an instance of IncomingMessage, but %s given.',
  15. v,
  16. );
  17. expect(throwable('str')).to.throw(error('"str"'));
  18. expect(throwable('')).to.throw(error('""'));
  19. expect(throwable(10)).to.throw(error('10'));
  20. expect(throwable(0)).to.throw(error('0'));
  21. expect(throwable(true)).to.throw(error('true'));
  22. expect(throwable(false)).to.throw(error('false'));
  23. expect(throwable(null)).to.throw(error('null'));
  24. expect(throwable({})).to.throw(error('Object'));
  25. expect(throwable([])).to.throw(error('Array'));
  26. expect(throwable(undefined)).to.throw(error('undefined'));
  27. expect(throwable(() => undefined)).to.throw(error('Function'));
  28. throwable(createRequestMock())();
  29. });
  30. it('returns the result object if no request body to parse', function () {
  31. const s = new RequestParser();
  32. const req = createRequestMock();
  33. const res = s.parse(req);
  34. expect(res).to.be.eql({
  35. query: {},
  36. cookie: {},
  37. body: undefined,
  38. headers: {host: 'localhost'},
  39. });
  40. });
  41. it('returns a Promise of the result object in case of the body parsing', async function () {
  42. const s = new RequestParser();
  43. const body = 'Lorem Ipsum is simply dummy text.';
  44. const req = createRequestMock({
  45. method: HTTP_METHOD.POST,
  46. headers: {'content-type': 'text/plain'},
  47. body,
  48. });
  49. const promise = s.parse(req);
  50. expect(promise).to.be.instanceof(Promise);
  51. const res = await promise;
  52. expect(res).to.be.eql({
  53. query: {},
  54. cookie: {},
  55. body,
  56. headers: {
  57. host: 'localhost',
  58. 'content-type': 'text/plain',
  59. 'content-length': String(Buffer.from(body).byteLength),
  60. },
  61. });
  62. });
  63. it('returns the parsed query in the result object', function () {
  64. const s = new RequestParser();
  65. const req = createRequestMock({path: '/path?p1=foo&p2=bar'});
  66. const res = s.parse(req);
  67. expect(res).to.be.eql({
  68. query: {p1: 'foo', p2: 'bar'},
  69. cookie: {},
  70. body: undefined,
  71. headers: {host: 'localhost'},
  72. });
  73. });
  74. it('returns the parsed cookie in the result object', function () {
  75. const s = new RequestParser();
  76. const req = createRequestMock({headers: {cookie: 'p1=foo; p2=bar;'}});
  77. const res = s.parse(req);
  78. expect(res).to.be.eql({
  79. query: {},
  80. cookie: {p1: 'foo', p2: 'bar'},
  81. body: undefined,
  82. headers: {
  83. host: 'localhost',
  84. cookie: 'p1=foo; p2=bar;',
  85. },
  86. });
  87. });
  88. it('returns the parsed body of the media type "text/plain" in the result object', async function () {
  89. const s = new RequestParser();
  90. const body = 'Lorem Ipsum is simply dummy text.';
  91. const req = createRequestMock({
  92. method: HTTP_METHOD.POST,
  93. headers: {'content-type': 'text/plain'},
  94. body,
  95. });
  96. const res = await s.parse(req);
  97. expect(res).to.be.eql({
  98. query: {},
  99. cookie: {},
  100. body,
  101. headers: {
  102. host: 'localhost',
  103. 'content-type': 'text/plain',
  104. 'content-length': String(Buffer.from(body).byteLength),
  105. },
  106. });
  107. });
  108. it('returns the parsed body of the media type "application/json" in the result object', async function () {
  109. const s = new RequestParser();
  110. const body = {foo: 'bar', baz: 'qux'};
  111. const json = JSON.stringify(body);
  112. const req = createRequestMock({
  113. method: HTTP_METHOD.POST,
  114. headers: {'content-type': 'application/json'},
  115. body,
  116. });
  117. const res = await s.parse(req);
  118. expect(res).to.be.eql({
  119. query: {},
  120. cookie: {},
  121. body,
  122. headers: {
  123. host: 'localhost',
  124. 'content-type': 'application/json',
  125. 'content-length': String(Buffer.from(json).byteLength),
  126. },
  127. });
  128. });
  129. });
  130. });