|
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,80 @@ 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 | + ].some((url) => resolvedConfig.server.cors.origin.some((regex) => test(regex, url)))).toBe(true) |
| 492 | + // Disallowed origins... |
| 493 | + expect([ |
| 494 | + 'http://laravel.com', |
| 495 | + 'https://laravel.com', |
| 496 | + 'http://laravel.com:8000', |
| 497 | + 'https://laravel.com:8000', |
| 498 | + 'http://128.0.0.1', |
| 499 | + 'https://128.0.0.1', |
| 500 | + 'http://128.0.0.1:8000', |
| 501 | + 'https://128.0.0.1:8000', |
| 502 | + 'https://example.com', |
| 503 | + 'http://example.com:8000', |
| 504 | + 'https://example.com:8000', |
| 505 | + 'http://exampletest', |
| 506 | + 'http://example.test:', |
| 507 | + ].some((url) => resolvedConfig.server.cors.origin.some((regex) => test(regex, url)))).toBe(false) |
| 508 | + |
| 509 | + fs.rmSync(path.join(__dirname, '.env')) |
| 510 | + }) |
| 511 | + |
| 512 | + it("respects the user's server.cors config", () => { |
| 513 | + const plugins = laravel({ |
| 514 | + input: 'resources/js/app.js', |
| 515 | + }) |
| 516 | + const resolvedConfig = plugins[0].config({ |
| 517 | + envDir: __dirname, |
| 518 | + server: { |
| 519 | + cors: true, |
| 520 | + } |
| 521 | + }, { |
| 522 | + mode: '', |
| 523 | + command: 'serve' |
| 524 | + }) |
| 525 | + |
| 526 | + expect(resolvedConfig.server.cors) |
| 527 | + }) |
452 | 528 | })
|
453 | 529 |
|
454 | 530 | describe('inertia-helpers', () => {
|
|
0 commit comments