@@ -34,6 +34,7 @@ import { Server } from '../utils/server';
3434import { CLIMock } from '../utils/test-config' ;
3535import { mkdir , rm , writeFile } from 'fs/promises' ;
3636import { Straightforward } from 'straightforward' ;
37+ import { AddressInfo } from 'net' ;
3738
3839describe ( 'Locations' , ( ) => {
3940 const apiKey = 'foo' ;
@@ -83,10 +84,10 @@ describe('Locations', () => {
8384
8485 describe ( 'CLI command' , ( ) => {
8586 const PROJECT_DIR = join ( __dirname , 'new-project' ) ;
86- async function fakeProjectSetup ( settings ) {
87+ async function fakeProjectSetup ( settings : any ) {
8788 await writeFile (
8889 join ( PROJECT_DIR , 'synthetics.config.ts' ) ,
89- `export default { project: ${ JSON . stringify ( settings ) } }`
90+ `export default ${ JSON . stringify ( settings ) } `
9091 ) ;
9192 }
9293
@@ -97,10 +98,13 @@ describe('Locations', () => {
9798 await rm ( PROJECT_DIR , { recursive : true , force : true } ) ;
9899 } ) ;
99100
100- const runLocations = async ( args : Array < string > = [ ] ) => {
101- const cli = new CLIMock ( true )
101+ const runLocations = async (
102+ args : Array < string > = [ ] ,
103+ cliEnv : NodeJS . ProcessEnv = { }
104+ ) => {
105+ const cli = new CLIMock ( )
102106 . args ( [ 'locations' , ...args ] )
103- . run ( { cwd : PROJECT_DIR , env : process . env } ) ;
107+ . run ( { cwd : PROJECT_DIR , env : { ... process . env , ... cliEnv } } ) ;
104108 expect ( await cli . exitCode ) . toBe ( 0 ) ;
105109 return cli . stderr ( ) ;
106110 } ;
@@ -121,81 +125,92 @@ describe('Locations', () => {
121125 } ) ;
122126
123127 it ( 'use project url settings for private locations' , async ( ) => {
124- await fakeProjectSetup ( { url : server . PREFIX } ) ;
128+ await fakeProjectSetup ( { project : { url : server . PREFIX } } ) ;
125129 const output = await runLocations ( [ '--auth' , apiKey ] ) ;
126130 expect ( output ) . toContain ( `custom location 1` ) ;
127131 expect ( output ) . toContain ( `custom location 2` ) ;
128132 } ) ;
129133
130134 describe ( 'Proxy options' , ( ) => {
131135 let requests : Array < any > = [ ] ;
132- let proxyServer ;
136+ let proxyServer : Straightforward ;
133137 let tlsServer ;
138+ let proxyPort : number ;
134139
135140 beforeAll ( async ( ) => {
136141 proxyServer = new Straightforward ( ) ;
137142 proxyServer . onConnect . use ( async ( { req } , next ) => {
138143 requests . push ( req ) ;
139144 return next ( ) ;
140145 } ) ;
141- await proxyServer . listen ( 8019 ) ;
146+ await proxyServer . listen ( ) ;
142147 tlsServer = await Server . create ( { tls : true } ) ;
143148 tlsServer . route ( '/internal/uptime/service/locations' , ( req , res ) => {
144149 res . setHeader ( 'content-type' , 'application/json' ) ;
145150 res . end ( JSON . stringify ( { locations : LOCATIONS } ) ) ;
146151 } ) ;
152+ ( { port : proxyPort } = proxyServer . server . address ( ) as AddressInfo ) ;
147153 } ) ;
148154
149155 afterAll ( async ( ) => {
150156 proxyServer . close ( ) ;
157+ tlsServer . close ( ) ;
151158 } ) ;
152159
153160 beforeEach ( ( ) => {
154161 requests = [ ] ;
155- delete process . env . HTTP_PROXY ,
156- process . env . HTTP_PROXYS ,
157- process . env . NO_PROXY ,
158- process . env . NODE_TLS_REJECT_UNAUTHORIZED ;
159162 } ) ;
160163
161164 it ( 'enables proxy based on HTTP_PROXY' , async ( ) => {
162- process . env . HTTP_PROXY = 'http://localhost:8019' ;
163- await fakeProjectSetup ( { url : server . PREFIX } ) ;
164- const output = await runLocations ( [ '--auth' , apiKey ] ) ;
165+ await fakeProjectSetup ( { project : { url : server . PREFIX } } ) ;
166+ const output = await runLocations ( [ '--auth' , apiKey ] , {
167+ HTTP_PROXY : `http://localhost:${ proxyPort } ` ,
168+ } ) ;
165169 expect ( output ) . toContain ( `custom location 1` ) ;
166170 expect ( requests ) . toHaveLength ( 1 ) ;
167171 } ) ;
168172
169173 it ( 'honors NO_PROXY with env variables' , async ( ) => {
170- process . env . HTTP_PROXY = 'http://localhost:8019' ;
171- process . env . NO_PROXY = '*' ;
172- await fakeProjectSetup ( { url : server . PREFIX } ) ;
173- const output = await runLocations ( [ '--auth' , apiKey ] ) ;
174+ await fakeProjectSetup ( { project : { url : server . PREFIX } } ) ;
175+ const output = await runLocations ( [ '--auth' , apiKey ] , {
176+ NO_PROXY : '*' ,
177+ HTTP_PROXY : `http://localhost:${ proxyPort } ` ,
178+ } ) ;
174179 expect ( output ) . toContain ( `custom location 1` ) ;
175180 expect ( requests ) . toHaveLength ( 0 ) ;
176181 } ) ;
177182
178183 it ( 'enables proxy based on HTTPS_PROXY' , async ( ) => {
179- process . env . HTTPS_PROXY = 'http://localhost:8019' ;
180- process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0' ;
181- await fakeProjectSetup ( { url : tlsServer . PREFIX } ) ;
182- const output = await runLocations ( [ '--auth' , apiKey ] ) ;
183- delete process . env . NODE_TLS_REJECT_UNAUTHORIZED ;
184+ await fakeProjectSetup ( { project : { url : tlsServer . PREFIX } } ) ;
185+ const output = await runLocations ( [ '--auth' , apiKey ] , {
186+ HTTPS_PROXY : `http://localhost: ${ proxyPort } ` ,
187+ NODE_TLS_REJECT_UNAUTHORIZED : '0' ,
188+ } ) ;
184189 expect ( output ) . toContain ( `custom location 1` ) ;
185190 expect ( requests ) . toHaveLength ( 1 ) ;
186191 } ) ;
187192
188193 it ( 'enables proxy based on --proxy-uri' , async ( ) => {
189- await fakeProjectSetup ( { url : server . PREFIX } ) ;
194+ await fakeProjectSetup ( { project : { url : server . PREFIX } } ) ;
190195 const output = await runLocations ( [
191196 '--auth' ,
192197 apiKey ,
193198 '--proxy-uri' ,
194- ' http://localhost:8019' ,
199+ ` http://localhost:${ proxyPort } ` ,
195200 ] ) ;
196201 expect ( output ) . toContain ( `custom location 1` ) ;
197202 expect ( requests ) . toHaveLength ( 1 ) ;
198203 } ) ;
204+
205+ it ( 'enables proxy based on proxy settings' , async ( ) => {
206+ await fakeProjectSetup ( {
207+ project : { url : server . PREFIX } ,
208+ proxy : { uri : `http://localhost:${ proxyPort } ` } ,
209+ } ) ;
210+ const output = await runLocations ( [ '--auth' , apiKey ] ) ;
211+ expect ( output ) . toContain ( `custom location 1` ) ;
212+ expect ( requests ) . toHaveLength ( 1 ) ;
213+ } ) ;
199214 } ) ;
200215 } ) ;
201216} ) ;
0 commit comments