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(nestjs/core): run
onApplicationListen
hook after listen server. …
- Loading branch information
Showing
4 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
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,82 @@ | ||
import { | ||
Controller, | ||
Get, | ||
INestApplication, | ||
Injectable, | ||
MiddlewareConsumer, | ||
NestMiddleware, | ||
Module, | ||
} from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { expect } from 'chai'; | ||
|
||
let number: number = 0; | ||
let applyRoute: string; | ||
@Injectable() | ||
class Middleware implements NestMiddleware { | ||
use(req, res, next) { | ||
number++; | ||
applyRoute = req.route.path; | ||
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 () => { | ||
number = 0; | ||
applyRoute = ''; | ||
app = (await Test.createTestingModule({ | ||
imports: [TestModule], | ||
}).compile()).createNestApplication(); | ||
|
||
await app.init(); | ||
}); | ||
|
||
it(`forRoutes(TestController) should execute middleware once when request url is equal match`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/a/test') | ||
.expect(200) | ||
.then(() => { | ||
expect(number).to.be.eq(1); | ||
expect(applyRoute).to.be.eq('/a/test') | ||
}); | ||
}); | ||
|
||
it(`forRoutes(TestController) should execute middleware once when request url not is equal match`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/a/1') | ||
.expect(200) | ||
.then(() => { | ||
expect(number).to.be.eq(1); | ||
expect(applyRoute).to.be.eq('/a/:id') | ||
}); | ||
}); | ||
|
||
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
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
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