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