Browse Source

Multipart test now uses a fixture instead of CURL

v0.7.4-release
Felix Geisendörfer 15 years ago
committed by Ryan Dahl
parent
commit
abbc624f52
  1. 20
      lib/multipart.js
  2. 11
      test/mjsunit/fixtures/multipart.js
  3. 20
      test/mjsunit/test-multipart.js
  4. 2
      test/mjsunit/test-readdir.js

20
lib/multipart.js

@ -4,7 +4,7 @@ exports.parse = function(options) {
var parts = {}; var parts = {};
stream.addListener('part', function(part) { stream.addListener('part', function(part) {
var name = part.headers['Content-Disposition'].name; var name = part.headers['content-disposition'].name;
var buffer = ''; var buffer = '';
part.addListener('body', function(chunk) { part.addListener('body', function(chunk) {
@ -23,6 +23,18 @@ exports.parse = function(options) {
return promise; return promise;
}; };
// Temporarly until http.ServerRequest.headers are case-insensitive
function getHeader(name, headers) {
var val;
for (var header in headers) {
if (header.toLowerCase() === name.toLowerCase()) {
val = headers[header];
break;
}
}
return val;
};
exports.Stream = function(options) { exports.Stream = function(options) {
node.EventEmitter.call(this); node.EventEmitter.call(this);
@ -40,13 +52,13 @@ proto.init = function(options) {
this.part = null; this.part = null;
if ('headers' in options) { if ('headers' in options) {
var req = options, contentType = req.headers['Content-Type']; var req = options, contentType = getHeader('content-type', req.headers);
if (contentType) { if (contentType) {
contentType = contentType.split(/; ?boundary=/) contentType = contentType.split(/; ?boundary=/)
this.boundary = '--'+contentType[1]; this.boundary = '--'+contentType[1];
} }
this.bytesTotal = req.headers['Content-Length']; this.bytesTotal = getHeader('content-length', req.headers);
var self = this; var self = this;
req req
@ -152,7 +164,7 @@ Part.prototype.write = function(chunk) {
return; return;
} else if (offset > 0) { } else if (offset > 0) {
var header = this.buffer.substr(0, offset).split(/: ?/); var header = this.buffer.substr(0, offset).split(/: ?/);
this.headers[header[0]] = header[1]; this.headers[header[0].toLowerCase()] = header[1];
this.buffer = this.buffer.substr(offset+2); this.buffer = this.buffer.substr(offset+2);
} else if (offset === false) { } else if (offset === false) {
return; return;

11
test/mjsunit/fixtures/multipart.js

@ -0,0 +1,11 @@
exports.reply = ["--AaB03x\r",
"content-disposition: form-data; name=\"reply\"\r",
"\r",
"yes\r",
"--AaB03x\r",
"content-disposition: form-data; name=\"fileupload\"; filename=\"dj.jpg\"\r",
"Content-Type: image/jpeg\r",
"Content-Transfer-Encoding: base64\r",
"\r",
"/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg\r",
"--AaB03x--\r\n"].join("\n");

20
test/mjsunit/test-multipart.js

@ -2,6 +2,8 @@ node.mixin(require("common.js"));
http = require("/http.js"); http = require("/http.js");
var multipart = require('/multipart.js'); var multipart = require('/multipart.js');
var fixture = require('fixtures/multipart.js');
var port = 8222; var port = 8222;
var parts_reveived = 0; var parts_reveived = 0;
var parts_complete = 0; var parts_complete = 0;
@ -13,12 +15,12 @@ var server = http.createServer(function(req, res) {
stream.addListener('part', function(part) { stream.addListener('part', function(part) {
parts_reveived++; parts_reveived++;
var name = part.headers['Content-Disposition'].name; var name = part.headers['content-disposition'].name;
if (parts_reveived == 1) { if (parts_reveived == 1) {
assertEquals('test-field', name); assertEquals('reply', name);
} else if (parts_reveived == 2) { } else if (parts_reveived == 2) {
assertEquals('test-file', name); assertEquals('fileupload', name);
} }
parts[name] = ''; parts[name] = '';
@ -27,9 +29,9 @@ var server = http.createServer(function(req, res) {
}); });
part.addListener('complete', function(chunk) { part.addListener('complete', function(chunk) {
if (parts_reveived == 1) { if (parts_reveived == 1) {
assertEquals('foobar', parts[name]); assertEquals('yes', parts[name]);
} else if (parts_reveived == 2) { } else if (parts_reveived == 2) {
assertEquals(node.fs.cat(__filename).wait(), parts[name]); assertEquals('/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg', parts[name]);
} }
parts_complete++; parts_complete++;
}); });
@ -44,9 +46,13 @@ var server = http.createServer(function(req, res) {
}); });
server.listen(port); server.listen(port);
var cmd = 'curl -H Expect: -F test-field=foobar -F test-file=@'+__filename+' http://localhost:'+port+'/'; var client = http.createClient(port);
var result = exec(cmd).wait(); var request = client.post('/', {'Content-Type': 'multipart/form-data; boundary=AaB03x', 'Content-Length': fixture.reply.length});
request.sendBody(fixture.reply, 'binary');
request.finish();
process.addListener('exit', function() { process.addListener('exit', function() {
puts("done");
assertEquals(2, parts_complete); assertEquals(2, parts_complete);
assertEquals(2, parts_reveived);
}); });

2
test/mjsunit/test-readdir.js

@ -7,7 +7,7 @@ puts("readdir " + fixturesDir);
promise.addCallback(function (files) { promise.addCallback(function (files) {
p(files); p(files);
assertArrayEquals(["a.js", "b","x.txt"], files.sort()); assertArrayEquals(["a.js", "b", "multipart.js", "x.txt"], files.sort());
}); });
promise.addErrback(function () { promise.addErrback(function () {

Loading…
Cancel
Save