Skip to content

Commit

Permalink
Update tests to confirm that chaining multiple middlewares works
Browse files Browse the repository at this point in the history
  • Loading branch information
iffa committed Feb 7, 2024
1 parent a954b4d commit 0fecbcf
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
6 changes: 3 additions & 3 deletions tests/fixtures/controllers/middlewaresExpressController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route, Get, Middlewares as GenericMiddlewares } from '@tsoa/runtime';
import { Middlewares as GenericMiddlewares, Get, Route } from '@tsoa/runtime';

import type { Request as ExpressRequest, Response as ExpressResponse, NextFunction as ExpressNextFunction, RequestHandler } from 'express';
import type { NextFunction as ExpressNextFunction, Request as ExpressRequest, Response as ExpressResponse, RequestHandler } from 'express';

function Middlewares(...mws: Array<RequestHandler | (() => Promise<RequestHandler>)>) {
return GenericMiddlewares(...mws);
Expand All @@ -22,7 +22,7 @@ function testMiddleware(key: string) {
@GenericMiddlewares<RequestHandler>(testMiddleware('route'))
@Route('MiddlewareTestExpress')
export class MiddlewareExpressController {
@Middlewares(testMiddleware('test1'))
@Middlewares(testMiddleware('test1'), testMiddleware('test2'))
@Get('/test1')
public async test1(): Promise<void> {
return;
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/controllers/middlewaresHapiController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Route, Get, Middlewares as GenericMiddlewares } from '@tsoa/runtime';
import { Middlewares as GenericMiddlewares, Get, Route } from '@tsoa/runtime';

import type { Request, ResponseToolkit, RouteOptionsPreAllOptions } from '@hapi/hapi';

Expand All @@ -22,7 +22,7 @@ function testMiddleware(key: string) {
@GenericMiddlewares<RouteOptionsPreAllOptions>(testMiddleware('route'))
@Route('MiddlewareTestHapi')
export class MiddlewareHapiController {
@Middlewares(testMiddleware('test1'))
@Middlewares(testMiddleware('test1'), testMiddleware('test2'))
@Get('/test1')
public async test1(): Promise<void> {
return;
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/controllers/middlewaresHierarchyController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route, Get, Middlewares as GenericMiddlewares, Controller } from '@tsoa/runtime';
import { Controller, Middlewares as GenericMiddlewares, Get, Route } from '@tsoa/runtime';

import type { Request, Response, NextFunction, RequestHandler } from 'express';
import type { NextFunction, Request, RequestHandler, Response } from 'express';

function Middlewares(...mws: Array<RequestHandler | (() => Promise<RequestHandler>)>) {
return GenericMiddlewares(...mws);
Expand Down Expand Up @@ -33,7 +33,7 @@ class NoopController extends IntermediateController {}
@GenericMiddlewares<RequestHandler>(testMiddleware('route'))
@Route('MiddlewareHierarchyTestExpress')
export class MiddlewareHierarchyTestExpress extends NoopController {
@Middlewares(testMiddleware('test1'))
@Middlewares(testMiddleware('test1'), testMiddleware('test2'))
@Get('/test1')
public async test1(): Promise<void> {
return;
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/controllers/middlewaresKoaController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Route, Get, Middlewares as GenericMiddlewares } from '@tsoa/runtime';
import { Middlewares as GenericMiddlewares, Get, Route } from '@tsoa/runtime';

import type { Context as KoaContext, Next as KoaNext, Middleware } from 'koa';

Expand All @@ -22,7 +22,7 @@ function testMiddleware(key: string) {
@GenericMiddlewares<Middleware>(testMiddleware('route'))
@Route('MiddlewareTestKoa')
export class MiddlewareKoaController {
@Middlewares(testMiddleware('test1'))
@Middlewares(testMiddleware('test1'), testMiddleware('test2'))
@Get('/test1')
public async test1(): Promise<void> {
return;
Expand Down
13 changes: 7 additions & 6 deletions tests/integration/express-server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { File } from '@tsoa/runtime';
import { expect } from 'chai';
import { readFileSync } from 'fs';
import 'mocha';
import { resolve } from 'path';
import * as request from 'supertest';
import { base64image } from '../fixtures/base64image';
import { stateOf } from '../fixtures/controllers/middlewaresExpressController';
import { state } from '../fixtures/controllers/middlewaresHierarchyController';
import { app } from '../fixtures/express/server';
import { File } from '@tsoa/runtime';
import {
Gender,
GenericModel,
Expand All @@ -16,10 +20,6 @@ import {
ValidateMapStringToNumber,
ValidateModel,
} from '../fixtures/testModel';
import { stateOf } from '../fixtures/controllers/middlewaresExpressController';
import { state } from '../fixtures/controllers/middlewaresHierarchyController';
import { readFileSync } from 'fs';
import { resolve } from 'path';

const basePath = '/v1';

Expand Down Expand Up @@ -434,6 +434,7 @@ describe('Express Server', () => {
(_err, _res) => {
expect(stateOf('route')).to.be.true;
expect(stateOf('test1')).to.be.true;
expect(stateOf('test2')).to.be.true;
},
204,
);
Expand All @@ -444,7 +445,7 @@ describe('Express Server', () => {
return verifyGetRequest(
basePath + '/MiddlewareHierarchyTestExpress/test1',
(_err, _res) => {
const expected = ['base', 'intermediate', 'route', 'test1'];
const expected = ['base', 'intermediate', 'route', 'test1', 'test2'];
expect(state()).to.eql(expected);
},
204,
Expand Down
9 changes: 5 additions & 4 deletions tests/integration/hapi-server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { File } from '@tsoa/runtime';
import { expect } from 'chai';
import { readFileSync } from 'fs';
import 'mocha';
import { resolve } from 'path';
import * as request from 'supertest';
import { stateOf } from '../fixtures/controllers/middlewaresHapiController';
import { server } from '../fixtures/hapi/server';
import { Gender, GenericModel, GenericRequest, Model, ParameterTestModel, TestClassModel, TestModel, ValidateMapStringToAny, ValidateMapStringToNumber, ValidateModel } from '../fixtures/testModel';
import { stateOf } from '../fixtures/controllers/middlewaresHapiController';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { File } from '@tsoa/runtime';

const basePath = '/v1';

Expand Down Expand Up @@ -330,6 +330,7 @@ describe('Hapi Server', () => {
(_err, _res) => {
expect(stateOf('route')).to.be.true;
expect(stateOf('test1')).to.be.true;
expect(stateOf('test2')).to.be.true;
},
204,
);
Expand Down
9 changes: 5 additions & 4 deletions tests/integration/koa-server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { File } from '@tsoa/runtime';
import { expect } from 'chai';
import { readFileSync } from 'fs';
import 'mocha';
import { resolve } from 'path';
import * as request from 'supertest';
import { stateOf } from '../fixtures/controllers/middlewaresKoaController';
import { server } from '../fixtures/koa/server';
import { Gender, GenericModel, GenericRequest, Model, ParameterTestModel, TestClassModel, TestModel, ValidateMapStringToAny, ValidateMapStringToNumber, ValidateModel } from '../fixtures/testModel';
import { stateOf } from '../fixtures/controllers/middlewaresKoaController';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { File } from '@tsoa/runtime';

const basePath = '/v1';

Expand Down Expand Up @@ -246,6 +246,7 @@ describe('Koa Server', () => {
(_err, _res) => {
expect(stateOf('route')).to.be.true;
expect(stateOf('test1')).to.be.true;
expect(stateOf('test2')).to.be.true;
},
204,
);
Expand Down

0 comments on commit 0fecbcf

Please sign in to comment.