|
@@ -1,7 +1,7 @@
|
|
|
import {describe} from 'mocha';
|
|
import {describe} from 'mocha';
|
|
|
import {Route} from './route.js';
|
|
import {Route} from './route.js';
|
|
|
import {expect} from './chai.js';
|
|
import {expect} from './chai.js';
|
|
|
-import {HTTP_METHOD} from './route.js';
|
|
|
|
|
|
|
+import {HttpMethod} from './route.js';
|
|
|
import {TrieRouter} from './trie-router.js';
|
|
import {TrieRouter} from './trie-router.js';
|
|
|
import {HOOK_NAME} from './hooks/index.js';
|
|
import {HOOK_NAME} from './hooks/index.js';
|
|
|
import {HookRegistry} from './hooks/index.js';
|
|
import {HookRegistry} from './hooks/index.js';
|
|
@@ -17,9 +17,9 @@ describe('TrieRouter', function () {
|
|
|
const router = new TrieRouter();
|
|
const router = new TrieRouter();
|
|
|
const path = '/path';
|
|
const path = '/path';
|
|
|
const handler = () => 'ok';
|
|
const handler = () => 'ok';
|
|
|
- const res = router.defineRoute({method: HTTP_METHOD.GET, path, handler});
|
|
|
|
|
|
|
+ const res = router.defineRoute({method: HttpMethod.GET, path, handler});
|
|
|
expect(res).to.be.instanceof(Route);
|
|
expect(res).to.be.instanceof(Route);
|
|
|
- expect(res.method).to.be.eq(HTTP_METHOD.GET);
|
|
|
|
|
|
|
+ expect(res.method).to.be.eq(HttpMethod.GET);
|
|
|
expect(res.path).to.be.eq(path);
|
|
expect(res.path).to.be.eq(path);
|
|
|
expect(res.handler).to.be.eq(handler);
|
|
expect(res.handler).to.be.eq(handler);
|
|
|
});
|
|
});
|
|
@@ -34,7 +34,7 @@ describe('TrieRouter', function () {
|
|
|
it('passes request context to the route handler', function (done) {
|
|
it('passes request context to the route handler', function (done) {
|
|
|
const router = new TrieRouter();
|
|
const router = new TrieRouter();
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/test',
|
|
path: '/test',
|
|
|
handler: ctx => {
|
|
handler: ctx => {
|
|
|
expect(ctx).to.be.instanceof(RequestContext);
|
|
expect(ctx).to.be.instanceof(RequestContext);
|
|
@@ -49,7 +49,7 @@ describe('TrieRouter', function () {
|
|
|
it('passes path parameters to the request context', function (done) {
|
|
it('passes path parameters to the request context', function (done) {
|
|
|
const router = new TrieRouter();
|
|
const router = new TrieRouter();
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/:p1-:p2',
|
|
path: '/:p1-:p2',
|
|
|
handler: ({params}) => {
|
|
handler: ({params}) => {
|
|
|
expect(params).to.be.eql({p1: 'foo', p2: 'bar'});
|
|
expect(params).to.be.eql({p1: 'foo', p2: 'bar'});
|
|
@@ -64,7 +64,7 @@ describe('TrieRouter', function () {
|
|
|
it('passes query parameters to the request context', function (done) {
|
|
it('passes query parameters to the request context', function (done) {
|
|
|
const router = new TrieRouter();
|
|
const router = new TrieRouter();
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
handler: ({query}) => {
|
|
handler: ({query}) => {
|
|
|
expect(query).to.be.eql({p1: 'foo', p2: 'bar'});
|
|
expect(query).to.be.eql({p1: 'foo', p2: 'bar'});
|
|
@@ -79,7 +79,7 @@ describe('TrieRouter', function () {
|
|
|
it('passes parsed cookie to the request context', function (done) {
|
|
it('passes parsed cookie to the request context', function (done) {
|
|
|
const router = new TrieRouter();
|
|
const router = new TrieRouter();
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
handler: ({cookie}) => {
|
|
handler: ({cookie}) => {
|
|
|
expect(cookie).to.be.eql({p1: 'foo', p2: 'bar'});
|
|
expect(cookie).to.be.eql({p1: 'foo', p2: 'bar'});
|
|
@@ -95,14 +95,14 @@ describe('TrieRouter', function () {
|
|
|
const router = new TrieRouter();
|
|
const router = new TrieRouter();
|
|
|
const body = 'Lorem Ipsum is simply dummy text.';
|
|
const body = 'Lorem Ipsum is simply dummy text.';
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.POST,
|
|
|
|
|
|
|
+ method: HttpMethod.POST,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
handler: ctx => {
|
|
handler: ctx => {
|
|
|
expect(ctx.body).to.be.eq(body);
|
|
expect(ctx.body).to.be.eq(body);
|
|
|
done();
|
|
done();
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
- const req = createRequestMock({method: HTTP_METHOD.POST, body});
|
|
|
|
|
|
|
+ const req = createRequestMock({method: HttpMethod.POST, body});
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
router.requestListener(req, res);
|
|
router.requestListener(req, res);
|
|
|
});
|
|
});
|
|
@@ -111,14 +111,14 @@ describe('TrieRouter', function () {
|
|
|
const router = new TrieRouter();
|
|
const router = new TrieRouter();
|
|
|
const data = {p1: 'foo', p2: 'bar'};
|
|
const data = {p1: 'foo', p2: 'bar'};
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.POST,
|
|
|
|
|
|
|
+ method: HttpMethod.POST,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
handler: ({body}) => {
|
|
handler: ({body}) => {
|
|
|
expect(body).to.be.eql(data);
|
|
expect(body).to.be.eql(data);
|
|
|
done();
|
|
done();
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
- const req = createRequestMock({method: HTTP_METHOD.POST, body: data});
|
|
|
|
|
|
|
+ const req = createRequestMock({method: HttpMethod.POST, body: data});
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
router.requestListener(req, res);
|
|
router.requestListener(req, res);
|
|
|
});
|
|
});
|
|
@@ -126,7 +126,7 @@ describe('TrieRouter', function () {
|
|
|
it('passes headers to the request context', function (done) {
|
|
it('passes headers to the request context', function (done) {
|
|
|
const router = new TrieRouter();
|
|
const router = new TrieRouter();
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
handler: ({headers}) => {
|
|
handler: ({headers}) => {
|
|
|
expect(headers).to.be.eql({
|
|
expect(headers).to.be.eql({
|
|
@@ -145,7 +145,7 @@ describe('TrieRouter', function () {
|
|
|
const router = new TrieRouter();
|
|
const router = new TrieRouter();
|
|
|
const resBody = 'Lorem Ipsum is simply dummy text.';
|
|
const resBody = 'Lorem Ipsum is simply dummy text.';
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
handler: () => resBody,
|
|
handler: () => resBody,
|
|
|
});
|
|
});
|
|
@@ -165,7 +165,7 @@ describe('TrieRouter', function () {
|
|
|
const router = new TrieRouter();
|
|
const router = new TrieRouter();
|
|
|
const error = new Error();
|
|
const error = new Error();
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
handler: () => {
|
|
handler: () => {
|
|
|
throw error;
|
|
throw error;
|
|
@@ -190,7 +190,7 @@ describe('TrieRouter', function () {
|
|
|
const order = [];
|
|
const order = [];
|
|
|
const body = 'OK';
|
|
const body = 'OK';
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
preHandler: [
|
|
preHandler: [
|
|
|
() => {
|
|
() => {
|
|
@@ -218,7 +218,7 @@ describe('TrieRouter', function () {
|
|
|
const order = [];
|
|
const order = [];
|
|
|
const body = 'OK';
|
|
const body = 'OK';
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
handler: () => {
|
|
handler: () => {
|
|
|
order.push('handler');
|
|
order.push('handler');
|
|
@@ -246,7 +246,7 @@ describe('TrieRouter', function () {
|
|
|
const order = [];
|
|
const order = [];
|
|
|
const body = 'OK';
|
|
const body = 'OK';
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
preHandler: [
|
|
preHandler: [
|
|
|
ctx => {
|
|
ctx => {
|
|
@@ -278,7 +278,7 @@ describe('TrieRouter', function () {
|
|
|
const body = 'OK';
|
|
const body = 'OK';
|
|
|
let requestContext;
|
|
let requestContext;
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
handler: ctx => {
|
|
handler: ctx => {
|
|
|
order.push('handler');
|
|
order.push('handler');
|
|
@@ -312,7 +312,7 @@ describe('TrieRouter', function () {
|
|
|
const order = [];
|
|
const order = [];
|
|
|
const body = 'OK';
|
|
const body = 'OK';
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
preHandler: [
|
|
preHandler: [
|
|
|
() => {
|
|
() => {
|
|
@@ -342,7 +342,7 @@ describe('TrieRouter', function () {
|
|
|
const order = [];
|
|
const order = [];
|
|
|
const body = 'OK';
|
|
const body = 'OK';
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
handler: () => {
|
|
handler: () => {
|
|
|
order.push('handler');
|
|
order.push('handler');
|
|
@@ -374,7 +374,7 @@ describe('TrieRouter', function () {
|
|
|
const handlerBody = 'bar';
|
|
const handlerBody = 'bar';
|
|
|
const postHandlerBody = 'baz';
|
|
const postHandlerBody = 'baz';
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
preHandler() {
|
|
preHandler() {
|
|
|
order.push('preHandler');
|
|
order.push('preHandler');
|
|
@@ -405,7 +405,7 @@ describe('TrieRouter', function () {
|
|
|
const handlerBody = 'foo';
|
|
const handlerBody = 'foo';
|
|
|
const postHandlerBody = 'bar';
|
|
const postHandlerBody = 'bar';
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
preHandler() {
|
|
preHandler() {
|
|
|
order.push('preHandler');
|
|
order.push('preHandler');
|
|
@@ -433,7 +433,7 @@ describe('TrieRouter', function () {
|
|
|
const order = [];
|
|
const order = [];
|
|
|
const body = 'OK';
|
|
const body = 'OK';
|
|
|
router.defineRoute({
|
|
router.defineRoute({
|
|
|
- method: HTTP_METHOD.GET,
|
|
|
|
|
|
|
+ method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
|
preHandler() {
|
|
preHandler() {
|
|
|
order.push('preHandler');
|
|
order.push('preHandler');
|