Skip to content

Commit 76b31ca

Browse files
committed
Add tests for the existing terminal interceptor
1 parent 109a2bf commit 76b31ca

File tree

5 files changed

+135
-3
lines changed

5 files changed

+135
-3
lines changed

package-lock.json

Lines changed: 9 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
@@ -61,6 +61,7 @@
6161
"@types/lodash": "^4.14.117",
6262
"@types/mocha": "^5.2.5",
6363
"@types/node": "^12.7.9",
64+
"@types/node-fetch": "^2.5.3",
6465
"@types/request-promise-native": "^1.0.15",
6566
"@types/rimraf": "^2.0.2",
6667
"@types/tmp": "0.0.33",
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import * as _ from 'lodash';
2+
import * as util from 'util';
3+
import { exec } from 'child_process';
4+
5+
import { expect } from 'chai';
6+
7+
import fetch from 'node-fetch';
8+
9+
import { setupInterceptor, itIsAvailable } from './interceptor-test-utils';
10+
11+
const execAsync = util.promisify(exec);
12+
13+
const interceptorSetup = setupInterceptor('existing-terminal');
14+
15+
describe('Existing terminal interceptor', function () {
16+
this.timeout(5000);
17+
18+
beforeEach(async () => {
19+
const { server } = await interceptorSetup;
20+
await server.start();
21+
});
22+
23+
afterEach(async () => {
24+
const { server, interceptor } = await interceptorSetup;
25+
await interceptor.deactivate(server.port);
26+
await server.stop();
27+
});
28+
29+
itIsAvailable(interceptorSetup);
30+
31+
it('can be activated', async () => {
32+
const { interceptor, server } = await interceptorSetup;
33+
34+
expect(interceptor.isActive(server.port)).to.equal(false);
35+
36+
const result = await interceptor.activate(server.port) as { port: number };
37+
expect(interceptor.isActive(server.port)).to.equal(false);
38+
await fetch(`http://localhost:${result.port}/setup`);
39+
expect(interceptor.isActive(server.port)).to.equal(true);
40+
41+
expect(interceptor.isActive(server.port + 1)).to.equal(false);
42+
43+
await interceptor.deactivate(server.port);
44+
expect(interceptor.isActive(server.port)).to.equal(false);
45+
});
46+
47+
it('can deactivate all', async () => {
48+
const { interceptor, server } = await interceptorSetup;
49+
50+
expect(interceptor.isActive(server.port)).to.equal(false);
51+
52+
const result = await interceptor.activate(server.port) as { port: number };
53+
await fetch(`http://localhost:${result.port}/setup`);
54+
expect(interceptor.isActive(server.port)).to.equal(true);
55+
56+
await interceptor.deactivateAll();
57+
expect(interceptor.isActive(server.port)).to.equal(false);
58+
});
59+
60+
it('can deactivate after failed activation', async () => {
61+
const { interceptor, server } = await interceptorSetup;
62+
63+
expect(interceptor.isActive(server.port)).to.equal(false);
64+
65+
const result = await interceptor.activate(server.port) as { port: number };
66+
expect(interceptor.isActive(server.port)).to.equal(false);
67+
68+
await interceptor.deactivateAll();
69+
expect(interceptor.isActive(server.port)).to.equal(false);
70+
71+
const setupResponse = await fetch(`http://localhost:${result.port}/setup`).catch(e => e);
72+
expect(setupResponse).to.be.instanceOf(Error);
73+
});
74+
75+
it("should intercept all popular JS libraries", async function () {
76+
this.timeout(10000);
77+
const { interceptor, server } = await interceptorSetup;
78+
const result = await interceptor.activate(server.port) as { port: number };
79+
80+
const mainRule = await server.get(/https?:\/\/example.com\/js\/.*/).thenReply(200);
81+
const stripeRule = await server.get('https://api.stripe.com/v1/customers').thenJson(200, {});
82+
83+
const scriptOutput = await execAsync(`
84+
. <(curl -sS http://localhost:${result.port}/setup);
85+
node "${require.resolve('./terminal-js-test-script')}"
86+
`, {
87+
shell: '/bin/bash'
88+
});
89+
90+
expect(scriptOutput.stdout).to.contain("HTTP Toolkit interception enabled");
91+
92+
const seenRequests = _.concat(...await Promise.all([
93+
mainRule.getSeenRequests(),
94+
stripeRule.getSeenRequests()
95+
])).map(r => r.url.replace(':443', '').replace(':80', ''));
96+
97+
// Built-in modules
98+
expect(seenRequests).to.include('http://example.com/js/http');
99+
expect(seenRequests).to.include('https://example.com/js/https');
100+
101+
// http & https with lots of popular libraries
102+
['http', 'https'].forEach((protocol) =>
103+
[
104+
'request',
105+
'axios',
106+
'superagent',
107+
'node-fetch',
108+
'got',
109+
'bent',
110+
'unirest',
111+
'reqwest',
112+
'needle'
113+
].forEach((library) =>
114+
expect(seenRequests).to.include(`${protocol}://example.com/js/${library}`)
115+
)
116+
);
117+
118+
// Special case modules that need manual handling:
119+
expect(seenRequests).to.include('https://api.stripe.com/v1/customers');
120+
});
121+
122+
});

test/interceptors/fresh-terminal.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getTerminalEnvVars } from '../../src/interceptors/terminal/terminal-env
88

99
const interceptorSetup = setupInterceptor('fresh-terminal');
1010

11-
describe('Terminal interceptor', function () {
11+
describe('Fresh terminal interceptor', function () {
1212
this.timeout(5000);
1313

1414
beforeEach(async () => {
@@ -49,7 +49,7 @@ describe('Terminal interceptor', function () {
4949

5050
// Spawn node, as if it was run inside an intercepted terminal
5151
const terminalEnvOverrides = getTerminalEnvVars(server.port, httpsConfig, process.env);
52-
const nodeScript = fork(require.resolve('./fresh-terminal-js-script'), [], {
52+
const nodeScript = fork(require.resolve('./terminal-js-test-script'), [], {
5353
execArgv: ['-r', require.resolve('../../overrides/path/prepend.js')],
5454
env: Object.assign({}, process.env, terminalEnvOverrides)
5555
});
@@ -67,7 +67,7 @@ describe('Terminal interceptor', function () {
6767
expect(seenRequests).to.include('http://example.com/js/http');
6868
expect(seenRequests).to.include('https://example.com/js/https');
6969

70-
// http & http with lots of popular libraries
70+
// http & https with lots of popular libraries
7171
['http', 'https'].forEach((protocol) =>
7272
[
7373
'request',
File renamed without changes.

0 commit comments

Comments
 (0)