Skip to content

Commit

Permalink
remove warning when used with HTTP2
Browse files Browse the repository at this point in the history
  • Loading branch information
tellnes committed Jul 5, 2020
1 parent 15e78ca commit 2ac1693
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
22 changes: 21 additions & 1 deletion index.js
Expand Up @@ -19,6 +19,13 @@ var parseUrl = require('parseurl')
var statuses = require('statuses')
var unpipe = require('unpipe')

var http2
try {
http2 = require('http2')
} catch (e) {
http2 = null
}

/**
* Module variables.
* @private
Expand Down Expand Up @@ -276,7 +283,9 @@ function send (req, res, status, headers, message) {

// response status
res.statusCode = status
res.statusMessage = statuses[status]
if (!isHTTP2Response(res)) {
res.statusMessage = statuses[status]
}

// response headers
setHeaders(res, headers)
Expand Down Expand Up @@ -329,3 +338,14 @@ function setHeaders (res, headers) {
res.setHeader(key, headers[key])
}
}

/**
* Check if a response object is HTTP2.
*
* @param {OutgoingMessage|Http2ServerResponse} res
* @private
*/

function isHTTP2Response (res) {
return http2 && res instanceof http2.Http2ServerResponse
}
30 changes: 30 additions & 0 deletions test/test.js
Expand Up @@ -513,3 +513,33 @@ describe('finalhandler(req, res)', function () {
})
})
})

describe('HTTP2', function () {
it('should not set statusMessage for HTTP2 response', function (done) {
var http2
try {
http2 = require('http2')
} catch (e) {
return done()
}

process.once('warning', function (warning) {
assert.fail(warning)
})

var server = http2.createServer(function (req, res) {
var done = finalhandler(req, res)
done()
})

server.listen(function () {
var address = server.address()
var client = http2.connect('http://localhost:' + address.port)
client.request({ ':path': '/' }).on('response', function () {
client.close()
server.close()
done()
})
})
})
})

0 comments on commit 2ac1693

Please sign in to comment.