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,common,testing): support overriding middleware for testing
closes: nestjs#8777
- Loading branch information
Showing
11 changed files
with
480 additions
and
14 deletions.
There are no files selected for viewing
191 changes: 191 additions & 0 deletions
191
integration/testing/middleware-override/e2e/middleware-override.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,191 @@ | ||
import { | ||
Injectable, | ||
MiddlewareConsumer, | ||
Module, | ||
NestMiddleware, | ||
} from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { expect } from 'chai'; | ||
|
||
describe('Middleware overriding', () => { | ||
@Injectable() | ||
class MiddlewareA implements NestMiddleware { | ||
use(req, res, next) { | ||
middlewareAApplied = true; | ||
next(); | ||
} | ||
} | ||
|
||
function MiddlewareAOverride(req, res, next) { | ||
middlewareAOverrideApplied = true; | ||
next(); | ||
} | ||
|
||
function MiddlewareB(req, res, next) { | ||
middlewareBApplied = true; | ||
next(); | ||
} | ||
|
||
@Injectable() | ||
class MiddlewareBOverride implements NestMiddleware { | ||
use(req, res, next) { | ||
middlewareBOverrideApplied = true; | ||
next(); | ||
} | ||
} | ||
|
||
@Injectable() | ||
class MiddlewareC implements NestMiddleware { | ||
use(req, res, next) { | ||
middlewareCApplied = true; | ||
next(); | ||
} | ||
} | ||
|
||
@Injectable() | ||
class MiddlewareC1Override implements NestMiddleware { | ||
use(req, res, next) { | ||
middlewareC1OverrideApplied = true; | ||
next(); | ||
} | ||
} | ||
|
||
function MiddlewareC2Override(req, res, next) { | ||
middlewareC2OverrideApplied = true; | ||
next(); | ||
} | ||
|
||
@Module({}) | ||
class AppModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
return consumer | ||
.apply(MiddlewareA) | ||
.forRoutes('a') | ||
.apply(MiddlewareB) | ||
.forRoutes('b') | ||
.apply(MiddlewareC) | ||
.forRoutes('c'); | ||
} | ||
} | ||
|
||
let middlewareAApplied: boolean; | ||
let middlewareAOverrideApplied: boolean; | ||
|
||
let middlewareBApplied: boolean; | ||
let middlewareBOverrideApplied: boolean; | ||
|
||
let middlewareCApplied: boolean; | ||
let middlewareC1OverrideApplied: boolean; | ||
let middlewareC2OverrideApplied: boolean; | ||
|
||
const resetMiddlewareApplicationFlags = () => { | ||
middlewareAApplied = | ||
middlewareAOverrideApplied = | ||
middlewareBApplied = | ||
middlewareBOverrideApplied = | ||
middlewareCApplied = | ||
middlewareC1OverrideApplied = | ||
middlewareC2OverrideApplied = | ||
false; | ||
}; | ||
|
||
beforeEach(() => { | ||
resetMiddlewareApplicationFlags(); | ||
}); | ||
it('should override class middleware', async () => { | ||
const testingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}) | ||
.overrideMiddleware(MiddlewareA) | ||
.useMiddleware(MiddlewareAOverride) | ||
.overrideMiddleware(MiddlewareC) | ||
.useMiddleware(MiddlewareC1Override, MiddlewareC2Override) | ||
.compile(); | ||
|
||
const app = testingModule.createNestApplication(); | ||
await app.init(); | ||
|
||
await request(app.getHttpServer()).get('/a'); | ||
|
||
expect(middlewareAApplied).to.be.false; | ||
expect(middlewareAOverrideApplied).to.be.true; | ||
expect(middlewareBApplied).to.be.false; | ||
expect(middlewareBOverrideApplied).to.be.false; | ||
expect(middlewareCApplied).to.be.false; | ||
expect(middlewareC1OverrideApplied).to.be.false; | ||
expect(middlewareC2OverrideApplied).to.be.false; | ||
resetMiddlewareApplicationFlags(); | ||
|
||
await request(app.getHttpServer()).get('/b'); | ||
|
||
expect(middlewareAApplied).to.be.false; | ||
expect(middlewareAOverrideApplied).to.be.false; | ||
expect(middlewareBApplied).to.be.true; | ||
expect(middlewareBOverrideApplied).to.be.false; | ||
expect(middlewareCApplied).to.be.false; | ||
expect(middlewareC1OverrideApplied).to.be.false; | ||
expect(middlewareC2OverrideApplied).to.be.false; | ||
resetMiddlewareApplicationFlags(); | ||
|
||
await request(app.getHttpServer()).get('/c'); | ||
|
||
expect(middlewareAApplied).to.be.false; | ||
expect(middlewareAOverrideApplied).to.be.false; | ||
expect(middlewareBApplied).to.be.false; | ||
expect(middlewareBOverrideApplied).to.be.false; | ||
expect(middlewareCApplied).to.be.false; | ||
expect(middlewareC1OverrideApplied).to.be.true; | ||
expect(middlewareC2OverrideApplied).to.be.true; | ||
resetMiddlewareApplicationFlags(); | ||
|
||
await app.close(); | ||
}); | ||
|
||
it('should override functional middleware', async () => { | ||
const testingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}) | ||
.overrideMiddleware(MiddlewareB) | ||
.useMiddleware(MiddlewareBOverride) | ||
.compile(); | ||
|
||
const app = testingModule.createNestApplication(); | ||
await app.init(); | ||
|
||
await request(app.getHttpServer()).get('/a'); | ||
|
||
expect(middlewareAApplied).to.be.true; | ||
expect(middlewareAOverrideApplied).to.be.false; | ||
expect(middlewareBApplied).to.be.false; | ||
expect(middlewareBOverrideApplied).to.be.false; | ||
expect(middlewareCApplied).to.be.false; | ||
expect(middlewareC1OverrideApplied).to.be.false; | ||
expect(middlewareC2OverrideApplied).to.be.false; | ||
resetMiddlewareApplicationFlags(); | ||
|
||
await request(app.getHttpServer()).get('/b'); | ||
|
||
expect(middlewareAApplied).to.be.false; | ||
expect(middlewareAOverrideApplied).to.be.false; | ||
expect(middlewareBApplied).to.be.false; | ||
expect(middlewareBOverrideApplied).to.be.true; | ||
expect(middlewareCApplied).to.be.false; | ||
expect(middlewareC1OverrideApplied).to.be.false; | ||
expect(middlewareC2OverrideApplied).to.be.false; | ||
resetMiddlewareApplicationFlags(); | ||
|
||
await request(app.getHttpServer()).get('/c'); | ||
|
||
expect(middlewareAApplied).to.be.false; | ||
expect(middlewareAOverrideApplied).to.be.false; | ||
expect(middlewareBApplied).to.be.false; | ||
expect(middlewareBOverrideApplied).to.be.false; | ||
expect(middlewareCApplied).to.be.true; | ||
expect(middlewareC1OverrideApplied).to.be.false; | ||
expect(middlewareC2OverrideApplied).to.be.false; | ||
resetMiddlewareApplicationFlags(); | ||
|
||
await app.close(); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": true, | ||
"removeComments": true, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"allowSyntheticDefaultImports": true, | ||
"target": "ES2021", | ||
"sourceMap": true, | ||
"outDir": "./dist", | ||
"baseUrl": "./", | ||
"incremental": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["src/**/*"] | ||
} |
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
Oops, something went wrong.