-
-
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.
Merge pull request #10484 from thiagomini/feature/8844-api-version-in…
…-route-info Feature/8844 api version in route info
- Loading branch information
Showing
8 changed files
with
251 additions
and
38 deletions.
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
integration/hello-world/e2e/middleware-with-versioning.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,147 @@ | ||
import { | ||
Controller, | ||
Get, | ||
INestApplication, | ||
MiddlewareConsumer, | ||
Module, | ||
RequestMethod, | ||
Version, | ||
VersioningOptions, | ||
VersioningType, | ||
VERSION_NEUTRAL, | ||
} from '@nestjs/common'; | ||
import { CustomVersioningOptions } from '@nestjs/common/interfaces'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { AppModule } from '../src/app.module'; | ||
|
||
const RETURN_VALUE = 'test'; | ||
const VERSIONED_VALUE = 'test_versioned'; | ||
|
||
@Controller() | ||
class TestController { | ||
@Version('1') | ||
@Get('versioned') | ||
versionedTest() { | ||
return RETURN_VALUE; | ||
} | ||
} | ||
|
||
@Module({ | ||
imports: [AppModule], | ||
controllers: [TestController], | ||
}) | ||
class TestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer | ||
.apply((req, res, next) => res.send(VERSIONED_VALUE)) | ||
.forRoutes({ | ||
path: '/versioned', | ||
version: '1', | ||
method: RequestMethod.ALL, | ||
}); | ||
} | ||
} | ||
|
||
describe('Middleware', () => { | ||
let app: INestApplication; | ||
|
||
describe('when using default URI versioning', () => { | ||
beforeEach(async () => { | ||
app = await createAppWithVersioning({ | ||
type: VersioningType.URI, | ||
defaultVersion: VERSION_NEUTRAL, | ||
}); | ||
}); | ||
|
||
it(`forRoutes({ path: '/versioned', version: '1', method: RequestMethod.ALL })`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/v1/versioned') | ||
.expect(200, VERSIONED_VALUE); | ||
}); | ||
}); | ||
|
||
describe('when default URI versioning with an alternative prefix', () => { | ||
beforeEach(async () => { | ||
app = await createAppWithVersioning({ | ||
type: VersioningType.URI, | ||
defaultVersion: VERSION_NEUTRAL, | ||
prefix: 'version', | ||
}); | ||
}); | ||
|
||
it(`forRoutes({ path: '/versioned', version: '1', method: RequestMethod.ALL })`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/version1/versioned') | ||
.expect(200, VERSIONED_VALUE); | ||
}); | ||
}); | ||
|
||
describe('when using HEADER versioning', () => { | ||
beforeEach(async () => { | ||
app = await createAppWithVersioning({ | ||
type: VersioningType.HEADER, | ||
header: 'version', | ||
}); | ||
}); | ||
|
||
it(`forRoutes({ path: '/versioned', version: '1', method: RequestMethod.ALL })`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/versioned') | ||
.set('version', '1') | ||
.expect(200, VERSIONED_VALUE); | ||
}); | ||
}); | ||
|
||
describe('when using MEDIA TYPE versioning', () => { | ||
beforeEach(async () => { | ||
app = await createAppWithVersioning({ | ||
type: VersioningType.MEDIA_TYPE, | ||
key: 'v', | ||
defaultVersion: VERSION_NEUTRAL, | ||
}); | ||
}); | ||
|
||
it(`forRoutes({ path: '/versioned', version: '1', method: RequestMethod.ALL })`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/versioned') | ||
.expect(200, VERSIONED_VALUE); | ||
}); | ||
}); | ||
|
||
describe('when using CUSTOM TYPE versioning', () => { | ||
beforeEach(async () => { | ||
const extractor: CustomVersioningOptions['extractor'] = () => '1'; | ||
|
||
app = await createAppWithVersioning({ | ||
type: VersioningType.CUSTOM, | ||
extractor, | ||
}); | ||
}); | ||
|
||
it(`forRoutes({ path: '/versioned', version: '1', method: RequestMethod.ALL })`, () => { | ||
return request(app.getHttpServer()) | ||
.get('/versioned') | ||
.expect(200, VERSIONED_VALUE); | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); | ||
|
||
async function createAppWithVersioning( | ||
versioningOptions: VersioningOptions, | ||
): Promise<INestApplication> { | ||
const app = ( | ||
await Test.createTestingModule({ | ||
imports: [TestModule], | ||
}).compile() | ||
).createNestApplication(); | ||
|
||
app.enableVersioning(versioningOptions); | ||
await app.init(); | ||
|
||
return app; | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/common/interfaces/middleware/middleware-configuration.interface.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
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.