|
3 | 3 | GitDirectoryResource, |
4 | 4 | BundledResource, |
5 | 5 | } from './resources'; |
6 | | -import { expect, describe, it, vi, beforeEach } from 'vitest'; |
| 6 | +import { expect, describe, it, vi, beforeEach, afterEach } from 'vitest'; |
7 | 7 | import { StreamedFile } from '@php-wasm/stream-compression'; |
8 | 8 | import { mkdtemp, rm, writeFile, mkdir } from 'fs/promises'; |
9 | 9 | import { tmpdir } from 'os'; |
@@ -232,6 +232,95 @@ describe('GitDirectoryResource', () => { |
232 | 232 | expect(name).toBe('https-github.com-WordPress-link-manager-trunk'); |
233 | 233 | }); |
234 | 234 | }); |
| 235 | + |
| 236 | + describe('CORS handling', () => { |
| 237 | + let originalFetch: typeof global.fetch; |
| 238 | + |
| 239 | + beforeEach(() => { |
| 240 | + originalFetch = global.fetch; |
| 241 | + }); |
| 242 | + |
| 243 | + afterEach(() => { |
| 244 | + global.fetch = originalFetch; |
| 245 | + }); |
| 246 | + |
| 247 | + it('should unwrap CORS URL in GitAuthenticationError', async () => { |
| 248 | + global.fetch = vi.fn().mockResolvedValue({ |
| 249 | + ok: false, |
| 250 | + status: 401, |
| 251 | + statusText: 'Unauthorized', |
| 252 | + }); |
| 253 | + |
| 254 | + const githubUrl = 'https://github.com/user/private-repo'; |
| 255 | + const resource = new GitDirectoryResource( |
| 256 | + { |
| 257 | + resource: 'git:directory', |
| 258 | + url: githubUrl, |
| 259 | + ref: 'main', |
| 260 | + }, |
| 261 | + undefined, |
| 262 | + { |
| 263 | + corsProxy: 'https://cors-proxy.com/', |
| 264 | + } |
| 265 | + ); |
| 266 | + |
| 267 | + await expect(resource.resolve()).rejects.toMatchObject({ |
| 268 | + name: 'GitAuthenticationError', |
| 269 | + repoUrl: githubUrl, |
| 270 | + status: 401, |
| 271 | + }); |
| 272 | + }); |
| 273 | + |
| 274 | + it('should preserve GitHub URL in GitAuthenticationError without CORS proxy', async () => { |
| 275 | + global.fetch = vi.fn().mockResolvedValue({ |
| 276 | + ok: false, |
| 277 | + status: 401, |
| 278 | + statusText: 'Unauthorized', |
| 279 | + }); |
| 280 | + |
| 281 | + const githubUrl = 'https://github.com/user/private-repo'; |
| 282 | + const resource = new GitDirectoryResource({ |
| 283 | + resource: 'git:directory', |
| 284 | + url: githubUrl, |
| 285 | + ref: 'main', |
| 286 | + }); |
| 287 | + |
| 288 | + await expect(resource.resolve()).rejects.toMatchObject({ |
| 289 | + name: 'GitAuthenticationError', |
| 290 | + repoUrl: githubUrl, |
| 291 | + status: 401, |
| 292 | + }); |
| 293 | + }); |
| 294 | + |
| 295 | + it('should call gitAdditionalHeadersCallback without CORS proxy', async () => { |
| 296 | + const githubUrl = 'https://github.com/user/private-repo'; |
| 297 | + const headerCallback = vi.fn().mockReturnValue({ |
| 298 | + Authorization: 'Bearer test-token', |
| 299 | + }); |
| 300 | + |
| 301 | + const resource = new GitDirectoryResource( |
| 302 | + { |
| 303 | + resource: 'git:directory', |
| 304 | + url: githubUrl, |
| 305 | + ref: 'main', |
| 306 | + }, |
| 307 | + undefined, |
| 308 | + { |
| 309 | + additionalHeaders: headerCallback, |
| 310 | + } |
| 311 | + ); |
| 312 | + |
| 313 | + // Call resolve - it will fail but that's okay, we just want to verify the callback |
| 314 | + try { |
| 315 | + await resource.resolve(); |
| 316 | + } catch { |
| 317 | + // Expected to fail - we're not mocking the entire git resolution |
| 318 | + } |
| 319 | + |
| 320 | + // Verify the callback was called with the GitHub URL (not CORS-wrapped) |
| 321 | + expect(headerCallback).toHaveBeenCalledWith(githubUrl); |
| 322 | + }); |
| 323 | + }); |
235 | 324 | }); |
236 | 325 |
|
237 | 326 | describe('BlueprintResource', () => { |
|
0 commit comments