1
1
import FormData from 'form-data' ;
2
- import type { Agent } from 'https' ;
2
+ import { HttpProxyAgent } from 'http-proxy-agent' ;
3
+ import { Agent } from 'https' ;
4
+ import { HttpsProxyAgent } from 'https-proxy-agent' ;
3
5
import { mock } from 'jest-mock-extended' ;
4
6
import type {
5
7
IHttpRequestMethods ,
@@ -19,6 +21,7 @@ import {
19
21
applyPaginationRequestData ,
20
22
convertN8nRequestToAxios ,
21
23
createFormDataObject ,
24
+ getAgentWithProxy ,
22
25
httpRequest ,
23
26
invokeAxios ,
24
27
parseRequestObject ,
@@ -28,7 +31,7 @@ import {
28
31
29
32
describe ( 'Request Helper Functions' , ( ) => {
30
33
describe ( 'proxyRequestToAxios' , ( ) => {
31
- const baseUrl = 'http ://example.de' ;
34
+ const baseUrl = 'https ://example.de' ;
32
35
const workflow = mock < Workflow > ( ) ;
33
36
const hooks = mock < ExecutionLifecycleHooks > ( ) ;
34
37
const additionalData = mock < IWorkflowExecuteAdditionalData > ( { hooks } ) ;
@@ -66,7 +69,7 @@ describe('Request Helper Functions', () => {
66
69
expect ( error . options ) . toMatchObject ( {
67
70
headers : { Accept : '*/*' } ,
68
71
method : 'get' ,
69
- url : 'http ://example.de/test' ,
72
+ url : 'https ://example.de/test' ,
70
73
} ) ;
71
74
expect ( error . config ) . toBeUndefined ( ) ;
72
75
expect ( error . message ) . toEqual ( '403 - "Forbidden"' ) ;
@@ -165,7 +168,7 @@ describe('Request Helper Functions', () => {
165
168
} ) ;
166
169
167
170
describe ( 'invokeAxios' , ( ) => {
168
- const baseUrl = 'http ://example.de' ;
171
+ const baseUrl = 'https ://example.de' ;
169
172
170
173
beforeEach ( ( ) => {
171
174
nock . cleanAll ( ) ;
@@ -346,7 +349,11 @@ describe('Request Helper Functions', () => {
346
349
const axiosOptions = await parseRequestObject ( requestObject ) ;
347
350
expect ( axiosOptions . beforeRedirect ) . toBeDefined ;
348
351
// eslint-disable-next-line @typescript-eslint/no-explicit-any
349
- const redirectOptions : Record < string , any > = { agents : { } , hostname : 'example.de' } ;
352
+ const redirectOptions : Record < string , any > = {
353
+ agents : { } ,
354
+ hostname : 'example.de' ,
355
+ href : requestObject . uri ,
356
+ } ;
350
357
axiosOptions . beforeRedirect ! ( redirectOptions , mock ( ) ) ;
351
358
expect ( redirectOptions . agent ) . toEqual ( redirectOptions . agents . https ) ;
352
359
expect ( ( redirectOptions . agent as Agent ) . options ) . toEqual ( {
@@ -862,4 +869,82 @@ describe('Request Helper Functions', () => {
862
869
scope . done ( ) ;
863
870
} ) ;
864
871
} ) ;
872
+
873
+ describe ( 'getAgentWithProxy' , ( ) => {
874
+ const baseUrlHttps = 'https://example.com' ;
875
+ const baseUrlHttp = 'http://example.com' ;
876
+ const proxyUrlHttps = 'http://proxy-for-https.com:8080/' ;
877
+ const proxyUrlHttp = 'http://proxy-for-http.com:8080/' ;
878
+
879
+ test ( 'should return a regular agent when no proxy is set' , async ( ) => {
880
+ const { agent, protocol } = getAgentWithProxy ( {
881
+ targetUrl : baseUrlHttps ,
882
+ } ) ;
883
+ expect ( protocol ) . toEqual ( 'https' ) ;
884
+ expect ( agent ) . toBeInstanceOf ( Agent ) ;
885
+ } ) ;
886
+
887
+ test ( 'should use a proxyConfig object' , async ( ) => {
888
+ const { agent, protocol } = getAgentWithProxy ( {
889
+ targetUrl : baseUrlHttps ,
890
+ proxyConfig : {
891
+ host : 'proxy-for-https.com' ,
892
+ port : 8080 ,
893
+ } ,
894
+ } ) ;
895
+ expect ( protocol ) . toEqual ( 'https' ) ;
896
+ expect ( ( agent as HttpsProxyAgent < string > ) . proxy . href ) . toEqual ( proxyUrlHttps ) ;
897
+ } ) ;
898
+
899
+ test ( 'should use a proxyConfig string' , async ( ) => {
900
+ const { agent, protocol } = getAgentWithProxy ( {
901
+ targetUrl : baseUrlHttps ,
902
+ proxyConfig : proxyUrlHttps ,
903
+ } ) ;
904
+ expect ( agent ) . toBeInstanceOf ( HttpsProxyAgent ) ;
905
+ expect ( protocol ) . toEqual ( 'https' ) ;
906
+ expect ( ( agent as HttpsProxyAgent < string > ) . proxy . href ) . toEqual ( proxyUrlHttps ) ;
907
+ } ) ;
908
+
909
+ describe ( 'environment variables' , ( ) => {
910
+ let originalEnv : NodeJS . ProcessEnv ;
911
+
912
+ beforeAll ( ( ) => {
913
+ originalEnv = { ...process . env } ;
914
+ process . env . HTTP_PROXY = proxyUrlHttp ;
915
+ process . env . HTTPS_PROXY = proxyUrlHttps ;
916
+ process . env . NO_PROXY = 'should-not-proxy.com' ;
917
+ } ) ;
918
+
919
+ afterAll ( ( ) => {
920
+ process . env = originalEnv ;
921
+ } ) ;
922
+
923
+ test ( 'should proxy http requests (HTTP_PROXY)' , async ( ) => {
924
+ const { agent, protocol } = getAgentWithProxy ( {
925
+ targetUrl : baseUrlHttp ,
926
+ } ) ;
927
+ expect ( protocol ) . toEqual ( 'http' ) ;
928
+ expect ( agent ) . toBeInstanceOf ( HttpProxyAgent ) ;
929
+ expect ( ( agent as HttpsProxyAgent < string > ) . proxy . href ) . toEqual ( proxyUrlHttp ) ;
930
+ } ) ;
931
+
932
+ test ( 'should proxy https requests (HTTPS_PROXY)' , async ( ) => {
933
+ const { agent, protocol } = getAgentWithProxy ( {
934
+ targetUrl : baseUrlHttps ,
935
+ } ) ;
936
+ expect ( protocol ) . toEqual ( 'https' ) ;
937
+ expect ( agent ) . toBeInstanceOf ( HttpsProxyAgent ) ;
938
+ expect ( ( agent as HttpsProxyAgent < string > ) . proxy . href ) . toEqual ( proxyUrlHttps ) ;
939
+ } ) ;
940
+
941
+ test ( 'should not proxy some hosts based on NO_PROXY' , async ( ) => {
942
+ const { agent, protocol } = getAgentWithProxy ( {
943
+ targetUrl : 'https://should-not-proxy.com/foo' ,
944
+ } ) ;
945
+ expect ( protocol ) . toEqual ( 'https' ) ;
946
+ expect ( agent ) . toBeInstanceOf ( Agent ) ;
947
+ } ) ;
948
+ } ) ;
949
+ } ) ;
865
950
} ) ;
0 commit comments