Skip to content

Commit 836ca9c

Browse files
committed
Update Mockttp for a massive key generation perf boost
1 parent df19e12 commit 836ca9c

File tree

7 files changed

+66
-39
lines changed

7 files changed

+66
-39
lines changed

package-lock.json

Lines changed: 26 additions & 17 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"graphql": "^14.0.2",
3838
"graphql-yoga": "^1.16.7",
3939
"lodash": "^4.17.11",
40-
"mockttp": "^0.13.0",
40+
"mockttp": "^0.14.0",
4141
"node-gsettings-wrapper": "^0.5.0",
4242
"osx-find-executable": "^1.0.0",
4343
"rimraf": "^2.6.2",

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ async function generateHTTPSConfig(configPath: string) {
2626
await Promise.all([
2727
canAccess(keyPath, fs.constants.R_OK),
2828
canAccess(certPath, fs.constants.R_OK)
29-
]).catch(() => {
30-
const newCertPair = generateCACertificate({
29+
]).catch(async () => {
30+
const newCertPair = await generateCACertificate({
3131
commonName: 'HTTP Toolkit CA'
3232
});
3333

test/interceptors/fresh-chrome.spec.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import * as _ from 'lodash';
22
import { CompletedRequest } from 'mockttp';
3-
import { getInterceptorAndServer, itIsAvailable, itCanBeActivated } from './interceptor-test-utils';
3+
import { setupInterceptor, itIsAvailable, itCanBeActivated } from './interceptor-test-utils';
44

5-
const { server, interceptor: chromeInterceptor } = getInterceptorAndServer('fresh-chrome');
5+
const interceptorSetup = setupInterceptor('fresh-chrome');
66

77
describe('Chrome interceptor', function () {
88
this.timeout(5000);
99

1010
beforeEach(async () => {
11+
const { server } = await interceptorSetup;
1112
await server.start();
1213
await server.anyRequest().thenPassThrough();
1314
});
1415

1516
afterEach(async () => {
17+
const { server, interceptor: chromeInterceptor } = await interceptorSetup;
18+
1619
await chromeInterceptor.deactivate(server.port);
1720
await server.stop();
1821
});
1922

20-
itIsAvailable(chromeInterceptor);
21-
itCanBeActivated(chromeInterceptor, server);
23+
itIsAvailable(interceptorSetup);
24+
itCanBeActivated(interceptorSetup);
2225

2326
it('successfully makes requests', async function () {
27+
const { server, interceptor: chromeInterceptor } = await interceptorSetup;
28+
2429
const exampleRequestReceived = new Promise<CompletedRequest>((resolve) =>
2530
server.on('request', (req) => {
2631
if (req.url.startsWith('https://amiusing.httptoolkit.tech')) {

test/interceptors/fresh-firefox.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
import * as _ from 'lodash';
22
import { CompletedRequest } from 'mockttp';
3-
import { getInterceptorAndServer, itIsAvailable, itCanBeActivated } from './interceptor-test-utils';
3+
import { setupInterceptor, itIsAvailable, itCanBeActivated } from './interceptor-test-utils';
44

5-
const { server, interceptor: firefoxInterceptor } = getInterceptorAndServer('fresh-firefox');
5+
const interceptorSetup = setupInterceptor('fresh-firefox');
66

77
describe('Firefox interceptor', function () {
88
this.timeout(5000);
99

1010
beforeEach(async () => {
11+
const { server } = await interceptorSetup;
1112
await server.start();
1213
await server.anyRequest().thenPassThrough();
1314
});
1415

1516
afterEach(async () => {
17+
const { server, interceptor: firefoxInterceptor } = await interceptorSetup;
1618
await firefoxInterceptor.deactivate(server.port);
1719
await server.stop();
1820
});
1921

20-
itIsAvailable(firefoxInterceptor);
21-
itCanBeActivated(firefoxInterceptor, server);
22+
itIsAvailable(interceptorSetup);
23+
itCanBeActivated(interceptorSetup);
2224

2325
// TODO: This doesn't work, as we need to manually accept the cert before
2426
// Firefox makes its HTTPS request to amiusing
2527
it.skip('successfully makes requests', async function () {
28+
const { server, interceptor: firefoxInterceptor } = await interceptorSetup;
29+
2630
const exampleRequestReceived = new Promise<CompletedRequest>((resolve) =>
2731
server.on('request', (req) => {
2832
if (req.url.startsWith('https://amiusing.httptoolkit.tech')) {
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import * as _ from 'lodash';
2-
import { CompletedRequest } from 'mockttp';
3-
import { getInterceptorAndServer, itIsAvailable, itCanBeActivated } from './interceptor-test-utils';
2+
import { setupInterceptor, itIsAvailable, itCanBeActivated } from './interceptor-test-utils';
43

5-
const { server, interceptor: terminalInterceptor } = getInterceptorAndServer('fresh-terminal');
4+
const interceptorSetup = setupInterceptor('fresh-terminal');
65

76
describe('Terminal interceptor', function () {
87
this.timeout(5000);
98

109
beforeEach(async () => {
10+
const { server } = await interceptorSetup;
1111
await server.start();
1212
await server.anyRequest().thenPassThrough();
1313
});
1414

1515
afterEach(async () => {
16+
const { server, interceptor: terminalInterceptor } = await interceptorSetup;
1617
await terminalInterceptor.deactivate(server.port);
1718
await server.stop();
1819
});
1920

20-
itIsAvailable(terminalInterceptor);
21-
itCanBeActivated(terminalInterceptor, server);
21+
itIsAvailable(interceptorSetup);
22+
itCanBeActivated(interceptorSetup);
2223

2324
});

test/interceptors/interceptor-test-utils.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@ import { getLocal, generateCACertificate, Mockttp } from 'mockttp';
99

1010
import { buildInterceptors, Interceptor } from '../../src/interceptors';
1111

12-
const getCertificateDetails = _.memoize((configPath: string) => {
12+
const getCertificateDetails = _.memoize(async (configPath: string) => {
1313
const keyPath = path.join(configPath, 'ca.key');
1414
const certPath = path.join(configPath, 'ca.pem');
1515

16-
const newCertPair = generateCACertificate({ commonName: 'HTTP Toolkit CA - DO NOT TRUST' });
16+
const newCertPair = await generateCACertificate({ commonName: 'HTTP Toolkit CA - DO NOT TRUST' });
1717

1818
fs.writeFileSync(keyPath, newCertPair.key);
1919
fs.writeFileSync(certPath, newCertPair.cert);
2020

2121
return { certPath, keyPath };
2222
});
2323

24-
export function getInterceptorAndServer(interceptor: string): { server: Mockttp, interceptor: Interceptor } {
24+
type InterceptorSetup = Promise<{
25+
server: Mockttp,
26+
interceptor: Interceptor
27+
}>
28+
29+
export async function setupInterceptor(interceptor: string): InterceptorSetup {
2530
const configPath = tmp.dirSync({ unsafeCleanup: true }).name;
2631

27-
const { certPath, keyPath } = getCertificateDetails(configPath);
32+
const { certPath, keyPath } = await getCertificateDetails(configPath);
2833

2934
const server = getLocal({ https: { certPath, keyPath } });
3035
const interceptors = buildInterceptors({ configPath, https: { certPath, keyPath } });
@@ -34,14 +39,17 @@ export function getInterceptorAndServer(interceptor: string): { server: Mockttp,
3439

3540
// Various tests that we'll want to reuse across interceptors:
3641

37-
export function itIsAvailable(interceptor: Interceptor) {
42+
export function itIsAvailable(interceptorSetup: InterceptorSetup) {
3843
it('is available', async () => {
44+
const { interceptor } = await interceptorSetup;
3945
expect(await interceptor.isActivable()).to.equal(true);
4046
});
4147
}
4248

43-
export function itCanBeActivated(interceptor: Interceptor, server: Mockttp) {
49+
export function itCanBeActivated(interceptorSetup: InterceptorSetup) {
4450
it('can be activated', async () => {
51+
const { interceptor, server } = await interceptorSetup;
52+
4553
expect(interceptor.isActive(server.port)).to.equal(false);
4654

4755
await interceptor.activate(server.port);

0 commit comments

Comments
 (0)