Skip to content

Commit 292fbf5

Browse files
committed
Add proxy options to synthetics config, additional unit tests
1 parent 5456cd9 commit 292fbf5

File tree

10 files changed

+228
-159
lines changed

10 files changed

+228
-159
lines changed

__tests__/locations/index.test.ts

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { Server } from '../utils/server';
3434
import { CLIMock } from '../utils/test-config';
3535
import { mkdir, rm, writeFile } from 'fs/promises';
3636
import { Straightforward } from 'straightforward';
37+
import { AddressInfo } from 'net';
3738

3839
describe('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
});

__tests__/options.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
*/
2525

2626
import { CliArgs } from '../src/common_types';
27-
import { normalizeOptions, parsePlaywrightOptions } from '../src/options';
27+
import {
28+
collectOpts,
29+
normalizeOptions,
30+
parseFileOption,
31+
parsePlaywrightOptions,
32+
} from '../src/options';
2833
import { join } from 'path';
2934

3035
describe('options', () => {
@@ -187,4 +192,27 @@ describe('options', () => {
187192
expect(Buffer.isBuffer(t.passphrase)).toBeFalsy();
188193
});
189194
});
195+
196+
describe('parseFileOption', () => {
197+
it('parses file', () => {
198+
expect(
199+
parseFileOption(join(__dirname, 'fixtures', 'synthetics.config.ts'))
200+
).toBeInstanceOf(Buffer);
201+
});
202+
it('parses string', () => {
203+
expect(parseFileOption('test')).toEqual('test');
204+
});
205+
});
206+
207+
describe('collectOpts', () => {
208+
it('collects options in the accumulator', () => {
209+
const opts = { a: 'a', b: 'b', c: true };
210+
const result = {};
211+
Object.entries(opts).forEach(([key, value]) => {
212+
collectOpts(key, result)(value);
213+
});
214+
215+
expect(result).toEqual(opts);
216+
});
217+
});
190218
});

__tests__/push/index.test.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { Server } from '../utils/server';
3333
import { CLIMock } from '../utils/test-config';
3434
import { THROTTLING_WARNING_MSG } from '../../src/helpers';
3535
import Straightforward from 'straightforward';
36+
import { AddressInfo } from 'net';
3637

3738
describe('Push', () => {
3839
let server: Server;
@@ -354,37 +355,36 @@ heartbeat.monitors:
354355
expect(output).toMatchSnapshot();
355356
});
356357
});
358+
357359
describe('Proxy options', () => {
358360
let requests: Array<any> = [];
359-
let proxyServer;
361+
let proxyServer: Straightforward;
360362
let tlsServer;
363+
let proxyPort: number;
361364

362365
beforeAll(async () => {
363366
proxyServer = new Straightforward();
364367
proxyServer.onConnect.use(async ({ req }, next) => {
365368
requests.push(req);
366369
return next();
367370
});
368-
await proxyServer.listen(8019);
371+
await proxyServer.listen();
369372
tlsServer = await createKibanaTestServer('8.6.0', true, (req: any) =>
370373
requests.push(req)
371374
);
375+
({ port: proxyPort } = proxyServer.server.address() as AddressInfo);
372376
});
373377

374378
afterAll(async () => {
375379
proxyServer.close();
380+
tlsServer.close();
376381
});
377382

378383
beforeEach(() => {
379384
requests = [];
380-
delete process.env.HTTP_PROXY,
381-
process.env.HTTP_PROXYS,
382-
process.env.NO_PROXY,
383-
process.env.NODE_TLS_REJECT_UNAUTHORIZED;
384385
});
385386

386387
it('enables proxy based on HTTP_PROXY', async () => {
387-
process.env.HTTP_PROXY = 'http://localhost:8019';
388388
await fakeProjectSetup(
389389
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
390390
{ locations: ['test-loc'], schedule: 3 }
@@ -398,6 +398,7 @@ heartbeat.monitors:
398398
);
399399
const output = await runPush([...DEFAULT_ARGS, '--tags', 'chunk'], {
400400
CHUNK_SIZE: '1',
401+
HTTP_PROXY: `http://localhost:${proxyPort}`,
401402
});
402403
await rm(testJourney, { force: true });
403404
expect(output).toContain('Added(2)');
@@ -407,8 +408,6 @@ heartbeat.monitors:
407408
});
408409

409410
it('honors NO_PROXY with env variables', async () => {
410-
process.env.HTTP_PROXY = 'http://localhost:8019';
411-
process.env.NO_PROXY = '*';
412411
await fakeProjectSetup(
413412
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
414413
{ locations: ['test-loc'], schedule: 3 }
@@ -422,6 +421,8 @@ heartbeat.monitors:
422421
);
423422
const output = await runPush([...DEFAULT_ARGS, '--tags', 'chunk'], {
424423
CHUNK_SIZE: '1',
424+
HTTP_PROXY: `http://localhost:${proxyPort}`,
425+
NO_PROXY: '*',
425426
});
426427
await rm(testJourney, { force: true });
427428
expect(output).toContain('Added(2)');
@@ -431,8 +432,6 @@ heartbeat.monitors:
431432
});
432433

433434
it('enables proxy based on HTTPS_PROXY', async () => {
434-
process.env.HTTPS_PROXY = 'http://localhost:8019';
435-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
436435
await fakeProjectSetup(
437436
{
438437
project: {
@@ -452,6 +451,8 @@ heartbeat.monitors:
452451
);
453452
const output = await runPush([...DEFAULT_ARGS, '--tags', 'chunk'], {
454453
CHUNK_SIZE: '1',
454+
HTTPS_PROXY: `http://localhost:${proxyPort}`,
455+
NODE_TLS_REJECT_UNAUTHORIZED: '0',
455456
});
456457
await rm(testJourney, { force: true });
457458
expect(output).toContain('Added(2)');
@@ -462,7 +463,10 @@ heartbeat.monitors:
462463

463464
it('enables proxy based on --proxy-uri', async () => {
464465
await fakeProjectSetup(
465-
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
466+
{
467+
project: { id: 'test-project', space: 'dummy', url: server.PREFIX },
468+
proxy: { uri: `http://localhost:${proxyPort}` },
469+
},
466470
{ locations: ['test-loc'], schedule: 3 }
467471
);
468472
const testJourney = join(PROJECT_DIR, 'chunk.journey.ts');
@@ -478,7 +482,7 @@ heartbeat.monitors:
478482
'--tags',
479483
'chunk',
480484
'--proxy-uri',
481-
'http://localhost:8019',
485+
`http://localhost:${proxyPort}`,
482486
],
483487
{
484488
CHUNK_SIZE: '1',
@@ -490,5 +494,30 @@ heartbeat.monitors:
490494
expect(output).toContain('✓ Pushed:');
491495
expect(requests.length).toBeGreaterThan(0);
492496
});
497+
498+
it('enables proxy based on proxy settings', async () => {
499+
await fakeProjectSetup(
500+
{
501+
project: { id: 'test-project', space: 'dummy', url: server.PREFIX },
502+
proxy: { uri: `http://localhost:${proxyPort}` },
503+
},
504+
{ locations: ['test-loc'], schedule: 3 }
505+
);
506+
const testJourney = join(PROJECT_DIR, 'chunk.journey.ts');
507+
await writeFile(
508+
testJourney,
509+
`import {journey, monitor} from '../../../';
510+
journey('a', () => monitor.use({ tags: ['chunk'] }));
511+
journey('b', () => monitor.use({ tags: ['chunk'] }));`
512+
);
513+
const output = await runPush([...DEFAULT_ARGS, '--tags', 'chunk'], {
514+
CHUNK_SIZE: '1',
515+
});
516+
await rm(testJourney, { force: true });
517+
expect(output).toContain('Added(2)');
518+
expect(output).toContain('creating or updating 1 monitors');
519+
expect(output).toContain('✓ Pushed:');
520+
expect(requests.length).toBeGreaterThan(0);
521+
});
493522
});
494523
});

0 commit comments

Comments
 (0)