Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multipart content-type headers detection #1275

Merged
merged 4 commits into from Nov 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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