Skip to content

Commit 3e8c574

Browse files
committed
Include a callback in existing terminal scripts to detect errors
1 parent 4a18c88 commit 3e8c574

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/interceptors/terminal/existing-terminal-interceptor.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,22 @@ export class ExistingTerminalInterceptor implements Interceptor {
4444
await server.start({ startPort: proxyPort + 1, endPort: 65535 });
4545

4646
const envVars = getTerminalEnvVars(proxyPort, this.config.https, 'runtime-inherit');
47-
const setupScript = getShellScript(envVars);
47+
const setupScript = getShellScript(server.urlFor('/success'), envVars);
4848

4949
const serverState = { server, isActive: false };
50-
await server
51-
.get('/setup')
52-
.thenCallback(() => {
53-
serverState.isActive = true;
54-
return {
55-
status: 200,
56-
headers: { "content-type": "text/x-shellscript" },
57-
body: setupScript
58-
};
59-
});
50+
51+
await server.get('/setup').thenCallback(() => {
52+
return {
53+
status: 200,
54+
headers: { "content-type": "text/x-shellscript" },
55+
body: setupScript
56+
};
57+
});
58+
59+
await server.post('/success').thenCallback(() => {
60+
serverState.isActive = true;
61+
return { status: 200 };
62+
});
6063

6164
this.servers[proxyPort] = serverState;
6265
return { port: server.port };

src/interceptors/terminal/terminal-scripts.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ end
7070
${END_CONFIG_SECTION}`;
7171

7272
// A source-able shell script. Should work for everything except fish, sadly.
73-
export const getShellScript = (env: { [name: string]: string }) => `${
73+
export const getShellScript = (callbackUrl: string, env: { [name: string]: string }) => `${
7474
_.map(env, (value, key) => ` export ${key}="${value.replace(/"/g, '\\"')}"`).join('\n')
7575
}
7676
@@ -80,6 +80,11 @@ export const getShellScript = (env: { [name: string]: string }) => `${
8080
alias node=node
8181
fi
8282
83+
if command -v curl >/dev/null 2>&1; then
84+
# Let the HTTP Toolkit app know this ran succesfully
85+
(curl --noproxy '*' -X POST "${callbackUrl}" >/dev/null 2>&1 &) &> /dev/null
86+
fi
87+
8388
echo 'HTTP Toolkit interception enabled'
8489
`;
8590

test/interceptors/existing-terminal.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('Existing terminal interceptor', function () {
3636
const result = await interceptor.activate(server.port) as { port: number };
3737
expect(interceptor.isActive(server.port)).to.equal(false);
3838
await fetch(`http://localhost:${result.port}/setup`);
39+
await fetch(`http://localhost:${result.port}/success`, { method: 'POST' });
3940
expect(interceptor.isActive(server.port)).to.equal(true);
4041

4142
expect(interceptor.isActive(server.port + 1)).to.equal(false);
@@ -51,6 +52,7 @@ describe('Existing terminal interceptor', function () {
5152

5253
const result = await interceptor.activate(server.port) as { port: number };
5354
await fetch(`http://localhost:${result.port}/setup`);
55+
await fetch(`http://localhost:${result.port}/success`, { method: 'POST' });
5456
expect(interceptor.isActive(server.port)).to.equal(true);
5557

5658
await interceptor.deactivateAll();
@@ -88,6 +90,7 @@ describe('Existing terminal interceptor', function () {
8890
});
8991

9092
expect(scriptOutput.stdout).to.contain("HTTP Toolkit interception enabled");
93+
expect(interceptor.isActive(server.port)).to.equal(true);
9194

9295
const seenRequests = _.concat(...await Promise.all([
9396
mainRule.getSeenRequests(),

0 commit comments

Comments
 (0)