1
1
'use strict'
2
2
3
3
const { test } = require ( 'tap' )
4
- const { request, setGlobalDispatcher, getGlobalDispatcher } = require ( '..' )
4
+ const { request, fetch , setGlobalDispatcher, getGlobalDispatcher } = require ( '..' )
5
5
const { InvalidArgumentError } = require ( '../lib/core/errors' )
6
6
const { readFileSync } = require ( 'fs' )
7
7
const { join } = require ( 'path' )
@@ -240,8 +240,9 @@ test('use proxy-agent with setGlobalDispatcher', async (t) => {
240
240
proxyAgent . close ( )
241
241
} )
242
242
243
- test ( 'ProxyAgent correctly sends headers when using fetch - #1355' , { skip : nodeMajor < 16 } , async ( t ) => {
244
- const { getGlobalDispatcher, setGlobalDispatcher, fetch } = require ( '../index' )
243
+ test ( 'ProxyAgent correctly sends headers when using fetch - #1355, #1623' , { skip : nodeMajor < 16 } , async ( t ) => {
244
+ t . plan ( 2 )
245
+ const defaultDispatcher = getGlobalDispatcher ( )
245
246
246
247
const server = await buildServer ( )
247
248
const proxy = await buildProxy ( )
@@ -250,7 +251,9 @@ test('ProxyAgent correctly sends headers when using fetch - #1355', { skip: node
250
251
const proxyUrl = `http://localhost:${ proxy . address ( ) . port } `
251
252
252
253
const proxyAgent = new ProxyAgent ( proxyUrl )
253
- const oldDispatcher = getGlobalDispatcher ( )
254
+ setGlobalDispatcher ( proxyAgent )
255
+
256
+ t . teardown ( ( ) => setGlobalDispatcher ( defaultDispatcher ) )
254
257
255
258
const expectedHeaders = {
256
259
host : `localhost:${ server . address ( ) . port } ` ,
@@ -263,16 +266,23 @@ test('ProxyAgent correctly sends headers when using fetch - #1355', { skip: node
263
266
'accept-encoding' : 'gzip, deflate'
264
267
}
265
268
269
+ const expectedProxyHeaders = {
270
+ host : `localhost:${ proxy . address ( ) . port } ` ,
271
+ connection : 'keep-alive'
272
+ }
273
+
274
+ proxy . on ( 'connect' , ( req , res ) => {
275
+ t . same ( req . headers , expectedProxyHeaders )
276
+ } )
277
+
266
278
server . on ( 'request' , ( req , res ) => {
267
279
t . same ( req . headers , expectedHeaders )
268
280
res . end ( 'goodbye' )
269
281
} )
270
282
271
- setGlobalDispatcher ( proxyAgent )
272
283
await fetch ( serverUrl , {
273
284
headers : { 'Test-header' : 'value' }
274
285
} )
275
- setGlobalDispatcher ( oldDispatcher )
276
286
277
287
server . close ( )
278
288
proxy . close ( )
0 commit comments