mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-13 17:14:02 +03:00
64e7557fb9
In several of the Playwright APIs, falsey values were not handled correctly. This changeset adds tests (and some fixes): - route.continue: If options.postData was the empty string, the continue failed to override the post data. - page.post (application/json with options.data: false|''|0|null): Raw falsey values were getting dropped (i.e. you can't do the equivalent of curl --header application/json … -d 'false'). This has been fixed with most values across all browsers, but an additional fix is needed for 'null' which the channel serializer treats extra specially. - testInfo.attach: This didn't get reported as an error when options.path was the empty string, but should have been. #11413 (and its fix #11414) inspired this search as they are the same class of bug.
240 lines
7.4 KiB
TypeScript
240 lines
7.4 KiB
TypeScript
/**
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { browserTest as it, expect } from './config/browserTest';
|
|
|
|
it('should intercept', async ({ browser, server }) => {
|
|
const context = await browser.newContext();
|
|
let intercepted = false;
|
|
await context.route('**/empty.html', route => {
|
|
intercepted = true;
|
|
const request = route.request();
|
|
expect(request.url()).toContain('empty.html');
|
|
expect(request.headers()['user-agent']).toBeTruthy();
|
|
expect(request.method()).toBe('GET');
|
|
expect(request.postData()).toBe(null);
|
|
expect(request.isNavigationRequest()).toBe(true);
|
|
expect(request.resourceType()).toBe('document');
|
|
expect(request.frame() === page.mainFrame()).toBe(true);
|
|
expect(request.frame().url()).toBe('about:blank');
|
|
route.continue();
|
|
});
|
|
const page = await context.newPage();
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
expect(response.ok()).toBe(true);
|
|
expect(intercepted).toBe(true);
|
|
await context.close();
|
|
});
|
|
|
|
it('should unroute', async ({ browser, server }) => {
|
|
const context = await browser.newContext();
|
|
const page = await context.newPage();
|
|
|
|
let intercepted = [];
|
|
await context.route('**/*', route => {
|
|
intercepted.push(1);
|
|
route.continue();
|
|
});
|
|
await context.route('**/empty.html', route => {
|
|
intercepted.push(2);
|
|
route.continue();
|
|
});
|
|
await context.route('**/empty.html', route => {
|
|
intercepted.push(3);
|
|
route.continue();
|
|
});
|
|
const handler4 = route => {
|
|
intercepted.push(4);
|
|
route.continue();
|
|
};
|
|
await context.route('**/empty.html', handler4);
|
|
await page.goto(server.EMPTY_PAGE);
|
|
expect(intercepted).toEqual([4]);
|
|
|
|
intercepted = [];
|
|
await context.unroute('**/empty.html', handler4);
|
|
await page.goto(server.EMPTY_PAGE);
|
|
expect(intercepted).toEqual([3]);
|
|
|
|
intercepted = [];
|
|
await context.unroute('**/empty.html');
|
|
await page.goto(server.EMPTY_PAGE);
|
|
expect(intercepted).toEqual([1]);
|
|
|
|
await context.close();
|
|
});
|
|
|
|
it('should yield to page.route', async ({ browser, server }) => {
|
|
const context = await browser.newContext();
|
|
await context.route('**/empty.html', route => {
|
|
route.fulfill({ status: 200, body: 'context' });
|
|
});
|
|
const page = await context.newPage();
|
|
await page.route('**/empty.html', route => {
|
|
route.fulfill({ status: 200, body: 'page' });
|
|
});
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
expect(response.ok()).toBe(true);
|
|
expect(await response.text()).toBe('page');
|
|
await context.close();
|
|
});
|
|
|
|
it('should fall back to context.route', async ({ browser, server }) => {
|
|
const context = await browser.newContext();
|
|
await context.route('**/empty.html', route => {
|
|
route.fulfill({ status: 200, body: 'context' });
|
|
});
|
|
const page = await context.newPage();
|
|
await page.route('**/non-empty.html', route => {
|
|
route.fulfill({ status: 200, body: 'page' });
|
|
});
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
expect(response.ok()).toBe(true);
|
|
expect(await response.text()).toBe('context');
|
|
await context.close();
|
|
});
|
|
|
|
it('should support Set-Cookie header', async ({ contextFactory, server, browserName, browserMajorVersion }) => {
|
|
it.fixme(browserName === 'webkit');
|
|
|
|
const context = await contextFactory();
|
|
const page = await context.newPage();
|
|
await page.route('https://example.com/', (route, request) => {
|
|
route.fulfill({
|
|
headers: {
|
|
'Set-Cookie': 'name=value; domain=.example.com; Path=/'
|
|
},
|
|
contentType: 'text/html',
|
|
body: 'done'
|
|
});
|
|
});
|
|
await page.goto('https://example.com');
|
|
const defaultSameSiteCookieValue = browserName === 'chromium' || (browserName === 'firefox' && browserMajorVersion >= 96) ? 'Lax' : 'None';
|
|
expect(await context.cookies()).toEqual([{
|
|
sameSite: defaultSameSiteCookieValue,
|
|
name: 'name',
|
|
value: 'value',
|
|
domain: '.example.com',
|
|
path: '/',
|
|
expires: -1,
|
|
httpOnly: false,
|
|
secure: false
|
|
}]);
|
|
});
|
|
|
|
it('should ignore secure Set-Cookie header for insecure requests', async ({ contextFactory, server, browserName }) => {
|
|
it.fixme(browserName === 'webkit');
|
|
|
|
const context = await contextFactory();
|
|
const page = await context.newPage();
|
|
await page.route('http://example.com/', (route, request) => {
|
|
route.fulfill({
|
|
headers: {
|
|
'Set-Cookie': 'name=value; domain=.example.com; Path=/; Secure'
|
|
},
|
|
contentType: 'text/html',
|
|
body: 'done'
|
|
});
|
|
});
|
|
await page.goto('http://example.com');
|
|
expect(await context.cookies()).toEqual([]);
|
|
});
|
|
|
|
it('should use Set-Cookie header in future requests', async ({ contextFactory, server, browserName, browserMajorVersion }) => {
|
|
it.fixme(browserName === 'webkit');
|
|
|
|
const context = await contextFactory();
|
|
const page = await context.newPage();
|
|
|
|
await page.route(server.EMPTY_PAGE, (route, request) => {
|
|
route.fulfill({
|
|
headers: {
|
|
'Set-Cookie': 'name=value'
|
|
},
|
|
contentType: 'text/html',
|
|
body: 'done'
|
|
});
|
|
});
|
|
await page.goto(server.EMPTY_PAGE);
|
|
const defaultSameSiteCookieValue = browserName === 'chromium' || (browserName === 'firefox' && browserMajorVersion >= 96) ? 'Lax' : 'None';
|
|
expect(await context.cookies()).toEqual([{
|
|
sameSite: defaultSameSiteCookieValue,
|
|
name: 'name',
|
|
value: 'value',
|
|
domain: 'localhost',
|
|
path: '/',
|
|
expires: -1,
|
|
httpOnly: false,
|
|
secure: false
|
|
}]);
|
|
|
|
let cookie = '';
|
|
server.setRoute('/foo.html', (req, res) => {
|
|
cookie = req.headers.cookie;
|
|
res.end();
|
|
});
|
|
await page.goto(server.PREFIX + '/foo.html');
|
|
expect(cookie).toBe('name=value');
|
|
});
|
|
|
|
it('should work with ignoreHTTPSErrors', async ({ browser, httpsServer }) => {
|
|
const context = await browser.newContext({ ignoreHTTPSErrors: true });
|
|
const page = await context.newPage();
|
|
|
|
await page.route('**/*', route => route.continue());
|
|
const response = await page.goto(httpsServer.EMPTY_PAGE);
|
|
expect(response.status()).toBe(200);
|
|
await context.close();
|
|
});
|
|
|
|
it('should support the times parameter with route matching', async ({ context, page, server }) => {
|
|
const intercepted = [];
|
|
await context.route('**/empty.html', route => {
|
|
intercepted.push(1);
|
|
route.continue();
|
|
}, { times: 1 });
|
|
await page.goto(server.EMPTY_PAGE);
|
|
await page.goto(server.EMPTY_PAGE);
|
|
await page.goto(server.EMPTY_PAGE);
|
|
expect(intercepted).toHaveLength(1);
|
|
});
|
|
|
|
it('should overwrite post body with empty string', async ({ context, server, page, browserName }) => {
|
|
await context.route('**/empty.html', route => {
|
|
route.continue({
|
|
postData: '',
|
|
});
|
|
});
|
|
|
|
const [req] = await Promise.all([
|
|
server.waitForRequest('/empty.html'),
|
|
page.setContent(`
|
|
<script>
|
|
(async () => {
|
|
await fetch('${server.EMPTY_PAGE}', {
|
|
method: 'POST',
|
|
body: 'original',
|
|
});
|
|
})()
|
|
</script>
|
|
`),
|
|
]);
|
|
|
|
const body = (await req.postBody).toString();
|
|
expect(body).toBe('');
|
|
});
|