Skip to content

Commit 1b24118

Browse files
committed
Fix node v18 failures
1 parent 55facd5 commit 1b24118

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

test/httpbis/httpbis.int.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as http from 'http';
22
import * as http2 from 'http2';
3-
import { Server } from 'net';
3+
import { Server, Socket } from 'net';
44
import { expect } from 'chai';
55
import {
66
createVerifier,
@@ -51,7 +51,11 @@ function createHttpServer(config: ServerConfig): TestServer {
5151

5252
function createHttp2Server(config: ServerConfig): TestServer {
5353
const requests: Request[] = [];
54+
const connections: Socket[] = [];
5455
const server = http2.createServer();
56+
server.on('connection', (conn) => {
57+
connections.push(conn);
58+
});
5559
server.on('stream', (stream, headers) => {
5660
const domain = headers[':authority'] ?? 'localhost';
5761
const request: Request = {
@@ -67,9 +71,28 @@ function createHttp2Server(config: ServerConfig): TestServer {
6771
server.once('listening', () => resolve());
6872
server.listen(config.port);
6973
}),
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+
}),
7184
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+
}),
7396
};
7497
}
7598

@@ -94,7 +117,7 @@ function makeHttpRequest(request: Request, port?: number): Promise<http.Incoming
94117
});
95118
}
96119

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; }> {
98121
return new Promise<{ headers: Record<string, string | string[]>; body: Buffer; }>((resolve, reject) => {
99122
const url = typeof request.url === 'string' ? new URL(request.url) : request.url;
100123
const client = http2.connect(request.url, {
@@ -114,22 +137,20 @@ function makeHttp2Request(request: Request, port?: number): Promise<{ headers: R
114137
':path': `${url.pathname}${url.search}`,
115138
});
116139
let headers: Record<string, string | string[]>;
117-
req.end();
140+
req.end(request.body);
118141
req.on('response', (h) => {
119142
headers = h as Record<string, string | string[]>;
120143
});
121144
req.on('error', (e) => {
122-
reject(e);
123-
client.close();
145+
client.close(() => reject(e));
124146
});
125147
const chunks: Buffer[] = [];
126148
req.on('data', (chunk) => chunks.push(chunk));
127149
req.on('end', () => {
128-
client.close();
129-
resolve({
150+
client.close(() => resolve({
130151
headers,
131152
body: Buffer.concat(chunks),
132-
});
153+
}));
133154
});
134155
});
135156
}
@@ -443,7 +464,9 @@ describe('httpbis', () => {
443464
'signature': 'sig-b24=:MEYCIQDXrmWrcxKWLQQm0zlwbFr5/KAlB9oHkfMpNRVCuGVHjQIhAKtljVKRuRoWv5dCKuc+GgP3eqLAq+Eg0d3olyR67BYK:',
444465
});
445466
stream.end('{"message": "good dog"}');
446-
stream.close();
467+
stream.close(undefined, () => {
468+
console.log('closed');
469+
});
447470
});
448471
return server.start();
449472
});
@@ -465,6 +488,7 @@ describe('httpbis', () => {
465488
'Signature-Input': 'sig-b21=();created=1618884473;keyid="test-key-rsa-pss";nonce="b3k2pp5k7z-50gnwp.yemd"',
466489
'Signature': 'sig-b21=:d2pmTvmbncD3xQm8E9ZV2828BjQWGgiwAaw5bAkgibUopemLJcWDy/lkbbHAve4cRAtx31Iq786U7it++wgGxbtRxf8Udx7zFZsckzXaJMkA7ChG52eSkFxykJeNqsrWH5S+oxNFlD4dzVuwe8DhTSja8xxbR/Z2cOGdCbzR72rgFWhzx2VjBqJzsPLMIQKhO4DGezXehhWwE56YCE+O6c0mKZsfxVrogUvA4HELjVKWmAvtl6UnCh8jYzuVG5WSb/QEVPnP5TmcAnLH1g+s++v6d4s8m0gCw1fV5/SITLq9mhho8K3+7EPYTU8IU1bLhdxO5Nyt8C8ssinQ98Xw9Q==:',
467490
},
491+
body: '{"hello": "world"}',
468492
}, 8080);
469493
expect(server.requests).to.have.lengthOf(1);
470494
const [request] = server.requests;
@@ -498,6 +522,7 @@ describe('httpbis', () => {
498522
'Signature-Input': 'sig-b21=();created=1618884473;keyid="test-key-rsa-pss";nonce="b3k2pp5k7z-50gnwp.yemd"',
499523
'Signature': 'sig-b21=:d2pmTvmbncD3xQm8E9ZV2828BjQWGgiwAaw5bAkgibUopemLJcWDy/lkbbHAve4cRAtx31Iq786U7it++wgGxbtRxf8Udx7zFZsckzXaJMkA7ChG52eSkFxykJeNqsrWH5S+oxNFlD4dzVuwe8DhTSja8xxbR/Z2cOGdCbzR72rgFWhzx2VjBqJzsPLMIQKhO4DGezXehhWwE56YCE+O6c0mKZsfxVrogUvA4HELjVKWmAvtl6UnCh8jYzuVG5WSb/QEVPnP5TmcAnLH1g+s++v6d4s8m0gCw1fV5/SITLq9mhho8K3+7EPYTU8IU1bLhdxO5Nyt8C8ssinQ98Xw9Q==:',
500524
},
525+
body: '{"hello": "world"}',
501526
}, 8080);
502527
expect(server.requests).to.have.lengthOf(1);
503528
const [request] = server.requests;
@@ -537,6 +562,7 @@ describe('httpbis', () => {
537562
'Signature-Input': 'sig-b22=("@authority" "content-digest" "@query-param";name="Pet");created=1618884473;keyid="test-key-rsa-pss";tag="header-example"',
538563
'Signature': 'sig-b22=:LjbtqUbfmvjj5C5kr1Ugj4PmLYvx9wVjZvD9GsTT4F7GrcQEdJzgI9qHxICagShLRiLMlAJjtq6N4CDfKtjvuJyE5qH7KT8UCMkSowOB4+ECxCmT8rtAmj/0PIXxi0A0nxKyB09RNrCQibbUjsLS/2YyFYXEu4TRJQzRw1rLEuEfY17SARYhpTlaqwZVtR8NV7+4UKkjqpcAoFqWFQh62s7Cl+H2fjBSpqfZUJcsIk4N6wiKYd4je2U/lankenQ99PZfB4jY3I5rSV2DSBVkSFsURIjYErOs0tFTQosMTAoxk//0RoKUqiYY8Bh0aaUEb0rQl3/XaVe4bXTugEjHSw==:',
539564
},
565+
body: '{"hello": "world"}',
540566
}, 8080);
541567
expect(server.requests).to.have.lengthOf(1);
542568
const [request] = server.requests;
@@ -570,6 +596,7 @@ describe('httpbis', () => {
570596
'Signature-Input': 'sig-b23=("date" "@method" "@path" "@query" "@authority" "content-type" "content-digest" "content-length");created=1618884473;keyid="test-key-rsa-pss"',
571597
'Signature': 'sig-b23=:bbN8oArOxYoyylQQUU6QYwrTuaxLwjAC9fbY2F6SVWvh0yBiMIRGOnMYwZ/5MR6fb0Kh1rIRASVxFkeGt683+qRpRRU5p2voTp768ZrCUb38K0fUxN0O0iC59DzYx8DFll5GmydPxSmme9v6ULbMFkl+V5B1TP/yPViV7KsLNmvKiLJH1pFkh/aYA2HXXZzNBXmIkoQoLd7YfW91kE9o/CCoC1xMy7JA1ipwvKvfrs65ldmlu9bpG6A9BmzhuzF8Eim5f8ui9eH8LZH896+QIF61ka39VBrohr9iyMUJpvRX2Zbhl5ZJzSRxpJyoEZAFL2FUo5fTIztsDZKEgM4cUA==:',
572598
},
599+
body: '{"hello": "world"}',
573600
}, 8080);
574601
expect(server.requests).to.have.lengthOf(1);
575602
const [request] = server.requests;
@@ -605,6 +632,7 @@ describe('httpbis', () => {
605632
'Signature-Input': 'sig-b23=("date" "@method" "@path" "@query" "@authority" "content-type" "content-digest" "content-length");created=1618884473;keyid="test-key-rsa-pss"',
606633
'Signature': 'sig-b23=:bbN8oArOxYoyylQQUU6QYwrTuaxLwjAC9fbY2F6SVWvh0yBiMIRGOnMYwZ/5MR6fb0Kh1rIRASVxFkeGt683+qRpRRU5p2voTp768ZrCUb38K0fUxN0O0iC59DzYx8DFll5GmydPxSmme9v6ULbMFkl+V5B1TP/yPViV7KsLNmvKiLJH1pFkh/aYA2HXXZzNBXmIkoQoLd7YfW91kE9o/CCoC1xMy7JA1ipwvKvfrs65ldmlu9bpG6A9BmzhuzF8Eim5f8ui9eH8LZH896+QIF61ka39VBrohr9iyMUJpvRX2Zbhl5ZJzSRxpJyoEZAFL2FUo5fTIztsDZKEgM4cUA==:',
607634
},
635+
body: '{"hello": "world"}',
608636
}, 8080);
609637
expect(server.requests).to.have.lengthOf(1);
610638
const [request] = server.requests;
@@ -643,6 +671,7 @@ describe('httpbis', () => {
643671
'Signature-Input': 'sig-b25=("date" "@authority" "content-type");created=1618884473;keyid="test-shared-secret"',
644672
'Signature': 'sig-b25=:pxcQw6G3AjtMBQjwo8XzkZf/bws5LelbaMk5rGIGtE8=:',
645673
},
674+
body: '{"hello": "world"}',
646675
}, 8080);
647676
expect(server.requests).to.have.lengthOf(1);
648677
const [request] = server.requests;
@@ -678,6 +707,7 @@ describe('httpbis', () => {
678707
'Signature-Input': 'sig-b26=("date" "@method" "@path" "@authority" "content-type" "content-length");created=1618884473;keyid="test-key-ed25519"',
679708
'Signature': 'sig-b26=:wqcAqbmYJ2ji2glfAMaRy4gruYYnx2nEFN2HN6jrnDnQCK1u02Gb04v9EDgwUPiu4A0w6vuQv5lIp5WPpBKRCw==:',
680709
},
710+
body: '{"hello": "world"}',
681711
}, 8080);
682712
expect(server.requests).to.have.lengthOf(1);
683713
const [request] = server.requests;

0 commit comments

Comments
 (0)