Skip to content

Commit

Permalink
fixes #977 change request body validation (#978)
Browse files Browse the repository at this point in the history
* fix #977 change request body validation

* Formatting

* Dont attempt to write when no body

* Add test for function with no body

Co-authored-by: Raees Iqbal <[email protected]>
  • Loading branch information
peter-wd-1 and RaeesBhatti authored Jul 6, 2020
1 parent efe3c5f commit e9c3c6b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/utils/serve-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function createCallback(response) {
)
return handleErr('Incorrect function response statusCode', response)
}
if (typeof lambdaResponse.body !== 'string') {
if (lambdaResponse.body && typeof lambdaResponse.body !== 'string') {
console.log(`${NETLIFYDEVERR} Your function response must have a string body. You gave:`, lambdaResponse.body)
return handleErr('Incorrect function response body', response)
}
Expand All @@ -66,7 +66,9 @@ function createCallback(response) {
const items = lambdaResponse.multiValueHeaders[key]
response.setHeader(key, items)
}
response.write(lambdaResponse.isBase64Encoded ? Buffer.from(lambdaResponse.body, 'base64') : lambdaResponse.body)
if (lambdaResponse.body) {
response.write(lambdaResponse.isBase64Encoded ? Buffer.from(lambdaResponse.body, 'base64') : lambdaResponse.body)
}
response.end()
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ test('functions rewrite echo with body', async t => {
t.deepEqual(response.queryStringParameters, { ding: 'dong' })
})

test('functions: no body', async t => {
const response = await fetch(`http://${host}/api/no-body?ding=dong`, {
method: 'POST',
body: 'some=thing',
})

t.is(await response.text(), '')
t.is(response.status, 200)
})

test('functions rewrite echo with Form body', async t => {
const form = new FormData()
form.append('some', 'thing')
Expand Down
5 changes: 5 additions & 0 deletions tests/dummy-site/functions/no-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.handler = async (event, context) => {
return {
statusCode: 200,
}
}

0 comments on commit e9c3c6b

Please sign in to comment.