Skip to content

Commit

Permalink
fix(qs): pass queryString config to co-body
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 26, 2018
1 parent 6011e06 commit 2b4e5ec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/BodyParser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ class BodyParser {
_parseJSON (req) {
return parse.json(req, {
limit: this.config.json.limit,
strict: this.config.json.strict
strict: this.config.json.strict,
queryString: this.config.json.queryString
})
}

Expand All @@ -142,7 +143,8 @@ class BodyParser {
*/
_parseForm (req) {
return parse.form(req, {
limit: this.config.form.limit
limit: this.config.form.limit,
queryString: this.config.form.queryString
})
}

Expand All @@ -160,7 +162,8 @@ class BodyParser {
*/
_parseRaw (req) {
return parse.text(req, {
limit: this.config.raw.limit
limit: this.config.raw.limit,
queryString: this.config.raw.queryString
})
}

Expand Down
36 changes: 36 additions & 0 deletions test/unit/body-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,40 @@ test.group('Body Parser', (group) => {

assert.deepEqual(body.fields, {})
})

test('pass queryString config co-body', async (assert) => {
/**
* Get method to render the form
*/
app.get = function (req, res) {
res.writeHead(200, { 'content-type': 'text/html' })
res.write(`
<form method='POST' action='/'>
<input type='text' name='items[1]' value="foo" />
<input type='text' name='items[2]' value="bar" />
<button type='submit'> Submit </button>
</form>
`)
res.end()
}

/**
* Post method to handle the form submission
*/
app.post = async function (request, res) {
const config = new Config()
config.set('bodyParser.form.queryString', { parseArrays: false })
const parser = new BodyParser(config)
await parser.handle({ request }, function () {})
res.writeHead(200, { 'content-type': 'application/json' })
res.write(JSON.stringify(request.body))
res.end()
}

const browser = new Browser()
await browser.visit(TEST_URL)
await browser.pressButton('Submit')

assert.deepEqual(JSON.parse(browser.text()), { items: { '1': 'foo', '2': 'bar' } })
})
})

0 comments on commit 2b4e5ec

Please sign in to comment.