Skip to content

Commit

Permalink
Merge pull request #1275 from simov/multipart-mixed
Browse files Browse the repository at this point in the history
Fix multipart content-type headers detection
  • Loading branch information
nylen committed Nov 18, 2014
2 parents da34535 + cd23ef7 commit 7092c68
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion request.js
Expand Up @@ -1430,7 +1430,7 @@ Request.prototype.multipart = function (multipart) {
}

var headerName = self.hasHeader('content-type')
if (!headerName || headerName.indexOf('multipart') === -1) {
if (!headerName || self.headers[headerName].indexOf('multipart') === -1) {
self.setHeader('content-type', 'multipart/related; boundary=' + self.boundary)
} else {
self.setHeader(headerName, self.headers[headerName].split(';')[0] + '; boundary=' + self.boundary)
Expand Down
27 changes: 26 additions & 1 deletion tests/test-multipart.js
Expand Up @@ -19,6 +19,20 @@ function runTest(t, a) {
return
}

if (a.headers) {
t.ok(req.headers['content-type'].match(/multipart\/mixed/))
} else {
t.ok(req.headers['content-type'].match(/multipart\/related/))
}

if (chunked) {
t.ok(req.headers['transfer-encoding'] === 'chunked')
t.notOk(req.headers['content-length'])
} else {
t.ok(req.headers['content-length'])
t.notOk(req.headers['transfer-encoding'])
}

// temp workaround
var data = ''
req.setEncoding('utf8')
Expand Down Expand Up @@ -72,6 +86,7 @@ function runTest(t, a) {

var reqOptions = {
url: 'http://localhost:8080/upload',
headers: (a.headers ? {'content-type': 'multipart/mixed'} : undefined),
multipart: a.array
? multipartData
: {chunked: a.chunked, data: multipartData}
Expand All @@ -96,10 +111,20 @@ var cases = [
{name: '-json +chunked', args: {json: false, array: false, chunked: true}},
{name: '-json -chunked', args: {json: false, array: false, chunked: false}},

{name: '-json +headers +array', args: {json: false, headers: true, array: true}},
{name: '-json +headers -array', args: {json: false, headers: true, array: false}},
{name: '-json +headers +chunked', args: {json: false, headers: true, array: false, chunked: true}},
{name: '-json +headers -chunked', args: {json: false, headers: true, array: false, chunked: false}},

{name: '+json +array', args: {json: true, array: true}},
{name: '+json -array', args: {json: true, array: false}},
{name: '+json +chunked', args: {json: true, array: false, chunked: true}},
{name: '+json -chunked', args: {json: true, array: false, chunked: false}}
{name: '+json -chunked', args: {json: true, array: false, chunked: false}},

{name: '+json +headers +array', args: {json: true, headers: true, array: true}},
{name: '+json +headers -array', args: {json: true, headers: true, array: false}},
{name: '+json +headers +chunked', args: {json: true, headers: true, array: false, chunked: true}},
{name: '+json +headers -chunked', args: {json: true, headers: true, array: false, chunked: false}}
]

cases.forEach(function (test) {
Expand Down

0 comments on commit 7092c68

Please sign in to comment.