@@ -32,6 +32,8 @@ import { createKibanaTestServer } from '../utils/kibana-test-server';
3232import { Server } from '../utils/server' ;
3333import { CLIMock } from '../utils/test-config' ;
3434import { THROTTLING_WARNING_MSG } from '../../src/helpers' ;
35+ import Straightforward from 'straightforward' ;
36+ import { AddressInfo } from 'net' ;
3537
3638describe ( 'Push' , ( ) => {
3739 let server : Server ;
@@ -47,8 +49,8 @@ describe('Push', () => {
4749 }
4850
4951 async function fakeProjectSetup (
50- settings ,
51- monitor ,
52+ settings : any ,
53+ monitor : any ,
5254 filename = 'synthetics.config.ts'
5355 ) {
5456 await writeFile (
@@ -353,4 +355,164 @@ heartbeat.monitors:
353355 expect ( output ) . toMatchSnapshot ( ) ;
354356 } ) ;
355357 } ) ;
358+
359+ describe ( 'Proxy options' , ( ) => {
360+ let requests : Array < any > = [ ] ;
361+ let proxyServer : Straightforward ;
362+ let tlsServer : any ;
363+ let proxyUrl : string ;
364+
365+ beforeAll ( async ( ) => {
366+ proxyServer = new Straightforward ( ) ;
367+ proxyServer . onConnect . use ( async ( { req } , next ) => {
368+ requests . push ( req ) ;
369+ return next ( ) ;
370+ } ) ;
371+ await proxyServer . listen ( Math . trunc ( 65e3 * Math . random ( ) ) ) ;
372+ tlsServer = await createKibanaTestServer ( '8.6.0' , true , ( req : any ) =>
373+ requests . push ( req )
374+ ) ;
375+ const server = proxyServer . server . address ( ) as AddressInfo ;
376+ proxyUrl = `http://127.0.0.1:${ server . port } ` ;
377+ } ) ;
378+
379+ afterAll ( async ( ) => {
380+ proxyServer ?. close ( ) ;
381+ tlsServer ?. close ( ) ;
382+ } ) ;
383+
384+ beforeEach ( ( ) => {
385+ requests = [ ] ;
386+ } ) ;
387+
388+ it ( 'enables proxy based on HTTP_PROXY' , async ( ) => {
389+ await fakeProjectSetup (
390+ { project : { id : 'test-project' , space : 'dummy' , url : server . PREFIX } } ,
391+ { locations : [ 'test-loc' ] , schedule : 3 }
392+ ) ;
393+ const testJourney = join ( PROJECT_DIR , 'chunk.journey.ts' ) ;
394+ await writeFile (
395+ testJourney ,
396+ `import {journey, monitor} from '../../../';
397+ journey('a', () => monitor.use({ tags: ['chunk'] }));
398+ journey('b', () => monitor.use({ tags: ['chunk'] }));`
399+ ) ;
400+ const output = await runPush ( [ ...DEFAULT_ARGS , '--tags' , 'chunk' ] , {
401+ CHUNK_SIZE : '1' ,
402+ HTTP_PROXY : proxyUrl ,
403+ } ) ;
404+ await rm ( testJourney , { force : true } ) ;
405+ expect ( output ) . toContain ( 'Added(2)' ) ;
406+ expect ( output ) . toContain ( 'creating or updating 1 monitors' ) ;
407+ expect ( output ) . toContain ( '✓ Pushed:' ) ;
408+ expect ( requests . length ) . toBeGreaterThan ( 0 ) ;
409+ } ) ;
410+
411+ it ( 'honors NO_PROXY with env variables' , async ( ) => {
412+ await fakeProjectSetup (
413+ { project : { id : 'test-project' , space : 'dummy' , url : server . PREFIX } } ,
414+ { locations : [ 'test-loc' ] , schedule : 3 }
415+ ) ;
416+ const testJourney = join ( PROJECT_DIR , 'chunk.journey.ts' ) ;
417+ await writeFile (
418+ testJourney ,
419+ `import {journey, monitor} from '../../../';
420+ journey('a', () => monitor.use({ tags: ['chunk'] }));
421+ journey('b', () => monitor.use({ tags: ['chunk'] }));`
422+ ) ;
423+ const output = await runPush ( [ ...DEFAULT_ARGS , '--tags' , 'chunk' ] , {
424+ CHUNK_SIZE : '1' ,
425+ HTTP_PROXY : proxyUrl ,
426+ NO_PROXY : '*' ,
427+ } ) ;
428+ await rm ( testJourney , { force : true } ) ;
429+ expect ( output ) . toContain ( 'Added(2)' ) ;
430+ expect ( output ) . toContain ( 'creating or updating 1 monitors' ) ;
431+ expect ( output ) . toContain ( '✓ Pushed:' ) ;
432+ expect ( requests ) . toHaveLength ( 0 ) ;
433+ } ) ;
434+
435+ it ( 'enables proxy based on HTTPS_PROXY' , async ( ) => {
436+ await fakeProjectSetup (
437+ {
438+ project : {
439+ id : 'test-project' ,
440+ space : 'dummy' ,
441+ url : tlsServer . PREFIX ,
442+ } ,
443+ } ,
444+ { locations : [ 'test-loc' ] , schedule : 3 }
445+ ) ;
446+ const testJourney = join ( PROJECT_DIR , 'chunk.journey.ts' ) ;
447+ await writeFile (
448+ testJourney ,
449+ `import {journey, monitor} from '../../../';
450+ journey('a', () => monitor.use({ tags: ['chunk'] }));
451+ journey('b', () => monitor.use({ tags: ['chunk'] }));`
452+ ) ;
453+ const output = await runPush ( [ ...DEFAULT_ARGS , '--tags' , 'chunk' ] , {
454+ CHUNK_SIZE : '1' ,
455+ HTTPS_PROXY : proxyUrl ,
456+ NODE_TLS_REJECT_UNAUTHORIZED : '0' ,
457+ } ) ;
458+ await rm ( testJourney , { force : true } ) ;
459+ expect ( output ) . toContain ( 'Added(2)' ) ;
460+ expect ( output ) . toContain ( 'creating or updating 1 monitors' ) ;
461+ expect ( output ) . toContain ( '✓ Pushed:' ) ;
462+ expect ( requests . length ) . toBeGreaterThan ( 0 ) ;
463+ } ) ;
464+
465+ it ( 'enables proxy based on --proxy-uri' , async ( ) => {
466+ await fakeProjectSetup (
467+ {
468+ project : { id : 'test-project' , space : 'dummy' , url : server . PREFIX } ,
469+ proxy : { uri : proxyUrl } ,
470+ } ,
471+ { locations : [ 'test-loc' ] , schedule : 3 }
472+ ) ;
473+ const testJourney = join ( PROJECT_DIR , 'chunk.journey.ts' ) ;
474+ await writeFile (
475+ testJourney ,
476+ `import {journey, monitor} from '../../../';
477+ journey('a', () => monitor.use({ tags: ['chunk'] }));
478+ journey('b', () => monitor.use({ tags: ['chunk'] }));`
479+ ) ;
480+ const output = await runPush (
481+ [ ...DEFAULT_ARGS , '--tags' , 'chunk' , '--proxy-uri' , proxyUrl ] ,
482+ {
483+ CHUNK_SIZE : '1' ,
484+ }
485+ ) ;
486+ await rm ( testJourney , { force : true } ) ;
487+ expect ( output ) . toContain ( 'Added(2)' ) ;
488+ expect ( output ) . toContain ( 'creating or updating 1 monitors' ) ;
489+ expect ( output ) . toContain ( '✓ Pushed:' ) ;
490+ expect ( requests . length ) . toBeGreaterThan ( 0 ) ;
491+ } ) ;
492+
493+ it ( 'enables proxy based on proxy settings' , async ( ) => {
494+ await fakeProjectSetup (
495+ {
496+ project : { id : 'test-project' , space : 'dummy' , url : server . PREFIX } ,
497+ proxy : { uri : proxyUrl } ,
498+ } ,
499+ { locations : [ 'test-loc' ] , schedule : 3 }
500+ ) ;
501+ const testJourney = join ( PROJECT_DIR , 'chunk.journey.ts' ) ;
502+ await writeFile (
503+ testJourney ,
504+ `import {journey, monitor} from '../../../';
505+ journey('a', () => monitor.use({ tags: ['chunk'] }));
506+ journey('b', () => monitor.use({ tags: ['chunk'] }));`
507+ ) ;
508+ const output = await runPush ( [ ...DEFAULT_ARGS , '--tags' , 'chunk' ] , {
509+ CHUNK_SIZE : '1' ,
510+ } ) ;
511+ await rm ( testJourney , { force : true } ) ;
512+ expect ( output ) . toContain ( 'Added(2)' ) ;
513+ expect ( output ) . toContain ( 'creating or updating 1 monitors' ) ;
514+ expect ( output ) . toContain ( '✓ Pushed:' ) ;
515+ expect ( requests . length ) . toBeGreaterThan ( 0 ) ;
516+ } ) ;
517+ } ) ;
356518} ) ;
0 commit comments