Skip to content

Commit d23017d

Browse files
committed
Add more tests
1 parent 8174ca0 commit d23017d

File tree

1 file changed

+90
-1
lines changed

1 file changed

+90
-1
lines changed

packages/playground/blueprints/src/lib/v1/resources.spec.ts

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
GitDirectoryResource,
44
BundledResource,
55
} from './resources';
6-
import { expect, describe, it, vi, beforeEach } from 'vitest';
6+
import { expect, describe, it, vi, beforeEach, afterEach } from 'vitest';
77
import { StreamedFile } from '@php-wasm/stream-compression';
88
import { mkdtemp, rm, writeFile, mkdir } from 'fs/promises';
99
import { tmpdir } from 'os';
@@ -232,6 +232,95 @@ describe('GitDirectoryResource', () => {
232232
expect(name).toBe('https-github.com-WordPress-link-manager-trunk');
233233
});
234234
});
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+
});
235324
});
236325

237326
describe('BlueprintResource', () => {

0 commit comments

Comments
 (0)