From 099e99b69454902818c400ad59d59cb58db8fba5 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Tue, 7 May 2013 07:55:28 -0700 Subject: [PATCH] Closes #830 --- docs/Reference.md | 1 + lib/payload.js | 6 +++--- lib/schema.js | 2 +- test/integration/payload.js | 26 +++++++++++++++++++++----- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/docs/Reference.md b/docs/Reference.md index 99aed86ee..e89a102d2 100755 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -429,6 +429,7 @@ The following options are available when adding a route: - application/x-www-form-urlencoded - multipart/form-data ([formidable](https://npmjs.org/package/formidable) is used for processing this data and is capable of receiving files as well as other form data. All values are assigned to their respective form names in `request.payload`. + - `'try'` - same as `'parse'` but does not return an error on failed parsing. Instead, leaves `request.payload` undefined.

- `cache` - if the route method is 'GET', the route can be configured to use the cache. The `cache` options are described in the [**catbox** module documentation](https://github.com/spumko/catbox#policy) with some additions: diff --git a/lib/payload.js b/lib/payload.js index 34c507ca8..8c5cee507 100755 --- a/lib/payload.js +++ b/lib/payload.js @@ -25,7 +25,7 @@ exports.read = function (request, next) { return next(); } - // Levels are: 'stream', 'raw', 'parse' + // Levels are: 'stream', 'raw', 'parse', 'try' var level = request.route.payload || (request.route.validate.payload || request.method === 'post' || request.method === 'put'|| request.method === 'patch' ? 'parse' : 'stream'); if (level === 'stream') { @@ -127,7 +127,7 @@ exports.read = function (request, next) { request.rawPayload = payload; - if (level !== 'parse') { // 'raw' + if (level === 'raw') { return finish(); } @@ -142,7 +142,7 @@ exports.read = function (request, next) { internals.parse(payload, req.headers, function (err, result) { if (err) { - return finish(err); + return finish(level !== 'try' ? err : null); } request.payload = result; diff --git a/lib/schema.js b/lib/schema.js index d5ad16d82..6b678e47f 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -124,7 +124,7 @@ exports.routeConfig = function (config) { internals.routeConfigSchema = { handler: [T.Object(), T.Function(), T.String().valid('notFound')], - payload: T.String().valid('stream', 'raw', 'parse').nullOk(), + payload: T.String().valid('stream', 'raw', 'parse', 'try').nullOk(), auth: [ T.Object({ mode: T.String().valid(['required', 'optional', 'try']).nullOk(), diff --git a/test/integration/payload.js b/test/integration/payload.js index 582ce9dfc..01da7ccdb 100755 --- a/test/integration/payload.js +++ b/test/integration/payload.js @@ -297,20 +297,26 @@ describe('Payload', function () { describe('parse mode', function () { - var handler = function (request) { + var handler = function () { + + this.reply(this.payload.key); + }; - request.reply(request.payload.key); + var textHandler = function () { + + this.reply(this.payload + '+456'); }; - var textHandler = function (request) { + var tryHandler = function () { - request.reply(request.payload + '+456'); + this.reply(this.rawPayload.toString() + 'failed'); }; var server = new Hapi.Server('localhost', 0, { timeout: { client: 50 } }); server.route({ method: 'POST', path: '/', config: { handler: handler, payload: 'parse' } }); server.route({ method: 'POST', path: '/text', config: { handler: textHandler } }); server.route({ method: '*', path: '/any', handler: handler }); + server.route({ method: 'POST', path: '/try', config: { handler: tryHandler, payload: 'try' } }); before(function (done) { @@ -335,7 +341,7 @@ describe('Payload', function () { this.push(null); }; - it('sets parse mode when route methos is * and request is POST, PATCH or PUT', function (done) { + it('sets parse mode when route methos is * and request is POST', function (done) { server.inject({ url: '/any', method: 'POST', headers: { 'Content-Type': 'application/json' }, payload: '{ "key": "09876" }' }, function (res) { @@ -438,6 +444,16 @@ describe('Payload', function () { done(); }); }); + + it('returns 200 on invalid payload with try mode', function (done) { + + server.inject({ method: 'POST', url: '/try', payload: 'tried but ' }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('tried but failed'); + done(); + }); + }); }); describe('unzip', function () {