-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhttp-frame.ts
More file actions
59 lines (48 loc) · 1.79 KB
/
http-frame.ts
File metadata and controls
59 lines (48 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { Route } from '@playwright/test'
import { HttpNetworkFrame } from 'msw/experimental'
import { fulfillResponse, handleRouteSafely } from '../utils.js'
import type { RequestHandler } from 'msw'
import type { UnhandledFrameHandle } from '../../node_modules/msw/lib/core/experimental/on-unhandled-frame.mjs'
import type { NetworkFrameResolutionContext } from '../../node_modules/msw/lib/core/experimental/frames/network-frame.mjs'
interface PlaywrightHttpNetworkFrameOptions {
request: Request
id?: string
route: Route
inferredBaseUrl?: string
}
export class PlaywrightHttpNetworkFrame extends HttpNetworkFrame {
#route: Route
#inferredBaseUrl?: string
constructor(options: PlaywrightHttpNetworkFrameOptions) {
super(options)
this.#route = options.route
this.#inferredBaseUrl = options.inferredBaseUrl
}
resolve(
handlers: Array<RequestHandler>,
onUnhandledFrame: UnhandledFrameHandle,
resolutionContext?: NetworkFrameResolutionContext,
): Promise<boolean | null> {
return super.resolve(handlers, onUnhandledFrame, {
...resolutionContext,
baseUrl: resolutionContext?.baseUrl ?? this.#inferredBaseUrl,
quiet: resolutionContext?.quiet !== false,
})
}
async respondWith(response?: Response): Promise<void> {
if (!response) return
if (response.status === 0) {
return await handleRouteSafely(() => this.#route.abort())
}
return await handleRouteSafely(() => fulfillResponse(this.#route, response))
}
passthrough(): Promise<void> {
return handleRouteSafely(() => this.#route.fallback())
}
errorWith(reason?: unknown): Promise<void> {
if (reason instanceof Response) {
return handleRouteSafely(() => fulfillResponse(this.#route, reason))
}
return handleRouteSafely(() => this.#route.abort())
}
}