Skip to content

Commit 46930eb

Browse files
committed
feat(response): add few more properties
add getters for response.{finished, headersSent, isPending}
1 parent 4d05d2d commit 46930eb

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/Response/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,42 @@ class Response {
3232
})
3333
}
3434

35+
/**
36+
* returns whether request has been
37+
* finished or not
38+
*
39+
* @method finished
40+
*
41+
* @return {Boolean}
42+
*/
43+
get finished () {
44+
return this.response.finished
45+
}
46+
47+
/**
48+
* returns whether request headers
49+
* have been sent or not
50+
*
51+
* @method headersSent
52+
*
53+
* @return {Boolean}
54+
*/
55+
get headersSent () {
56+
return this.response.headersSent
57+
}
58+
59+
/**
60+
* returns whether a request is pending
61+
* or not
62+
*
63+
* @method isPending
64+
*
65+
* @return {Boolean}
66+
*/
67+
get isPending () {
68+
return (!this.headersSent && !this.finished)
69+
}
70+
3571
/**
3672
* sets key/value pair on response header
3773
*

test/unit/response.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,40 @@ describe('Response', function () {
365365
const response = new Response({name: 'bar'}, {setHeader: function () {}})
366366
expect(response.foo()).to.equal('bar')
367367
})
368+
369+
it('should return true for isPending when request has not been ended', function * () {
370+
const server = http.createServer((req, res) => {
371+
const request = new Request(req, res, Config)
372+
const Response = new ResponseBuilder({}, Route, Config)
373+
const response = new Response(request, res)
374+
const isPending = response.isPending
375+
response.send({isPending})
376+
})
377+
const res = yield supertest(server).get('/').expect(200).end()
378+
expect(res.body.isPending).to.equal(true)
379+
})
380+
381+
it('should return false for finished when request has not been ended', function * () {
382+
const server = http.createServer((req, res) => {
383+
const request = new Request(req, res, Config)
384+
const Response = new ResponseBuilder({}, Route, Config)
385+
const response = new Response(request, res)
386+
const finished = response.finished
387+
response.send({finished})
388+
})
389+
const res = yield supertest(server).get('/').expect(200).end()
390+
expect(res.body.finished).to.equal(false)
391+
})
392+
393+
it('should return false for headersSent when request has not been ended', function * () {
394+
const server = http.createServer((req, res) => {
395+
const request = new Request(req, res, Config)
396+
const Response = new ResponseBuilder({}, Route, Config)
397+
const response = new Response(request, res)
398+
const headersSent = response.headersSent
399+
response.send({headersSent})
400+
})
401+
const res = yield supertest(server).get('/').expect(200).end()
402+
expect(res.body.headersSent).to.equal(false)
403+
})
368404
})

0 commit comments

Comments
 (0)