From f86c1783b739e1f495405a9e86af1e4326468ef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Tue, 5 Jan 2010 22:37:51 +0100 Subject: [PATCH] Handle bad requests in simplified parser API --- lib/multipart.js | 10 +++++++++- test/mjsunit/test-multipart.js | 9 +++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/multipart.js b/lib/multipart.js index 3f74af9f62..af6f51fc1a 100644 --- a/lib/multipart.js +++ b/lib/multipart.js @@ -1,9 +1,17 @@ var sys = require("sys"); exports.parse = function(options) { - var stream = new exports.Stream(options); var promise = new process.Promise(); + try { + var stream = new exports.Stream(options); + } catch (e) { + setTimeout(function() { + promise.emitError(e); + }, 0); + return promise; + } + var parts = {}; stream.addListener('part', function(part) { var name = part.name; diff --git a/test/mjsunit/test-multipart.js b/test/mjsunit/test-multipart.js index 7a651790b6..092379c470 100644 --- a/test/mjsunit/test-multipart.js +++ b/test/mjsunit/test-multipart.js @@ -12,7 +12,7 @@ var badRequests = 0; var parts = {}; var respond = function(res, text) { requests++; - if (requests == 3) { + if (requests == 4) { server.close(); } @@ -29,6 +29,7 @@ var server = http.createServer(function(req, res) { }) .addErrback(function() { badRequests++; + respond(res, 'no thanks'); }); return; } @@ -88,9 +89,13 @@ var badRequest = client.request('POST', '/', {'Content-Type': 'something', 'Cont badRequest.sendBody(fixture.reply, 'binary'); badRequest.finish(); +var simpleBadRequest = client.request('POST', '/', {'X-Use-Simple-Api': 'yes', 'Content-Type': 'something', 'Content-Length': fixture.reply.length}); +simpleBadRequest.sendBody(fixture.reply, 'binary'); +simpleBadRequest.finish(); + process.addListener('exit', function() { puts("done"); assert.equal(2, parts_complete); assert.equal(2, parts_reveived); - assert.equal(1, badRequests); + assert.equal(2, badRequests); }); \ No newline at end of file