forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-http-client-leaky-with-double-response.js
More file actions
44 lines (40 loc) · 1.15 KB
/
Copy pathtest-http-client-leaky-with-double-response.js
File metadata and controls
44 lines (40 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
// Flags: --expose-gc
const common = require('../common');
const http = require('http');
const assert = require('assert');
const { onGC } = require('../common/gc');
function createServer() {
const server = http.createServer(common.mustCall((req, res) => {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ hello: 'world' }));
req.socket.write('HTTP/1.1 400 Bad Request\r\n\r\n');
}));
return new Promise((resolve) => {
server.listen(0, common.mustCall(() => {
resolve(server);
}));
});
}
async function main() {
const server = await createServer();
const req = http.get({
port: server.address().port,
}, common.mustCall((res) => {
const chunks = [];
res.on('data', common.mustCallAtLeast((c) => chunks.push(c), 1));
res.on('end', common.mustCall(() => {
const body = Buffer.concat(chunks).toString('utf8');
const data = JSON.parse(body);
assert.strictEqual(data.hello, 'world');
}));
}));
const timer = setInterval(global.gc, 300);
onGC(req, {
ongc: common.mustCall(() => {
clearInterval(timer);
server.close();
})
});
}
main();