fix(fetch): throw when methods are called on disposed context (#27868)
Fixes #27822
This commit is contained in:
parent
7bab19d807
commit
c7b6a76437
|
|
@ -183,6 +183,10 @@ context cookies from the response. The method will automatically follow redirect
|
|||
All responses returned by [`method: APIRequestContext.get`] and similar methods are stored in the memory, so that you can later call [`method: APIResponse.body`]. This method
|
||||
discards all stored responses, and makes [`method: APIResponse.body`] throw "Response disposed" error.
|
||||
|
||||
If this [APIRequestContext] is obtained via [`property: BrowserContext.request`] or [`property: Page.request`], it will keep working until its owning [BrowserContext] closes.
|
||||
|
||||
If this [APIRequestContext] was created by [`method: APIRequest.newContext`], this method discards all its resources, calling any method on disposed [APIRequestContext] will throw an exception.
|
||||
|
||||
## async method: APIRequestContext.fetch
|
||||
* since: v1.16
|
||||
- returns: <[APIResponse]>
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
*/
|
||||
|
||||
import type * as channels from '@protocol/channels';
|
||||
import type { APIRequestContext } from '../fetch';
|
||||
import { APIRequestContext } from '../fetch';
|
||||
import type { CallMetadata } from '../instrumentation';
|
||||
import type { Request, Response, Route } from '../network';
|
||||
import { WebSocket } from '../network';
|
||||
|
|
@ -191,6 +191,8 @@ export class APIRequestContextDispatcher extends Dispatcher<APIRequestContext, c
|
|||
tracing,
|
||||
});
|
||||
|
||||
this.addObjectListener(APIRequestContext.Events.Dispose, () => this._dispose());
|
||||
|
||||
this.adopt(tracing);
|
||||
}
|
||||
|
||||
|
|
|
|||
10
packages/playwright-core/types/types.d.ts
vendored
10
packages/playwright-core/types/types.d.ts
vendored
|
|
@ -15262,6 +15262,16 @@ export interface APIRequestContext {
|
|||
* stored responses, and makes
|
||||
* [apiResponse.body()](https://playwright.dev/docs/api/class-apiresponse#api-response-body) throw "Response disposed"
|
||||
* error.
|
||||
*
|
||||
* If this {@link APIRequestContext} is obtained via
|
||||
* [browserContext.request](https://playwright.dev/docs/api/class-browsercontext#browser-context-request) or
|
||||
* [page.request](https://playwright.dev/docs/api/class-page#page-request), it will keep working until its owning
|
||||
* {@link BrowserContext} closes.
|
||||
*
|
||||
* If this {@link APIRequestContext} was created by
|
||||
* [apiRequest.newContext([options])](https://playwright.dev/docs/api/class-apirequest#api-request-new-context), this
|
||||
* method discards all its resources, calling any method on disposed {@link APIRequestContext} will throw an
|
||||
* exception.
|
||||
*/
|
||||
dispose(): Promise<void>;
|
||||
|
||||
|
|
|
|||
|
|
@ -1192,3 +1192,9 @@ it('should update host header on redirect', async ({ context, server }) => {
|
|||
|
||||
expect((await reqPromise).headers.host).toBe(new URL(server.CROSS_PROCESS_PREFIX).host);
|
||||
});
|
||||
|
||||
it('should keep working after dispose', async ({ context, server }) => {
|
||||
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/27822' });
|
||||
await context.request.dispose();
|
||||
await expect(await context.request.get(server.EMPTY_PAGE)).toBeOK();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -451,3 +451,10 @@ it('should serialize post data on the client', async ({ playwright, server }) =>
|
|||
// expect(serverRequest.rawHeaders).toContain('vaLUE');
|
||||
await request.dispose();
|
||||
});
|
||||
|
||||
it('should throw after dispose', async ({ playwright, server }) => {
|
||||
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/27822' });
|
||||
const request = await playwright.request.newContext();
|
||||
await request.dispose();
|
||||
await expect(request.get(server.EMPTY_PAGE)).rejects.toThrow('Target page, context or browser has been closed');
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue