@@ -9,67 +9,73 @@ const { createSecureServer, connect } = require('http2');
99const { get } = require ( 'https' ) ;
1010const { parse } = require ( 'url' ) ;
1111
12+ const countdown = ( count , done ) => ( ) => -- count === 0 && done ( ) ;
13+
14+ function loadKey ( keyname ) {
15+ return readFileSync ( join ( fixturesDir , 'keys' , keyname ) ) ;
16+ }
17+
1218const key = loadKey ( 'agent8-key.pem' ) ;
1319const cert = loadKey ( 'agent8-cert.pem' ) ;
1420const ca = loadKey ( 'fake-startcom-root-cert.pem' ) ;
1521
16- function loadKey ( keyname ) {
17- return readFileSync (
18- join ( fixturesDir , 'keys' , keyname ) , 'binary' ) ;
22+ const clientOptions = { secureContext : createSecureContext ( { ca } ) } ;
23+
24+ function onRequest ( request , response ) {
25+ response . writeHead ( 200 , { 'content-type' : 'application/json' } ) ;
26+ response . end ( JSON . stringify ( {
27+ alpnProtocol : request . socket . alpnProtocol ,
28+ httpVersion : request . httpVersion
29+ } ) ) ;
30+ }
31+
32+ function onSession ( session ) {
33+ const headers = {
34+ ':path' : '/' ,
35+ ':method' : 'GET' ,
36+ ':scheme' : 'https' ,
37+ ':authority' : `localhost:${ this . server . address ( ) . port } `
38+ } ;
39+
40+ const request = session . request ( headers ) ;
41+ request . on ( 'response' , mustCall ( ( headers ) => {
42+ strictEqual ( headers [ ':status' ] , '200' ) ;
43+ strictEqual ( headers [ 'content-type' ] , 'application/json' ) ;
44+ } ) ) ;
45+ request . setEncoding ( 'utf8' ) ;
46+ let raw = '' ;
47+ request . on ( 'data' , ( chunk ) => { raw += chunk ; } ) ;
48+ request . on ( 'end' , mustCall ( ( ) => {
49+ const { alpnProtocol, httpVersion } = JSON . parse ( raw ) ;
50+ strictEqual ( alpnProtocol , 'h2' ) ;
51+ strictEqual ( httpVersion , '2.0' ) ;
52+
53+ session . destroy ( ) ;
54+ this . cleanup ( ) ;
55+ } ) ) ;
56+ request . end ( ) ;
1957}
2058
2159// HTTP/2 & HTTP/1.1 server
2260{
2361 const server = createSecureServer (
2462 { cert, key } ,
25- mustCall ( ( request , response ) => {
26- response . writeHead ( 200 , { 'content-type' : 'application/json' } ) ;
27- response . end ( JSON . stringify ( {
28- alpnProtocol : request . socket . alpnProtocol ,
29- httpVersion : request . httpVersion
30- } ) ) ;
31- } , 2 )
63+ mustCall ( onRequest , 2 )
3264 ) ;
3365
3466 server . listen ( 0 ) ;
3567
3668 server . on ( 'listening' , mustCall ( ( ) => {
3769 const port = server . address ( ) . port ;
3870 const origin = `https://localhost:${ port } ` ;
39- const clientOptions = { secureContext : createSecureContext ( { ca } ) } ;
4071
41- let count = 2 ;
72+ const cleanup = countdown ( 2 , ( ) => server . close ( ) ) ;
4273
4374 // HTTP/2 client
4475 connect (
4576 origin ,
46- { secureContext : createSecureContext ( { ca } ) } ,
47- mustCall ( ( session ) => {
48- const headers = {
49- ':path' : '/' ,
50- ':method' : 'GET' ,
51- ':scheme' : 'https' ,
52- ':authority' : `localhost:${ port } `
53- } ;
54-
55- const request = session . request ( headers ) ;
56- request . on ( 'response' , mustCall ( ( headers ) => {
57- strictEqual ( headers [ ':status' ] , '200' ) ;
58- strictEqual ( headers [ 'content-type' ] , 'application/json' ) ;
59- } ) ) ;
60- request . setEncoding ( 'utf8' ) ;
61- let raw = '' ;
62- request . on ( 'data' , ( chunk ) => { raw += chunk ; } ) ;
63- request . on ( 'end' , mustCall ( ( ) => {
64- const data = JSON . parse ( raw ) ;
65- strictEqual ( data . alpnProtocol , 'h2' ) ;
66- strictEqual ( data . httpVersion , '2.0' ) ;
67-
68- session . destroy ( ) ;
69- if ( -- count === 0 ) server . close ( ) ;
70- } ) ) ;
71- request . end ( ) ;
72- } )
77+ clientOptions ,
78+ mustCall ( onSession . bind ( { cleanup, server } ) )
7379 ) ;
7480
7581 // HTTP/1.1 client
@@ -84,11 +90,11 @@ function loadKey(keyname) {
8490 let raw = '' ;
8591 response . on ( 'data' , ( chunk ) => { raw += chunk ; } ) ;
8692 response . on ( 'end' , mustCall ( ( ) => {
87- const data = JSON . parse ( raw ) ;
88- strictEqual ( data . alpnProtocol , false ) ;
89- strictEqual ( data . httpVersion , '1.1' ) ;
93+ const { alpnProtocol , httpVersion } = JSON . parse ( raw ) ;
94+ strictEqual ( alpnProtocol , false ) ;
95+ strictEqual ( httpVersion , '1.1' ) ;
9096
91- if ( -- count === 0 ) server . close ( ) ;
97+ cleanup ( ) ;
9298 } ) ) ;
9399 } )
94100 ) ;
@@ -97,27 +103,28 @@ function loadKey(keyname) {
97103
98104// HTTP/2-only server
99105{
100- const server = createSecureServer ( { cert, key, allowHTTP1 : false } ) ;
106+ const server = createSecureServer (
107+ { cert, key, allowHTTP1 : false } ,
108+ mustCall ( onRequest )
109+ ) ;
101110
102111 server . listen ( 0 ) ;
103112
104113 server . on ( 'listening' , mustCall ( ( ) => {
105114 const port = server . address ( ) . port ;
106115 const origin = `https://localhost:${ port } ` ;
107- const clientOptions = { secureContext : createSecureContext ( { ca } ) } ;
108116
109- let count = 2 ;
117+ const cleanup = countdown ( 2 , ( ) => server . close ( ) ) ;
110118
111119 // HTTP/2 client
112- connect ( origin , clientOptions , mustCall ( ( session ) => {
113- session . destroy ( ) ;
114- if ( -- count === 0 ) server . close ( ) ;
115- } ) ) ;
120+ connect (
121+ origin ,
122+ clientOptions ,
123+ mustCall ( onSession . bind ( { cleanup, server } ) )
124+ ) ;
116125
117126 // HTTP/1.1 client
118127 get ( Object . assign ( parse ( origin ) , clientOptions ) , mustNotCall ( ) )
119- . on ( 'error' , mustCall ( ( ) => {
120- if ( -- count === 0 ) server . close ( ) ;
121- } ) ) ;
128+ . on ( 'error' , mustCall ( cleanup ) ) ;
122129 } ) ) ;
123130}
0 commit comments