diff --git a/lib/har.js b/lib/har.js index 83453a327..ceb1cd107 100644 --- a/lib/har.js +++ b/lib/har.js @@ -60,43 +60,53 @@ Har.prototype.prep = function (data) { } // prep body - switch (data.postData.mimeType) { - case 'multipart/mixed': - case 'multipart/related': - case 'multipart/form-data': - case 'multipart/alternative': - // reset values - data.postData.mimeType = 'multipart/form-data' - break - - case 'application/x-www-form-urlencoded': - if (!data.postData.params) { - data.postData.text = '' - } else { - data.postData.paramsObj = data.postData.params.reduce(this.reducer, {}) - - // always overwrite - data.postData.text = qs.stringify(data.postData.paramsObj) - } - break - - case 'text/json': - case 'text/x-json': - case 'application/json': - case 'application/x-json': - data.postData.mimeType = 'application/json' - - if (data.postData.text) { - try { - data.postData.jsonObj = JSON.parse(data.postData.text) - } catch (e) { - this.request.debug(e) - - // force back to text/plain - data.postData.mimeType = 'text/plain' - } + function some (arr) { + return arr.some(function (type) { + return data.postData.mimeType.indexOf(type) === 0 + }) + } + + if (some([ + 'multipart/mixed', + 'multipart/related', + 'multipart/form-data', + 'multipart/alternative'])) { + + // reset values + data.postData.mimeType = 'multipart/form-data' + } + + else if (some([ + 'application/x-www-form-urlencoded'])) { + + if (!data.postData.params) { + data.postData.text = '' + } else { + data.postData.paramsObj = data.postData.params.reduce(this.reducer, {}) + + // always overwrite + data.postData.text = qs.stringify(data.postData.paramsObj) + } + } + + else if (some([ + 'text/json', + 'text/x-json', + 'application/json', + 'application/x-json'])) { + + data.postData.mimeType = 'application/json' + + if (data.postData.text) { + try { + data.postData.jsonObj = JSON.parse(data.postData.text) + } catch (e) { + this.request.debug(e) + + // force back to text/plain + data.postData.mimeType = 'text/plain' } - break + } } return data @@ -152,51 +162,50 @@ Har.prototype.options = function (options) { options.headers = req.headersObj } - switch (req.postData.mimeType) { - case 'application/x-www-form-urlencoded': - options.form = req.postData.paramsObj - break - - case 'application/json': - if (req.postData.jsonObj) { - options.body = req.postData.jsonObj - options.json = true - } - break - - case 'multipart/form-data': - options.formData = {} + function test (type) { + return req.postData.mimeType.indexOf(type) === 0 + } + if (test('application/x-www-form-urlencoded')) { + options.form = req.postData.paramsObj + } + else if (test('application/json')) { + if (req.postData.jsonObj) { + options.body = req.postData.jsonObj + options.json = true + } + } + else if (test('multipart/form-data')) { + options.formData = {} - req.postData.params.forEach(function (param) { - var attachment = {} + req.postData.params.forEach(function (param) { + var attachment = {} - if (!param.fileName && !param.fileName && !param.contentType) { - options.formData[param.name] = param.value - return - } + if (!param.fileName && !param.fileName && !param.contentType) { + options.formData[param.name] = param.value + return + } - // attempt to read from disk! - if (param.fileName && !param.value) { - attachment.value = fs.createReadStream(param.fileName) - } else if (param.value) { - attachment.value = param.value - } + // attempt to read from disk! + if (param.fileName && !param.value) { + attachment.value = fs.createReadStream(param.fileName) + } else if (param.value) { + attachment.value = param.value + } - if (param.fileName) { - attachment.options = { - filename: param.fileName, - contentType: param.contentType ? param.contentType : null - } + if (param.fileName) { + attachment.options = { + filename: param.fileName, + contentType: param.contentType ? param.contentType : null } - - options.formData[param.name] = attachment - }) - break - - default: - if (req.postData.text) { - options.body = req.postData.text } + + options.formData[param.name] = attachment + }) + } + else { + if (req.postData.text) { + options.body = req.postData.text + } } return options diff --git a/tests/fixtures/har.json b/tests/fixtures/har.json index 49799374c..4864a1b28 100644 --- a/tests/fixtures/har.json +++ b/tests/fixtures/har.json @@ -8,7 +8,7 @@ } ], "postData": { - "mimeType": "application/x-www-form-urlencoded", + "mimeType": "application/x-www-form-urlencoded; charset=UTF-8", "params": [ { "name": "foo",