Skip to content

Commit 1c40c94

Browse files
authored
chore: only throw the proxy on launch required on win/CR (#6350)
1 parent 263a0fd commit 1c40c94

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

docs/src/api/params.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,13 @@ Actual picture of each page will be scaled down if necessary to fit the specifie
447447
- `username` <[string]> Optional username to use if HTTP proxy requires authentication.
448448
- `password` <[string]> Optional password to use if HTTP proxy requires authentication.
449449

450-
Network proxy settings to use with this context. Note that browser needs to be launched with the global proxy for this
451-
option to work. If all contexts override the proxy, global proxy will be never used and can be any string, for example
452-
`launch({ proxy: { server: 'per-context' } })`.
450+
Network proxy settings to use with this context.
451+
452+
:::note
453+
For Chromium on Windows the browser needs to be launched with the global proxy for this option to work. If all
454+
contexts override the proxy, global proxy will be never used and can be any string, for example
455+
`launch({ proxy: { server: 'http://per-context' } })`.
456+
:::
453457

454458
## select-options-values
455459
* langs: java, js

src/server/browserContext.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import * as os from 'os';
1819
import { TimeoutSettings } from '../utils/timeoutSettings';
1920
import { debugMode, mkdirIfNeeded, createGuid } from '../utils/utils';
2021
import { Browser, BrowserOptions } from './browser';
@@ -405,8 +406,8 @@ export function validateBrowserContextOptions(options: types.BrowserContextOptio
405406
options.recordVideo.size!.height &= ~1;
406407
}
407408
if (options.proxy) {
408-
if (!browserOptions.proxy)
409-
throw new Error(`Browser needs to be launched with the global proxy. If all contexts override the proxy, global proxy will be never used and can be any string, for example "launch({ proxy: { server: 'per-context' } })"`);
409+
if (!browserOptions.proxy && browserOptions.isChromium && os.platform() === 'win32')
410+
throw new Error(`Browser needs to be launched with the global proxy. If all contexts override the proxy, global proxy will be never used and can be any string, for example "launch({ proxy: { server: 'http://per-context' } })"`);
410411
options.proxy = normalizeProxySettings(options.proxy);
411412
}
412413
if (debugMode() === 'inspector')

tests/browsercontext-proxy.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,37 @@ it.afterAll(async () => {
2626
await browser.close();
2727
});
2828

29-
it('should throw for missing global proxy', async ({ browserType, browserOptions, server }) => {
29+
it('should throw for missing global proxy on Chromium Windows', async ({ browserName, platform, browserType, browserOptions, server }) => {
30+
it.skip(browserName !== 'chromium' || platform !== 'win32');
31+
3032
delete browserOptions.proxy;
3133
const browser = await browserType.launch(browserOptions);
3234
const error = await browser.newContext({ proxy: { server: `localhost:${server.PORT}` } }).catch(e => e);
3335
expect(error.toString()).toContain('Browser needs to be launched with the global proxy');
3436
await browser.close();
3537
});
3638

39+
it('should work when passing the proxy only on the context level', async ({browserName, platform, browserType, browserOptions, contextOptions, server}) => {
40+
// Currently an upstream bug in the network stack of Chromium which leads that
41+
// the wrong proxy gets used in the BrowserContext.
42+
it.fixme(browserName === 'chromium' && platform === 'win32');
43+
44+
server.setRoute('/target.html', async (req, res) => {
45+
res.end('<html><title>Served by the proxy</title></html>');
46+
});
47+
delete browserOptions.proxy;
48+
const browserWithoutProxyInLaunch = await browserType.launch(browserOptions);
49+
const context = await browserWithoutProxyInLaunch.newContext({
50+
...contextOptions,
51+
proxy: { server: `localhost:${server.PORT}` }
52+
});
53+
54+
const page = await context.newPage();
55+
await page.goto('http://non-existent.com/target.html');
56+
expect(await page.title()).toBe('Served by the proxy');
57+
await browserWithoutProxyInLaunch.close();
58+
});
59+
3760
it('should throw for bad server value', async ({ contextOptions }) => {
3861
const error = await browser.newContext({
3962
...contextOptions,

types/types.d.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8694,9 +8694,11 @@ export interface Browser extends EventEmitter {
86948694
permissions?: Array<string>;
86958695

86968696
/**
8697-
* Network proxy settings to use with this context. Note that browser needs to be launched with the global proxy for this
8698-
* option to work. If all contexts override the proxy, global proxy will be never used and can be any string, for example
8699-
* `launch({ proxy: { server: 'per-context' } })`.
8697+
* Network proxy settings to use with this context.
8698+
*
8699+
* > NOTE: For Chromium on Windows the browser needs to be launched with the global proxy for this option to work. If all
8700+
* contexts override the proxy, global proxy will be never used and can be any string, for example `launch({ proxy: {
8701+
* server: 'http://per-context' } })`.
87008702
*/
87018703
proxy?: {
87028704
/**
@@ -10545,9 +10547,11 @@ export interface BrowserContextOptions {
1054510547
permissions?: Array<string>;
1054610548

1054710549
/**
10548-
* Network proxy settings to use with this context. Note that browser needs to be launched with the global proxy for this
10549-
* option to work. If all contexts override the proxy, global proxy will be never used and can be any string, for example
10550-
* `launch({ proxy: { server: 'per-context' } })`.
10550+
* Network proxy settings to use with this context.
10551+
*
10552+
* > NOTE: For Chromium on Windows the browser needs to be launched with the global proxy for this option to work. If all
10553+
* contexts override the proxy, global proxy will be never used and can be any string, for example `launch({ proxy: {
10554+
* server: 'http://per-context' } })`.
1055110555
*/
1055210556
proxy?: {
1055310557
/**

0 commit comments

Comments
 (0)