@@ -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+
131145const 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