Skip to content

Commit cbcb0f7

Browse files
authored
Merge pull request #1 from proxymesh/test-all-modules-and-fix-failures
Test all modules in npm test; fix axios/got/superagent on 4xx
2 parents 967f12e + 9dac053 commit cbcb0f7

4 files changed

Lines changed: 40 additions & 7 deletions

File tree

lib/core/proxy-headers-agent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ export class ProxyHeadersAgent extends Agent {
9494
this.proxyAuth,
9595
this.proxyHeaders
9696
);
97-
9897
proxySocket.write(connectRequest);
9998
});
10099

@@ -153,7 +152,8 @@ export class ProxyHeadersAgent extends Agent {
153152
});
154153
});
155154

156-
return proxySocket;
155+
// Do not return proxySocket - the agent must use the stream from the callback
156+
// (tlsSocket) so the CONNECT handshake completes before any request is written.
157157
}
158158
}
159159

lib/core/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ export function buildConnectRequest(targetHost, targetPort, proxyAuth, proxyHead
5656
lines.push(`Proxy-Authorization: Basic ${proxyAuth}`);
5757
}
5858

59-
for (const [key, value] of Object.entries(proxyHeaders)) {
59+
const entries = proxyHeaders instanceof Map
60+
? [...proxyHeaders.entries()]
61+
: Object.entries(proxyHeaders || {});
62+
for (const [key, value] of entries) {
6063
lines.push(`${key}: ${value}`);
6164
}
6265

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"LICENSE"
4040
],
4141
"scripts": {
42-
"test": "node test/test_proxy_headers.js",
42+
"test": "node test/test_proxy_headers.js core axios node-fetch got undici superagent",
4343
"test:verbose": "node test/test_proxy_headers.js -v",
4444
"lint": "eslint lib test",
4545
"prepublishOnly": "npm test"

test/test_proxy_headers.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ function checkHeader(headers, headerName) {
128128
return null;
129129
}
130130

131+
/**
132+
* When SEND_PROXY_HEADER and SEND_PROXY_VALUE are set and we're checking the same header,
133+
* the proxy response must echo that value (e.g. X-ProxyMesh-IP).
134+
* @returns {string|null} Error message if expectation not met, null if OK or N/A.
135+
*/
136+
function validateSentHeaderValue(config, headerValue) {
137+
if (!config.sendProxyHeader || !config.sendProxyValue) return null;
138+
if (config.proxyHeader.toLowerCase() !== config.sendProxyHeader.toLowerCase()) return null;
139+
const expected = String(config.sendProxyValue).trim();
140+
const actual = headerValue ? String(headerValue).trim() : '';
141+
if (actual === expected) return null;
142+
return `Expected ${config.proxyHeader} to equal ${expected} (sent in request) but got ${actual}`;
143+
}
144+
131145
const AVAILABLE_TESTS = {
132146
async core(config) {
133147
try {
@@ -159,7 +173,10 @@ const AVAILABLE_TESTS = {
159173
? checkHeader(capturedHeaders, config.proxyHeader)
160174
: null;
161175

162-
if (headerValue) {
176+
const sentErr = validateSentHeaderValue(config, headerValue);
177+
if (sentErr) {
178+
resolve(new TestResult('core', false, null, sentErr, res.statusCode));
179+
} else if (headerValue) {
163180
resolve(new TestResult('core', true, headerValue, null, res.statusCode));
164181
} else {
165182
resolve(new TestResult('core', false, null,
@@ -189,9 +206,13 @@ const AVAILABLE_TESTS = {
189206
proxyHeaders: config.proxyHeadersToSend,
190207
});
191208

192-
const response = await client.get(config.testUrl);
209+
const response = await client.get(config.testUrl, {
210+
validateStatus: () => true,
211+
});
193212
const headerValue = checkHeader(response.headers, config.proxyHeader);
194213

214+
const sentErr = validateSentHeaderValue(config, headerValue);
215+
if (sentErr) return new TestResult('axios', false, null, sentErr);
195216
if (headerValue) {
196217
return new TestResult('axios', true, headerValue, null, response.status);
197218
}
@@ -214,6 +235,8 @@ const AVAILABLE_TESTS = {
214235

215236
const headerValue = checkHeader(response.proxyHeaders, config.proxyHeader);
216237

238+
const sentErr = validateSentHeaderValue(config, headerValue);
239+
if (sentErr) return new TestResult('node-fetch', false, null, sentErr);
217240
if (headerValue) {
218241
return new TestResult('node-fetch', true, headerValue, null, response.status);
219242
}
@@ -232,11 +255,14 @@ const AVAILABLE_TESTS = {
232255
const client = await createProxyGot({
233256
proxy: config.proxyUrl,
234257
proxyHeaders: config.proxyHeadersToSend,
258+
gotOptions: { throwHttpErrors: false },
235259
});
236260

237261
const response = await client(config.testUrl);
238262
const headerValue = checkHeader(response.headers, config.proxyHeader);
239263

264+
const sentErr = validateSentHeaderValue(config, headerValue);
265+
if (sentErr) return new TestResult('got', false, null, sentErr);
240266
if (headerValue) {
241267
return new TestResult('got', true, headerValue, null, response.statusCode);
242268
}
@@ -259,6 +285,8 @@ const AVAILABLE_TESTS = {
259285

260286
const headerValue = checkHeader(proxyHeaders, config.proxyHeader);
261287

288+
const sentErr = validateSentHeaderValue(config, headerValue);
289+
if (sentErr) return new TestResult('undici', false, null, sentErr);
262290
if (headerValue) {
263291
return new TestResult('undici', true, headerValue, null, statusCode);
264292
}
@@ -279,9 +307,11 @@ const AVAILABLE_TESTS = {
279307
proxyHeaders: config.proxyHeadersToSend,
280308
});
281309

282-
const response = await client.get(config.testUrl);
310+
const response = await client.get(config.testUrl).ok(() => true);
283311
const headerValue = checkHeader(response.headers, config.proxyHeader);
284312

313+
const sentErr = validateSentHeaderValue(config, headerValue);
314+
if (sentErr) return new TestResult('superagent', false, null, sentErr);
285315
if (headerValue) {
286316
return new TestResult('superagent', true, headerValue, null, response.status);
287317
}

0 commit comments

Comments
 (0)