|
5 | 5 | // for the test to read back. The fixtures install them before a test starts, |
6 | 6 | // so nothing reaches the system. |
7 | 7 | import type { ElectronApplication } from '@playwright/test'; |
| 8 | +import { MOCK_FILES } from '../files.ts'; |
| 9 | +import { readShared } from '../shared.ts'; |
8 | 10 |
|
9 | 11 | /** What the stand-ins recorded, in the main process. */ |
10 | 12 | export interface MainRecord { |
@@ -149,6 +151,59 @@ export const mainProcess = (app: ElectronApplication) => ({ |
149 | 151 | answers, |
150 | 152 | ), |
151 | 153 |
|
| 154 | + /** |
| 155 | + * Serves, in the web views' session (whose requests the app's downloads |
| 156 | + * use too), sites a test would otherwise reach over the internet, at their |
| 157 | + * own https URLs, so the app's host checks and injected scripts run as on |
| 158 | + * the real sites: |
| 159 | + * - `loginPage`: every page of Tsinghua's login site (id.tsinghua.edu.cn); |
| 160 | + * - `mockFilesDir`: the mock account's files (MOCK_FILES in |
| 161 | + * src/data/mock.ts), from this checkout's `mock-files`. |
| 162 | + * |
| 163 | + * Every other request goes out as usual, but through this handler, which |
| 164 | + * DevTools' `Fetch` doesn't see: loadHtml (webviews.ts) can't answer a |
| 165 | + * load then, so only tests that need it call it. |
| 166 | + */ |
| 167 | + serveSites: (sites: { loginPage?: string; mockFilesDir?: string }) => |
| 168 | + app.evaluate( |
| 169 | + ({ net, session }, given) => { |
| 170 | + const fs = process.getBuiltinModule('node:fs'); |
| 171 | + const path = process.getBuiltinModule('node:path'); |
| 172 | + session |
| 173 | + .fromPartition(given.partition) |
| 174 | + .protocol.handle('https', request => { |
| 175 | + const url = new URL(request.url); |
| 176 | + const mockFile = `${url.origin}${url.pathname}`.startsWith( |
| 177 | + given.mockFiles, |
| 178 | + ) |
| 179 | + ? decodeURIComponent( |
| 180 | + url.pathname.slice(new URL(given.mockFiles).pathname.length), |
| 181 | + ) |
| 182 | + : undefined; |
| 183 | + if (url.hostname === 'id.tsinghua.edu.cn' && given.loginPage) { |
| 184 | + return new Response(given.loginPage, { |
| 185 | + headers: { 'content-type': 'text/html; charset=utf-8' }, |
| 186 | + }); |
| 187 | + } |
| 188 | + if (mockFile !== undefined && given.mockFilesDir) { |
| 189 | + const file = path.join(given.mockFilesDir, mockFile); |
| 190 | + return file.startsWith(given.mockFilesDir + path.sep) && |
| 191 | + fs.statSync(file, { throwIfNoEntry: false })?.isFile() |
| 192 | + ? new Response(fs.readFileSync(file), { |
| 193 | + headers: { 'content-type': 'application/octet-stream' }, |
| 194 | + }) |
| 195 | + : new Response('Not found', { status: 404 }); |
| 196 | + } |
| 197 | + return net.fetch(request, { bypassCustomProtocolHandlers: true }); |
| 198 | + }); |
| 199 | + }, |
| 200 | + { |
| 201 | + ...sites, |
| 202 | + mockFiles: MOCK_FILES, |
| 203 | + partition: readShared('webviewPartition.json').partition, |
| 204 | + }, |
| 205 | + ), |
| 206 | + |
152 | 207 | /** What the stand-ins have recorded so far. */ |
153 | 208 | record: () => |
154 | 209 | app.evaluate(() => { |
|
0 commit comments