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" }));
```
### option: Route.fulfill._response
- `_response` <[Response]>
Intercepted response. Will be used to populate all response fields not explicitely overridden.
### option: Route.fulfill.status
- `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) => {
let useInterceptedResponseBody;
let { status: statusOption, headers: headersOption, body: bodyOption } = options;
if (options._response) {
statusOption ||= options._response.status();
headersOption ||= options._response.headers();
if (options.response) {
statusOption ||= options.response.status();
headersOption ||= options.response.headers();
if (options.body === undefined && options.path === undefined) {
if (options._response === this._interceptedResponse)
if (options.response === this._interceptedResponse)
useInterceptedResponseBody = true;
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');
await page.route('**/*', async route => {
// @ts-expect-error
const _response = await route._continueToResponse({});
const response = await route._continueToResponse({});
await route.fulfill({
_response,
// @ts-expect-error
response,
status: 201,
body: ''
});
@ -92,7 +93,8 @@ it('should fulfill with any response', async ({page, server, browserName, browse
// @ts-expect-error
await route._continueToResponse({});
await route.fulfill({
_response: sampleResponse,
// @ts-expect-error
response: sampleResponse,
status: 201,
contentType: 'text/plain'
});
@ -124,8 +126,9 @@ it('should support fulfill after intercept', async ({page, server}) => {
const requestPromise = server.waitForRequest('/title.html');
await page.route('**', async route => {
// @ts-expect-error
const _response = await route._continueToResponse();
await route.fulfill({ _response });
const response = await route._continueToResponse();
// @ts-expect-error
await route.fulfill({ response });
});
const response = await page.goto(server.PREFIX + '/title.html');
const request = await requestPromise;
@ -143,8 +146,9 @@ it('should intercept failures', async ({page, browserName, browserMajorVersion,
await page.route('**', async route => {
try {
// @ts-expect-error
const _response = await route._continueToResponse();
await route.fulfill({ _response });
const response = await route._continueToResponse();
// @ts-expect-error
await route.fulfill({ response });
} catch (e) {
error = e;
}
@ -163,13 +167,14 @@ it('should support request overrides', async ({page, server, browserName, browse
const requestPromise = server.waitForRequest('/empty.html');
await page.route('**/foo', async route => {
// @ts-expect-error
const _response = await route._continueToResponse({
const response = await route._continueToResponse({
url: server.EMPTY_PAGE,
method: 'POST',
headers: {'foo': 'bar'},
postData: 'my data',
});
await route.fulfill({ _response });
// @ts-expect-error
await route.fulfill({ response });
});
await page.goto(server.PREFIX + '/foo');
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.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}) => {
@ -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.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}) => {
@ -238,7 +245,8 @@ it('should give access to the intercepted response body', async ({page, server})
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}) => {
@ -314,8 +322,9 @@ it('should fulfill original response after redirects', async ({page, browserName
await page.route('**/*', async route => {
++routeCalls;
// @ts-expect-error
const _response = await route._continueToResponse({});
await route.fulfill({ _response });
const response = await route._continueToResponse({});
// @ts-expect-error
await route.fulfill({ response });
});
const response = await page.goto(server.PREFIX + '/redirect/1.html');
expect(requestUrls).toEqual(expectedUrls);

5
types/types.d.ts vendored
View File

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