forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): middleware should run once for one request. (nestjs#1628)
- Loading branch information
2218301630@qq.com
authored and
2218301630@qq.com
committed
Sep 24, 2019
1 parent
61992c7
commit 76230ed
Showing
2 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
integration/hello-world/e2e/middleware-execute-once.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { | ||
Controller, | ||
Get, | ||
INestApplication, | ||
Injectable, | ||
MiddlewareConsumer, | ||
NestMiddleware, | ||
Module, | ||
} from '@nestjs/common'; | ||
import { Test } from '../../../packages/testing'; | ||
import * as request from 'supertest'; | ||
import { expect } from 'chai'; | ||
|
||
let number: number = 0; | ||
|
||
@Injectable() | ||
class Middleware implements NestMiddleware { | ||
use(req, res, next) { | ||
number++; | ||
next(); | ||
} | ||
} | ||
|
||
@Controller('/a') | ||
class TestController { | ||
@Get('/test') | ||
testA() { | ||
return ''; | ||
} | ||
@Get('/:id') | ||
testB() { | ||
return ''; | ||
} | ||
} | ||
|
||
@Module({ | ||
controllers: [TestController], | ||
}) | ||
class TestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer.apply(Middleware).forRoutes(TestController); | ||
} | ||
} | ||
|
||
describe('Middleware (execution once)', () => { | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
app = (await Test.createTestingModule({ | ||
imports: [TestModule], | ||
}).compile()).createNestApplication(); | ||
|
||
await app.init(); | ||
}); | ||
|
||
it(`forRoutes(TestController) should execute middleware once`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/a/test') | ||
.expect(200) | ||
.then(() => { | ||
expect(number).to.be.eq(1); | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters