| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 728472a commit 8f32746
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,9 +21,10 @@ const server = https.createServer({ | |||
| 21 | 21 | cert: fixtures.readKey('agent8-cert.pem'), | |
| 22 | 22 | key: fixtures.readKey('agent8-key.pem'), | |
| 23 | 23 | }, (req, res) => { | |
| 24 | + console.log(`[Upstream server] responding to request for ${inspect(req.url)}`); | ||
| 24 | 25 | requests.add(`https://localhost:${server.address().port}${req.url}`); | |
| 25 | 26 | res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
| 26 | - res.end(`Response for ${req.url}`); | ||
| 27 | + res.end(`Response for ${inspect(req.url)}`); | ||
| 27 | 28 | }); | |
| 28 | 29 | ||
| 29 | 30 | server.listen(0); | |
@@ -54,7 +55,7 @@ https.globalAgent = new https.Agent({ | |||
| 54 | 55 | ||
| 55 | 56 | const severHost = `localhost:${server.address().port}`; | |
| 56 | 57 | ||
| 57 | - let counter = testCases.length; | ||
| 58 | + let counter = 0; | ||
| 58 | 59 | const expectedUrls = new Set(); | |
| 59 | 60 | const expectedProxyLogs = new Set(); | |
| 60 | 61 | for (const testCase of testCases) { | |
@@ -69,15 +70,20 @@ for (const testCase of testCases) { | |||
| 69 | 70 | https.request(url, (res) => { | |
| 70 | 71 | res.on('error', common.mustNotCall()); | |
| 71 | 72 | res.setEncoding('utf8'); | |
| 72 | - res.on('data', () => {}); | ||
| 73 | - res.on('end', common.mustCall(() => { | ||
| 74 | - console.log(`#${counter--} eneded response for: ${inspect(url)}`); | ||
| 73 | + res.on('data', (data) => { | ||
| 74 | + console.log(`[Proxy client] Received response from server for ${inspect(url)}: ${data.toString()}`); | ||
| 75 | + }); | ||
| 76 | + res.on('close', common.mustCall(() => { | ||
| 77 | + console.log(`[Proxy client] #${++counter} closed request for: ${inspect(url)}`); | ||
| 75 | 78 | // Finished all test cases. | |
| 76 | - if (counter === 0) { | ||
| 77 | - proxy.close(); | ||
| 78 | - server.close(); | ||
| 79 | - assert.deepStrictEqual(requests, expectedUrls); | ||
| 80 | - assert.deepStrictEqual(new Set(logs), expectedProxyLogs); | ||
| 79 | + if (counter === testCases.length) { | ||
| 80 | + setImmediate(() => { | ||
| 81 | + console.log('All requests completed, shutting down.'); | ||
| 82 | + proxy.close(); | ||
| 83 | + server.close(); | ||
| 84 | + assert.deepStrictEqual(requests, expectedUrls); | ||
| 85 | + assert.deepStrictEqual(new Set(logs), expectedProxyLogs); | ||
| 86 | + }); | ||
| 81 | 87 | } | |
| 82 | 88 | })); | |
| 83 | 89 | }).on('error', common.mustNotCall()).end(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,7 +63,7 @@ exports.createProxyServer = function(options = {}) { | |||
| 63 | 63 | }); | |
| 64 | 64 | ||
| 65 | 65 | res.on('error', (err) => { | |
| 66 | - logs.push({ error: err, source: 'proxy response' }); | ||
| 66 | + logs.push({ error: err, source: 'client response for request' }); | ||
| 67 | 67 | }); | |
| 68 | 68 | ||
| 69 | 69 | req.pipe(proxyReq, { end: true }); | |
@@ -75,7 +75,7 @@ exports.createProxyServer = function(options = {}) { | |||
| 75 | 75 | const [hostname, port] = req.url.split(':'); | |
| 76 | 76 | ||
| 77 | 77 | res.on('error', (err) => { | |
| 78 | - logs.push({ error: err, source: 'proxy response' }); | ||
| 78 | + logs.push({ error: err, source: 'client response for connect' }); | ||
| 79 | 79 | }); | |
| 80 | 80 | ||
| 81 | 81 | const proxyReq = net.connect(port, hostname, () => { | |
@@ -90,9 +90,13 @@ exports.createProxyServer = function(options = {}) { | |||
| 90 | 90 | }); | |
| 91 | 91 | ||
| 92 | 92 | proxyReq.on('error', (err) => { | |
| 93 | - logs.push({ error: err, source: 'proxy request' }); | ||
| 94 | - res.write('HTTP/1.1 500 Connection Error\r\n\r\n'); | ||
| 95 | - res.end('Proxy error: ' + err.message); | ||
| 93 | + logs.push({ error: err, source: 'proxy connect' }); | ||
| 94 | + // The proxy client might have already closed the connection | ||
| 95 | + // when the upstream connection fails. | ||
| 96 | + if (!res.writableEnded) { | ||
| 97 | + res.write('HTTP/1.1 500 Connection Error\r\n\r\n'); | ||
| 98 | + res.end('Proxy error: ' + err.message); | ||
| 99 | + } | ||
| 96 | 100 | }); | |
| 97 | 101 | }); | |
| 98 | 102 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments