Skip to content

Commit

Permalink
Closes hapijs#830
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed May 7, 2013
1 parent 5270085 commit 26143a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<p></p>
- `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:
Expand Down
6 changes: 3 additions & 3 deletions lib/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -127,7 +127,7 @@ exports.read = function (request, next) {

request.rawPayload = payload;

if (level !== 'parse') { // 'raw'
if (level === 'raw') {
return finish();
}

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
26 changes: 21 additions & 5 deletions test/integration/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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) {

Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 26143a1

Please sign in to comment.