Skip to content

Commit e02a051

Browse files
authored
test/nginx/request(): use WHATWG Responses (#2045)
Simplifies test `request()` implementation to use standard `Response` classes. The `request()` function aims to be as similar to `fetch()` as possible, while allowing for non-standard behaviour necessary for tests. Not using `Response` was an oversight in the initial implementation.
1 parent d42f25b commit e02a051

3 files changed

Lines changed: 21 additions & 24 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const http = require('node:http');
2+
const https = require('node:https');
3+
4+
const log = (...args) => console.log('[mocha-setup]', ...args);
5+
6+
module.exports = {
7+
mochaHooks: {
8+
afterAll() {
9+
log('Cleaning up HTTP(S) Response objects whose bodies have not been read...');
10+
http.globalAgent.destroy();
11+
https.globalAgent.destroy();
12+
13+
log('Cleanup complete.');
14+
},
15+
},
16+
};

test/nginx/src/mocha/request.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { isIPv6 } = require('node:net');
2-
const { Readable } = require('node:stream');
32

43
module.exports = request;
54

@@ -16,29 +15,11 @@ function request(url, { body, ...options }={}) {
1615
const req = getProtocolImplFrom(url).request({ ...options, ...preserve(url) }, res => {
1716
res.on('error', reject);
1817

19-
const body = new Readable({ read:() => {} });
20-
res.on('error', err => body.destroy(err));
21-
res.on('data', data => body.push(data));
22-
res.on('end', () => body.push(null));
23-
24-
const text = () => new Promise((resolve, reject) => {
25-
const chunks = [];
26-
body.on('error', reject);
27-
body.on('data', data => chunks.push(data));
28-
body.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
29-
});
30-
31-
const status = res.statusCode;
32-
33-
resolve({
34-
status,
35-
ok: status >= 200 && status < 300,
36-
statusText: res.statusText,
37-
body,
38-
text,
39-
json: async () => JSON.parse(await text()),
18+
resolve(new Response(res, {
19+
status: res.statusCode,
20+
statusText: res.statusMessage,
4021
headers: new Headers(res.headers),
41-
});
22+
}));
4223
});
4324
req.on('error', reject);
4425
if(body !== undefined) req.write(body);

test/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"lint": "ESLINT_USE_FLAT_CONFIG=false npx eslint --cache .",
55
"test:github-actions": "mocha ./github-actions.spec.js",
66
"test:nginx": "npm run test:nginx:mocha && npm run test:nginx:playwright",
7-
"test:nginx:mocha": "NODE_TLS_REJECT_UNAUTHORIZED=0 mocha ./nginx/src/mocha",
7+
"test:nginx:mocha": "NODE_TLS_REJECT_UNAUTHORIZED=0 mocha './nginx/src/mocha/**/*.spec.js' --require ./nginx/src/mocha/mocha.setup.js",
88
"test:nginx:playwright": "NODE_TLS_REJECT_UNAUTHORIZED=0 playwright test",
99
"test:service": "mocha ./service.spec.js",
1010
"test": "npm run lint && npm run test:github-actions && npm run test:nginx && npm run test:service"

0 commit comments

Comments
 (0)