var http = require('http')
, request = require('../index')
var localFile = path.join(__dirname, 'unicycle.jpg')
multipart: [{name: 'field', body: 'value'}]
expected: {chunked: false}
multipart: [{name: 'file', body: null}]
expected: {chunked: true}
headers: {'transfer-encoding': 'chunked'},
multipart: [{name: 'field', body: 'value'}]
expected: {chunked: true}
multipart: {data: [{name: 'field', body: 'value'}]}
expected: {chunked: false}
multipart: {data: [{name: 'file', body: null}]}
expected: {chunked: true}
headers: {'transfer-encoding': 'chunked'},
multipart: {data: [{name: 'field', body: 'value'}]}
expected: {chunked: true}
'+object -chunked -stream': {
multipart: {chunked: false, data: [{name: 'field', body: 'value'}]}
expected: {chunked: false}
'+object -chunked +stream': {
multipart: {chunked: false, data: [{name: 'file', body: null}]}
expected: {chunked: true}
'+object +chunked -stream': {
multipart: {chunked: true, data: [{name: 'field', body: 'value'}]}
expected: {chunked: true}
'+object +encoding -chunked': {
headers: {'transfer-encoding': 'chunked'},
multipart: {chunked: false, data: [{name: 'field', body: 'value'}]}
expected: {chunked: true}
function runTest(t, test) {
var server = http.createServer(function(req, res) {
t.ok(req.headers['content-type'].match(/^multipart\/related; boundary=[^\s;]+$/))
if (test.expected.chunked) {
t.ok(req.headers['transfer-encoding'] === 'chunked')
t.notOk(req.headers['content-length'])
t.ok(req.headers['content-length'])
t.notOk(req.headers['transfer-encoding'])
req.on('data', function(d) {
req.on('end', function() {
if (test.expected.chunked && data.indexOf('name: file') !== -1) {
t.ok(data.indexOf('name: file') !== -1)
t.ok(data.indexOf('2005:06:21 01:44:12') !== -1)
t.ok(data.indexOf('name: field') !== -1)
var parts = test.options.multipart.data || test.options.multipart
t.ok(data.indexOf(parts[0].body) !== -1)
server.listen(6767, function() {
var parts = test.options.multipart.data || test.options.multipart
if (parts[0].name === 'file') {
parts[0].body = fs.createReadStream(localFile)
request.post('http://localhost:6767', test.options, function (err, res, body) {
t.equal(res.statusCode, 200)
server.close(function () {
Object.keys(cases).forEach(function (name) {
tape('multipart-encoding ' + name, function(t) {