Browse Source

Clean up multipart test case

v0.7.4-release
Felix Geisendörfer 15 years ago
committed by Ryan Dahl
parent
commit
7d947f8723
  1. 74
      test/mjsunit/test-multipart.js

74
test/mjsunit/test-multipart.js

@ -1,16 +1,18 @@
process.mixin(require("./common")); process.mixin(require("./common"));
http = require("http"); http = require("http");
var multipart = require('multipart'); var
var fixture = require('./fixtures/multipart'); PORT = 8222,
var port = 8222; multipart = require('multipart'),
var parts_reveived = 0; fixture = require('./fixtures/multipart'),
var parts_complete = 0;
var requests = 0; requests = 0,
var badRequests = 0; badRequests = 0,
var parts = {}; partsReceived = 0,
var respond = function(res, text) { partsComplete = 0,
respond = function(res, text) {
requests++; requests++;
if (requests == 4) { if (requests == 4) {
server.close(); server.close();
@ -19,7 +21,7 @@ var respond = function(res, text) {
res.sendHeader(200, {"Content-Type": "text/plain"}); res.sendHeader(200, {"Content-Type": "text/plain"});
res.sendBody(text); res.sendBody(text);
res.finish(); res.finish();
}; };
var server = http.createServer(function(req, res) { var server = http.createServer(function(req, res) {
if (req.headers['x-use-simple-api']) { if (req.headers['x-use-simple-api']) {
@ -43,14 +45,15 @@ var server = http.createServer(function(req, res) {
return; return;
} }
var parts = {};
stream.addListener('part', function(part) { stream.addListener('part', function(part) {
parts_reveived++; partsReceived++;
var name = part.name; var name = part.name;
if (parts_reveived == 1) { if (partsReceived == 1) {
assert.equal('reply', name); assert.equal('reply', name);
} else if (parts_reveived == 2) { } else if (partsReceived == 2) {
assert.equal('fileupload', name); assert.equal('fileupload', name);
} }
@ -60,12 +63,15 @@ var server = http.createServer(function(req, res) {
}); });
part.addListener('complete', function(chunk) { part.addListener('complete', function(chunk) {
assert.equal(0, part.buffer.length); assert.equal(0, part.buffer.length);
if (parts_reveived == 1) { if (partsReceived == 1) {
assert.equal('yes', parts[name]); assert.equal('yes', parts[name]);
} else if (parts_reveived == 2) { } else if (partsReceived == 2) {
assert.equal('/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg', parts[name]); assert.equal(
'/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg',
parts[name]
);
} }
parts_complete++; partsComplete++;
}); });
}); });
@ -73,29 +79,43 @@ var server = http.createServer(function(req, res) {
respond(res, 'thanks'); respond(res, 'thanks');
}); });
}); });
server.listen(port); server.listen(PORT);
var client = http.createClient(PORT);
var client = http.createClient(port); var request = client.request('POST', '/', {
var request = client.request('POST', '/', {'Content-Type': 'multipart/form-data; boundary=AaB03x', 'Content-Length': fixture.reply.length}); 'Content-Type': 'multipart/form-data; boundary=AaB03x',
'Content-Length': fixture.reply.length
});
request.sendBody(fixture.reply, 'binary'); request.sendBody(fixture.reply, 'binary');
request.finish(); request.finish();
var client = http.createClient(port); var simpleRequest = client.request('POST', '/', {
var simpleRequest = client.request('POST', '/', {'X-Use-Simple-Api': 'yes', 'Content-Type': 'multipart/form-data; boundary=AaB03x', 'Content-Length': fixture.reply.length}); 'X-Use-Simple-Api': 'yes',
'Content-Type': 'multipart/form-data; boundary=AaB03x',
'Content-Length': fixture.reply.length
});
simpleRequest.sendBody(fixture.reply, 'binary'); simpleRequest.sendBody(fixture.reply, 'binary');
simpleRequest.finish(); simpleRequest.finish();
var badRequest = client.request('POST', '/', {'Content-Type': 'something', 'Content-Length': fixture.reply.length}); var badRequest = client.request('POST', '/', {
'Content-Type': 'invalid!',
'Content-Length': fixture.reply.length
});
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}); var simpleBadRequest = client.request('POST', '/', {
'X-Use-Simple-Api': 'yes',
'Content-Type': 'something',
'Content-Length': fixture.reply.length
});
simpleBadRequest.sendBody(fixture.reply, 'binary'); simpleBadRequest.sendBody(fixture.reply, 'binary');
simpleBadRequest.finish(); simpleBadRequest.finish();
process.addListener('exit', function() { process.addListener('exit', function() {
puts("done"); puts("done");
assert.equal(2, parts_complete); assert.equal(2, partsComplete);
assert.equal(2, parts_reveived); assert.equal(2, partsReceived);
assert.equal(2, badRequests); assert.equal(2, badRequests);
}); });
Loading…
Cancel
Save