create-response-mock.spec.js 4.1 KB

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