| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- import {expect} from './chai.js';
- import {format} from '@e22m4u/js-format';
- import {createRequestMock} from './utils/index.js';
- import {RequestContext} from './request-context.js';
- import {ServiceContainer} from '@e22m4u/js-service';
- import {createResponseMock} from './utils/index.js';
- describe('RequestContext', function () {
- describe('constructor', function () {
- it('requires the parameter "container" to be the ServiceContainer', function () {
- const req = createRequestMock();
- const res = createResponseMock();
- const throwable = v => () => new RequestContext(v, req, res);
- const error = v =>
- format(
- 'The parameter "container" of RequestContext.constructor ' +
- 'should be an instance of ServiceContainer, but %s was given.',
- v,
- );
- expect(throwable('str')).to.throw(error('"str"'));
- expect(throwable('')).to.throw(error('""'));
- expect(throwable(10)).to.throw(error('10'));
- expect(throwable(0)).to.throw(error('0'));
- expect(throwable(true)).to.throw(error('true'));
- expect(throwable(false)).to.throw(error('false'));
- expect(throwable(null)).to.throw(error('null'));
- expect(throwable({})).to.throw(error('Object'));
- expect(throwable([])).to.throw(error('Array'));
- expect(throwable(undefined)).to.throw(error('undefined'));
- throwable(new ServiceContainer())();
- });
- it('requires the parameter "request" to be the ServiceContainer', function () {
- const res = createResponseMock();
- const cnt = new ServiceContainer();
- const throwable = v => () => new RequestContext(cnt, v, res);
- const error = v =>
- format(
- 'The parameter "request" of RequestContext.constructor ' +
- 'should be an instance of IncomingMessage, but %s was given.',
- v,
- );
- expect(throwable('str')).to.throw(error('"str"'));
- expect(throwable('')).to.throw(error('""'));
- expect(throwable(10)).to.throw(error('10'));
- expect(throwable(0)).to.throw(error('0'));
- expect(throwable(true)).to.throw(error('true'));
- expect(throwable(false)).to.throw(error('false'));
- expect(throwable(null)).to.throw(error('null'));
- expect(throwable({})).to.throw(error('Object'));
- expect(throwable([])).to.throw(error('Array'));
- expect(throwable(undefined)).to.throw(error('undefined'));
- throwable(createRequestMock())();
- });
- it('requires the parameter "response" to be the ServiceContainer', function () {
- const req = createRequestMock();
- const cnt = new ServiceContainer();
- const throwable = v => () => new RequestContext(cnt, req, v);
- const error = v =>
- format(
- 'The parameter "response" of RequestContext.constructor ' +
- 'should be an instance of ServerResponse, but %s was given.',
- v,
- );
- expect(throwable('str')).to.throw(error('"str"'));
- expect(throwable('')).to.throw(error('""'));
- expect(throwable(10)).to.throw(error('10'));
- expect(throwable(0)).to.throw(error('0'));
- expect(throwable(true)).to.throw(error('true'));
- expect(throwable(false)).to.throw(error('false'));
- expect(throwable(null)).to.throw(error('null'));
- expect(throwable({})).to.throw(error('Object'));
- expect(throwable([])).to.throw(error('Array'));
- expect(throwable(undefined)).to.throw(error('undefined'));
- throwable(createResponseMock())();
- });
- it('sets properties from given arguments', function () {
- const req = createRequestMock();
- const res = createResponseMock();
- const cnt = new ServiceContainer();
- const ctx = new RequestContext(cnt, req, res);
- expect(ctx.container).to.be.eq(cnt);
- expect(ctx.req).to.be.eq(req);
- expect(ctx.res).to.be.eq(res);
- });
- });
- describe('method', function () {
- it('returns the method name in upper case', function () {
- const req = createRequestMock({method: 'post'});
- const res = createResponseMock();
- const cnt = new ServiceContainer();
- const ctx = new RequestContext(cnt, req, res);
- expect(ctx.method).to.be.eq('POST');
- });
- });
- describe('path', function () {
- it('returns the request pathname with the query string', function () {
- const req = createRequestMock({path: '/pathname?foo=bar'});
- const res = createResponseMock();
- const cnt = new ServiceContainer();
- const ctx = new RequestContext(cnt, req, res);
- expect(req.url).to.be.eq('/pathname?foo=bar');
- expect(ctx.path).to.be.eq('/pathname?foo=bar');
- });
- });
- describe('pathname', function () {
- it('returns the request pathname without the query string', function () {
- const req = createRequestMock({path: '/pathname?foo=bar'});
- const res = createResponseMock();
- const cnt = new ServiceContainer();
- const ctx = new RequestContext(cnt, req, res);
- expect(req.url).to.be.eq('/pathname?foo=bar');
- expect(ctx.pathname).to.be.eq('/pathname');
- });
- it('sets the cache to the "_pathname" property and uses is for next accesses', function () {
- const req = createRequestMock({path: '/pathname'});
- const res = createResponseMock();
- const cnt = new ServiceContainer();
- const ctx = new RequestContext(cnt, req, res);
- expect(ctx._pathname).to.be.undefined;
- expect(ctx.pathname).to.be.eq('/pathname');
- expect(ctx._pathname).to.be.eq('/pathname');
- ctx._pathname = '/overridden';
- expect(ctx.pathname).to.be.eq('/overridden');
- });
- });
- });
|