Skip to content

Commit e143578

Browse files
committed
Create a remote client proxy test with CAs to check serialization
1 parent 6f329a1 commit e143578

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

src/serialization/serialization.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ export function serializeProxyConfig(
346346
? { cert: caDefinition.cert.toString('utf8') } // Stringify in case of buffers
347347
: caDefinition
348348
)
349-
}
349+
};
350350
}
351351
}
352352

test/integration/proxying/upstream-proxying.spec.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import _ = require("lodash");
22
import * as fs from 'fs/promises';
33
import request = require("request-promise-native");
44

5-
import { getLocal, Mockttp, MockedEndpoint } from "../../..";
5+
import { getLocal, Mockttp, MockedEndpoint, getAdminServer, getRemote } from "../../..";
66
import {
77
expect,
88
nodeOnly
@@ -352,8 +352,107 @@ nodeOnly(() => {
352352
// And it went via the intermediate proxy
353353
expect((await proxyEndpoint.getSeenRequests()).length).to.equal(1);
354354
});
355+
});
356+
357+
describe("with a remote client", () => {
358+
359+
const adminServer = getAdminServer();
360+
361+
before(() => adminServer.start());
362+
after(() => adminServer.stop());
363+
364+
beforeEach(async () => {
365+
server = getRemote();
366+
await server.start();
367+
368+
// Configure Request to use the *first* server as a proxy
369+
process.env = _.merge({}, process.env, server.proxyEnv);
370+
});
371+
372+
describe("to an HTTPS proxy", () => {
373+
374+
const intermediateProxy = getLocal({
375+
https: {
376+
keyPath: './test/fixtures/untrusted-ca.key',
377+
certPath: './test/fixtures/untrusted-ca.pem'
378+
}
379+
});
380+
// HTTPS proxy - note that the remote server is plain HTTP.
381+
382+
let proxyEndpoint: MockedEndpoint;
383+
384+
beforeEach(async () => {
385+
await intermediateProxy.start();
386+
proxyEndpoint = await intermediateProxy.forAnyRequest().thenPassThrough(); // Totally neutral proxy
387+
});
388+
389+
afterEach(() => intermediateProxy.stop());
390+
391+
it("should not trust unknown proxy CAs by default", async () => {
392+
// Remote server sends fixed response on this one URL:
393+
await remoteServer.forGet('/test-url').thenReply(200, "Remote server says hi!");
394+
395+
// Mockttp forwards requests via our intermediate proxy
396+
await server.forAnyRequest().thenPassThrough({
397+
proxyConfig: {
398+
proxyUrl: intermediateProxy.url
399+
}
400+
});
401+
402+
const result = await request.get(remoteServer.urlFor("/test-url")).catch(e => e);
403+
404+
expect(result).to.be.instanceOf(Error);
405+
expect(result.message).to.match(/self(-| )signed certificate/); // Dash varies by Node version
406+
});
407+
408+
it("should trust the remote proxy's CA if explicitly specified", async () => {
409+
// Remote server sends fixed response on this one URL:
410+
await remoteServer.forGet('/test-url').thenReply(200, "Remote server says hi!");
411+
412+
// Mockttp forwards requests via our intermediate proxy
413+
await server.forAnyRequest().thenPassThrough({
414+
proxyConfig: {
415+
proxyUrl: intermediateProxy.url,
416+
trustedCAs: [
417+
{ cert: await fs.readFile('./test/fixtures/untrusted-ca.pem') }
418+
]
419+
}
420+
});
421+
422+
const response = await request.get(remoteServer.urlFor("/test-url"));
423+
424+
// We get a successful response
425+
expect(response).to.equal("Remote server says hi!");
426+
// And it went via the intermediate proxy
427+
expect((await proxyEndpoint.getSeenRequests()).length).to.equal(1);
428+
});
429+
430+
it("should trust the remote proxy's CA if explicitly specified as additional", async () => {
431+
// Remote server sends fixed response on this one URL:
432+
await remoteServer.forGet('/test-url').thenReply(200, "Remote server says hi!");
433+
434+
// Mockttp forwards requests via our intermediate proxy
435+
await server.forAnyRequest().thenPassThrough({
436+
proxyConfig: {
437+
proxyUrl: intermediateProxy.url,
438+
additionalTrustedCAs: [
439+
{ cert: (await fs.readFile('./test/fixtures/untrusted-ca.pem')).toString() }
440+
]
441+
}
442+
});
443+
444+
const response = await request.get(remoteServer.urlFor("/test-url"));
445+
446+
// We get a successful response
447+
expect(response).to.equal("Remote server says hi!");
448+
// And it went via the intermediate proxy
449+
expect((await proxyEndpoint.getSeenRequests()).length).to.equal(1);
450+
});
451+
452+
});
355453

356454
});
357455

358456
});
457+
359458
});

0 commit comments

Comments
 (0)