| 
1 | 1 | import { afterEach, describe, expect, it, vi } from 'vitest'  | 
 | 2 | +import fs from 'fs'  | 
2 | 3 | import laravel from '../src'  | 
3 | 4 | import { resolvePageComponent } from '../src/inertia-helpers';  | 
 | 5 | +import path from 'path';  | 
4 | 6 | 
 
  | 
5 | 7 | vi.mock('fs', async () => {  | 
6 | 8 |     const actual = await vi.importActual<typeof import('fs')>('fs')  | 
@@ -449,6 +451,81 @@ describe('laravel-vite-plugin', () => {  | 
449 | 451 |             config: { delay: 123 }  | 
450 | 452 |         })  | 
451 | 453 |     })  | 
 | 454 | + | 
 | 455 | +    it('configures default cors.origin values', () => {  | 
 | 456 | +        const test = (pattern: RegExp|string, value: string) => pattern instanceof RegExp ? pattern.test(value) : pattern === value  | 
 | 457 | +        fs.writeFileSync(path.join(__dirname, '.env'), 'APP_URL=http://example.com')  | 
 | 458 | + | 
 | 459 | +        const plugins = laravel({  | 
 | 460 | +            input: 'resources/js/app.js',  | 
 | 461 | +        })  | 
 | 462 | +        const resolvedConfig = plugins[0].config({ envDir: __dirname }, {  | 
 | 463 | +            mode: '',  | 
 | 464 | +            command: 'serve'  | 
 | 465 | +        })  | 
 | 466 | + | 
 | 467 | +        // Allowed origins...  | 
 | 468 | +        expect([  | 
 | 469 | +            // localhost  | 
 | 470 | +            'http://localhost',  | 
 | 471 | +            'https://localhost',  | 
 | 472 | +            'http://localhost:8080',  | 
 | 473 | +            'https://localhost:8080',  | 
 | 474 | +            // 127.0.0.1  | 
 | 475 | +            'http://127.0.0.1',  | 
 | 476 | +            'https://127.0.0.1',  | 
 | 477 | +            'http://127.0.0.1:8000',  | 
 | 478 | +            'https://127.0.0.1:8000',  | 
 | 479 | +            // *.test  | 
 | 480 | +            'http://laravel.test',  | 
 | 481 | +            'https://laravel.test',  | 
 | 482 | +            'http://laravel.test:8000',  | 
 | 483 | +            'https://laravel.test:8000',  | 
 | 484 | +            'http://my-app.test',  | 
 | 485 | +            'https://my-app.test',  | 
 | 486 | +            'http://my-app.test:8000',  | 
 | 487 | +            'https://my-app.test:8000',  | 
 | 488 | +            'https://my-app.test:8',  | 
 | 489 | +            // APP_URL  | 
 | 490 | +            'http://example.com',  | 
 | 491 | +            'https://subdomain.my-app.test',  | 
 | 492 | +        ].some((url) => resolvedConfig.server.cors.origin.some((regex) => test(regex, url)))).toBe(true)  | 
 | 493 | +        // Disallowed origins...  | 
 | 494 | +        expect([  | 
 | 495 | +            'http://laravel.com',  | 
 | 496 | +            'https://laravel.com',  | 
 | 497 | +            'http://laravel.com:8000',  | 
 | 498 | +            'https://laravel.com:8000',  | 
 | 499 | +            'http://128.0.0.1',  | 
 | 500 | +            'https://128.0.0.1',  | 
 | 501 | +            'http://128.0.0.1:8000',  | 
 | 502 | +            'https://128.0.0.1:8000',  | 
 | 503 | +            'https://example.com',  | 
 | 504 | +            'http://example.com:8000',  | 
 | 505 | +            'https://example.com:8000',  | 
 | 506 | +            'http://exampletest',  | 
 | 507 | +            'http://example.test:',  | 
 | 508 | +        ].some((url) => resolvedConfig.server.cors.origin.some((regex) => test(regex, url)))).toBe(false)  | 
 | 509 | + | 
 | 510 | +        fs.rmSync(path.join(__dirname, '.env'))  | 
 | 511 | +    })  | 
 | 512 | + | 
 | 513 | +    it("respects the user's server.cors config", () => {  | 
 | 514 | +        const plugins = laravel({  | 
 | 515 | +            input: 'resources/js/app.js',  | 
 | 516 | +        })  | 
 | 517 | +        const resolvedConfig = plugins[0].config({  | 
 | 518 | +            envDir: __dirname,  | 
 | 519 | +            server: {  | 
 | 520 | +                cors: true,  | 
 | 521 | +            }  | 
 | 522 | +        }, {  | 
 | 523 | +            mode: '',  | 
 | 524 | +            command: 'serve'  | 
 | 525 | +        })  | 
 | 526 | + | 
 | 527 | +        expect(resolvedConfig.server.cors).toBe(true)  | 
 | 528 | +    })  | 
452 | 529 | })  | 
453 | 530 | 
 
  | 
454 | 531 | describe('inertia-helpers', () => {  | 
 | 
0 commit comments