Skip to content

Commit 25942eb

Browse files
test: add tests for Swagger UI disabling
1 parent 8b2c6a3 commit 25942eb

File tree

1 file changed

+69
-10
lines changed

1 file changed

+69
-10
lines changed

e2e/express.e2e-spec.ts

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('Express Swagger', () => {
8686
);
8787
SwaggerModule.setup(SWAGGER_RELATIVE_URL, app, swaggerDocument, {
8888
// to showcase that in new implementation u can use custom swagger-ui path. Useful when using e.g. webpack
89-
customSwaggerUiPath: path.resolve(`./node_modules/swagger-ui-dist`),
89+
customSwaggerUiPath: path.resolve(`./node_modules/swagger-ui-dist`)
9090
});
9191

9292
await app.init();
@@ -114,6 +114,55 @@ describe('Express Swagger', () => {
114114
});
115115
});
116116

117+
describe('disabled Swagger UI but served JSON/YAML definitions', () => {
118+
const SWAGGER_RELATIVE_URL = '/apidoc';
119+
120+
beforeEach(async () => {
121+
const swaggerDocument = SwaggerModule.createDocument(
122+
app,
123+
builder.build()
124+
);
125+
SwaggerModule.setup(SWAGGER_RELATIVE_URL, app, swaggerDocument, {
126+
swaggerUiEnabled: false
127+
});
128+
129+
await app.init();
130+
});
131+
132+
afterEach(async () => {
133+
await app.close();
134+
});
135+
136+
it('should serve the JSON definition file', async () => {
137+
const response = await request(app.getHttpServer()).get(
138+
`${SWAGGER_RELATIVE_URL}-json`
139+
);
140+
141+
expect(response.status).toEqual(200);
142+
expect(Object.keys(response.body).length).toBeGreaterThan(0);
143+
});
144+
145+
it('should serve the YAML definition file', async () => {
146+
const response = await request(app.getHttpServer()).get(
147+
`${SWAGGER_RELATIVE_URL}-yaml`
148+
);
149+
150+
expect(response.status).toEqual(200);
151+
expect(response.text.length).toBeGreaterThan(0);
152+
});
153+
154+
it.each([
155+
'/apidoc',
156+
'/apidoc/',
157+
'/apidoc/swagger-ui-bundle.js',
158+
'/apidoc/swagger-ui-init.js'
159+
])('should not serve "%s"', async (file) => {
160+
const response = await request(app.getHttpServer()).get(file);
161+
162+
expect(response.status).toEqual(404);
163+
});
164+
});
165+
117166
describe('custom documents endpoints', () => {
118167
const JSON_CUSTOM_URL = '/apidoc-json';
119168
const YAML_CUSTOM_URL = '/apidoc-yaml';
@@ -154,10 +203,10 @@ describe('Express Swagger', () => {
154203
`${JSON_CUSTOM_URL}?description=My%20custom%20description`
155204
);
156205

157-
expect(response.body.info.description).toBe("My custom description");
206+
expect(response.body.info.description).toBe('My custom description');
158207
});
159208

160-
it('yaml document should be server in the custom url', async () => {
209+
it('yaml document should be served in the custom url', async () => {
161210
const response = await request(app.getHttpServer()).get(YAML_CUSTOM_URL);
162211

163212
expect(response.status).toEqual(200);
@@ -168,7 +217,7 @@ describe('Express Swagger', () => {
168217
const response = await request(app.getHttpServer()).get(
169218
`${YAML_CUSTOM_URL}?description=My%20custom%20description`
170219
);
171-
expect(response.text).toContain("My custom description");
220+
expect(response.text).toContain('My custom description');
172221
});
173222
});
174223

@@ -244,13 +293,17 @@ describe('Express Swagger', () => {
244293
customfavIcon: CUSTOM_FAVICON,
245294
customSiteTitle: CUSTOM_SITE_TITLE,
246295
customCssUrl: CUSTOM_CSS_URL,
247-
patchDocumentOnRequest<ExpressRequest, ExpressResponse> (req, res, document) {
296+
patchDocumentOnRequest<ExpressRequest, ExpressResponse>(
297+
req,
298+
res,
299+
document
300+
) {
248301
return {
249302
...document,
250303
info: {
251304
description: req.query.description
252305
}
253-
}
306+
};
254307
}
255308
});
256309

@@ -313,23 +366,29 @@ describe('Express Swagger', () => {
313366
);
314367

315368
SwaggerModule.setup('/:customer/', app, swaggerDocument, {
316-
patchDocumentOnRequest<ExpressRequest, ExpressResponse> (req, res, document) {
369+
patchDocumentOnRequest<ExpressRequest, ExpressResponse>(
370+
req,
371+
res,
372+
document
373+
) {
317374
return {
318375
...document,
319376
info: {
320377
description: `${req.params.customer}'s API documentation`
321378
}
322-
}
379+
};
323380
}
324381
});
325382

326383
await app.init();
327384

328-
const response: Response = await request(app.getHttpServer()).get('/customer-1/swagger-ui-init.js');
385+
const response: Response = await request(app.getHttpServer()).get(
386+
'/customer-1/swagger-ui-init.js'
387+
);
329388

330389
await app.close();
331390
expect(response.text).toContain("customer-1's API documentation");
332-
})
391+
});
333392

334393
afterEach(async () => {
335394
await app.close();

0 commit comments

Comments
 (0)