-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): routing platform independence (polishing)
- Loading branch information
Showing
8 changed files
with
337 additions
and
443 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import * as SwaggerParser from 'swagger-parser'; | ||
import { DocumentBuilder, SwaggerModule } from '../lib'; | ||
import { ApplicationModule } from './src/app.module'; | ||
import { | ||
ExpressAdapter, | ||
NestExpressApplication | ||
} from '@nestjs/platform-express'; | ||
|
||
describe('Express Swagger', () => { | ||
let app: NestExpressApplication; | ||
let builder: DocumentBuilder; | ||
|
||
beforeEach(async () => { | ||
app = await NestFactory.create<NestExpressApplication>( | ||
ApplicationModule, | ||
new ExpressAdapter(), | ||
{ logger: false } | ||
); | ||
|
||
builder = new DocumentBuilder() | ||
.setTitle('Cats example') | ||
.setDescription('The cats API description') | ||
.setVersion('1.0') | ||
.addTag('cats') | ||
.addBasicAuth() | ||
.addBearerAuth() | ||
.addOAuth2() | ||
.addApiKey() | ||
.addApiKey({ type: 'apiKey' }, 'key1') | ||
.addApiKey({ type: 'apiKey' }, 'key2') | ||
.addCookieAuth() | ||
.addSecurityRequirements('bearer') | ||
.addSecurityRequirements({ basic: [], cookie: [] }); | ||
}); | ||
|
||
it('should produce a valid OpenAPI 3.0 schema', async () => { | ||
const document = SwaggerModule.createDocument(app, builder.build()); | ||
const doc = JSON.stringify(document, null, 2); | ||
|
||
try { | ||
let api = await SwaggerParser.validate(document as any); | ||
console.log( | ||
'API name: %s, Version: %s', | ||
api.info.title, | ||
api.info.version | ||
); | ||
expect(api.info.title).toEqual('Cats example'); | ||
} catch (err) { | ||
console.log(doc); | ||
expect(err).toBeUndefined(); | ||
} | ||
}); | ||
|
||
it('should fix colons in url', async () => { | ||
const document = SwaggerModule.createDocument(app, builder.build()); | ||
expect(document.paths['/express:colon:another/{prop}']).toBeDefined(); | ||
}); | ||
|
||
it('should setup multiple routes', async () => { | ||
const document1 = SwaggerModule.createDocument(app, builder.build()); | ||
SwaggerModule.setup('/swagger1', app, document1); | ||
|
||
const document2 = SwaggerModule.createDocument(app, builder.build()); | ||
SwaggerModule.setup('/swagger2', app, document2); | ||
|
||
await app.init(); | ||
expect(app.getHttpAdapter().getInstance()).toBeDefined(); | ||
}); | ||
}); |
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.