Skip to content

Commit

Permalink
use endAfterHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
sogaani committed Sep 21, 2018
1 parent 97c0d7a commit e5b45d4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Expand Up @@ -26,7 +26,6 @@ before_install:
- "test $TRAVIS_NODE_VERSION != '0.6' || npm rm --save-dev istanbul"
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
- "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev $(grep -E '\"eslint\\S*\"' package.json | cut -d'\"' -f2)"
- "test -z $(echo $HTTP2_TEST) || npm install https://github.com/visionmedia/superagent.git"

# Update Node.js modules
- "test ! -d node_modules || npm prune"
Expand All @@ -40,7 +39,5 @@ after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
matrix:
include:
- node_js: "9.5"
env: HTTP2_TEST=1
- node_js: "8.9"
- node_js: "10.11"
env: HTTP2_TEST=1
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -97,7 +97,7 @@ function typeis (value, types_) {
*/

function hasbody (req) {
return (ishttp2(req) && (req.stream.readable || !req.stream._readableState.sync)) ||
return (ishttp2(req) && !req.stream.endAfterHeaders) ||
req.headers['transfer-encoding'] !== undefined ||
!isNaN(req.headers['content-length'])
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -34,6 +34,7 @@
"scripts": {
"lint": "eslint --plugin markdown --ext js,md .",
"test": "mocha --reporter spec --check-leaks --bail test/",
"test-http2": "HTTP2_TEST=1 mocha --reporter spec --check-leaks --bail test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
},
Expand Down
63 changes: 52 additions & 11 deletions test/test.js
Expand Up @@ -7,8 +7,6 @@ var Readable

if (process.env.HTTP2_TEST) {
http2 = require('http2')
request = require('superagent')
request.http2 = true
Readable = require('stream').Readable
}

Expand Down Expand Up @@ -216,9 +214,11 @@ describe('typeis.hasBody(req)', function () {
assert.strictEqual(typeis.hasBody(req), true)
})

it('should be true when 0', function () {
var req = {headers: {'content-length': '0'}}
assert.strictEqual(typeis.hasBody(req), true)
it('should be true when 0', function (done) {
createZeroLengthBodyRequest('', function (req) {
assert.strictEqual(typeis.hasBody(req), true)
done()
})
})

it('should be false when bogus', function () {
Expand Down Expand Up @@ -333,9 +333,15 @@ function createRequest (type, callback) {
var s = new Readable()
s.push('hello')
s.push(null)
var req = request.post('localhost:' + server.address().port + '/')
.set('content-type', type || undefined)
s.pipe(req)

var session = http2.connect('http://localhost:' + server.address().port)
var headers = {
':path': '/',
':method': 'post',
'content-type': type || undefined
}
var request = session.request(headers)
s.pipe(request)
})
} else {
var req = {
Expand All @@ -356,9 +362,16 @@ function createBodylessRequest (type, callback) {
})

server = server.listen(function () {
request.get('localhost:' + server.address().port + '/')
.set('content-type', type || undefined)
.end()
var session = http2.connect('http://localhost:' + server.address().port)
var headers = {
':path': '/',
':method': 'get',
'content-type': type || ''
}
var option = {
endStream: true
}
session.request(headers, option)
})
} else {
var req = {
Expand All @@ -369,3 +382,31 @@ function createBodylessRequest (type, callback) {
callback(req)
}
}

function createZeroLengthBodyRequest (type, callback) {
if (process.env.HTTP2_TEST) {
var server = http2.createServer(function (req, res) {
callback(req)
server.close()
})

server = server.listen(function () {
var session = http2.connect('http://localhost:' + server.address().port)
var headers = {
':path': '/',
':method': 'get',
'content-type': type || ''
}
var request = session.request(headers)
request.end()
})
} else {
var req = {
headers: {
'content-type': type || '',
'content-length': 0
}
}
callback(req)
}
}

0 comments on commit e5b45d4

Please sign in to comment.