forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-http-server-keep-alive-timeout.js
More file actions
122 lines (104 loc) · 2.93 KB
/
test-http-server-keep-alive-timeout.js
File metadata and controls
122 lines (104 loc) · 2.93 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
module.exports.testServerKeepAliveTimeout = function(common, isHttps) {
let http;
let net;
let createOptions;
let serverOptions;
let createServer;
if (isHttps) {
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}
http = require('https');
net = require('tls');
createOptions = function(server) {
return {
port: server.address().port,
allowHalfOpen: true,
rejectUnauthorized: false
};
};
const fs = require('fs');
serverOptions = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
};
createServer = function(fn) {
return http.createServer(serverOptions, fn);
};
} else {
http = require('http');
net = require('net');
createOptions = function(server) {
return {
port: server.address().port,
allowHalfOpen: true,
};
};
createServer = function(fn) {
return http.createServer(fn);
};
}
const assert = require('assert');
const tests = [];
function test(fn) {
if (!tests.length) {
process.nextTick(run);
}
tests.push(fn);
}
function run() {
const fn = tests.shift();
if (fn) fn(run);
}
test(function serverKeepAliveTimeoutWithPipeline(cb) {
let requestCount = 0;
process.on('exit', function() {
assert.strictEqual(requestCount, 3);
});
const server = createServer((req, res) => {
requestCount++;
res.end();
});
server.setTimeout(500, common.mustCall((socket) => {
// End this test and call `run()` for the next test (if any).
socket.destroy();
server.close();
cb();
}));
server.keepAliveTimeout = 50;
server.listen(0, common.mustCall(() => {
const c = net.connect(createOptions(server), () => {
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
});
}));
});
test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
let requestCount = 0;
process.on('exit', () => {
assert.strictEqual(requestCount, 3);
});
const server = createServer((req, res) => {
requestCount++;
});
server.setTimeout(500, common.mustCall((socket) => {
// End this test and call `run()` for the next test (if any).
socket.destroy();
server.close();
cb();
}));
server.keepAliveTimeout = 50;
server.listen(0, common.mustCall(() => {
const c = net.connect(createOptions(server), () => {
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
});
}));
});
};
const common = require('../common');
module.exports.testServerKeepAliveTimeout(common, false);