Skip to content

Commit f0ef3e6

Browse files
committed
test: refactor test-http-server-keep-alive-timeout
Parameterize and merge two similar test sources. Http's test has function and http's test require it.
1 parent 489454d commit f0ef3e6

File tree

2 files changed

+116
-155
lines changed

2 files changed

+116
-155
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,122 @@
11
'use strict';
22

3-
const common = require('../common');
4-
const assert = require('assert');
5-
const http = require('http');
6-
const net = require('net');
3+
module.exports.testServerKeepAliveTimeout = function(common, isHttps) {
4+
let http;
5+
let net;
6+
let createOptions;
7+
let serverOptions;
8+
let createServer;
79

8-
const tests = [];
10+
if (isHttps) {
11+
if (!common.hasCrypto) {
12+
common.skip('missing crypto');
13+
return;
14+
}
915

10-
function test(fn) {
11-
if (!tests.length) {
12-
process.nextTick(run);
13-
}
14-
tests.push(fn);
15-
}
16-
17-
function run() {
18-
const fn = tests.shift();
19-
if (fn) fn(run);
20-
}
21-
22-
test(function serverEndKeepAliveTimeoutWithPipeline(cb) {
23-
let requestCount = 0;
24-
process.on('exit', () => {
25-
assert.strictEqual(requestCount, 3);
26-
});
27-
const server = http.createServer((req, res) => {
28-
requestCount++;
29-
res.end();
30-
});
31-
server.setTimeout(500, common.mustCall((socket) => {
32-
// End this test and call `run()` for the next test (if any).
33-
socket.destroy();
34-
server.close();
35-
cb();
36-
}));
37-
server.keepAliveTimeout = 50;
38-
server.listen(0, common.mustCall(() => {
39-
const options = {
40-
port: server.address().port,
41-
allowHalfOpen: true
16+
http = require('https');
17+
net = require('tls');
18+
19+
createOptions = function(server) {
20+
return {
21+
port: server.address().port,
22+
allowHalfOpen: true,
23+
rejectUnauthorized: false
24+
};
4225
};
43-
const c = net.connect(options, () => {
44-
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
45-
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
46-
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
47-
});
48-
}));
49-
});
5026

51-
test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
52-
let requestCount = 0;
53-
process.on('exit', () => {
54-
assert.strictEqual(requestCount, 3);
55-
});
56-
const server = http.createServer((req, res) => {
57-
requestCount++;
58-
});
59-
server.setTimeout(500, common.mustCall((socket) => {
60-
// End this test and call `run()` for the next test (if any).
61-
socket.destroy();
62-
server.close();
63-
cb();
64-
}));
65-
server.keepAliveTimeout = 50;
66-
server.listen(0, common.mustCall(() => {
67-
const options = {
68-
port: server.address().port,
69-
allowHalfOpen: true
27+
const fs = require('fs');
28+
serverOptions = {
29+
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
30+
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
7031
};
71-
const c = net.connect(options, () => {
72-
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
73-
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
74-
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
32+
33+
createServer = function(fn) {
34+
return http.createServer(serverOptions, fn);
35+
};
36+
37+
38+
} else {
39+
http = require('http');
40+
net = require('net');
41+
42+
createOptions = function(server) {
43+
return {
44+
port: server.address().port,
45+
allowHalfOpen: true,
46+
};
47+
};
48+
49+
createServer = function(fn) {
50+
return http.createServer(fn);
51+
};
52+
53+
}
54+
55+
const assert = require('assert');
56+
const tests = [];
57+
58+
function test(fn) {
59+
if (!tests.length) {
60+
process.nextTick(run);
61+
}
62+
tests.push(fn);
63+
}
64+
65+
function run() {
66+
const fn = tests.shift();
67+
if (fn) fn(run);
68+
}
69+
70+
test(function serverKeepAliveTimeoutWithPipeline(cb) {
71+
let requestCount = 0;
72+
process.on('exit', function() {
73+
assert.strictEqual(requestCount, 3);
74+
});
75+
const server = createServer((req, res) => {
76+
requestCount++;
77+
res.end();
7578
});
76-
}));
77-
});
79+
server.setTimeout(500, common.mustCall((socket) => {
80+
// End this test and call `run()` for the next test (if any).
81+
socket.destroy();
82+
server.close();
83+
cb();
84+
}));
85+
server.keepAliveTimeout = 50;
86+
server.listen(0, common.mustCall(() => {
87+
const c = net.connect(createOptions(server), () => {
88+
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
89+
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
90+
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
91+
});
92+
}));
93+
});
94+
95+
test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
96+
let requestCount = 0;
97+
process.on('exit', () => {
98+
assert.strictEqual(requestCount, 3);
99+
});
100+
const server = createServer((req, res) => {
101+
requestCount++;
102+
});
103+
server.setTimeout(500, common.mustCall((socket) => {
104+
// End this test and call `run()` for the next test (if any).
105+
socket.destroy();
106+
server.close();
107+
cb();
108+
}));
109+
server.keepAliveTimeout = 50;
110+
server.listen(0, common.mustCall(() => {
111+
const c = net.connect(createOptions(server), () => {
112+
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
113+
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
114+
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
115+
});
116+
}));
117+
});
118+
};
119+
120+
121+
const common = require('../common');
122+
module.exports.testServerKeepAliveTimeout(common, false);
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,5 @@
11
'use strict';
22

33
const common = require('../common');
4-
if (!common.hasCrypto) {
5-
common.skip('missing crypto');
6-
return;
7-
}
8-
const assert = require('assert');
9-
const https = require('https');
10-
const tls = require('tls');
11-
const fs = require('fs');
12-
13-
const tests = [];
14-
15-
const serverOptions = {
16-
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
17-
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
18-
};
19-
20-
function test(fn) {
21-
if (!tests.length) {
22-
process.nextTick(run);
23-
}
24-
tests.push(fn);
25-
}
26-
27-
function run() {
28-
const fn = tests.shift();
29-
if (fn) fn(run);
30-
}
31-
32-
test(function serverKeepAliveTimeoutWithPipeline(cb) {
33-
let requestCount = 0;
34-
process.on('exit', function() {
35-
assert.strictEqual(requestCount, 3);
36-
});
37-
const server = https.createServer(serverOptions, (req, res) => {
38-
requestCount++;
39-
res.end();
40-
});
41-
server.setTimeout(500, common.mustCall((socket) => {
42-
// End this test and call `run()` for the next test (if any).
43-
socket.destroy();
44-
server.close();
45-
cb();
46-
}));
47-
server.keepAliveTimeout = 50;
48-
server.listen(0, common.mustCall(() => {
49-
const options = {
50-
port: server.address().port,
51-
allowHalfOpen: true,
52-
rejectUnauthorized: false
53-
};
54-
const c = tls.connect(options, () => {
55-
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
56-
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
57-
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
58-
});
59-
}));
60-
});
61-
62-
test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) {
63-
let requestCount = 0;
64-
process.on('exit', () => {
65-
assert.strictEqual(requestCount, 3);
66-
});
67-
const server = https.createServer(serverOptions, (req, res) => {
68-
requestCount++;
69-
});
70-
server.setTimeout(500, common.mustCall((socket) => {
71-
// End this test and call `run()` for the next test (if any).
72-
socket.destroy();
73-
server.close();
74-
cb();
75-
}));
76-
server.keepAliveTimeout = 50;
77-
server.listen(0, common.mustCall(() => {
78-
const options = {
79-
port: server.address().port,
80-
allowHalfOpen: true,
81-
rejectUnauthorized: false
82-
};
83-
const c = tls.connect(options, () => {
84-
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n');
85-
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n');
86-
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n');
87-
});
88-
}));
89-
});
4+
const timeoutTest = require('./test-http-server-keep-alive-timeout');
5+
timeoutTest.testServerKeepAliveTimeout(common, true);

0 commit comments

Comments
 (0)