|
@@ -1,16 +1,21 @@
|
|
|
import {expect} from 'chai';
|
|
import {expect} from 'chai';
|
|
|
import {format} from '@e22m4u/js-format';
|
|
import {format} from '@e22m4u/js-format';
|
|
|
-import {createRequestMock} from './utils/index.js';
|
|
|
|
|
import {RequestContext} from './request-context.js';
|
|
import {RequestContext} from './request-context.js';
|
|
|
import {ServiceContainer} from '@e22m4u/js-service';
|
|
import {ServiceContainer} from '@e22m4u/js-service';
|
|
|
-import {createResponseMock} from './utils/index.js';
|
|
|
|
|
|
|
+
|
|
|
|
|
+import {
|
|
|
|
|
+ createRouteMock,
|
|
|
|
|
+ createRequestMock,
|
|
|
|
|
+ createResponseMock,
|
|
|
|
|
+} from './utils/index.js';
|
|
|
|
|
|
|
|
describe('RequestContext', function () {
|
|
describe('RequestContext', function () {
|
|
|
describe('constructor', function () {
|
|
describe('constructor', function () {
|
|
|
it('requires the parameter "container" to be the ServiceContainer', function () {
|
|
it('requires the parameter "container" to be the ServiceContainer', function () {
|
|
|
const req = createRequestMock();
|
|
const req = createRequestMock();
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
- const throwable = v => () => new RequestContext(v, req, res);
|
|
|
|
|
|
|
+ const route = createRouteMock();
|
|
|
|
|
+ const throwable = v => () => new RequestContext(v, req, res, route);
|
|
|
const error = v =>
|
|
const error = v =>
|
|
|
format(
|
|
format(
|
|
|
'The parameter "container" of RequestContext.constructor ' +
|
|
'The parameter "container" of RequestContext.constructor ' +
|
|
@@ -32,8 +37,9 @@ describe('RequestContext', function () {
|
|
|
|
|
|
|
|
it('requires the parameter "request" to be the ServiceContainer', function () {
|
|
it('requires the parameter "request" to be the ServiceContainer', function () {
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
|
|
+ const route = createRouteMock();
|
|
|
const cont = new ServiceContainer();
|
|
const cont = new ServiceContainer();
|
|
|
- const throwable = v => () => new RequestContext(cont, v, res);
|
|
|
|
|
|
|
+ const throwable = v => () => new RequestContext(cont, v, res, route);
|
|
|
const error = v =>
|
|
const error = v =>
|
|
|
format(
|
|
format(
|
|
|
'The parameter "request" of RequestContext.constructor ' +
|
|
'The parameter "request" of RequestContext.constructor ' +
|
|
@@ -55,8 +61,9 @@ describe('RequestContext', function () {
|
|
|
|
|
|
|
|
it('requires the parameter "response" to be the ServiceContainer', function () {
|
|
it('requires the parameter "response" to be the ServiceContainer', function () {
|
|
|
const req = createRequestMock();
|
|
const req = createRequestMock();
|
|
|
|
|
+ const route = createRouteMock();
|
|
|
const cont = new ServiceContainer();
|
|
const cont = new ServiceContainer();
|
|
|
- const throwable = v => () => new RequestContext(cont, req, v);
|
|
|
|
|
|
|
+ const throwable = v => () => new RequestContext(cont, req, v, route);
|
|
|
const error = v =>
|
|
const error = v =>
|
|
|
format(
|
|
format(
|
|
|
'The parameter "response" of RequestContext.constructor ' +
|
|
'The parameter "response" of RequestContext.constructor ' +
|
|
@@ -76,22 +83,39 @@ describe('RequestContext', function () {
|
|
|
throwable(createResponseMock())();
|
|
throwable(createResponseMock())();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('sets properties from given arguments', function () {
|
|
|
|
|
|
|
+ it('requires the parameter "route" to be the Route', function () {
|
|
|
const req = createRequestMock();
|
|
const req = createRequestMock();
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
const cont = new ServiceContainer();
|
|
const cont = new ServiceContainer();
|
|
|
- const ctx = new RequestContext(cont, req, res);
|
|
|
|
|
- expect(ctx.container).to.be.eq(cont);
|
|
|
|
|
- expect(ctx.request).to.be.eq(req);
|
|
|
|
|
- expect(ctx.response).to.be.eq(res);
|
|
|
|
|
|
|
+ const throwable = v => () => new RequestContext(cont, req, res, v);
|
|
|
|
|
+ const error = v =>
|
|
|
|
|
+ format(
|
|
|
|
|
+ 'The parameter "route" of RequestContext.constructor ' +
|
|
|
|
|
+ 'should be an instance of Route, 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(createRouteMock())();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('sets an empty object to the "meta" property', function () {
|
|
|
|
|
|
|
+ it('sets properties from given arguments', function () {
|
|
|
const req = createRequestMock();
|
|
const req = createRequestMock();
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
|
|
+ const route = createRouteMock();
|
|
|
const cont = new ServiceContainer();
|
|
const cont = new ServiceContainer();
|
|
|
- const ctx = new RequestContext(cont, req, res);
|
|
|
|
|
- expect(ctx.meta).to.be.eql({});
|
|
|
|
|
|
|
+ const ctx = new RequestContext(cont, req, res, route);
|
|
|
|
|
+ expect(ctx.container).to.be.eq(cont);
|
|
|
|
|
+ expect(ctx.request).to.be.eq(req);
|
|
|
|
|
+ expect(ctx.response).to.be.eq(res);
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -99,8 +123,9 @@ describe('RequestContext', function () {
|
|
|
it('returns the method name in upper case', function () {
|
|
it('returns the method name in upper case', function () {
|
|
|
const req = createRequestMock({method: 'post'});
|
|
const req = createRequestMock({method: 'post'});
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
|
|
+ const route = createRouteMock();
|
|
|
const cont = new ServiceContainer();
|
|
const cont = new ServiceContainer();
|
|
|
- const ctx = new RequestContext(cont, req, res);
|
|
|
|
|
|
|
+ const ctx = new RequestContext(cont, req, res, route);
|
|
|
expect(ctx.method).to.be.eq('POST');
|
|
expect(ctx.method).to.be.eq('POST');
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
@@ -109,8 +134,9 @@ describe('RequestContext', function () {
|
|
|
it('returns the request pathname with the query string', function () {
|
|
it('returns the request pathname with the query string', function () {
|
|
|
const req = createRequestMock({path: '/pathname?foo=bar'});
|
|
const req = createRequestMock({path: '/pathname?foo=bar'});
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
|
|
+ const route = createRouteMock();
|
|
|
const cont = new ServiceContainer();
|
|
const cont = new ServiceContainer();
|
|
|
- const ctx = new RequestContext(cont, req, res);
|
|
|
|
|
|
|
+ const ctx = new RequestContext(cont, req, res, route);
|
|
|
expect(req.url).to.be.eq('/pathname?foo=bar');
|
|
expect(req.url).to.be.eq('/pathname?foo=bar');
|
|
|
expect(ctx.path).to.be.eq('/pathname?foo=bar');
|
|
expect(ctx.path).to.be.eq('/pathname?foo=bar');
|
|
|
});
|
|
});
|
|
@@ -120,8 +146,9 @@ describe('RequestContext', function () {
|
|
|
it('returns the request pathname without the query string', function () {
|
|
it('returns the request pathname without the query string', function () {
|
|
|
const req = createRequestMock({path: '/pathname?foo=bar'});
|
|
const req = createRequestMock({path: '/pathname?foo=bar'});
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
|
|
+ const route = createRouteMock();
|
|
|
const cont = new ServiceContainer();
|
|
const cont = new ServiceContainer();
|
|
|
- const ctx = new RequestContext(cont, req, res);
|
|
|
|
|
|
|
+ const ctx = new RequestContext(cont, req, res, route);
|
|
|
expect(req.url).to.be.eq('/pathname?foo=bar');
|
|
expect(req.url).to.be.eq('/pathname?foo=bar');
|
|
|
expect(ctx.pathname).to.be.eq('/pathname');
|
|
expect(ctx.pathname).to.be.eq('/pathname');
|
|
|
});
|
|
});
|
|
@@ -129,8 +156,9 @@ describe('RequestContext', function () {
|
|
|
it('sets the cache to the "_pathname" property and uses is for next accesses', function () {
|
|
it('sets the cache to the "_pathname" property and uses is for next accesses', function () {
|
|
|
const req = createRequestMock({path: '/pathname'});
|
|
const req = createRequestMock({path: '/pathname'});
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
|
|
+ const route = createRouteMock();
|
|
|
const cont = new ServiceContainer();
|
|
const cont = new ServiceContainer();
|
|
|
- const ctx = new RequestContext(cont, req, res);
|
|
|
|
|
|
|
+ const ctx = new RequestContext(cont, req, res, route);
|
|
|
expect(ctx._pathname).to.be.undefined;
|
|
expect(ctx._pathname).to.be.undefined;
|
|
|
expect(ctx.pathname).to.be.eq('/pathname');
|
|
expect(ctx.pathname).to.be.eq('/pathname');
|
|
|
expect(ctx._pathname).to.be.eq('/pathname');
|
|
expect(ctx._pathname).to.be.eq('/pathname');
|