Skip to content

Commit 2990ada

Browse files
committed
add sendStatus support
1 parent 20303b2 commit 2990ada

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lib/response.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,10 @@ class RESPONSE {
372372

373373

374374

375-
// TODO: sendStatus
375+
// Convenience method for sending status codes
376+
sendStatus(status) {
377+
this.status(status).send(UTILS.statusLookup(status))
378+
}
376379

377380

378381
// Convenience method for setting CORS headers
@@ -456,10 +459,10 @@ class RESPONSE {
456459
: { headers: UTILS.stringifyHeaders(this._headers) },
457460
{
458461
statusCode: this._statusCode,
459-
statusDescription: this._request.interface === 'alb' ? `${this._statusCode} ${UTILS.statusLookup(this._statusCode)}` : undefined,
460462
body: this._request.method === 'HEAD' ? '' : UTILS.encodeBody(body,this._serializer),
461463
isBase64Encoded: this._isBase64
462-
}
464+
},
465+
this._request.interface === 'alb' ? { statusDescription: `${this._statusCode} ${UTILS.statusLookup(this._statusCode)}` } : {}
463466
)
464467

465468
// Trigger the callback function

test/responses.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ api.get('/testJSONPResponse', function(req,res) {
5252
res.jsonp({ foo: 'bar' })
5353
})
5454

55+
api.get('/testSendStatus', function(req,res) {
56+
res.sendStatus(200)
57+
})
58+
59+
api.get('/testSendStatus403', function(req,res) {
60+
res.sendStatus(403)
61+
})
62+
5563
// Secondary route
5664
api2.get('/testJSONPResponse', function(req,res) {
5765
res.jsonp({ foo: 'bar' })
@@ -141,6 +149,18 @@ describe('Response Tests:', function() {
141149
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '', isBase64Encoded: false })
142150
}) // end it
143151

152+
it('sendStatus 200', async function() {
153+
let _event = Object.assign({},event,{ path: '/testSendStatus'})
154+
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
155+
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: 'OK', isBase64Encoded: false })
156+
}) // end it
157+
158+
it('sendStatus 403', async function() {
159+
let _event = Object.assign({},event,{ path: '/testSendStatus403'})
160+
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
161+
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 403, body: 'Forbidden', isBase64Encoded: false })
162+
}) // end it
163+
144164
it('JSONP response (default callback)', async function() {
145165
let _event = Object.assign({},event,{ path: '/testJSONPResponse' })
146166
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))

0 commit comments

Comments
 (0)