GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test: simplify test-tls-set-secure-context · nodejs/node@81a2194

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • test/parallel
    • test-tls-set-secure-context.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎test/parallel/test-tls-set-secure-context.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-tls-set-secure-context.js
+28-41Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ if (!common.hasCrypto)
99
// secure context is changed.
1010

1111
const assert = require('assert');
12+
const events = require('events');
1213
const https = require('https');
14+
const timers = require('timers/promises');
1315
const fixtures = require('../common/fixtures');
1416
const credentialOptions = [
1517
{
@@ -43,10 +45,10 @@ server.listen(0, common.mustCall(() => {
4345
const { port } = server.address();
4446
const firstRequest = makeRequest(port, 1);
4547

46-
async function makeRemainingRequests() {
48+
(async function makeRemainingRequests() {
4749
// Wait until the first request is guaranteed to have been handled.
48-
if (!firstResponse) {
49-
return setImmediate(makeRemainingRequests);
50+
while (!firstResponse) {
51+
await timers.setImmediate();
5052
}
5153

5254
assert.strictEqual(await makeRequest(port, 2), 'success');
@@ -56,53 +58,38 @@ server.listen(0, common.mustCall(() => {
5658
const errorMessageRegex = common.hasOpenSSL3 ?
5759
/^Error: self-signed certificate$/ :
5860
/^Error: self signed certificate$/;
59-
await assert.rejects(async () => {
60-
await makeRequest(port, 3);
61-
}, errorMessageRegex);
61+
await assert.rejects(makeRequest(port, 3), errorMessageRegex);
6262

6363
server.setSecureContext(credentialOptions[0]);
6464
assert.strictEqual(await makeRequest(port, 4), 'success');
6565

6666
server.setSecureContext(credentialOptions[1]);
6767
firstResponse.end('fun!');
68-
await assert.rejects(async () => {
69-
await makeRequest(port, 5);
70-
}, errorMessageRegex);
68+
await assert.rejects(makeRequest(port, 5), errorMessageRegex);
7169

7270
assert.strictEqual(await firstRequest, 'multi-request-success-fun!');
7371
server.close();
74-
}
75-
76-
makeRemainingRequests();
72+
})().then(common.mustCall());
7773
}));
7874

79-
function makeRequest(port, id) {
80-
return new Promise((resolve, reject) => {
81-
const options = {
82-
rejectUnauthorized: true,
83-
ca: credentialOptions[0].ca,
84-
servername: 'agent1',
85-
headers: { id }
86-
};
87-
88-
let errored = false;
89-
https.get(`https://localhost:${port}`, options, (res) => {
90-
let response = '';
91-
92-
res.setEncoding('utf8');
93-
94-
res.on('data', (chunk) => {
95-
response += chunk;
96-
});
97-
98-
res.on('end', common.mustCall(() => {
99-
resolve(response);
100-
}));
101-
}).on('error', (err) => {
102-
errored = true;
103-
reject(err);
104-
}).on('finish', () => {
105-
assert.strictEqual(errored, false);
106-
});
107-
});
75+
async function makeRequest(port, id) {
76+
const options = {
77+
rejectUnauthorized: true,
78+
ca: credentialOptions[0].ca,
79+
servername: 'agent1',
80+
headers: { id },
81+
agent: new https.Agent()
82+
};
83+
84+
const req = https.get(`https://localhost:${port}`, options);
85+
86+
let errored = false;
87+
req.on('error', () => errored = true);
88+
req.on('finish', () => assert.strictEqual(errored, false));
89+
90+
const [res] = await events.once(req, 'response');
91+
res.setEncoding('utf8');
92+
let response = '';
93+
for await (const chunk of res) response += chunk;
94+
return response;
10895
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.