test: improve test coverage for Request.continue (#4096)

Drive-by: add clarification to docs/api.md regarding
chaning "URL".

References #4030
This commit is contained in:
Andrey Lushnikov 2019-03-05 14:57:15 -08:00 committed by GitHub
parent f32d77e074
commit 1623bef264
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 15 deletions

View File

@ -3214,7 +3214,7 @@ Exception is immediately thrown if the request interception is not enabled.
#### request.continue([overrides]) #### request.continue([overrides])
- `overrides` <[Object]> Optional request overwrites, which can be one of the following: - `overrides` <[Object]> Optional request overwrites, which can be one of the following:
- `url` <[string]> If set, the request url will be changed - `url` <[string]> If set, the request url will be changed. This is not a redirect. The request will be silently forwarded to the new url. For example, the address bar will show the original url.
- `method` <[string]> If set changes the request method (e.g. `GET` or `POST`) - `method` <[string]> If set changes the request method (e.g. `GET` or `POST`)
- `postData` <[string]> If set changes the post data of request - `postData` <[string]> If set changes the post data of request
- `headers` <[Object]> If set changes the request HTTP headers - `headers` <[Object]> If set changes the request HTTP headers

View File

@ -513,20 +513,6 @@ module.exports.addTests = function({testRunner, expect, CHROME}) {
]); ]);
expect(request.headers['referer']).toBe('http://google.com/'); expect(request.headers['referer']).toBe('http://google.com/');
}); });
it_fails_ffox('should amend HTTP headers', async({page, server}) => {
await page.setRequestInterception(true);
page.on('request', request => {
const headers = Object.assign({}, request.headers());
headers['FOO'] = 'bar';
request.continue({ headers });
});
await page.goto(server.EMPTY_PAGE);
const [request] = await Promise.all([
server.waitForRequest('/sleep.zzz'),
page.evaluate(() => fetch('/sleep.zzz'))
]);
expect(request.headers['foo']).toBe('bar');
});
it('should fail navigation when aborting main resource', async({page, server}) => { it('should fail navigation when aborting main resource', async({page, server}) => {
await page.setRequestInterception(true); await page.setRequestInterception(true);
page.on('request', request => request.abort()); page.on('request', request => request.abort());
@ -731,6 +717,66 @@ module.exports.addTests = function({testRunner, expect, CHROME}) {
}); });
}); });
describe_fails_ffox('Request.continue', function() {
it('should work', async({page, server}) => {
await page.setRequestInterception(true);
page.on('request', request => request.continue());
await page.goto(server.EMPTY_PAGE);
});
it('should amend HTTP headers', async({page, server}) => {
await page.setRequestInterception(true);
page.on('request', request => {
const headers = Object.assign({}, request.headers());
headers['FOO'] = 'bar';
request.continue({ headers });
});
await page.goto(server.EMPTY_PAGE);
const [request] = await Promise.all([
server.waitForRequest('/sleep.zzz'),
page.evaluate(() => fetch('/sleep.zzz'))
]);
expect(request.headers['foo']).toBe('bar');
});
it('should redirect in a way non-observable to page', async({page, server}) => {
await page.setRequestInterception(true);
page.on('request', request => {
const redirectURL = request.url().includes('/empty.html') ? server.PREFIX + '/consolelog.html' : undefined;
request.continue({ url: redirectURL });
});
let consoleMessage = null;
page.on('console', msg => consoleMessage = msg);
await page.goto(server.EMPTY_PAGE);
expect(page.url()).toBe(server.EMPTY_PAGE);
expect(consoleMessage.text()).toBe('yellow');
});
it('should amend method', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
await page.setRequestInterception(true);
page.on('request', request => {
request.continue({ method: 'POST' });
});
const [request] = await Promise.all([
server.waitForRequest('/sleep.zzz'),
page.evaluate(() => fetch('/sleep.zzz'))
]);
expect(request.method).toBe('POST');
});
it('should amend post data', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
await page.setRequestInterception(true);
page.on('request', request => {
request.continue({ postData: 'doggo' });
});
const [serverRequest] = await Promise.all([
server.waitForRequest('/sleep.zzz'),
page.evaluate(() => fetch('/sleep.zzz', { method: 'POST', body: 'birdy' }))
]);
expect(await serverRequest.postBody).toBe('doggo');
});
});
describe_fails_ffox('Request.respond', function() { describe_fails_ffox('Request.respond', function() {
it('should work', async({page, server}) => { it('should work', async({page, server}) => {
await page.setRequestInterception(true); await page.setRequestInterception(true);

View File

@ -189,6 +189,11 @@ class TestServer {
else else
throw error; throw error;
}); });
request.postBody = new Promise(resolve => {
let body = '';
request.on('data', chunk => body += chunk);
request.on('end', () => resolve(body));
});
const pathName = url.parse(request.url).path; const pathName = url.parse(request.url).path;
if (this._auths.has(pathName)) { if (this._auths.has(pathName)) {
const auth = this._auths.get(pathName); const auth = this._auths.get(pathName);