Skip to content

Commit 6a19383

Browse files
committed
Test that Chrome makes real & successful requests
1 parent 38b87d2 commit 6a19383

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

test/interceptors.spec.ts

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,68 @@
1+
import * as _ from 'lodash';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
14
import * as tmp from 'tmp';
2-
3-
import { buildInterceptors } from '../src/interceptors';
45
import { expect } from 'chai';
6+
import { getLocal, CompletedRequest, generateCACertificate, Mockttp } from 'mockttp';
7+
8+
import { buildInterceptors, Interceptor } from '../src/interceptors';
59

610
describe('Fresh chrome interceptor', () => {
711

8-
const configPath = tmp.dirSync({ unsafeCleanup: true }).name;
9-
const interceptors = buildInterceptors({ configPath });
12+
let server: Mockttp;
13+
let freshChrome: Interceptor;
1014

11-
it('is available', async () => {
12-
const freshChrome = interceptors['fresh-chrome'];
15+
before(function () {
16+
this.timeout(10000);
17+
18+
const configPath = tmp.dirSync({ unsafeCleanup: true }).name;
1319

20+
const keyPath = path.join(configPath, 'ca.key');
21+
const certPath = path.join(configPath, 'ca.pem');
22+
const newCertPair = generateCACertificate({ commonName: 'HTTP Toolkit CA - DO NOT TRUST' });
23+
fs.writeFileSync(keyPath, newCertPair.key);
24+
fs.writeFileSync(certPath, newCertPair.cert);
25+
26+
server = getLocal({ https: { certPath, keyPath } });
27+
const interceptors = buildInterceptors({ configPath });
28+
freshChrome = interceptors['fresh-chrome']
29+
});
30+
31+
beforeEach(() => server.start());
32+
afterEach(async () => {
33+
await freshChrome.deactivate(server.port);
34+
await server.stop();
35+
});
36+
37+
it('is available', async () => {
1438
expect(await freshChrome.isActivable()).to.equal(true);
1539
});
1640

1741
it('can be activated', async () => {
18-
const freshChrome = interceptors['fresh-chrome'];
19-
expect(freshChrome.isActive(8000)).to.equal(false);
42+
expect(freshChrome.isActive(server.port)).to.equal(false);
43+
44+
await freshChrome.activate(server.port);
45+
expect(freshChrome.isActive(server.port)).to.equal(true);
46+
expect(freshChrome.isActive(server.port + 1)).to.equal(false);
47+
48+
await freshChrome.deactivate(server.port);
49+
expect(freshChrome.isActive(server.port)).to.equal(false);
50+
});
51+
52+
it('successfully makes requests', async () => {
53+
await server.anyRequest().thenPassThrough();
54+
55+
const exampleRequestReceived = new Promise<CompletedRequest>((resolve) =>
56+
server.on('request', (req) => {
57+
if (req.url.startsWith('https://example.com')) {
58+
resolve(req);
59+
}
60+
})
61+
);
2062

21-
await freshChrome.activate(8000);
22-
expect(freshChrome.isActive(8000)).to.equal(true);
23-
expect(freshChrome.isActive(9000)).to.equal(false);
63+
await freshChrome.activate(server.port);
2464

25-
await freshChrome.deactivate(8000);
26-
expect(freshChrome.isActive(8000)).to.equal(false);
65+
// Only resolves if example.com request is sent successfully
66+
await exampleRequestReceived;
2767
});
2868
});

0 commit comments

Comments
 (0)