|
5 | 5 | use Illuminate\Http\Request; |
6 | 6 | use Illuminate\Http\Response; |
7 | 7 | use Illuminate\Support\Facades\Route; |
| 8 | +use Illuminate\Support\Facades\Vite; |
| 9 | +use Illuminate\Testing\TestResponse; |
8 | 10 | use Laravel\Boost\Middleware\InjectBoost; |
9 | 11 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
10 | 12 | use Symfony\Component\HttpFoundation\JsonResponse; |
@@ -89,3 +91,30 @@ function createMiddlewareResponse($response): SymfonyResponse |
89 | 91 | 'with head and body tags' => '<html><head><title>Test</title></head><body></body></html>', |
90 | 92 | 'without head/body tags' => '<html>Test</html>', |
91 | 93 | ]); |
| 94 | + |
| 95 | +it('handles CSP nonce attribute correctly', function ($nonce, $assertions) { |
| 96 | + if ($nonce) { |
| 97 | + Vite::useCspNonce($nonce); |
| 98 | + } |
| 99 | + |
| 100 | + Route::get('injection-test', fn () => view('test::injection-test')) |
| 101 | + ->middleware(InjectBoost::class); |
| 102 | + |
| 103 | + $response = $this->get('injection-test')->assertViewIs('test::injection-test'); |
| 104 | + |
| 105 | + $assertions($response); |
| 106 | +})->with([ |
| 107 | + 'with CSP nonce configured' => [ |
| 108 | + 'test-nonce', |
| 109 | + fn (TestResponse $response) => $response |
| 110 | + ->assertSee('nonce="test-nonce"', false) |
| 111 | + ->assertSee('id="browser-logger-active"', false), |
| 112 | + ], |
| 113 | + 'without CSP nonce configured' => [ |
| 114 | + null, |
| 115 | + fn (TestResponse $response) => $response |
| 116 | + ->assertSee('<script id="browser-logger-active">', false) |
| 117 | + ->assertDontSee('nonce=', false) |
| 118 | + ->assertDontSee('test-nonce', false), |
| 119 | + ], |
| 120 | +]); |
0 commit comments