From 25942eb3b47f2a56f9f55681fc9f0236012559ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Gr=C3=A9goire?= Date: Wed, 7 Feb 2024 18:00:32 +0100 Subject: [PATCH] test: add tests for Swagger UI disabling --- e2e/express.e2e-spec.ts | 79 +++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 10 deletions(-) diff --git a/e2e/express.e2e-spec.ts b/e2e/express.e2e-spec.ts index 5a3f11677..25610c17a 100644 --- a/e2e/express.e2e-spec.ts +++ b/e2e/express.e2e-spec.ts @@ -86,7 +86,7 @@ describe('Express Swagger', () => { ); SwaggerModule.setup(SWAGGER_RELATIVE_URL, app, swaggerDocument, { // to showcase that in new implementation u can use custom swagger-ui path. Useful when using e.g. webpack - customSwaggerUiPath: path.resolve(`./node_modules/swagger-ui-dist`), + customSwaggerUiPath: path.resolve(`./node_modules/swagger-ui-dist`) }); await app.init(); @@ -114,6 +114,55 @@ describe('Express Swagger', () => { }); }); + describe('disabled Swagger UI but served JSON/YAML definitions', () => { + const SWAGGER_RELATIVE_URL = '/apidoc'; + + beforeEach(async () => { + const swaggerDocument = SwaggerModule.createDocument( + app, + builder.build() + ); + SwaggerModule.setup(SWAGGER_RELATIVE_URL, app, swaggerDocument, { + swaggerUiEnabled: false + }); + + await app.init(); + }); + + afterEach(async () => { + await app.close(); + }); + + it('should serve the JSON definition file', async () => { + const response = await request(app.getHttpServer()).get( + `${SWAGGER_RELATIVE_URL}-json` + ); + + expect(response.status).toEqual(200); + expect(Object.keys(response.body).length).toBeGreaterThan(0); + }); + + it('should serve the YAML definition file', async () => { + const response = await request(app.getHttpServer()).get( + `${SWAGGER_RELATIVE_URL}-yaml` + ); + + expect(response.status).toEqual(200); + expect(response.text.length).toBeGreaterThan(0); + }); + + it.each([ + '/apidoc', + '/apidoc/', + '/apidoc/swagger-ui-bundle.js', + '/apidoc/swagger-ui-init.js' + ])('should not serve "%s"', async (file) => { + const response = await request(app.getHttpServer()).get(file); + + expect(response.status).toEqual(404); + }); + }); + describe('custom documents endpoints', () => { const JSON_CUSTOM_URL = '/apidoc-json'; const YAML_CUSTOM_URL = '/apidoc-yaml'; @@ -154,10 +203,10 @@ describe('Express Swagger', () => { `${JSON_CUSTOM_URL}?description=My%20custom%20description` ); - expect(response.body.info.description).toBe("My custom description"); + expect(response.body.info.description).toBe('My custom description'); }); - it('yaml document should be server in the custom url', async () => { + it('yaml document should be served in the custom url', async () => { const response = await request(app.getHttpServer()).get(YAML_CUSTOM_URL); expect(response.status).toEqual(200); @@ -168,7 +217,7 @@ describe('Express Swagger', () => { const response = await request(app.getHttpServer()).get( `${YAML_CUSTOM_URL}?description=My%20custom%20description` ); - expect(response.text).toContain("My custom description"); + expect(response.text).toContain('My custom description'); }); }); @@ -244,13 +293,17 @@ describe('Express Swagger', () => { customfavIcon: CUSTOM_FAVICON, customSiteTitle: CUSTOM_SITE_TITLE, customCssUrl: CUSTOM_CSS_URL, - patchDocumentOnRequest (req, res, document) { + patchDocumentOnRequest( + req, + res, + document + ) { return { ...document, info: { description: req.query.description } - } + }; } }); @@ -313,23 +366,29 @@ describe('Express Swagger', () => { ); SwaggerModule.setup('/:customer/', app, swaggerDocument, { - patchDocumentOnRequest (req, res, document) { + patchDocumentOnRequest( + req, + res, + document + ) { return { ...document, info: { description: `${req.params.customer}'s API documentation` } - } + }; } }); await app.init(); - const response: Response = await request(app.getHttpServer()).get('/customer-1/swagger-ui-init.js'); + const response: Response = await request(app.getHttpServer()).get( + '/customer-1/swagger-ui-init.js' + ); await app.close(); expect(response.text).toContain("customer-1's API documentation"); - }) + }); afterEach(async () => { await app.close();