-
Notifications
You must be signed in to change notification settings - Fork 24
Fixed bug in Response validator #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,22 @@ const expect = require('chai').expect | |
| describe('express joi', function() { | ||
| var schema, mod | ||
|
|
||
| function getRequester(middleware) { | ||
| const errorHandler = (err, req, res, next) => { | ||
| let fixupResponse = require('./express-joi-validation.js').fixupResponse | ||
| if (err) { | ||
| if (err.error && err.error.isJoi) { | ||
| res.status(422).json({ | ||
| type: err.type, | ||
| message: err.error.toString() | ||
| }) | ||
| } else { | ||
| fixupResponse(res) | ||
| res.status(500).send({ message: err.message }) | ||
| } | ||
| } else next(err) | ||
| } | ||
|
|
||
| function getRequester(middleware, errMiddlware = undefined) { | ||
| const app = require('express')() | ||
|
|
||
| // Must apply params middleware inline with the route to match param names | ||
|
|
@@ -94,6 +109,12 @@ describe('express joi', function() { | |
| const { key } = req.params | ||
| res.json({ key: +key || 'none' }) | ||
| }) | ||
| app.get('/error', middleware, (req, res, next) => { | ||
| next(new Error('an error')) | ||
| }) | ||
| if (errMiddlware) { | ||
| app.use(errMiddlware) | ||
| } | ||
|
|
||
| return supertest(app) | ||
| } | ||
|
|
@@ -248,13 +269,31 @@ describe('express joi', function() { | |
| .expect(200) | ||
| }) | ||
|
|
||
| it('should pass an error to subsequent handler if it is asked', function() { | ||
| it('should pass an error to subsequent handler if it is asked', function(done) { | ||
| const middleware = mod.response(schema, { | ||
| passError: true | ||
| }) | ||
| return getRequester(middleware) | ||
| const res = getRequester(middleware, errorHandler) | ||
| .get('/response/one') | ||
| .expect(500) | ||
| .expect(422) | ||
| .end(function(err, res) { | ||
| expect(res.body.type).to.contain('response') | ||
| expect(res.body.message).to.contain('ValidationError:') | ||
| done() | ||
| }) | ||
| }) | ||
|
|
||
| it('Allow error handler to work if error occured in request handler', function(done) { | ||
| const middleware = mod.response(schema, { | ||
| passError: true | ||
| }) | ||
| const res = getRequester(middleware, errorHandler) | ||
| .get('/error') | ||
| .expect(422) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a 500 error, yet the test is passing. That's odd. Checking I'll need to look into why supertest is not asserting it correctly since it seems to be the same for other tests...
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The errorHandler sets the status code to 422, so I think this works as expected |
||
| .end(function(err, res) { | ||
| expect(res.body.message).to.contain('an error') | ||
| done() | ||
| }) | ||
| }) | ||
|
|
||
| it('should return an alternative status for failure', function() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be best not to expose this. Make the fix-up automatic in all scenarios, including
response, ifpassErroris enabled. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see your last comment. Right, this is a tricky...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is indeed Hacky. Do you have any other suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had this idea stewing in my head around providing schemas for certain status code ranges. It's a bit more complex though. Something like:
If no schema is provided for 5xx, then perhaps validation is bypassed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a very good interface.
It seems to me that it will still need the mechanism of overriding the
jsonfunction on theResponseobject and require restoring the originaljsonfunction