mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
feat: move test
This commit is contained in:
@@ -77,6 +77,26 @@ describe('Middleware', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('when using default URI versioning with the global prefix', () => {
|
||||
beforeEach(async () => {
|
||||
app = await createAppWithVersioning(
|
||||
{
|
||||
type: VersioningType.URI,
|
||||
defaultVersion: VERSION_NEUTRAL,
|
||||
},
|
||||
async (app: INestApplication) => {
|
||||
app.setGlobalPrefix('api');
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it(`forRoutes({ path: '/versioned', version: '1', method: RequestMethod.ALL })`, () => {
|
||||
return request(app.getHttpServer())
|
||||
.get('/api/v1/versioned')
|
||||
.expect(200, VERSIONED_VALUE);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when using HEADER versioning', () => {
|
||||
beforeEach(async () => {
|
||||
app = await createAppWithVersioning({
|
||||
@@ -133,6 +153,7 @@ describe('Middleware', () => {
|
||||
|
||||
async function createAppWithVersioning(
|
||||
versioningOptions: VersioningOptions,
|
||||
beforeInit?: (app: INestApplication) => Promise<void>,
|
||||
): Promise<INestApplication> {
|
||||
const app = (
|
||||
await Test.createTestingModule({
|
||||
@@ -141,6 +162,9 @@ async function createAppWithVersioning(
|
||||
).createNestApplication();
|
||||
|
||||
app.enableVersioning(versioningOptions);
|
||||
if (beforeInit) {
|
||||
await beforeInit(app);
|
||||
}
|
||||
await app.init();
|
||||
|
||||
return app;
|
||||
|
||||
@@ -119,6 +119,17 @@ describe('Global prefix', () => {
|
||||
await request(server).get('/api/v1/middleware/foo').expect(404);
|
||||
});
|
||||
|
||||
it(`should get the params in the global prefix`, async () => {
|
||||
app.setGlobalPrefix('/api/:tenantId');
|
||||
|
||||
server = app.getHttpServer();
|
||||
await app.init();
|
||||
|
||||
await request(server)
|
||||
.get('/api/test/params')
|
||||
.expect(200, { '0': 'params', tenantId: 'test' });
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
@@ -7,6 +7,11 @@ export class AppController {
|
||||
return 'Hello: ' + req.extras?.data;
|
||||
}
|
||||
|
||||
@Get('params')
|
||||
getParams(@Req() req): any {
|
||||
return req.middlewareParams;
|
||||
}
|
||||
|
||||
@Get('health')
|
||||
getHealth(): string {
|
||||
return 'up';
|
||||
|
||||
@@ -22,6 +22,11 @@ export class AppModule {
|
||||
req.extras = { data: 'Data attached in middleware' };
|
||||
next();
|
||||
})
|
||||
.forRoutes({ path: '*', method: RequestMethod.GET })
|
||||
.apply((req, res, next) => {
|
||||
req.middlewareParams = req.params;
|
||||
next();
|
||||
})
|
||||
.forRoutes({ path: '*', method: RequestMethod.GET });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
import {
|
||||
INestApplication,
|
||||
RequestMethod,
|
||||
VersioningType,
|
||||
} from '@nestjs/common';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as request from 'supertest';
|
||||
import { AppModule } from '../src/app.module';
|
||||
|
||||
describe('Middleware', () => {
|
||||
let server;
|
||||
let app: INestApplication;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
}).compile();
|
||||
|
||||
app = module.createNestApplication();
|
||||
app.enableVersioning({ type: VersioningType.URI });
|
||||
});
|
||||
|
||||
it(`should get the params in the global prefix`, async () => {
|
||||
app.setGlobalPrefix('/api/:tenantId');
|
||||
|
||||
server = app.getHttpServer();
|
||||
await app.init();
|
||||
|
||||
await request(server)
|
||||
.get('/api/test/foo')
|
||||
.expect(200, {
|
||||
param: { '0': 'foo', tenantId: 'test' },
|
||||
});
|
||||
|
||||
await request(server)
|
||||
.get('/api/test/v2/foo')
|
||||
.expect(200, {
|
||||
version: 'v2',
|
||||
param: { '0': 'foo', tenantId: 'test' },
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
});
|
||||
});
|
||||
@@ -1,15 +0,0 @@
|
||||
import { Controller, Get, Req, Version } from '@nestjs/common';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
@Get('foo')
|
||||
async foo(@Req() req) {
|
||||
return req.extras;
|
||||
}
|
||||
|
||||
@Version('2')
|
||||
@Get('foo')
|
||||
async fooV2(@Req() req) {
|
||||
return req.extras;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [AppController],
|
||||
})
|
||||
export class AppModule {
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
consumer
|
||||
.apply((req, res, next) => {
|
||||
req.extras = { param: req.params };
|
||||
next();
|
||||
})
|
||||
.forRoutes({ path: '*', method: RequestMethod.GET })
|
||||
.apply((req, res, next) => {
|
||||
req.extras = {
|
||||
version: 'v2',
|
||||
param: req.params,
|
||||
};
|
||||
next();
|
||||
})
|
||||
.forRoutes({ path: '*', method: RequestMethod.GET, version: '2' });
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"declaration": false,
|
||||
"noImplicitAny": false,
|
||||
"removeComments": true,
|
||||
"noLib": false,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"target": "es6",
|
||||
"sourceMap": true,
|
||||
"allowJs": true,
|
||||
"outDir": "./dist",
|
||||
"paths": {
|
||||
"@nestjs/common": ["../../../packages/common"],
|
||||
"@nestjs/common/*": ["../../../packages/common/*"],
|
||||
"@nestjs/core": ["../../../packages/core"],
|
||||
"@nestjs/core/*": ["../../../packages/core/*"],
|
||||
"@nestjs/microservices": ["../../../packages/microservices"],
|
||||
"@nestjs/microservices/*": ["../../../packages/microservices/*"],
|
||||
"@nestjs/websockets": ["../../../packages/websockets"],
|
||||
"@nestjs/websockets/*": ["../../../packages/websockets/*"],
|
||||
"@nestjs/testing": ["../../../packages/websockets"],
|
||||
"@nestjs/testing/*": ["../../../packages/websockets/*"],
|
||||
"@nestjs/platform-express": ["../../../packages/platform-express"],
|
||||
"@nestjs/platform-express/*": ["../../../packages/platform-express/*"],
|
||||
"@nestjs/platform-socket.io": ["../../../packages/platform-socket.io"],
|
||||
"@nestjs/platform-socket.io/*": ["../../../packages/platform-socket.io/*"],
|
||||
"@nestjs/platform-ws": ["../../../packages/platform-ws"],
|
||||
"@nestjs/platform-ws/*": ["../../../packages/platform-ws/*"]
|
||||
}
|
||||
},
|
||||
"include": [
|
||||
"src/**/*",
|
||||
"e2e/**/*"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user