From 90061b5a42a0a19be3f82a7cf8cd630ea5245602 Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Sun, 17 Jan 2021 13:41:11 -0500 Subject: [PATCH] test: add test for default response >=400 --- .../response.validation.defaults.yaml | 42 ++++++++++++++ test/response.validation.defaults.spec.ts | 56 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 test/resources/response.validation.defaults.yaml create mode 100644 test/response.validation.defaults.spec.ts diff --git a/test/resources/response.validation.defaults.yaml b/test/resources/response.validation.defaults.yaml new file mode 100644 index 00000000..518adc85 --- /dev/null +++ b/test/resources/response.validation.defaults.yaml @@ -0,0 +1,42 @@ +openapi: '3.0.0' +info: + version: 1.0.0 + title: Sample +servers: + - url: /v1 +paths: + /default_inline: + get: + parameters: + - name: q + in: 'query' + required: true + schema: + type: string + responses: + '200': + description: Success + content: + 'application/json': + schema: + type: object + properties: + data: + description: Some data + type: string + required: + - data + default: + description: Unexpected error + content: + 'application/json': + schema: + required: + - code + - message + properties: + code: + type: integer + format: int32 + message: + type: string \ No newline at end of file diff --git a/test/response.validation.defaults.spec.ts b/test/response.validation.defaults.spec.ts new file mode 100644 index 00000000..84bf385c --- /dev/null +++ b/test/response.validation.defaults.spec.ts @@ -0,0 +1,56 @@ +import * as path from 'path'; +import { expect } from 'chai'; +import * as request from 'supertest'; +import { createApp } from './common/app'; + +const apiSpecPath = path.join( + 'test', + 'resources', + 'response.validation.defaults.yaml', +); + +describe('response validation with type coercion', () => { + let app = null; + + before(async () => { + // set up express app + app = await createApp( + { + apiSpec: apiSpecPath, + validateResponses: true, + }, + 3005, + (app) => { + app.get(`${app.basePath}/default_inline`, (req, res) => { + const q = req.query.q; + + if (q === '200') { + return res.status(200).json({ data: 'good' }); + } else if (q === '400') { + return res.status(400).json({ message: 'message', code: 400 }); + } else if (q === '400_bad') { + return res.status(400).json({ bad: 'malformed' }); + } + }); + }, + ); + }); + + after(() => { + app.server.close(); + }); + + it('should validate 200 using explicit response', async () => + request(app).get(`${app.basePath}/default_inline?q=200`).expect(200)); + + it('should validate undeclared 400 using default response', async () => + request(app).get(`${app.basePath}/default_inline?q=400`).expect(400)); + + it('should validate undeclared 400 using default response', async () => + request(app) + .get(`${app.basePath}/default_inline?q=400_bad`) + .expect(500) + .then((r) => { + expect(r.body.message).to.include('should have required property'); + })); +});