1
- var tap = require ( 'tap' ) ;
2
- var test = tap . test ;
3
- var url = require ( 'url' ) ;
4
- var http = require ( 'http' ) ;
5
- var nock = require ( 'nock' ) ;
6
- var request = require ( '../src/lib/request' ) ;
7
-
8
- var proxyPort = 4242 ;
9
- var httpRequestHost = 'http://localhost:8000' ;
10
- var httpsRequestHost = 'https://snyk.io:443' ;
11
- var requestPath = '/api/v1/verify/token' ;
1
+ const tap = require ( 'tap' ) ;
2
+ const test = tap . test ;
3
+ const url = require ( 'url' ) ;
4
+ const http = require ( 'http' ) ;
5
+ const nock = require ( 'nock' ) ;
6
+ const request = require ( '../src/lib/request' ) ;
7
+
8
+ const proxyPort = 4242 ;
9
+ const httpRequestHost = 'http://localhost:8000' ;
10
+ const httpsRequestHost = 'https://snyk.io:443' ;
11
+ const requestPath = '/api/v1/verify/token' ;
12
12
13
13
/**
14
14
* Verify support for http(s) proxy from environments variables
@@ -51,10 +51,12 @@ test('request respects proxy environment variables', function(t) {
51
51
t . teardown ( ( ) => {
52
52
process . env . NO_PROXY = tmpNoProxy ;
53
53
delete process . env . http_proxy ;
54
+ delete process . env . HTTP_PROXY ;
55
+ delete global . GLOBAL_AGENT ;
54
56
} ) ;
55
57
56
58
process . env . http_proxy = `http://localhost:${ proxyPort } ` ;
57
- var proxy = http . createServer ( function ( req , res ) {
59
+ const proxy = http . createServer ( function ( req , res ) {
58
60
t . equal ( req . url , httpRequestHost + requestPath , 'http_proxy url ok' ) ;
59
61
res . end ( ) ;
60
62
} ) ;
@@ -63,6 +65,7 @@ test('request respects proxy environment variables', function(t) {
63
65
return request ( { method : 'post' , url : httpRequestHost + requestPath } )
64
66
. catch ( ( err ) => t . fail ( err . message ) )
65
67
. then ( ( ) => {
68
+ t . equal ( process . env . http_proxy , process . env . HTTP_PROXY ) ;
66
69
proxy . close ( ) ;
67
70
} ) ;
68
71
} ) ;
@@ -76,6 +79,7 @@ test('request respects proxy environment variables', function(t) {
76
79
t . teardown ( ( ) => {
77
80
process . env . NO_PROXY = tmpNoProxy ;
78
81
delete process . env . HTTP_PROXY ;
82
+ delete global . GLOBAL_AGENT ;
79
83
} ) ;
80
84
81
85
process . env . HTTP_PROXY = `http://localhost:${ proxyPort } ` ;
@@ -93,8 +97,20 @@ test('request respects proxy environment variables', function(t) {
93
97
} ) ;
94
98
95
99
t . test ( 'https_proxy' , function ( t ) {
100
+ // NO_PROXY is set in CircleCI and brakes test purpose
101
+ const tmpNoProxy = process . env . NO_PROXY ;
102
+ delete process . env . NO_PROXY ;
103
+
104
+ t . teardown ( ( ) => {
105
+ process . env . NO_PROXY = tmpNoProxy ;
106
+ delete process . env . https_proxy ;
107
+ delete process . env . HTTPS_PROXY ;
108
+ delete global . GLOBAL_AGENT ;
109
+ } ) ;
110
+
111
+ // eslint-disable-next-line @typescript-eslint/camelcase
96
112
process . env . https_proxy = `http://localhost:${ proxyPort } ` ;
97
- var proxy = http . createServer ( ) ;
113
+ const proxy = http . createServer ( ) ;
98
114
proxy . setTimeout ( 1000 ) ;
99
115
proxy . on ( 'connect' , ( req , cltSocket , head ) => {
100
116
const proxiedUrl = url . parse ( `https://${ req . url } ` ) ;
@@ -123,14 +139,24 @@ test('request respects proxy environment variables', function(t) {
123
139
return request ( { method : 'post' , url : httpsRequestHost + requestPath } )
124
140
. catch ( ( ) => { } ) // client socket being closed generates an error here
125
141
. then ( ( ) => {
142
+ t . equal ( process . env . https_proxy , process . env . HTTPS_PROXY ) ;
126
143
proxy . close ( ) ;
127
- delete process . env . https_proxy ;
128
144
} ) ;
129
145
} ) ;
130
146
131
147
t . test ( 'HTTPS_PROXY' , function ( t ) {
148
+ // NO_PROXY is set in CircleCI and brakes test purpose
149
+ const tmpNoProxy = process . env . NO_PROXY ;
150
+ delete process . env . NO_PROXY ;
151
+
152
+ t . teardown ( ( ) => {
153
+ process . env . NO_PROXY = tmpNoProxy ;
154
+ delete process . env . HTTPS_PROXY ;
155
+ delete global . GLOBAL_AGENT ;
156
+ } ) ;
157
+
132
158
process . env . HTTPS_PROXY = `http://localhost:${ proxyPort } ` ;
133
- var proxy = http . createServer ( ) ;
159
+ const proxy = http . createServer ( ) ;
134
160
proxy . setTimeout ( 1000 ) ;
135
161
proxy . on ( 'connect' , ( req , cltSocket , head ) => {
136
162
const proxiedUrl = url . parse ( `https://${ req . url } ` ) ;
@@ -160,7 +186,6 @@ test('request respects proxy environment variables', function(t) {
160
186
. catch ( ( ) => { } ) // client socket being closed generates an error here
161
187
. then ( ( ) => {
162
188
proxy . close ( ) ;
163
- delete process . env . HTTPS_PROXY ;
164
189
} ) ;
165
190
} ) ;
166
191
} ) ;
0 commit comments