chore: hide Route.fulfill._response from API (#8483)

This commit is contained in:
Max Schmitt 2021-08-27 00:44:20 +02:00 committed by GitHub
parent 8ebd7851c2
commit 998f2ab959
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 30 deletions

View file

@ -181,11 +181,6 @@ page.route("**/xhr_endpoint", lambda route: route.fulfill(path="mock_data.json")
await page.RouteAsync("**/xhr_endpoint", route => route.FulfillAsync(new RouteFulfillOptions { Path = "mock_data.json" })); await page.RouteAsync("**/xhr_endpoint", route => route.FulfillAsync(new RouteFulfillOptions { Path = "mock_data.json" }));
``` ```
### option: Route.fulfill._response
- `_response` <[Response]>
Intercepted response. Will be used to populate all response fields not explicitely overridden.
### option: Route.fulfill.status ### option: Route.fulfill.status
- `status` <[int]> - `status` <[int]>

View file

@ -267,18 +267,18 @@ export class Route extends ChannelOwner<channels.RouteChannel, channels.RouteIni
}); });
} }
async fulfill(options: { _response?: Response, status?: number, headers?: Headers, contentType?: string, body?: string | Buffer, path?: string } = {}) { async fulfill(options: { response?: Response, status?: number, headers?: Headers, contentType?: string, body?: string | Buffer, path?: string } = {}) {
return this._wrapApiCall(async (channel: channels.RouteChannel) => { return this._wrapApiCall(async (channel: channels.RouteChannel) => {
let useInterceptedResponseBody; let useInterceptedResponseBody;
let { status: statusOption, headers: headersOption, body: bodyOption } = options; let { status: statusOption, headers: headersOption, body: bodyOption } = options;
if (options._response) { if (options.response) {
statusOption ||= options._response.status(); statusOption ||= options.response.status();
headersOption ||= options._response.headers(); headersOption ||= options.response.headers();
if (options.body === undefined && options.path === undefined) { if (options.body === undefined && options.path === undefined) {
if (options._response === this._interceptedResponse) if (options.response === this._interceptedResponse)
useInterceptedResponseBody = true; useInterceptedResponseBody = true;
else else
bodyOption = await options._response.body(); bodyOption = await options.response.body();
} }
} }

View file

@ -44,9 +44,10 @@ it('should fulfill response with empty body', async ({page, server, browserName,
it.skip(browserName === 'chromium' && browserMajorVersion <= 91, 'Fails in Electron that uses old Chromium'); it.skip(browserName === 'chromium' && browserMajorVersion <= 91, 'Fails in Electron that uses old Chromium');
await page.route('**/*', async route => { await page.route('**/*', async route => {
// @ts-expect-error // @ts-expect-error
const _response = await route._continueToResponse({}); const response = await route._continueToResponse({});
await route.fulfill({ await route.fulfill({
_response, // @ts-expect-error
response,
status: 201, status: 201,
body: '' body: ''
}); });
@ -92,7 +93,8 @@ it('should fulfill with any response', async ({page, server, browserName, browse
// @ts-expect-error // @ts-expect-error
await route._continueToResponse({}); await route._continueToResponse({});
await route.fulfill({ await route.fulfill({
_response: sampleResponse, // @ts-expect-error
response: sampleResponse,
status: 201, status: 201,
contentType: 'text/plain' contentType: 'text/plain'
}); });
@ -124,8 +126,9 @@ it('should support fulfill after intercept', async ({page, server}) => {
const requestPromise = server.waitForRequest('/title.html'); const requestPromise = server.waitForRequest('/title.html');
await page.route('**', async route => { await page.route('**', async route => {
// @ts-expect-error // @ts-expect-error
const _response = await route._continueToResponse(); const response = await route._continueToResponse();
await route.fulfill({ _response }); // @ts-expect-error
await route.fulfill({ response });
}); });
const response = await page.goto(server.PREFIX + '/title.html'); const response = await page.goto(server.PREFIX + '/title.html');
const request = await requestPromise; const request = await requestPromise;
@ -143,8 +146,9 @@ it('should intercept failures', async ({page, browserName, browserMajorVersion,
await page.route('**', async route => { await page.route('**', async route => {
try { try {
// @ts-expect-error // @ts-expect-error
const _response = await route._continueToResponse(); const response = await route._continueToResponse();
await route.fulfill({ _response }); // @ts-expect-error
await route.fulfill({ response });
} catch (e) { } catch (e) {
error = e; error = e;
} }
@ -163,13 +167,14 @@ it('should support request overrides', async ({page, server, browserName, browse
const requestPromise = server.waitForRequest('/empty.html'); const requestPromise = server.waitForRequest('/empty.html');
await page.route('**/foo', async route => { await page.route('**/foo', async route => {
// @ts-expect-error // @ts-expect-error
const _response = await route._continueToResponse({ const response = await route._continueToResponse({
url: server.EMPTY_PAGE, url: server.EMPTY_PAGE,
method: 'POST', method: 'POST',
headers: {'foo': 'bar'}, headers: {'foo': 'bar'},
postData: 'my data', postData: 'my data',
}); });
await route.fulfill({ _response }); // @ts-expect-error
await route.fulfill({ response });
}); });
await page.goto(server.PREFIX + '/foo'); await page.goto(server.PREFIX + '/foo');
const request = await requestPromise; const request = await requestPromise;
@ -197,7 +202,8 @@ it('should give access to the intercepted response', async ({page, server}) => {
expect(response.url()).toBe(server.PREFIX + '/title.html'); expect(response.url()).toBe(server.PREFIX + '/title.html');
expect(response.headers()['content-type']).toBe('text/html; charset=utf-8'); expect(response.headers()['content-type']).toBe('text/html; charset=utf-8');
await Promise.all([route.fulfill({ _response: response }), evalPromise]); // @ts-expect-error
await Promise.all([route.fulfill({ response }), evalPromise]);
}); });
it('should give access to the intercepted response status text', async ({page, server, browserName}) => { it('should give access to the intercepted response status text', async ({page, server, browserName}) => {
@ -220,7 +226,8 @@ it('should give access to the intercepted response status text', async ({page, s
expect(response.statusText()).toBe('You are awesome'); expect(response.statusText()).toBe('You are awesome');
expect(response.url()).toBe(server.PREFIX + '/title.html'); expect(response.url()).toBe(server.PREFIX + '/title.html');
await Promise.all([route.fulfill({ _response: response }), evalPromise]); // @ts-expect-error
await Promise.all([route.fulfill({ response }), evalPromise]);
}); });
it('should give access to the intercepted response body', async ({page, server}) => { it('should give access to the intercepted response body', async ({page, server}) => {
@ -238,7 +245,8 @@ it('should give access to the intercepted response body', async ({page, server})
expect((await response.text())).toBe('{"foo": "bar"}\n'); expect((await response.text())).toBe('{"foo": "bar"}\n');
await Promise.all([route.fulfill({ _response: response }), evalPromise]); // @ts-expect-error
await Promise.all([route.fulfill({ response }), evalPromise]);
}); });
it('should be abortable after interception', async ({page, server, browserName}) => { it('should be abortable after interception', async ({page, server, browserName}) => {
@ -314,8 +322,9 @@ it('should fulfill original response after redirects', async ({page, browserName
await page.route('**/*', async route => { await page.route('**/*', async route => {
++routeCalls; ++routeCalls;
// @ts-expect-error // @ts-expect-error
const _response = await route._continueToResponse({}); const response = await route._continueToResponse({});
await route.fulfill({ _response }); // @ts-expect-error
await route.fulfill({ response });
}); });
const response = await page.goto(server.PREFIX + '/redirect/1.html'); const response = await page.goto(server.PREFIX + '/redirect/1.html');
expect(requestUrls).toEqual(expectedUrls); expect(requestUrls).toEqual(expectedUrls);

5
types/types.d.ts vendored
View file

@ -11940,11 +11940,6 @@ export interface Route {
* @param options * @param options
*/ */
fulfill(options?: { fulfill(options?: {
/**
* Intercepted response. Will be used to populate all response fields not explicitely overridden.
*/
_response?: Response;
/** /**
* Response body. * Response body.
*/ */