Skip to content

Commit 24ba9fa

Browse files
committed
Add unit test for proxy settings
1 parent 9846500 commit 24ba9fa

File tree

8 files changed

+284
-63
lines changed

8 files changed

+284
-63
lines changed

__tests__/locations/index.test.ts

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ import { LOCATIONS } from '../fixtures/locationinfo';
3333
import { Server } from '../utils/server';
3434
import { CLIMock } from '../utils/test-config';
3535
import { mkdir, rm, writeFile } from 'fs/promises';
36-
// import { createProxyServer } from 'http-proxy';
37-
// import http from "http";
36+
import { Straightforward } from 'straightforward';
3837

3938
describe('Locations', () => {
4039
const apiKey = 'foo';
@@ -102,8 +101,7 @@ describe('Locations', () => {
102101
const cli = new CLIMock(true)
103102
.args(['locations', ...args])
104103
.run({ cwd: PROJECT_DIR, env: process.env });
105-
// expect(await cli.exitCode).toBe(0);
106-
console.log(cli.stderr());
104+
expect(await cli.exitCode).toBe(0);
107105
return cli.stderr();
108106
};
109107

@@ -130,47 +128,73 @@ describe('Locations', () => {
130128
});
131129

132130
describe('Proxy options', () => {
133-
// let requests: Array<any> = [];
134-
// let proxyServer;
131+
let requests: Array<any> = [];
132+
let proxyServer;
133+
let tlsServer;
135134

136135
beforeAll(async () => {
137-
await fakeProjectSetup({ url: server.PREFIX });
138-
// proxyServer = createProxyServer({ target: server.PREFIX }).listen(8019);
139-
// proxyServer.on('proxyReq', function (proxyReq, req) {
140-
// requests.push(req);
141-
// });
142-
// const proxy = createProxyServer({});
143-
144-
//
145-
// Create your custom server and just call `proxy.web()` to proxy
146-
// a web request to the target passed in the options
147-
// also you can use `proxy.ws()` to proxy a websockets request
148-
//
149-
// proxyServer = http.createServer(function (req, res) {
150-
// proxy.web(req, res, { target: server.PREFIX });
151-
// }).listen(8019);
136+
proxyServer = new Straightforward();
137+
proxyServer.onConnect.use(async ({ req }, next) => {
138+
requests.push(req);
139+
return next();
140+
});
141+
await proxyServer.listen(8019);
142+
tlsServer = await Server.create({ tls: true });
143+
tlsServer.route('/internal/uptime/service/locations', (req, res) => {
144+
res.setHeader('content-type', 'application/json');
145+
res.end(JSON.stringify({ locations: LOCATIONS }));
146+
});
152147
});
153148

154-
// afterAll(async () => {
155-
// proxyServer.close();
156-
// });
149+
afterAll(async () => {
150+
proxyServer.close();
151+
});
157152

158-
// beforeEach(() => {
159-
// requests = []
160-
// });
153+
beforeEach(() => {
154+
requests = [];
155+
delete process.env.HTTP_PROXY,
156+
process.env.HTTP_PROXYS,
157+
process.env.NO_PROXY,
158+
process.env.NODE_TLS_REJECT_UNAUTHORIZED;
159+
});
161160

162161
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+
expect(output).toContain(`custom location 1`);
166+
expect(requests).toHaveLength(1);
167+
});
168+
169+
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+
expect(output).toContain(`custom location 1`);
175+
expect(requests).toHaveLength(0);
176+
});
177+
178+
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+
expect(output).toContain(`custom location 1`);
185+
expect(requests).toHaveLength(1);
186+
});
187+
188+
it('enables proxy based on --proxy-uri', async () => {
189+
await fakeProjectSetup({ url: server.PREFIX });
163190
const output = await runLocations([
164-
'--url',
165-
server.PREFIX,
166191
'--auth',
167192
apiKey,
168193
'--proxy-uri',
169-
'http://localhost:9191',
194+
'http://localhost:8019',
170195
]);
171-
console.log(output);
172-
// expect(requests).toHaveLength(1);
173196
expect(output).toContain(`custom location 1`);
197+
expect(requests).toHaveLength(1);
174198
});
175199
});
176200
});

__tests__/push/index.test.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { createKibanaTestServer } from '../utils/kibana-test-server';
3232
import { Server } from '../utils/server';
3333
import { CLIMock } from '../utils/test-config';
3434
import { THROTTLING_WARNING_MSG } from '../../src/helpers';
35+
import Straightforward from 'straightforward';
3536

3637
describe('Push', () => {
3738
let server: Server;
@@ -353,4 +354,141 @@ heartbeat.monitors:
353354
expect(output).toMatchSnapshot();
354355
});
355356
});
357+
describe('Proxy options', () => {
358+
let requests: Array<any> = [];
359+
let proxyServer;
360+
let tlsServer;
361+
362+
beforeAll(async () => {
363+
proxyServer = new Straightforward();
364+
proxyServer.onConnect.use(async ({ req }, next) => {
365+
requests.push(req);
366+
return next();
367+
});
368+
await proxyServer.listen(8019);
369+
tlsServer = await createKibanaTestServer('8.6.0', true, (req: any) =>
370+
requests.push(req)
371+
);
372+
});
373+
374+
afterAll(async () => {
375+
proxyServer.close();
376+
});
377+
378+
beforeEach(() => {
379+
requests = [];
380+
delete process.env.HTTP_PROXY,
381+
process.env.HTTP_PROXYS,
382+
process.env.NO_PROXY,
383+
process.env.NODE_TLS_REJECT_UNAUTHORIZED;
384+
});
385+
386+
it('enables proxy based on HTTP_PROXY', async () => {
387+
process.env.HTTP_PROXY = 'http://localhost:8019';
388+
await fakeProjectSetup(
389+
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
390+
{ locations: ['test-loc'], schedule: 3 }
391+
);
392+
const testJourney = join(PROJECT_DIR, 'chunk.journey.ts');
393+
await writeFile(
394+
testJourney,
395+
`import {journey, monitor} from '../../../';
396+
journey('a', () => monitor.use({ tags: ['chunk'] }));
397+
journey('b', () => monitor.use({ tags: ['chunk'] }));`
398+
);
399+
const output = await runPush([...DEFAULT_ARGS, '--tags', 'chunk'], {
400+
CHUNK_SIZE: '1',
401+
});
402+
await rm(testJourney, { force: true });
403+
expect(output).toContain('Added(2)');
404+
expect(output).toContain('creating or updating 1 monitors');
405+
expect(output).toContain('✓ Pushed:');
406+
expect(requests.length).toBeGreaterThan(0);
407+
});
408+
409+
it('honors NO_PROXY with env variables', async () => {
410+
process.env.HTTP_PROXY = 'http://localhost:8019';
411+
process.env.NO_PROXY = '*';
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+
});
426+
await rm(testJourney, { force: true });
427+
expect(output).toContain('Added(2)');
428+
expect(output).toContain('creating or updating 1 monitors');
429+
expect(output).toContain('✓ Pushed:');
430+
expect(requests).toHaveLength(0);
431+
});
432+
433+
it('enables proxy based on HTTPS_PROXY', async () => {
434+
process.env.HTTPS_PROXY = 'http://localhost:8019';
435+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
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+
});
456+
await rm(testJourney, { force: true });
457+
expect(output).toContain('Added(2)');
458+
expect(output).toContain('creating or updating 1 monitors');
459+
expect(output).toContain('✓ Pushed:');
460+
expect(requests.length).toBeGreaterThan(0);
461+
});
462+
463+
it('enables proxy based on --proxy-uri', async () => {
464+
await fakeProjectSetup(
465+
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
466+
{ locations: ['test-loc'], schedule: 3 }
467+
);
468+
const testJourney = join(PROJECT_DIR, 'chunk.journey.ts');
469+
await writeFile(
470+
testJourney,
471+
`import {journey, monitor} from '../../../';
472+
journey('a', () => monitor.use({ tags: ['chunk'] }));
473+
journey('b', () => monitor.use({ tags: ['chunk'] }));`
474+
);
475+
const output = await runPush(
476+
[
477+
...DEFAULT_ARGS,
478+
'--tags',
479+
'chunk',
480+
'--proxy-uri',
481+
'http://localhost:8019',
482+
],
483+
{
484+
CHUNK_SIZE: '1',
485+
}
486+
);
487+
await rm(testJourney, { force: true });
488+
expect(output).toContain('Added(2)');
489+
expect(output).toContain('creating or updating 1 monitors');
490+
expect(output).toContain('✓ Pushed:');
491+
expect(requests.length).toBeGreaterThan(0);
492+
});
493+
});
356494
});

__tests__/utils/kibana-test-server.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,40 @@ import {
2929
PutResponse,
3030
} from '../../src/push/kibana_api';
3131

32-
export const createKibanaTestServer = async (kibanaVersion: string) => {
33-
const server = await Server.create({ port: 0 });
34-
server.route('/s/dummy/api/status', (req, res) =>
35-
res.end(JSON.stringify({ version: { number: kibanaVersion } }))
36-
);
37-
server.route('/s/dummy/api/stats', (req, res) =>
38-
res.end(JSON.stringify({ kibana: { version: kibanaVersion } }))
39-
);
32+
export const createKibanaTestServer = async (
33+
kibanaVersion: string,
34+
tls?: boolean,
35+
reqCallback?: any
36+
) => {
37+
const server = await Server.create({ port: 0, tls });
38+
server.route('/s/dummy/api/status', (req, res) => {
39+
(
40+
reqCallback ??
41+
(() => {
42+
return;
43+
})
44+
)(req);
45+
res.end(JSON.stringify({ version: { number: kibanaVersion } }));
46+
});
47+
server.route('/s/dummy/api/stats', (req, res) => {
48+
(
49+
reqCallback ??
50+
(() => {
51+
return;
52+
})
53+
)(req);
54+
res.end(JSON.stringify({ kibana: { version: kibanaVersion } }));
55+
});
4056
// Legacy
4157
server.route(
4258
'/s/dummy/api/synthetics/service/project/monitors',
4359
async (req, res) => {
60+
(
61+
reqCallback ??
62+
(() => {
63+
return;
64+
})
65+
)(req);
4466
await new Promise(r => setTimeout(r, 20));
4567
req.on('data', chunks => {
4668
const schema = JSON.parse(chunks.toString()) as LegacyAPISchema;
@@ -66,6 +88,12 @@ export const createKibanaTestServer = async (kibanaVersion: string) => {
6688
// Post 8.6
6789
const basePath = '/s/dummy/api/synthetics/project/test-project/monitors';
6890
server.route(basePath, (req, res) => {
91+
(
92+
reqCallback ??
93+
(() => {
94+
return;
95+
})
96+
)(req);
6997
const getResp = {
7098
total: 2,
7199
monitors: [
@@ -76,6 +104,12 @@ export const createKibanaTestServer = async (kibanaVersion: string) => {
76104
res.end(JSON.stringify(getResp));
77105
});
78106
server.route(basePath + '/_bulk_update', (req, res) => {
107+
(
108+
reqCallback ??
109+
(() => {
110+
return;
111+
})
112+
)(req);
79113
const updateResponse = {
80114
createdMonitors: ['j1', 'j2'],
81115
updatedMonitors: [],
@@ -84,6 +118,12 @@ export const createKibanaTestServer = async (kibanaVersion: string) => {
84118
res.end(JSON.stringify(updateResponse));
85119
});
86120
server.route(basePath + '/_bulk_delete', (req, res) => {
121+
(
122+
reqCallback ??
123+
(() => {
124+
return;
125+
})
126+
)(req);
87127
res.end(JSON.stringify({ deleted_monitors: ['j3', 'j4'] }));
88128
});
89129
return server;

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"prettier": "^2.7.1",
100100
"rimraf": "^3.0.2",
101101
"semantic-release": "^22.0.3",
102+
"straightforward": "^4.2.2",
102103
"ts-jest": "^29.1.1",
103104
"typescript": "^5.1.6"
104105
},

0 commit comments

Comments
 (0)