Browse Source

Handle bad requests in simplified parser API

v0.7.4-release
Felix Geisendörfer 15 years ago
parent
commit
f86c1783b7
  1. 10
      lib/multipart.js
  2. 9
      test/mjsunit/test-multipart.js

10
lib/multipart.js

@ -1,9 +1,17 @@
var sys = require("sys"); var sys = require("sys");
exports.parse = function(options) { exports.parse = function(options) {
var stream = new exports.Stream(options);
var promise = new process.Promise(); var promise = new process.Promise();
try {
var stream = new exports.Stream(options);
} catch (e) {
setTimeout(function() {
promise.emitError(e);
}, 0);
return promise;
}
var parts = {}; var parts = {};
stream.addListener('part', function(part) { stream.addListener('part', function(part) {
var name = part.name; var name = part.name;

9
test/mjsunit/test-multipart.js

@ -12,7 +12,7 @@ var badRequests = 0;
var parts = {}; var parts = {};
var respond = function(res, text) { var respond = function(res, text) {
requests++; requests++;
if (requests == 3) { if (requests == 4) {
server.close(); server.close();
} }
@ -29,6 +29,7 @@ var server = http.createServer(function(req, res) {
}) })
.addErrback(function() { .addErrback(function() {
badRequests++; badRequests++;
respond(res, 'no thanks');
}); });
return; return;
} }
@ -88,9 +89,13 @@ var badRequest = client.request('POST', '/', {'Content-Type': 'something', 'Cont
badRequest.sendBody(fixture.reply, 'binary'); badRequest.sendBody(fixture.reply, 'binary');
badRequest.finish(); 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() { process.addListener('exit', function() {
puts("done"); puts("done");
assert.equal(2, parts_complete); assert.equal(2, parts_complete);
assert.equal(2, parts_reveived); assert.equal(2, parts_reveived);
assert.equal(1, badRequests); assert.equal(2, badRequests);
}); });
Loading…
Cancel
Save