|
1 | 1 | 'use strict';
|
2 | 2 |
|
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; |
7 | 9 |
|
8 |
| -const tests = []; |
| 10 | + if (isHttps) { |
| 11 | + if (!common.hasCrypto) { |
| 12 | + common.skip('missing crypto'); |
| 13 | + return; |
| 14 | + } |
9 | 15 |
|
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 | + }; |
42 | 25 | };
|
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 |
| -}); |
50 | 26 |
|
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') |
70 | 31 | };
|
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(); |
75 | 78 | });
|
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); |
0 commit comments