@@ -9,11 +9,11 @@ const { Writable, pipeline, PassThrough, Readable } = require('node:stream')
99const { test, plan } = require ( 'tap' )
1010const pem = require ( 'https-pem' )
1111
12- const { Client } = require ( '..' )
12+ const { Client, Agent } = require ( '..' )
1313
1414const isGreaterThanv20 = Number ( process . version . slice ( 1 ) . split ( '.' ) [ 0 ] ) >= 20
1515
16- plan ( 18 )
16+ plan ( 19 )
1717
1818test ( 'Should support H2 connection' , async t => {
1919 const body = [ ]
@@ -947,3 +947,52 @@ test(
947947 t . equal ( Buffer . concat ( requestChunks ) . toString ( 'utf-8' ) , expectedBody )
948948 }
949949)
950+
951+ test ( 'Agent should support H2 connection' , async t => {
952+ const body = [ ]
953+ const server = createSecureServer ( pem )
954+
955+ server . on ( 'stream' , ( stream , headers ) => {
956+ t . equal ( headers [ 'x-my-header' ] , 'foo' )
957+ t . equal ( headers [ ':method' ] , 'GET' )
958+ stream . respond ( {
959+ 'content-type' : 'text/plain; charset=utf-8' ,
960+ 'x-custom-h2' : 'hello' ,
961+ ':status' : 200
962+ } )
963+ stream . end ( 'hello h2!' )
964+ } )
965+
966+ server . listen ( 0 )
967+ await once ( server , 'listening' )
968+
969+ const client = new Agent ( {
970+ connect : {
971+ rejectUnauthorized : false
972+ } ,
973+ allowH2 : true
974+ } )
975+
976+ t . plan ( 6 )
977+ t . teardown ( server . close . bind ( server ) )
978+ t . teardown ( client . close . bind ( client ) )
979+
980+ const response = await client . request ( {
981+ origin : `https://localhost:${ server . address ( ) . port } ` ,
982+ path : '/' ,
983+ method : 'GET' ,
984+ headers : {
985+ 'x-my-header' : 'foo'
986+ }
987+ } )
988+
989+ response . body . on ( 'data' , chunk => {
990+ body . push ( chunk )
991+ } )
992+
993+ await once ( response . body , 'end' )
994+ t . equal ( response . statusCode , 200 )
995+ t . equal ( response . headers [ 'content-type' ] , 'text/plain; charset=utf-8' )
996+ t . equal ( response . headers [ 'x-custom-h2' ] , 'hello' )
997+ t . equal ( Buffer . concat ( body ) . toString ( 'utf8' ) , 'hello h2!' )
998+ } )
0 commit comments