-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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 runs once for matching route
- Loading branch information
1 parent
83098e2
commit 9f50226
Showing
2 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
integration/hello-world/e2e/middleware-run-match-route.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,103 @@ | ||
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'; | ||
|
||
/** | ||
* Number of times that the middleware was executed. | ||
*/ | ||
let triggerCounter = 0; | ||
@Injectable() | ||
class Middleware implements NestMiddleware { | ||
use(req, res, next) { | ||
triggerCounter++; | ||
next(); | ||
} | ||
} | ||
|
||
@Controller() | ||
class TestController { | ||
@Get('/test') | ||
testA() {} | ||
|
||
@Get('/:id') | ||
testB() {} | ||
|
||
@Get('/:id/test') | ||
testC() {} | ||
|
||
@Get('/:id/:nested') | ||
testD() {} | ||
} | ||
|
||
@Module({ | ||
controllers: [TestController], | ||
}) | ||
class TestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer.apply(Middleware).forRoutes(TestController); | ||
} | ||
} | ||
|
||
describe('Middleware (run on route match)', () => { | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
triggerCounter = 0; | ||
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('/test') | ||
.expect(200) | ||
.then(() => { | ||
expect(triggerCounter).to.be.eq(1); | ||
}); | ||
}); | ||
|
||
it(`forRoutes(TestController) should execute middleware once when request url is not equal match`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/1') | ||
.expect(200) | ||
.then(() => { | ||
expect(triggerCounter).to.be.eq(1); | ||
}); | ||
}); | ||
|
||
it(`forRoutes(TestController) should execute middleware once when request url is not of nested params`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/1/test') | ||
.expect(200) | ||
.then(() => { | ||
expect(triggerCounter).to.be.eq(1); | ||
}); | ||
}); | ||
|
||
it(`forRoutes(TestController) should execute middleware once when request url is of nested params`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/1/abc') | ||
.expect(200) | ||
.then(() => { | ||
expect(triggerCounter).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