Skip to content

Commit

Permalink
test: improve test coverage for Request.continue (#4096)
Browse files Browse the repository at this point in the history
Drive-by: add clarification to docs/api.md regarding
chaning "URL".

References #4030
  • Loading branch information
aslushnikov committed Mar 5, 2019
1 parent f32d77e commit 1623bef
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Expand Up @@ -3214,7 +3214,7 @@ Exception is immediately thrown if the request interception is not enabled.

#### request.continue([overrides])
- `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`)
- `postData` <[string]> If set changes the post data of request
- `headers` <[Object]> If set changes the request HTTP headers
Expand Down
74 changes: 60 additions & 14 deletions test/network.spec.js
Expand Up @@ -513,20 +513,6 @@ module.exports.addTests = function({testRunner, expect, CHROME}) {
]);
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}) => {
await page.setRequestInterception(true);
page.on('request', request => request.abort());
Expand Down Expand Up @@ -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() {
it('should work', async({page, server}) => {
await page.setRequestInterception(true);
Expand Down
5 changes: 5 additions & 0 deletions utils/testserver/index.js
Expand Up @@ -189,6 +189,11 @@ class TestServer {
else
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;
if (this._auths.has(pathName)) {
const auth = this._auths.get(pathName);
Expand Down

0 comments on commit 1623bef

Please sign in to comment.