Skip to content

Commit

Permalink
Tidy up PAC tests slightly to fully confirm proxying
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Sep 25, 2024
1 parent 6f3795a commit 52c6912
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions test/integration/proxying/upstream-proxying.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,22 +459,22 @@ nodeOnly(() => {
keyPath: './test/fixtures/test-ca.key',
certPath: './test/fixtures/test-ca.pem'
};

const intermediateProxy = getLocal({ https });

beforeEach(async () => {
server = getLocal({ https });
await server.start();

await intermediateProxy.start();

process.env = _.merge({}, process.env, server.proxyEnv);
});

afterEach(async () => {
await intermediateProxy.stop();
});

it("should forward traffic to intermediateProxy using PAC file", async () => {
const pacFile = `function FindProxyForURL(url, host) { return "PROXY ${url.parse(intermediateProxy.url).host}"; }`;
await remoteServer.forGet('/proxy-all').thenReply(200, pacFile);
Expand All @@ -485,43 +485,56 @@ nodeOnly(() => {
proxyUrl: `pac+${remoteServer.url}/proxy-all`
}
});

await intermediateProxy.forAnyRequest().thenPassThrough({
ignoreHostHttpsErrors: true,
beforeRequest: (req) => {
expect(req.url).to.equal('https://example.com/');
return {
response: {
statusCode: 200,
body: 'Proxied'
}
};
}
});
// make request
await request.get('https://example.com/');

// make a request that hits the proxy based on PAC file
expect(await request.get('https://example.com/')).to.equal('Proxied');
});

it("should bypass intermediateProxy using PAC file", async () => {
const pacFile = `function FindProxyForURL(url, host) { if (host.endsWith(".org")) return "DIRECT"; return "PROXY ${url.parse(intermediateProxy.url).host}"; }`;
const pacFile = `function FindProxyForURL(url, host) { if (host.endsWith(".com")) { return "PROXY ${url.parse(intermediateProxy.url).host}"; } else { return "DIRECT"; } }`;
await remoteServer.forGet('/proxy-bypass').thenReply(200, pacFile);
await remoteServer.forGet('/remote-response').thenReply(200, 'Remote response');

await server.forAnyRequest().thenPassThrough({
ignoreHostHttpsErrors: true,
proxyConfig: {
proxyUrl: `pac+${remoteServer.url}/proxy-bypass`
}
});

await intermediateProxy.forAnyRequest().thenPassThrough({
ignoreHostHttpsErrors: true,
beforeRequest: (req) => {
expect(req.url).to.not.equal('https://example.org/');
return {
response: {
statusCode: 200,
body: 'Proxied'
}
};
}
});

// make a request that hits the proxy based on PAC file
await request.get('https://example.com/');
expect(await request.get('https://example.com/')).to.equal('Proxied');

// make a request that bypasses proxy based on PAC file
await request.get('https://example.org/');
expect(await request.get(remoteServer.urlFor('/remote-response'))).to.equal('Remote response');
});

it("should fallback to intermediateProxy using PAC file", async () => {
const pacFile = `function FindProxyForURL(url, host) { return "PROXY invalid-proxy:8080; PROXY ${url.parse(intermediateProxy.url).host};"; }`;
await remoteServer.forGet('/proxy-fallback').thenReply(200, pacFile);
Expand All @@ -532,16 +545,22 @@ nodeOnly(() => {
proxyUrl: `pac+${remoteServer.url}/proxy-fallback`
}
});

await intermediateProxy.forAnyRequest().thenPassThrough({
ignoreHostHttpsErrors: true,
beforeRequest: (req) => {
expect(req.url).to.equal('https://example.com/');
return {
response: {
statusCode: 200,
body: 'Proxied'
}
};
}
});
// make a request
await request.get('https://example.com/');

// make a request that hits the proxy based on PAC file
expect(await request.get('https://example.com/')).to.equal('Proxied');
});
});

Expand Down

0 comments on commit 52c6912

Please sign in to comment.