@@ -15,23 +15,22 @@ const {
15
15
getGlobalDispatcher,
16
16
setGlobalDispatcher,
17
17
request,
18
- Pool,
19
- Dispatcher
18
+ Pool
20
19
} = require ( '../..' )
21
20
const { InvalidArgumentError } = require ( '../../lib/core/errors' )
22
- const { Proxy } = interceptors
21
+ const { proxy : proxyInterceptor } = interceptors
23
22
24
23
test ( 'should throw error when no uri is provided' , t => {
25
24
t = tspl ( t , { plan : 2 } )
26
- t . throws ( ( ) => new Proxy ( ) , InvalidArgumentError )
27
- t . throws ( ( ) => new Proxy ( null , { } ) , InvalidArgumentError )
25
+ t . throws ( ( ) => proxyInterceptor ( ) , InvalidArgumentError )
26
+ t . throws ( ( ) => proxyInterceptor ( { } ) , InvalidArgumentError )
28
27
} )
29
28
30
29
test ( 'using auth in combination with token should throw' , t => {
31
30
t = tspl ( t , { plan : 1 } )
32
31
t . throws (
33
32
( ) =>
34
- new Proxy ( { } , {
33
+ proxyInterceptor ( {
35
34
auth : 'foo' ,
36
35
token : 'Bearer bar' ,
37
36
uri : 'http://example.com'
@@ -42,26 +41,20 @@ test('using auth in combination with token should throw', t => {
42
41
43
42
test ( 'should accept string, URL and object as options' , t => {
44
43
t = tspl ( t , { plan : 3 } )
45
- t . doesNotThrow ( ( ) => new Proxy ( { } , 'http://example.com' ) )
46
- t . doesNotThrow ( ( ) => new Proxy ( { } , new URL ( 'http://example.com' ) ) )
47
- t . doesNotThrow ( ( ) => new Proxy ( { } , { uri : 'http://example.com' } ) )
44
+ t . doesNotThrow ( ( ) => proxyInterceptor ( 'http://example.com' ) )
45
+ t . doesNotThrow ( ( ) => proxyInterceptor ( new URL ( 'http://example.com' ) ) )
46
+ t . doesNotThrow ( ( ) => proxyInterceptor ( { uri : 'http://example.com' } ) )
48
47
} )
49
48
50
- test ( 'use proxy-agent to connect through proxy' , { skip : true } , async t => {
51
- t = tspl ( t , { plan : 8 } )
52
- const CustomDispatcher = class extends Dispatcher {
53
- constructor ( dispatcher ) {
54
- super ( )
55
- this . dispatcher = dispatcher
56
- }
57
-
58
- dispatch ( opts , handler ) {
59
- t . ok ( true , 'should call dispatch' )
60
- return this . dispatcher . dispatch ( opts , handler )
61
- }
49
+ test ( 'should work with nested dispatch' , async t => {
50
+ t = tspl ( t , { plan : 7 } )
51
+ let counter = 0
52
+ const customDispatch = dispatcher => {
53
+ const binded = dispatcher . dispatch . bind ( dispatcher )
54
+ return ( opts , handler ) => {
55
+ counter ++
62
56
63
- close ( ) {
64
- return this . dispatcher . close ( )
57
+ return binded ( opts , handler )
65
58
}
66
59
}
67
60
const server = await buildServer ( )
@@ -73,9 +66,9 @@ test('use proxy-agent to connect through proxy', { skip: true }, async t => {
73
66
const client = new Client ( serverUrl )
74
67
const parsedOrigin = new URL ( serverUrl )
75
68
const dispatcher = client . compose ( [
76
- ( dispatcher ) => new CustomDispatcher ( dispatcher ) ,
77
- ( dispatcher ) => new Proxy ( dispatcher , proxyUrl ) ,
78
- ( dispatcher ) => new CustomDispatcher ( dispatcher )
69
+ customDispatch , // not called
70
+ proxyInterceptor ( proxyUrl ) , // chain restarted here
71
+ customDispatch
79
72
] )
80
73
81
74
proxy . on ( 'connect' , ( ) => {
@@ -107,13 +100,14 @@ test('use proxy-agent to connect through proxy', { skip: true }, async t => {
107
100
'keep-alive' ,
108
101
'should remain the connection open'
109
102
)
103
+ t . equal ( counter , 1 , 'should call customDispatch twice' )
110
104
111
105
server . close ( )
112
106
proxy . close ( )
113
107
await dispatcher . close ( )
114
108
} )
115
109
116
- test ( 'use proxy-interceptor to connect through proxy when composing dispatcher ' , async t => {
110
+ test ( 'use proxy-agent to connect through proxy' , async t => {
117
111
t = tspl ( t , { plan : 6 } )
118
112
const server = await buildServer ( )
119
113
const proxy = await buildProxy ( )
@@ -124,9 +118,7 @@ test('use proxy-interceptor to connect through proxy when composing dispatcher',
124
118
const client = new Client ( serverUrl )
125
119
const parsedOrigin = new URL ( serverUrl )
126
120
127
- const dispatcher = client . compose (
128
- dispatcher => new Proxy ( dispatcher , proxyUrl )
129
- )
121
+ const dispatcher = client . compose ( proxyInterceptor ( proxyUrl ) )
130
122
131
123
proxy . on ( 'connect' , ( ) => {
132
124
t . ok ( true , 'should connect to proxy' )
@@ -194,7 +186,7 @@ test('use proxy agent to connect through proxy using Pool', async t => {
194
186
}
195
187
const client = new Client ( serverUrl )
196
188
const dispatcher = client . compose (
197
- dispatcher => new Proxy ( dispatcher , {
189
+ proxyInterceptor ( {
198
190
auth : Buffer . from ( 'user:pass' ) . toString ( 'base64' ) ,
199
191
uri : proxyUrl ,
200
192
clientFactory
@@ -227,7 +219,7 @@ test('use proxy-agent to connect through proxy using path with params', async t
227
219
const proxyUrl = `http://localhost:${ proxy . address ( ) . port } `
228
220
const client = new Client ( serverUrl )
229
221
const parsedOrigin = new URL ( serverUrl )
230
- const dispatcher = client . compose ( dispatcher => new Proxy ( dispatcher , proxyUrl ) )
222
+ const dispatcher = client . compose ( proxyInterceptor ( proxyUrl ) )
231
223
232
224
proxy . on ( 'connect' , ( ) => {
233
225
t . ok ( true , 'should call proxy' )
@@ -272,7 +264,7 @@ test('use proxy-agent to connect through proxy with basic auth in URL', async t
272
264
const proxyUrl = `http://user:pass@localhost:${ proxy . address ( ) . port } `
273
265
const client = new Client ( serverUrl )
274
266
const parsedOrigin = new URL ( serverUrl )
275
- const dispatcher = client . compose ( dispatcher => new Proxy ( dispatcher , proxyUrl ) )
267
+ const dispatcher = client . compose ( proxyInterceptor ( proxyUrl ) )
276
268
277
269
proxy . authenticate = function ( req , fn ) {
278
270
t . ok ( true , 'authentication should be called' )
@@ -326,7 +318,7 @@ test('use proxy-agent with auth', async t => {
326
318
const client = new Client ( serverUrl )
327
319
const parsedOrigin = new URL ( serverUrl )
328
320
const dispatcher = client . compose (
329
- dispatcher => new Proxy ( dispatcher , {
321
+ proxyInterceptor ( {
330
322
auth : Buffer . from ( 'user:pass' ) . toString ( 'base64' ) ,
331
323
uri : proxyUrl
332
324
} )
@@ -384,7 +376,7 @@ test('use proxy-agent with token', async t => {
384
376
const client = new Client ( serverUrl )
385
377
const parsedOrigin = new URL ( serverUrl )
386
378
const dispatcher = client . compose (
387
- dispatcher => new Proxy ( dispatcher , {
379
+ proxyInterceptor ( {
388
380
token : `Bearer ${ Buffer . from ( 'user:pass' ) . toString ( 'base64' ) } ` ,
389
381
uri : proxyUrl
390
382
} )
@@ -441,7 +433,7 @@ test('use proxy-agent with custom headers', async t => {
441
433
const proxyUrl = `http://user:pass@localhost:${ proxy . address ( ) . port } `
442
434
const client = new Client ( serverUrl )
443
435
const dispatcher = client . compose (
444
- dispatcher => new Proxy ( dispatcher , {
436
+ proxyInterceptor ( {
445
437
uri : proxyUrl ,
446
438
headers : {
447
439
'User-Agent' : 'Foobar/1.0.0'
@@ -479,7 +471,7 @@ test('sending proxy-authorization in request headers should throw', async t => {
479
471
const proxyUrl = `http://localhost:${ proxy . address ( ) . port } `
480
472
const client = new Client ( serverUrl )
481
473
const dispatcher = client . compose (
482
- dispatcher => new Proxy ( dispatcher , {
474
+ proxyInterceptor ( {
483
475
uri : proxyUrl
484
476
} )
485
477
)
@@ -541,7 +533,7 @@ test('use proxy-agent with setGlobalDispatcher', async t => {
541
533
const parsedOrigin = new URL ( serverUrl )
542
534
const defaultDispatcher = getGlobalDispatcher ( )
543
535
544
- setGlobalDispatcher ( defaultDispatcher . compose ( dispatcher => new Proxy ( dispatcher , proxyUrl ) ) )
536
+ setGlobalDispatcher ( defaultDispatcher . compose ( proxyInterceptor ( proxyUrl ) ) )
545
537
after ( ( ) => setGlobalDispatcher ( defaultDispatcher ) )
546
538
547
539
proxy . on ( 'connect' , ( ) => {
@@ -587,7 +579,7 @@ test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', async
587
579
588
580
const serverUrl = `http://localhost:${ server . address ( ) . port } `
589
581
const proxyUrl = `http://localhost:${ proxy . address ( ) . port } `
590
- setGlobalDispatcher ( defaultDispatcher . compose ( dispatcher => new Proxy ( dispatcher , proxyUrl ) ) )
582
+ setGlobalDispatcher ( defaultDispatcher . compose ( proxyInterceptor ( proxyUrl ) ) )
591
583
592
584
after ( ( ) => setGlobalDispatcher ( defaultDispatcher ) )
593
585
@@ -638,7 +630,7 @@ test('should throw when proxy does not return 200', async t => {
638
630
return false
639
631
}
640
632
641
- const client = new Client ( serverUrl ) . compose ( dispatcher => new Proxy ( dispatcher , proxyUrl ) )
633
+ const client = new Client ( serverUrl ) . compose ( proxyInterceptor ( proxyUrl ) )
642
634
try {
643
635
await client . request ( { path : '/' , method : 'GET' } )
644
636
t . fail ( )
@@ -664,7 +656,7 @@ test('pass ProxyAgent proxy status code error when using fetch - #2161', async t
664
656
return false
665
657
}
666
658
667
- const client = new Client ( serverUrl ) . compose ( dispatcher => new Proxy ( dispatcher , proxyUrl ) )
659
+ const client = new Client ( serverUrl ) . compose ( proxyInterceptor ( proxyUrl ) )
668
660
try {
669
661
await fetch ( serverUrl , { dispatcher : client } )
670
662
t . fail ( )
@@ -686,7 +678,7 @@ test('Proxy via HTTP to HTTPS endpoint', async t => {
686
678
const serverUrl = `https://localhost:${ server . address ( ) . port } `
687
679
const proxyUrl = `http://localhost:${ proxy . address ( ) . port } `
688
680
const client = new Client ( serverUrl ) . compose (
689
- dispatcher => new Proxy ( dispatcher , {
681
+ proxyInterceptor ( {
690
682
uri : proxyUrl ,
691
683
requestTls : {
692
684
ca : [
@@ -750,7 +742,7 @@ test('Proxy via HTTPS to HTTPS endpoint', async t => {
750
742
const serverUrl = `https://localhost:${ server . address ( ) . port } `
751
743
const proxyUrl = `https://localhost:${ proxy . address ( ) . port } `
752
744
const proxyAgent = new Client ( serverUrl ) . compose (
753
- dispatcher => new Proxy ( dispatcher , {
745
+ proxyInterceptor ( {
754
746
uri : proxyUrl ,
755
747
proxyTls : {
756
748
ca : [
@@ -825,7 +817,7 @@ test('Proxy via HTTPS to HTTP endpoint', async t => {
825
817
const serverUrl = `http://localhost:${ server . address ( ) . port } `
826
818
const proxyUrl = `https://localhost:${ proxy . address ( ) . port } `
827
819
const proxyAgent = new Client ( serverUrl ) . compose (
828
- dispatcher => new Proxy ( dispatcher , {
820
+ proxyInterceptor ( {
829
821
uri : proxyUrl ,
830
822
proxyTls : {
831
823
ca : [
@@ -881,7 +873,7 @@ test('Proxy via HTTP to HTTP endpoint', async t => {
881
873
882
874
const serverUrl = `http://localhost:${ server . address ( ) . port } `
883
875
const proxyUrl = `http://localhost:${ proxy . address ( ) . port } `
884
- const proxyAgent = new Client ( serverUrl ) . compose ( dispatcher => new Proxy ( dispatcher , proxyUrl ) )
876
+ const proxyAgent = new Client ( serverUrl ) . compose ( proxyInterceptor ( proxyUrl ) )
885
877
886
878
server . on ( 'request' , function ( req , res ) {
887
879
t . ok ( ! req . connection . encrypted )
0 commit comments