Skip to content

Commit

Permalink
chore: only throw the proxy on launch required on win/CR (#6350)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Apr 29, 2021
1 parent 263a0fd commit 1c40c94
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
10 changes: 7 additions & 3 deletions docs/src/api/params.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,13 @@ Actual picture of each page will be scaled down if necessary to fit the specifie
- `username` <[string]> Optional username to use if HTTP proxy requires authentication.
- `password` <[string]> Optional password to use if HTTP proxy requires authentication.

Network proxy settings to use with this context. Note that browser needs to be launched with the global proxy for this
option to work. If all contexts override the proxy, global proxy will be never used and can be any string, for example
`launch({ proxy: { server: 'per-context' } })`.
Network proxy settings to use with this context.

:::note
For Chromium on Windows the browser needs to be launched with the global proxy for this option to work. 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' } })`.
:::

## select-options-values
* langs: java, js
Expand Down
5 changes: 3 additions & 2 deletions src/server/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import * as os from 'os';
import { TimeoutSettings } from '../utils/timeoutSettings';
import { debugMode, mkdirIfNeeded, createGuid } from '../utils/utils';
import { Browser, BrowserOptions } from './browser';
Expand Down Expand Up @@ -405,8 +406,8 @@ export function validateBrowserContextOptions(options: types.BrowserContextOptio
options.recordVideo.size!.height &= ~1;
}
if (options.proxy) {
if (!browserOptions.proxy)
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' } })"`);
if (!browserOptions.proxy && browserOptions.isChromium && os.platform() === 'win32')
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' } })"`);
options.proxy = normalizeProxySettings(options.proxy);
}
if (debugMode() === 'inspector')
Expand Down
25 changes: 24 additions & 1 deletion tests/browsercontext-proxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,37 @@ it.afterAll(async () => {
await browser.close();
});

it('should throw for missing global proxy', async ({ browserType, browserOptions, server }) => {
it('should throw for missing global proxy on Chromium Windows', async ({ browserName, platform, browserType, browserOptions, server }) => {
it.skip(browserName !== 'chromium' || platform !== 'win32');

delete browserOptions.proxy;
const browser = await browserType.launch(browserOptions);
const error = await browser.newContext({ proxy: { server: `localhost:${server.PORT}` } }).catch(e => e);
expect(error.toString()).toContain('Browser needs to be launched with the global proxy');
await browser.close();
});

it('should work when passing the proxy only on the context level', async ({browserName, platform, browserType, browserOptions, contextOptions, server}) => {
// Currently an upstream bug in the network stack of Chromium which leads that
// the wrong proxy gets used in the BrowserContext.
it.fixme(browserName === 'chromium' && platform === 'win32');

server.setRoute('/target.html', async (req, res) => {
res.end('<html><title>Served by the proxy</title></html>');
});
delete browserOptions.proxy;
const browserWithoutProxyInLaunch = await browserType.launch(browserOptions);
const context = await browserWithoutProxyInLaunch.newContext({
...contextOptions,
proxy: { server: `localhost:${server.PORT}` }
});

const page = await context.newPage();
await page.goto('http://non-existent.com/target.html');
expect(await page.title()).toBe('Served by the proxy');
await browserWithoutProxyInLaunch.close();
});

it('should throw for bad server value', async ({ contextOptions }) => {
const error = await browser.newContext({
...contextOptions,
Expand Down
16 changes: 10 additions & 6 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8694,9 +8694,11 @@ export interface Browser extends EventEmitter {
permissions?: Array<string>;

/**
* Network proxy settings to use with this context. Note that browser needs to be launched with the global proxy for this
* option to work. If all contexts override the proxy, global proxy will be never used and can be any string, for example
* `launch({ proxy: { server: 'per-context' } })`.
* Network proxy settings to use with this context.
*
* > NOTE: For Chromium on Windows the browser needs to be launched with the global proxy for this option to work. 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' } })`.
*/
proxy?: {
/**
Expand Down Expand Up @@ -10545,9 +10547,11 @@ export interface BrowserContextOptions {
permissions?: Array<string>;

/**
* Network proxy settings to use with this context. Note that browser needs to be launched with the global proxy for this
* option to work. If all contexts override the proxy, global proxy will be never used and can be any string, for example
* `launch({ proxy: { server: 'per-context' } })`.
* Network proxy settings to use with this context.
*
* > NOTE: For Chromium on Windows the browser needs to be launched with the global proxy for this option to work. 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' } })`.
*/
proxy?: {
/**
Expand Down

0 comments on commit 1c40c94

Please sign in to comment.