-
-
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.
- Loading branch information
Showing
211 changed files
with
4,365 additions
and
24,030 deletions.
There are no files selected for viewing
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
77 changes: 77 additions & 0 deletions
77
integration/hello-world/e2e/router-module-middleware.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,77 @@ | ||
import { | ||
Controller, | ||
Get, | ||
INestApplication, | ||
MiddlewareConsumer, | ||
Module, | ||
} from '@nestjs/common'; | ||
import { RouterModule } from '@nestjs/core'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { ApplicationModule } from '../src/app.module'; | ||
|
||
const RETURN_VALUE = 'test'; | ||
const SCOPED_VALUE = 'test_scoped'; | ||
|
||
@Controller() | ||
class TestController { | ||
@Get('test') | ||
test() { | ||
return RETURN_VALUE; | ||
} | ||
|
||
@Get('test2') | ||
test2() { | ||
return RETURN_VALUE; | ||
} | ||
} | ||
|
||
@Module({ | ||
imports: [ApplicationModule], | ||
controllers: [TestController], | ||
}) | ||
class TestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer | ||
.apply((req, res, next) => res.send(SCOPED_VALUE)) | ||
.forRoutes(TestController); | ||
} | ||
} | ||
|
||
describe('RouterModule with Middleware functions', () => { | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
app = ( | ||
await Test.createTestingModule({ | ||
imports: [ | ||
TestModule, | ||
RouterModule.register([ | ||
{ | ||
path: '/module-path/', | ||
module: TestModule, | ||
}, | ||
]), | ||
], | ||
}).compile() | ||
).createNestApplication(); | ||
|
||
await app.init(); | ||
}); | ||
|
||
it(`forRoutes(TestController) - /test`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/module-path/test') | ||
.expect(200, SCOPED_VALUE); | ||
}); | ||
|
||
it(`forRoutes(TestController) - /test2`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/module-path/test2') | ||
.expect(200, SCOPED_VALUE); | ||
}); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Controller, Get, INestApplication, Module } from '@nestjs/common'; | ||
import { RouterModule, Routes } from '@nestjs/core'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
|
||
describe('RouterModule', () => { | ||
let app: INestApplication; | ||
|
||
abstract class BaseController { | ||
@Get() | ||
getName() { | ||
return this.constructor.name; | ||
} | ||
} | ||
|
||
@Controller('/parent-controller') | ||
class ParentController extends BaseController {} | ||
@Controller('/child-controller') | ||
class ChildController extends BaseController {} | ||
@Controller('no-slash-controller') | ||
class NoSlashController extends BaseController {} | ||
|
||
class UnknownController {} | ||
@Module({ controllers: [ParentController] }) | ||
class ParentModule {} | ||
|
||
@Module({ controllers: [ChildController] }) | ||
class ChildModule {} | ||
|
||
@Module({}) | ||
class AuthModule {} | ||
@Module({}) | ||
class PaymentsModule {} | ||
|
||
@Module({ controllers: [NoSlashController] }) | ||
class NoSlashModule {} | ||
|
||
const routes1: Routes = [ | ||
{ | ||
path: 'parent', | ||
module: ParentModule, | ||
children: [ | ||
{ | ||
path: 'child', | ||
module: ChildModule, | ||
}, | ||
], | ||
}, | ||
]; | ||
const routes2: Routes = [ | ||
{ path: 'v1', children: [AuthModule, PaymentsModule, NoSlashModule] }, | ||
]; | ||
|
||
@Module({ | ||
imports: [ParentModule, ChildModule, RouterModule.register(routes1)], | ||
}) | ||
class MainModule {} | ||
|
||
@Module({ | ||
imports: [ | ||
AuthModule, | ||
PaymentsModule, | ||
NoSlashModule, | ||
RouterModule.register(routes2), | ||
], | ||
}) | ||
class AppModule {} | ||
|
||
before(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [MainModule, AppModule], | ||
}).compile(); | ||
|
||
app = moduleRef.createNestApplication(); | ||
await app.init(); | ||
}); | ||
|
||
it('should hit the "ParentController"', async () => { | ||
return request(app.getHttpServer()) | ||
.get('/parent/parent-controller') | ||
.expect(200, 'ParentController'); | ||
}); | ||
|
||
it('should hit the "ChildController"', async () => { | ||
return request(app.getHttpServer()) | ||
.get('/parent/child/child-controller') | ||
.expect(200, 'ChildController'); | ||
}); | ||
|
||
it('should hit the "NoSlashController"', async () => { | ||
return request(app.getHttpServer()) | ||
.get('/v1/no-slash-controller') | ||
.expect(200, 'NoSlashController'); | ||
}); | ||
|
||
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
Oops, something went wrong.