Skip to content

Commit 92d05cc

Browse files
committed
Added test cases (http2) that will fail if using undocumented res._implicitHeader()
1 parent 1010fad commit 92d05cc

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

test/session.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ var Cookie = require('../session/cookie')
1616

1717
var min = 60 * 1000;
1818

19+
var describeHttp2 = describe.skip
20+
try {
21+
var http2 = require('http2')
22+
describeHttp2 = describe
23+
} catch (err) {
24+
if (err) {
25+
console.log('http2 tests disabled.')
26+
}
27+
}
28+
1929
describe('session()', function(){
2030
it('should export constructors', function(){
2131
assert.strictEqual(typeof session.Session, 'function')
@@ -2294,6 +2304,37 @@ describe('session()', function(){
22942304
})
22952305
})
22962306
})
2307+
2308+
describeHttp2('http2', function () {
2309+
it('should work with http2 server', function (done) {
2310+
var server = createHttp2Server(null, function (req, res) {
2311+
res.setHeader(http2.constants.HTTP2_HEADER_CONTENT_TYPE, 'text/plain')
2312+
res.end('Hello, world!')
2313+
})
2314+
server.on('listening', function () {
2315+
var client = createHttp2Client(server.address().port)
2316+
// using ES5 as Node.js <=4.0.0 does not have Computed Property Names
2317+
var reqHeaders = {}
2318+
reqHeaders[http2.constants.HTTP2_HEADER_PATH] = '/'
2319+
var request = client.request(reqHeaders)
2320+
request.on('response', function (headers) {
2321+
assert.strictEqual(headers[http2.constants.HTTP2_HEADER_STATUS], 200)
2322+
assert.strictEqual(headers[http2.constants.HTTP2_HEADER_CONTENT_TYPE], 'text/plain')
2323+
})
2324+
var chunks = []
2325+
request.on('data', function (chunk) {
2326+
chunks.push(chunk)
2327+
})
2328+
request.on('end', function () {
2329+
closeHttp2(client, server, function () {
2330+
assert.strictEqual(Buffer.concat(chunks).toString(), 'Hello, world!')
2331+
done()
2332+
})
2333+
})
2334+
request.end()
2335+
})
2336+
})
2337+
})
22972338
})
22982339

22992340
function cookie(res) {
@@ -2317,6 +2358,48 @@ function createServer (options, respond) {
23172358
return server.on('request', createRequestListener(opts, fn))
23182359
}
23192360

2361+
function createHttp2Server (options, respond) {
2362+
var fn = respond
2363+
var opts = options
2364+
var server = http2.createServer()
2365+
2366+
// setup, options, respond
2367+
if (typeof arguments[0] === 'function') {
2368+
opts = arguments[1]
2369+
fn = arguments[2]
2370+
2371+
server.on('request', arguments[0])
2372+
}
2373+
2374+
server.on('request', createRequestListener(opts, fn))
2375+
server.listen(0, '127.0.0.1')
2376+
return server
2377+
}
2378+
2379+
function createHttp2Client (port) {
2380+
return http2.connect('http://127.0.0.1:' + port)
2381+
}
2382+
2383+
function closeHttp2 (client, server, callback) {
2384+
if (typeof client.shutdown === 'function') {
2385+
// this is the node v8.x way of closing the connections
2386+
client.shutdown({}, function () {
2387+
server.close(function () {
2388+
callback()
2389+
})
2390+
})
2391+
} else {
2392+
// this is the node v9.x onwards way of closing the connections
2393+
client.close(function () {
2394+
// force existing connections to time out after 1ms.
2395+
// this is done to force the server to close in some cases where it wouldn't do it otherwise.
2396+
server.close(function () {
2397+
callback()
2398+
})
2399+
})
2400+
}
2401+
}
2402+
23202403
function createRequestListener(opts, fn) {
23212404
var _session = createSession(opts)
23222405
var respond = fn || end

0 commit comments

Comments
 (0)