From 57d3c0ee6e160e0b2c856bdf4ebb6755ac8b1305 Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Thu, 14 Jan 2021 20:21:49 -0500 Subject: [PATCH] fix: #469 - Response validation skipped on status codes >=400 --- src/framework/modded.express.mung.ts | 2 +- test/resources/response.validation.yaml | 15 +++++++++++++++ test/response.validation.spec.ts | 19 +++++++++++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/framework/modded.express.mung.ts b/src/framework/modded.express.mung.ts index 298a07e6..fc6c0a51 100644 --- a/src/framework/modded.express.mung.ts +++ b/src/framework/modded.express.mung.ts @@ -32,7 +32,7 @@ mung.json = function json(fn, options) { let originalJson = json; res.json = original; if (res.headersSent) return res; - if (!mungError && res.statusCode >= 400) return original.call(this, json); + if (!mungError && res.statusCode >= 500) return original.call(this, json); // Run the munger try { diff --git a/test/resources/response.validation.yaml b/test/resources/response.validation.yaml index a33f2b1c..550c4b8e 100644 --- a/test/resources/response.validation.yaml +++ b/test/resources/response.validation.yaml @@ -14,6 +14,21 @@ info: servers: - url: /v1 paths: + /error: + get: + responses: + '400': + description: empty + content: + application/json: + schema: + type: object + required: + - oops + properties: + oops: + type: string + /ref_response_body: get: operationId: refResponseBody diff --git a/test/response.validation.spec.ts b/test/response.validation.spec.ts index f3e2647d..377140f2 100644 --- a/test/response.validation.spec.ts +++ b/test/response.validation.spec.ts @@ -19,11 +19,17 @@ describe(packageJson.name, () => { }, 3005, (app) => { + app.get(`${app.basePath}/error`, (req, res) => { + return res.status(400).json({ + message: 'test', + code: 400, + }); + }); app.get(`${app.basePath}/ref_response_body`, (req, res) => { return res.json({ id: 213, name: 'name', kids: [] }); }); app.get(`${app.basePath}/empty_response`, (req, res) => { - return res.status(204).end(); + return res.status(204).send(); }); app.get(`${app.basePath}/boolean`, (req, res) => { return res.json(req.query.value); @@ -144,7 +150,7 @@ describe(packageJson.name, () => { expect(r.body).to.have.property('code').that.equals(500); })); - it('should fail if response is response is empty', async () => + it('should fail if response is empty', async () => request(app) .get(`${app.basePath}/pets?mode=empty_response`) .expect(500) @@ -153,11 +159,20 @@ describe(packageJson.name, () => { expect(r.body).to.have.property('code').that.equals(500); })); + it('should return throw 500 on invalid error response', async () => + request(app) + .get(`${app.basePath}/error`) + .expect(500) + .then((r) => { + expect(r.body.message).to.include('required property'); + })); + it('should return 204 for endpoints that return empty response', async () => request(app) .get(`${app.basePath}/empty_response`) .expect(204) .then((r) => { + console.log(r.body); expect(r.body).to.be.empty; }));