Skip to content

Commit

Permalink
Fix hasBody with http2 request while or after 'end' event
Browse files Browse the repository at this point in the history
  • Loading branch information
sogaani committed Aug 9, 2018
1 parent 9efda35 commit 97c0d7a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function typeis (value, types_) {
*/

function hasbody (req) {
return (ishttp2(req) && (req.stream.readable || req.stream._readableState.endEmitted)) ||
return (ishttp2(req) && (req.stream.readable || !req.stream._readableState.sync)) ||
req.headers['transfer-encoding'] !== undefined ||
!isNaN(req.headers['content-length'])
}
Expand Down
53 changes: 53 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,36 @@ describe('typeis.hasBody(req)', function () {
})
})

it('should not indicate body after end event occurred', function (done) {
createBodylessRequest('', function (req) {
var data = ''
req.on('data', function (chunk) {
data += chunk
})
req.on('end', function (chunk) {
process.nextTick(function () {
assert.strictEqual(data, '')
assert.strictEqual(typeis.hasBody(req), false)
done()
})
})
})
})

it('should not indicate body while end event occurred', function (done) {
createBodylessRequest('', function (req) {
var data = ''
req.on('data', function (chunk) {
data += chunk
})
req.on('end', function (chunk) {
assert.strictEqual(data, '')
assert.strictEqual(typeis.hasBody(req), false)
done()
})
})
})

it('should indicate body', function (done) {
createRequest('', function (req) {
assert.strictEqual(typeis.hasBody(req), true)
Expand All @@ -265,6 +295,29 @@ describe('typeis.hasBody(req)', function () {
})
})
})

it('should indicate body while end event occurred', function (done) {
createRequest('', function (req) {
var data = ''
req.on('data', function (chunk) {
data += chunk
})
req.on('end', function (chunk) {
assert.strictEqual(data, 'hello')
assert.strictEqual(typeis.hasBody(req), true)
done()
})
})
})

it('should indicate body while data event', function (done) {
createRequest('', function (req) {
req.on('data', function (chunk) {
assert.strictEqual(typeis.hasBody(req), true)
done()
})
})
})
})
}
})
Expand Down

0 comments on commit 97c0d7a

Please sign in to comment.