Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/http/helpers/body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = function parseBody (req) {
// Paranoid deep copy
let request = JSON.parse(JSON.stringify(req))
let headers = request.headers
// Note: content-type header may have multiple, comma-separated values. matching w/ includes may match to multiple different types
let contentType = type => headers?.['content-type']?.includes(type) || headers?.['Content-Type']?.includes(type)

let isString = typeof request.body === 'string'
Expand All @@ -39,17 +40,14 @@ module.exports = function parseBody (req) {
throw Error('Invalid request body encoding or invalid JSON')
}
}

if (isPlainText || isXml) {
else if (isPlainText || isXml) {
request.body = new Buffer.from(request.body, 'base64').toString()
}

if (isFormURLEncoded) {
else if (isFormURLEncoded) {
let data = new Buffer.from(request.body, 'base64').toString()
request.body = qs.parse(data)
}

if (isMultiPartFormData || isOctetStream) {
else if (isMultiPartFormData || isOctetStream) {
request.body = request.body.base64
? request.body
: { base64: request.body }
Expand Down
13 changes: 13 additions & 0 deletions test/unit/src/http/helpers/body-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ let octetStream = { 'Content-Type': 'application/octet-stream' }
let text = { 'Content-Type': 'text/plain' }
let xmlText = { 'Content-Type': 'text/xml' }
let xmlApp = { 'Content-Type': 'application/xml' }
let multipleTypes = { 'Content-Type': 'application/json, text/plain' }

test('Borked requests', t => {
t.plan(1)

let req = {
body: str(hi),
headers: multipleTypes,
isBase64Encoded: false,
}
t.equals(str(parseBody(req)), str(hi), `body matches ${str(req.body)}`)

})

test('Architect v10+ requests', t => {
t.plan(6)
Expand Down