@@ -18,10 +18,6 @@ const options = { |
18 | 18 | cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem'), |
19 | 19 | }; |
20 | 20 |
|
21 | | -const server = https.createServer(options, (req, res) => { |
22 | | - res.end('hello world\n'); |
23 | | -}); |
24 | | - |
25 | 21 | const expectedHeader = /^HTTP\/1.1 200 OK/; |
26 | 22 | const expectedBody = /hello world\n/; |
27 | 23 | const expectCertError = /^Error: unable to verify the first certificate$/; |
@@ -42,83 +38,109 @@ const checkRequest = (socket, server) => { |
42 | 38 | })); |
43 | 39 | }; |
44 | 40 |
|
| 41 | +function createServer() { |
| 42 | + return https.createServer(options, (req, res) => { |
| 43 | + res.end('hello world\n'); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
45 | 47 | // use option connect |
46 | | -server.listen(0, common.mustCall(() => { |
47 | | - const port = server.address().port; |
48 | | - const host = 'localhost'; |
49 | | - const options = { |
50 | | - port: port, |
51 | | - host: host, |
52 | | - rejectUnauthorized: false, |
53 | | - _agentKey: agent.getName({ |
| 48 | +{ |
| 49 | + const server = createServer(); |
| 50 | + server.listen(0, common.mustCall(() => { |
| 51 | + const port = server.address().port; |
| 52 | + const host = 'localhost'; |
| 53 | + const options = { |
54 | 54 | port: port, |
55 | 55 | host: host, |
56 | | - }), |
57 | | - }; |
| 56 | + rejectUnauthorized: false, |
| 57 | + _agentKey: agent.getName({ |
| 58 | + port: port, |
| 59 | + host: host, |
| 60 | + }), |
| 61 | + }; |
58 | 62 |
|
59 | | - const socket = agent.createConnection(options); |
60 | | - checkRequest(socket, server); |
61 | | -})); |
| 63 | + const socket = agent.createConnection(options); |
| 64 | + checkRequest(socket, server); |
| 65 | + })); |
| 66 | +} |
62 | 67 |
|
63 | 68 | // use port and option connect |
64 | | -server.listen(0, common.mustCall(() => { |
65 | | - const port = server.address().port; |
66 | | - const host = 'localhost'; |
67 | | - const options = { |
68 | | - rejectUnauthorized: false, |
69 | | - _agentKey: agent.getName({ |
70 | | - port: port, |
71 | | - host: host, |
72 | | - }), |
73 | | - }; |
74 | | - const socket = agent.createConnection(port, options); |
75 | | - checkRequest(socket, server); |
76 | | -})); |
| 69 | +{ |
| 70 | + const server = createServer(); |
| 71 | + server.listen(0, common.mustCall(() => { |
| 72 | + const port = server.address().port; |
| 73 | + const host = 'localhost'; |
| 74 | + const options = { |
| 75 | + rejectUnauthorized: false, |
| 76 | + _agentKey: agent.getName({ |
| 77 | + port: port, |
| 78 | + host: host, |
| 79 | + }), |
| 80 | + }; |
| 81 | + const socket = agent.createConnection(port, options); |
| 82 | + checkRequest(socket, server); |
| 83 | + })); |
| 84 | +} |
77 | 85 |
|
78 | 86 | // use port and host and option connect |
79 | | -server.listen(0, common.mustCall(() => { |
80 | | - const port = server.address().port; |
81 | | - const host = 'localhost'; |
82 | | - const options = { |
83 | | - rejectUnauthorized: false, |
84 | | - _agentKey: agent.getName({ |
85 | | - port: port, |
86 | | - host: host, |
87 | | - }), |
88 | | - }; |
89 | | - const socket = agent.createConnection(port, host, options); |
90 | | - checkRequest(socket, server); |
91 | | -})); |
| 87 | +{ |
| 88 | + const server = createServer(); |
| 89 | + server.listen(0, common.mustCall(() => { |
| 90 | + const port = server.address().port; |
| 91 | + const host = 'localhost'; |
| 92 | + const options = { |
| 93 | + rejectUnauthorized: false, |
| 94 | + _agentKey: agent.getName({ |
| 95 | + port: port, |
| 96 | + host: host, |
| 97 | + }), |
| 98 | + }; |
| 99 | + const socket = agent.createConnection(port, host, options); |
| 100 | + checkRequest(socket, server); |
| 101 | + })); |
| 102 | +} |
92 | 103 |
|
93 | 104 | // use port and host and option does not have agentKey |
94 | | -server.listen(0, common.mustCall(() => { |
95 | | - const port = server.address().port; |
96 | | - const host = 'localhost'; |
97 | | - const options = { |
98 | | - rejectUnauthorized: false, |
99 | | - }; |
100 | | - const socket = agent.createConnection(port, host, options); |
101 | | - checkRequest(socket, server); |
102 | | -})); |
| 105 | +{ |
| 106 | + const server = createServer(); |
| 107 | + server.listen(0, common.mustCall(() => { |
| 108 | + const port = server.address().port; |
| 109 | + const host = 'localhost'; |
| 110 | + const options = { |
| 111 | + rejectUnauthorized: false, |
| 112 | + }; |
| 113 | + const socket = agent.createConnection(port, host, options); |
| 114 | + checkRequest(socket, server); |
| 115 | + })); |
| 116 | +} |
103 | 117 |
|
104 | 118 | // options is null |
105 | | -server.listen(0, common.mustCall(() => { |
106 | | - const port = server.address().port; |
107 | | - const host = 'localhost'; |
108 | | - const options = null; |
109 | | - const socket = agent.createConnection(port, host, options); |
110 | | - socket.on('error', common.mustCall((e) => { |
111 | | - assert(expectCertError.test(e.toString())); |
| 119 | +{ |
| 120 | + const server = createServer(); |
| 121 | + server.listen(0, common.mustCall(() => { |
| 122 | + const port = server.address().port; |
| 123 | + const host = 'localhost'; |
| 124 | + const options = null; |
| 125 | + const socket = agent.createConnection(port, host, options); |
| 126 | + socket.on('error', common.mustCall((e) => { |
| 127 | + assert(expectCertError.test(e.toString())); |
| 128 | + server.close(); |
| 129 | + })); |
112 | 130 | })); |
113 | | -})); |
| 131 | +} |
114 | 132 |
|
115 | 133 | // options is undefined |
116 | | -server.listen(0, common.mustCall(() => { |
117 | | - const port = server.address().port; |
118 | | - const host = 'localhost'; |
119 | | - const options = undefined; |
120 | | - const socket = agent.createConnection(port, host, options); |
121 | | - socket.on('error', common.mustCall((e) => { |
122 | | - assert(expectCertError.test(e.toString())); |
| 134 | +{ |
| 135 | + const server = createServer(); |
| 136 | + server.listen(0, common.mustCall(() => { |
| 137 | + const port = server.address().port; |
| 138 | + const host = 'localhost'; |
| 139 | + const options = undefined; |
| 140 | + const socket = agent.createConnection(port, host, options); |
| 141 | + socket.on('error', common.mustCall((e) => { |
| 142 | + assert(expectCertError.test(e.toString())); |
| 143 | + server.close(); |
| 144 | + })); |
123 | 145 | })); |
124 | | -})); |
| 146 | +} |