create-response-mock.spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {describe} from 'mocha';
  2. import {expect} from '../chai.js';
  3. import {PassThrough} from 'stream';
  4. import {createResponseMock} from './create-response-mock.js';
  5. describe('createResponseMock', function () {
  6. it('returns an instance of PassThrough', function () {
  7. const res = createResponseMock();
  8. expect(res).to.be.instanceof(PassThrough);
  9. });
  10. describe('setEncoding', function () {
  11. it('sets the given encoding and returns the response', function () {
  12. const res = createResponseMock();
  13. expect(res._encoding).to.be.undefined;
  14. const ret = res.setEncoding('utf-8');
  15. expect(ret).to.be.eq(res);
  16. expect(res._encoding).to.be.eq('utf-8');
  17. });
  18. });
  19. describe('getEncoding', function () {
  20. it('returns encoding', function () {
  21. const res = createResponseMock();
  22. expect(res._encoding).to.be.undefined;
  23. const ret1 = res.getEncoding();
  24. expect(ret1).to.be.undefined;
  25. res._encoding = 'utf-8';
  26. const ret2 = res.getEncoding();
  27. expect(ret2).to.be.eq('utf-8');
  28. });
  29. });
  30. describe('headersSent', function () {
  31. it('returns false if the response is not sent', function () {
  32. const res = createResponseMock();
  33. expect(res._headersSent).to.be.false;
  34. expect(res.headersSent).to.be.false;
  35. });
  36. it('returns a value of the "_headersSent" property', function () {
  37. const res = createResponseMock();
  38. expect(res._headersSent).to.be.false;
  39. expect(res.headersSent).to.be.false;
  40. res._headersSent = true;
  41. expect(res.headersSent).to.be.true;
  42. });
  43. });
  44. describe('setHeader', function () {
  45. it('sets the given header and returns the response', function () {
  46. const res = createResponseMock();
  47. expect(res._headers['foo']).to.be.eq(undefined);
  48. const ret = res.setHeader('foo', 'bar');
  49. expect(ret).to.be.eq(res);
  50. expect(res._headers['foo']).to.be.eq('bar');
  51. });
  52. it('throws an error if headers is sent', function () {
  53. const res = createResponseMock();
  54. res._headersSent = true;
  55. const throwable = () => res.setHeader('foo');
  56. expect(throwable).to.throw(
  57. 'Error [ERR_HTTP_HEADERS_SENT]: ' +
  58. 'Cannot set headers after they are sent to the client',
  59. );
  60. });
  61. it('sets the header value as a string', function () {
  62. const res = createResponseMock();
  63. expect(res._headers['num']).to.be.eq(undefined);
  64. const ret = res.setHeader('num', 10);
  65. expect(ret).to.be.eq(res);
  66. expect(res._headers['num']).to.be.eq('10');
  67. });
  68. });
  69. describe('getHeader', function () {
  70. it('returns the header value if exists', function () {
  71. const res = createResponseMock();
  72. res._headers['foo'] = 'bar';
  73. const ret = res.getHeader('foo');
  74. expect(ret).to.be.eq('bar');
  75. });
  76. it('uses case-insensitivity lookup', function () {
  77. const res = createResponseMock();
  78. res._headers['foo'] = 'bar';
  79. const ret = res.getHeader('FOO');
  80. expect(ret).to.be.eq('bar');
  81. });
  82. });
  83. describe('getHeaders', function () {
  84. it('returns a copy of the headers object', function () {
  85. const res = createResponseMock();
  86. const ret1 = res.getHeaders();
  87. res._headers['foo'] = 'bar';
  88. res._headers['baz'] = 'qux';
  89. const ret2 = res.getHeaders();
  90. expect(ret1).to.be.eql({});
  91. expect(ret2).to.be.eql({foo: 'bar', baz: 'qux'});
  92. expect(ret1).not.to.be.eq(res._headers);
  93. expect(ret2).not.to.be.eq(res._headers);
  94. });
  95. });
  96. describe('getBody', function () {
  97. it('returns a promise of the stream content', async function () {
  98. const body = 'Lorem Ipsum is simply dummy text.';
  99. const res = createResponseMock();
  100. res.end(body);
  101. const promise = res.getBody();
  102. expect(promise).to.be.instanceof(Promise);
  103. await expect(promise).to.eventually.be.eq(body);
  104. });
  105. });
  106. describe('Stream', function () {
  107. it('sets "headerSent" to true when the stream ends', function (done) {
  108. const res = createResponseMock();
  109. expect(res.headersSent).to.be.false;
  110. res.on('end', () => {
  111. expect(res.headersSent).to.be.true;
  112. done();
  113. });
  114. res.end('test');
  115. });
  116. });
  117. });