request-context.spec.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {expect} from './chai.js';
  2. import {format} from '@e22m4u/js-format';
  3. import {createRequestMock} from './utils/index.js';
  4. import {RequestContext} from './request-context.js';
  5. import {ServiceContainer} from '@e22m4u/js-service';
  6. import {createResponseMock} from './utils/index.js';
  7. describe('RequestContext', function () {
  8. describe('constructor', function () {
  9. it('requires the parameter "container" to be the ServiceContainer', function () {
  10. const req = createRequestMock();
  11. const res = createResponseMock();
  12. const throwable = v => () => new RequestContext(v, req, res);
  13. const error = v =>
  14. format(
  15. 'The parameter "container" of RequestContext.constructor ' +
  16. 'should be an instance of ServiceContainer, but %s was given.',
  17. v,
  18. );
  19. expect(throwable('str')).to.throw(error('"str"'));
  20. expect(throwable('')).to.throw(error('""'));
  21. expect(throwable(10)).to.throw(error('10'));
  22. expect(throwable(0)).to.throw(error('0'));
  23. expect(throwable(true)).to.throw(error('true'));
  24. expect(throwable(false)).to.throw(error('false'));
  25. expect(throwable(null)).to.throw(error('null'));
  26. expect(throwable({})).to.throw(error('Object'));
  27. expect(throwable([])).to.throw(error('Array'));
  28. expect(throwable(undefined)).to.throw(error('undefined'));
  29. throwable(new ServiceContainer())();
  30. });
  31. it('requires the parameter "request" to be the ServiceContainer', function () {
  32. const res = createResponseMock();
  33. const cnt = new ServiceContainer();
  34. const throwable = v => () => new RequestContext(cnt, v, res);
  35. const error = v =>
  36. format(
  37. 'The parameter "request" of RequestContext.constructor ' +
  38. 'should be an instance of IncomingMessage, but %s was given.',
  39. v,
  40. );
  41. expect(throwable('str')).to.throw(error('"str"'));
  42. expect(throwable('')).to.throw(error('""'));
  43. expect(throwable(10)).to.throw(error('10'));
  44. expect(throwable(0)).to.throw(error('0'));
  45. expect(throwable(true)).to.throw(error('true'));
  46. expect(throwable(false)).to.throw(error('false'));
  47. expect(throwable(null)).to.throw(error('null'));
  48. expect(throwable({})).to.throw(error('Object'));
  49. expect(throwable([])).to.throw(error('Array'));
  50. expect(throwable(undefined)).to.throw(error('undefined'));
  51. throwable(createRequestMock())();
  52. });
  53. it('requires the parameter "response" to be the ServiceContainer', function () {
  54. const req = createRequestMock();
  55. const cnt = new ServiceContainer();
  56. const throwable = v => () => new RequestContext(cnt, req, v);
  57. const error = v =>
  58. format(
  59. 'The parameter "response" of RequestContext.constructor ' +
  60. 'should be an instance of ServerResponse, but %s was given.',
  61. v,
  62. );
  63. expect(throwable('str')).to.throw(error('"str"'));
  64. expect(throwable('')).to.throw(error('""'));
  65. expect(throwable(10)).to.throw(error('10'));
  66. expect(throwable(0)).to.throw(error('0'));
  67. expect(throwable(true)).to.throw(error('true'));
  68. expect(throwable(false)).to.throw(error('false'));
  69. expect(throwable(null)).to.throw(error('null'));
  70. expect(throwable({})).to.throw(error('Object'));
  71. expect(throwable([])).to.throw(error('Array'));
  72. expect(throwable(undefined)).to.throw(error('undefined'));
  73. throwable(createResponseMock())();
  74. });
  75. it('sets properties from given arguments', function () {
  76. const req = createRequestMock();
  77. const res = createResponseMock();
  78. const cnt = new ServiceContainer();
  79. const ctx = new RequestContext(cnt, req, res);
  80. expect(ctx.container).to.be.eq(cnt);
  81. expect(ctx.req).to.be.eq(req);
  82. expect(ctx.res).to.be.eq(res);
  83. });
  84. });
  85. describe('method', function () {
  86. it('returns the method name in upper case', function () {
  87. const req = createRequestMock({method: 'post'});
  88. const res = createResponseMock();
  89. const cnt = new ServiceContainer();
  90. const ctx = new RequestContext(cnt, req, res);
  91. expect(ctx.method).to.be.eq('POST');
  92. });
  93. });
  94. describe('path', function () {
  95. it('returns the request pathname with the query string', function () {
  96. const req = createRequestMock({path: '/pathname?foo=bar'});
  97. const res = createResponseMock();
  98. const cnt = new ServiceContainer();
  99. const ctx = new RequestContext(cnt, req, res);
  100. expect(req.url).to.be.eq('/pathname?foo=bar');
  101. expect(ctx.path).to.be.eq('/pathname?foo=bar');
  102. });
  103. });
  104. describe('pathname', function () {
  105. it('returns the request pathname without the query string', function () {
  106. const req = createRequestMock({path: '/pathname?foo=bar'});
  107. const res = createResponseMock();
  108. const cnt = new ServiceContainer();
  109. const ctx = new RequestContext(cnt, req, res);
  110. expect(req.url).to.be.eq('/pathname?foo=bar');
  111. expect(ctx.pathname).to.be.eq('/pathname');
  112. });
  113. it('sets the cache to the "_pathname" property and uses is for next accesses', function () {
  114. const req = createRequestMock({path: '/pathname'});
  115. const res = createResponseMock();
  116. const cnt = new ServiceContainer();
  117. const ctx = new RequestContext(cnt, req, res);
  118. expect(ctx._pathname).to.be.undefined;
  119. expect(ctx.pathname).to.be.eq('/pathname');
  120. expect(ctx._pathname).to.be.eq('/pathname');
  121. ctx._pathname = '/overridden';
  122. expect(ctx.pathname).to.be.eq('/overridden');
  123. });
  124. });
  125. });