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

Implement loose matching for har mime types #1876

Merged
merged 2 commits into from Oct 30, 2015
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
57 changes: 33 additions & 24 deletions lib/har.js
Expand Up @@ -60,16 +60,25 @@ Har.prototype.prep = function (data) {
}

// prep body
switch (data.postData.mimeType) {
case 'multipart/mixed':
case 'multipart/related':
case 'multipart/form-data':
case 'multipart/alternative':
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'
break
}

else if (some([
'application/x-www-form-urlencoded'])) {

case 'application/x-www-form-urlencoded':
if (!data.postData.params) {
data.postData.text = ''
} else {
Expand All @@ -78,12 +87,14 @@ Har.prototype.prep = function (data) {
// always overwrite
data.postData.text = qs.stringify(data.postData.paramsObj)
}
break
}

else if (some([
'text/json',
'text/x-json',
'application/json',
'application/x-json'])) {

case 'text/json':
case 'text/x-json':
case 'application/json':
case 'application/x-json':
data.postData.mimeType = 'application/json'

if (data.postData.text) {
Expand All @@ -96,7 +107,6 @@ Har.prototype.prep = function (data) {
data.postData.mimeType = 'text/plain'
}
}
break
}

return data
Expand Down Expand Up @@ -152,19 +162,19 @@ Har.prototype.options = function (options) {
options.headers = req.headersObj
}

switch (req.postData.mimeType) {
case 'application/x-www-form-urlencoded':
function test (type) {
return req.postData.mimeType.indexOf(type) === 0
}
if (test('application/x-www-form-urlencoded')) {
options.form = req.postData.paramsObj
break

case 'application/json':
}
else if (test('application/json')) {
if (req.postData.jsonObj) {
options.body = req.postData.jsonObj
options.json = true
}
break

case 'multipart/form-data':
}
else if (test('multipart/form-data')) {
options.formData = {}

req.postData.params.forEach(function (param) {
Expand All @@ -191,9 +201,8 @@ Har.prototype.options = function (options) {

options.formData[param.name] = attachment
})
break

default:
}
else {
if (req.postData.text) {
options.body = req.postData.text
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/har.json
Expand Up @@ -8,7 +8,7 @@
}
],
"postData": {
"mimeType": "application/x-www-form-urlencoded",
"mimeType": "application/x-www-form-urlencoded; charset=UTF-8",
"params": [
{
"name": "foo",
Expand Down