Skip to content

Commit

Permalink
Merge pull request #25 from loadimpact/fix/postDataConflict
Browse files Browse the repository at this point in the history
Change validation and parse order of postData params/text.
  • Loading branch information
legander authored Mar 13, 2020
2 parents b2661b2 + f8e7d02 commit ab119ca
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 14 deletions.
37 changes: 37 additions & 0 deletions example/postDataConflict.har
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"log": {
"entries": [
{
"index": 0,
"request": {
"method": "POST",
"url": "http://test.loadimpact.com/login",
"headers": [
{
"name": "Content-Type",
"value": "application/x-www-form-urlencoded"
}
],
"postData": {
"mimeType": "application/x-www-form-urlencoded",
"text": "foo0=bar0&foo1=bar1",
"params": [
{
"name": "foo0",
"value": "bar0"
},
{
"name": "foo3",
"value": "bar3"
},
{
"name": "foo1",
"value": "bar1"
}
]
}
}
}
]
}
}
9 changes: 7 additions & 2 deletions src/aid.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ function isBlacklistedHeader (headerName = '') {
)
}

Object.assign(exports, {
function seralizeURLSearchParams (postDataParams = []) {
return postDataParams.map(({ name, value }) => [name, value].join('=')).join('&')
}

module.exports = {
seralizeURLSearchParams,
empty,
isBlacklistedHeader,
emptyObject,
extrinsic,
nought
})
}
8 changes: 5 additions & 3 deletions src/parse/postData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const params = require('./params')

function postData (node, spec) {
spec.type = node.mimeType
if (node.text) {
spec.value = node.text
} else if (node.params && node.params.length) {

if (node.params && node.params.length) {
spec.params = new Map()
params(node.params, spec.params)
} else if (node.text) {
spec.value = node.text
}

if (node.comment) {
spec.comment = node.comment
}
Expand Down
25 changes: 19 additions & 6 deletions src/validate/postData.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const params = require('./params')
const { empty, emptyObject } = require('../aid')
const { empty, emptyObject, seralizeURLSearchParams } = require('../aid')
const { InvalidArchiveError } = require('../error')

/*
Expand Down Expand Up @@ -27,36 +27,49 @@ function validate (node, i) {
`Missing post data MIME type (${i})`
)
}

if (typeof node.mimeType !== 'string') {
throw new InvalidArchiveError(
{ name: 'InvalidPostDataType' },
`Invalid post data MIME type (${i}): must be string`
)
}

if (node.params && !Array.isArray(node.params)) {
throw new InvalidArchiveError(
{ name: 'InvalidPostDataParams' },
`Invalid post data params (${i}): must be array`
)
}

if (node.text && typeof node.text !== 'string') {
throw new InvalidArchiveError(
{ name: 'InvalidPostDataText' },
`Invalid post data text (${i}): must be string`
)
}

if (node.comment && typeof node.comment !== 'string') {
throw new InvalidArchiveError(
{ name: 'InvalidComment' },
`Invalid post data comment (${i}): must be string`
)
}
if (node.params && node.params.length && node.text) {
throw new InvalidArchiveError(
{ name: 'PostDataConflict' },
`Post data conflict (${i}): specify 1 of params or text`
)

if (
node.params &&
node.params.length &&
node.text &&
node.mimeType.includes('application/x-www-form-urlencoded')
) {
if (seralizeURLSearchParams(node.params) !== node.text) {
throw new InvalidArchiveError(
{ name: 'PostDataConflict' },
`Post data conflict (${i}): params and text must be equal if both are provided`
)
}
}

if (
node.params &&
node.params.length &&
Expand Down
6 changes: 3 additions & 3 deletions standalone.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions test/unit/validate/postData.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ test.serial('invalid structured type', t => {
}, { name: 'InvalidPostDataType' })
})

test.serial('invalid postData combination', t => {
t.throws(() => {
postData({
mimeType: 'application/x-www-form-urlencoded',
params: [ { name: 'foo', value: 1 } ],
text: 'bar=2'
}, 0, makeAssay())
}, { name: 'PostDataConflict' })
})

test.serial('valid postData combination', t => {
postData({
mimeType: 'application/x-www-form-urlencoded',
params: [
{ name: 'foo', value: 1 },
{ name: 'bar', value: 2 }
],
text: 'foo=1&bar=2'
})
t.true(params.calledOnce)
})

test.serial('valid minimal', t => {
postData({ mimeType: 'text/plain' })
t.true(params.notCalled)
Expand Down

0 comments on commit ab119ca

Please sign in to comment.