1
1
import * as http from 'http' ;
2
2
import * as http2 from 'http2' ;
3
- import { Server } from 'net' ;
3
+ import { Server , Socket } from 'net' ;
4
4
import { expect } from 'chai' ;
5
5
import {
6
6
createVerifier ,
@@ -51,7 +51,11 @@ function createHttpServer(config: ServerConfig): TestServer {
51
51
52
52
function createHttp2Server ( config : ServerConfig ) : TestServer {
53
53
const requests : Request [ ] = [ ] ;
54
+ const connections : Socket [ ] = [ ] ;
54
55
const server = http2 . createServer ( ) ;
56
+ server . on ( 'connection' , ( conn ) => {
57
+ connections . push ( conn ) ;
58
+ } ) ;
55
59
server . on ( 'stream' , ( stream , headers ) => {
56
60
const domain = headers [ ':authority' ] ?? 'localhost' ;
57
61
const request : Request = {
@@ -67,9 +71,28 @@ function createHttp2Server(config: ServerConfig): TestServer {
67
71
server . once ( 'listening' , ( ) => resolve ( ) ) ;
68
72
server . listen ( config . port ) ;
69
73
} ) ,
70
- stop : ( ) => new Promise < void > ( ( resolve ) => server . close ( ( ) => resolve ( ) ) ) ,
74
+ stop : ( ) => new Promise < void > ( ( resolve ) => {
75
+ Promise . all ( connections . map ( ( conn ) => new Promise < void > ( ( done ) => {
76
+ if ( conn . destroyed ) {
77
+ done ( ) ;
78
+ } else {
79
+ conn . destroy ( ) ;
80
+ conn . on ( 'close' , done ) ;
81
+ }
82
+ } ) ) ) . then ( ( ) => server . close ( ( ) => resolve ( ) ) ) ;
83
+ } ) ,
71
84
requests,
72
- clear : ( ) => requests . splice ( 0 ) ,
85
+ clear : ( ) => new Promise < void > ( ( resolve ) => {
86
+ requests . splice ( 0 ) ;
87
+ Promise . all ( connections . map ( ( conn ) => new Promise < void > ( ( closed ) => {
88
+ if ( conn . destroyed ) {
89
+ closed ( ) ;
90
+ } else {
91
+ conn . destroy ( ) ;
92
+ conn . on ( 'close' , closed ) ;
93
+ }
94
+ } ) ) ) . then ( ( ) => resolve ( ) ) ;
95
+ } ) ,
73
96
} ;
74
97
}
75
98
@@ -94,7 +117,7 @@ function makeHttpRequest(request: Request, port?: number): Promise<http.Incoming
94
117
} ) ;
95
118
}
96
119
97
- function makeHttp2Request ( request : Request , port ?: number ) : Promise < { headers : Record < string , string | string [ ] > ; body : Buffer ; } > {
120
+ function makeHttp2Request ( request : Request & { body ?: string ; } , port ?: number ) : Promise < { headers : Record < string , string | string [ ] > ; body : Buffer ; } > {
98
121
return new Promise < { headers : Record < string , string | string [ ] > ; body : Buffer ; } > ( ( resolve , reject ) => {
99
122
const url = typeof request . url === 'string' ? new URL ( request . url ) : request . url ;
100
123
const client = http2 . connect ( request . url , {
@@ -114,22 +137,20 @@ function makeHttp2Request(request: Request, port?: number): Promise<{ headers: R
114
137
':path' : `${ url . pathname } ${ url . search } ` ,
115
138
} ) ;
116
139
let headers : Record < string , string | string [ ] > ;
117
- req . end ( ) ;
140
+ req . end ( request . body ) ;
118
141
req . on ( 'response' , ( h ) => {
119
142
headers = h as Record < string , string | string [ ] > ;
120
143
} ) ;
121
144
req . on ( 'error' , ( e ) => {
122
- reject ( e ) ;
123
- client . close ( ) ;
145
+ client . close ( ( ) => reject ( e ) ) ;
124
146
} ) ;
125
147
const chunks : Buffer [ ] = [ ] ;
126
148
req . on ( 'data' , ( chunk ) => chunks . push ( chunk ) ) ;
127
149
req . on ( 'end' , ( ) => {
128
- client . close ( ) ;
129
- resolve ( {
150
+ client . close ( ( ) => resolve ( {
130
151
headers,
131
152
body : Buffer . concat ( chunks ) ,
132
- } ) ;
153
+ } ) ) ;
133
154
} ) ;
134
155
} ) ;
135
156
}
@@ -443,7 +464,9 @@ describe('httpbis', () => {
443
464
'signature' : 'sig-b24=:MEYCIQDXrmWrcxKWLQQm0zlwbFr5/KAlB9oHkfMpNRVCuGVHjQIhAKtljVKRuRoWv5dCKuc+GgP3eqLAq+Eg0d3olyR67BYK:' ,
444
465
} ) ;
445
466
stream . end ( '{"message": "good dog"}' ) ;
446
- stream . close ( ) ;
467
+ stream . close ( undefined , ( ) => {
468
+ console . log ( 'closed' ) ;
469
+ } ) ;
447
470
} ) ;
448
471
return server . start ( ) ;
449
472
} ) ;
@@ -465,6 +488,7 @@ describe('httpbis', () => {
465
488
'Signature-Input' : 'sig-b21=();created=1618884473;keyid="test-key-rsa-pss";nonce="b3k2pp5k7z-50gnwp.yemd"' ,
466
489
'Signature' : 'sig-b21=:d2pmTvmbncD3xQm8E9ZV2828BjQWGgiwAaw5bAkgibUopemLJcWDy/lkbbHAve4cRAtx31Iq786U7it++wgGxbtRxf8Udx7zFZsckzXaJMkA7ChG52eSkFxykJeNqsrWH5S+oxNFlD4dzVuwe8DhTSja8xxbR/Z2cOGdCbzR72rgFWhzx2VjBqJzsPLMIQKhO4DGezXehhWwE56YCE+O6c0mKZsfxVrogUvA4HELjVKWmAvtl6UnCh8jYzuVG5WSb/QEVPnP5TmcAnLH1g+s++v6d4s8m0gCw1fV5/SITLq9mhho8K3+7EPYTU8IU1bLhdxO5Nyt8C8ssinQ98Xw9Q==:' ,
467
490
} ,
491
+ body : '{"hello": "world"}' ,
468
492
} , 8080 ) ;
469
493
expect ( server . requests ) . to . have . lengthOf ( 1 ) ;
470
494
const [ request ] = server . requests ;
@@ -498,6 +522,7 @@ describe('httpbis', () => {
498
522
'Signature-Input' : 'sig-b21=();created=1618884473;keyid="test-key-rsa-pss";nonce="b3k2pp5k7z-50gnwp.yemd"' ,
499
523
'Signature' : 'sig-b21=:d2pmTvmbncD3xQm8E9ZV2828BjQWGgiwAaw5bAkgibUopemLJcWDy/lkbbHAve4cRAtx31Iq786U7it++wgGxbtRxf8Udx7zFZsckzXaJMkA7ChG52eSkFxykJeNqsrWH5S+oxNFlD4dzVuwe8DhTSja8xxbR/Z2cOGdCbzR72rgFWhzx2VjBqJzsPLMIQKhO4DGezXehhWwE56YCE+O6c0mKZsfxVrogUvA4HELjVKWmAvtl6UnCh8jYzuVG5WSb/QEVPnP5TmcAnLH1g+s++v6d4s8m0gCw1fV5/SITLq9mhho8K3+7EPYTU8IU1bLhdxO5Nyt8C8ssinQ98Xw9Q==:' ,
500
524
} ,
525
+ body : '{"hello": "world"}' ,
501
526
} , 8080 ) ;
502
527
expect ( server . requests ) . to . have . lengthOf ( 1 ) ;
503
528
const [ request ] = server . requests ;
@@ -537,6 +562,7 @@ describe('httpbis', () => {
537
562
'Signature-Input' : 'sig-b22=("@authority" "content-digest" "@query-param";name="Pet");created=1618884473;keyid="test-key-rsa-pss";tag="header-example"' ,
538
563
'Signature' : 'sig-b22=:LjbtqUbfmvjj5C5kr1Ugj4PmLYvx9wVjZvD9GsTT4F7GrcQEdJzgI9qHxICagShLRiLMlAJjtq6N4CDfKtjvuJyE5qH7KT8UCMkSowOB4+ECxCmT8rtAmj/0PIXxi0A0nxKyB09RNrCQibbUjsLS/2YyFYXEu4TRJQzRw1rLEuEfY17SARYhpTlaqwZVtR8NV7+4UKkjqpcAoFqWFQh62s7Cl+H2fjBSpqfZUJcsIk4N6wiKYd4je2U/lankenQ99PZfB4jY3I5rSV2DSBVkSFsURIjYErOs0tFTQosMTAoxk//0RoKUqiYY8Bh0aaUEb0rQl3/XaVe4bXTugEjHSw==:' ,
539
564
} ,
565
+ body : '{"hello": "world"}' ,
540
566
} , 8080 ) ;
541
567
expect ( server . requests ) . to . have . lengthOf ( 1 ) ;
542
568
const [ request ] = server . requests ;
@@ -570,6 +596,7 @@ describe('httpbis', () => {
570
596
'Signature-Input' : 'sig-b23=("date" "@method" "@path" "@query" "@authority" "content-type" "content-digest" "content-length");created=1618884473;keyid="test-key-rsa-pss"' ,
571
597
'Signature' : 'sig-b23=:bbN8oArOxYoyylQQUU6QYwrTuaxLwjAC9fbY2F6SVWvh0yBiMIRGOnMYwZ/5MR6fb0Kh1rIRASVxFkeGt683+qRpRRU5p2voTp768ZrCUb38K0fUxN0O0iC59DzYx8DFll5GmydPxSmme9v6ULbMFkl+V5B1TP/yPViV7KsLNmvKiLJH1pFkh/aYA2HXXZzNBXmIkoQoLd7YfW91kE9o/CCoC1xMy7JA1ipwvKvfrs65ldmlu9bpG6A9BmzhuzF8Eim5f8ui9eH8LZH896+QIF61ka39VBrohr9iyMUJpvRX2Zbhl5ZJzSRxpJyoEZAFL2FUo5fTIztsDZKEgM4cUA==:' ,
572
598
} ,
599
+ body : '{"hello": "world"}' ,
573
600
} , 8080 ) ;
574
601
expect ( server . requests ) . to . have . lengthOf ( 1 ) ;
575
602
const [ request ] = server . requests ;
@@ -605,6 +632,7 @@ describe('httpbis', () => {
605
632
'Signature-Input' : 'sig-b23=("date" "@method" "@path" "@query" "@authority" "content-type" "content-digest" "content-length");created=1618884473;keyid="test-key-rsa-pss"' ,
606
633
'Signature' : 'sig-b23=:bbN8oArOxYoyylQQUU6QYwrTuaxLwjAC9fbY2F6SVWvh0yBiMIRGOnMYwZ/5MR6fb0Kh1rIRASVxFkeGt683+qRpRRU5p2voTp768ZrCUb38K0fUxN0O0iC59DzYx8DFll5GmydPxSmme9v6ULbMFkl+V5B1TP/yPViV7KsLNmvKiLJH1pFkh/aYA2HXXZzNBXmIkoQoLd7YfW91kE9o/CCoC1xMy7JA1ipwvKvfrs65ldmlu9bpG6A9BmzhuzF8Eim5f8ui9eH8LZH896+QIF61ka39VBrohr9iyMUJpvRX2Zbhl5ZJzSRxpJyoEZAFL2FUo5fTIztsDZKEgM4cUA==:' ,
607
634
} ,
635
+ body : '{"hello": "world"}' ,
608
636
} , 8080 ) ;
609
637
expect ( server . requests ) . to . have . lengthOf ( 1 ) ;
610
638
const [ request ] = server . requests ;
@@ -643,6 +671,7 @@ describe('httpbis', () => {
643
671
'Signature-Input' : 'sig-b25=("date" "@authority" "content-type");created=1618884473;keyid="test-shared-secret"' ,
644
672
'Signature' : 'sig-b25=:pxcQw6G3AjtMBQjwo8XzkZf/bws5LelbaMk5rGIGtE8=:' ,
645
673
} ,
674
+ body : '{"hello": "world"}' ,
646
675
} , 8080 ) ;
647
676
expect ( server . requests ) . to . have . lengthOf ( 1 ) ;
648
677
const [ request ] = server . requests ;
@@ -678,6 +707,7 @@ describe('httpbis', () => {
678
707
'Signature-Input' : 'sig-b26=("date" "@method" "@path" "@authority" "content-type" "content-length");created=1618884473;keyid="test-key-ed25519"' ,
679
708
'Signature' : 'sig-b26=:wqcAqbmYJ2ji2glfAMaRy4gruYYnx2nEFN2HN6jrnDnQCK1u02Gb04v9EDgwUPiu4A0w6vuQv5lIp5WPpBKRCw==:' ,
680
709
} ,
710
+ body : '{"hello": "world"}' ,
681
711
} , 8080 ) ;
682
712
expect ( server . requests ) . to . have . lengthOf ( 1 ) ;
683
713
const [ request ] = server . requests ;
0 commit comments