feat(fetch): browser proxy credentials (#8760)
This commit is contained in:
parent
afe92a6fcf
commit
6263361284
|
|
@ -40,12 +40,13 @@ export async function playwrightFetch(context: BrowserContext, params: types.Fet
|
||||||
}
|
}
|
||||||
|
|
||||||
const method = params.method?.toUpperCase() || 'GET';
|
const method = params.method?.toUpperCase() || 'GET';
|
||||||
|
const proxy = context._options.proxy || context._browser.options.proxy;
|
||||||
let agent;
|
let agent;
|
||||||
if (context._options.proxy) {
|
if (proxy) {
|
||||||
// TODO: support bypass proxy
|
// TODO: support bypass proxy
|
||||||
const proxyOpts = url.parse(context._options.proxy.server);
|
const proxyOpts = url.parse(proxy.server);
|
||||||
if (context._options.proxy.username)
|
if (proxy.username)
|
||||||
proxyOpts.auth = `${context._options.proxy.username}:${context._options.proxy.password || ''}`;
|
proxyOpts.auth = `${proxy.username}:${proxy.password || ''}`;
|
||||||
agent = new HttpsProxyAgent(proxyOpts);
|
agent = new HttpsProxyAgent(proxyOpts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -282,6 +282,26 @@ it('should work with context level proxy', async ({browserOptions, browserType,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should pass proxy credentials', async ({browserType, browserOptions, server, proxyServer}) => {
|
||||||
|
proxyServer.forwardTo(server.PORT);
|
||||||
|
let auth;
|
||||||
|
proxyServer.setAuthHandler(req => {
|
||||||
|
auth = req.headers['proxy-authorization'];
|
||||||
|
return !!auth;
|
||||||
|
});
|
||||||
|
const browser = await browserType.launch({
|
||||||
|
...browserOptions,
|
||||||
|
proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: 'secret' }
|
||||||
|
});
|
||||||
|
const context = await browser.newContext();
|
||||||
|
// @ts-expect-error
|
||||||
|
const response = await context._fetch('http://non-existent.com/simple.json');
|
||||||
|
expect(proxyServer.connectHosts).toContain('non-existent.com:80');
|
||||||
|
expect(auth).toBe('Basic ' + Buffer.from('user:secret').toString('base64'));
|
||||||
|
expect(await response.json()).toEqual({foo: 'bar'});
|
||||||
|
await browser.close();
|
||||||
|
});
|
||||||
|
|
||||||
it('should work with http credentials', async ({context, server}) => {
|
it('should work with http credentials', async ({context, server}) => {
|
||||||
server.setAuth('/empty.html', 'user', 'pass');
|
server.setAuth('/empty.html', 'user', 'pass');
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue