Skip to content

Commit

Permalink
fix: #469 - Response validation skipped on status codes >=400
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Jan 15, 2021
1 parent cb7792d commit 57d3c0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/framework/modded.express.mung.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions test/resources/response.validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions test/response.validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}));

Expand Down

0 comments on commit 57d3c0e

Please sign in to comment.