Skip to content

Commit

Permalink
add sendStatus support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Dec 26, 2018
1 parent 20303b2 commit 2990ada
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,10 @@ class RESPONSE {



// TODO: sendStatus
// Convenience method for sending status codes
sendStatus(status) {
this.status(status).send(UTILS.statusLookup(status))
}


// Convenience method for setting CORS headers
Expand Down Expand Up @@ -456,10 +459,10 @@ class RESPONSE {
: { headers: UTILS.stringifyHeaders(this._headers) },
{
statusCode: this._statusCode,
statusDescription: this._request.interface === 'alb' ? `${this._statusCode} ${UTILS.statusLookup(this._statusCode)}` : undefined,
body: this._request.method === 'HEAD' ? '' : UTILS.encodeBody(body,this._serializer),
isBase64Encoded: this._isBase64
}
},
this._request.interface === 'alb' ? { statusDescription: `${this._statusCode} ${UTILS.statusLookup(this._statusCode)}` } : {}
)

// Trigger the callback function
Expand Down
20 changes: 20 additions & 0 deletions test/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ api.get('/testJSONPResponse', function(req,res) {
res.jsonp({ foo: 'bar' })
})

api.get('/testSendStatus', function(req,res) {
res.sendStatus(200)
})

api.get('/testSendStatus403', function(req,res) {
res.sendStatus(403)
})

// Secondary route
api2.get('/testJSONPResponse', function(req,res) {
res.jsonp({ foo: 'bar' })
Expand Down Expand Up @@ -141,6 +149,18 @@ describe('Response Tests:', function() {
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '', isBase64Encoded: false })
}) // end it

it('sendStatus 200', async function() {
let _event = Object.assign({},event,{ path: '/testSendStatus'})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: 'OK', isBase64Encoded: false })
}) // end it

it('sendStatus 403', async function() {
let _event = Object.assign({},event,{ path: '/testSendStatus403'})
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 403, body: 'Forbidden', isBase64Encoded: false })
}) // end it

it('JSONP response (default callback)', async function() {
let _event = Object.assign({},event,{ path: '/testJSONPResponse' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
Expand Down

0 comments on commit 2990ada

Please sign in to comment.