|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tonysm\TailwindCss\Tests; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Tonysm\TailwindCss\Http\Middleware\AddLinkHeaderForPreloadedAssets; |
| 7 | + |
| 8 | +class PreloadingHeaderTest extends TestCase |
| 9 | +{ |
| 10 | + /** @test */ |
| 11 | + public function no_link_header_when_not_preloading() |
| 12 | + { |
| 13 | + config()->set('tailwindcss.build.manifest_file_path', __DIR__.'/stubs/test-manifest.json'); |
| 14 | + |
| 15 | + $tailwindcss = tailwindcss('css/app.css', preload: false); |
| 16 | + |
| 17 | + $response = resolve(AddLinkHeaderForPreloadedAssets::class)->handle( |
| 18 | + Request::create('/'), |
| 19 | + fn () => response('hello world'), |
| 20 | + ); |
| 21 | + |
| 22 | + $this->assertEquals('http://localhost/css/app-123.css', (string) $tailwindcss); |
| 23 | + $this->assertEquals('hello world', $response->content()); |
| 24 | + $this->assertNull($response->headers->get('Link', null)); |
| 25 | + } |
| 26 | + |
| 27 | + /** @test */ |
| 28 | + public function adds_link_header_when_preloading() |
| 29 | + { |
| 30 | + config()->set('tailwindcss.build.manifest_file_path', __DIR__.'/stubs/test-manifest.json'); |
| 31 | + |
| 32 | + $tailwindcss = tailwindcss('css/app.css', preload: true); |
| 33 | + |
| 34 | + $response = resolve(AddLinkHeaderForPreloadedAssets::class)->handle( |
| 35 | + Request::create('/'), |
| 36 | + fn () => response('hello world'), |
| 37 | + ); |
| 38 | + |
| 39 | + $this->assertEquals($asset = 'http://localhost/css/app-123.css', (string) $tailwindcss); |
| 40 | + $this->assertEquals('hello world', $response->content()); |
| 41 | + $this->assertEquals("<{$asset}>; rel=preload; as=style", $response->headers->get('Link', null)); |
| 42 | + } |
| 43 | + |
| 44 | + /** @test */ |
| 45 | + public function keeps_existing_preloading_link_header() |
| 46 | + { |
| 47 | + config()->set('tailwindcss.build.manifest_file_path', __DIR__.'/stubs/test-manifest.json'); |
| 48 | + |
| 49 | + $tailwindcss = tailwindcss('css/app.css', preload: true); |
| 50 | + |
| 51 | + $response = resolve(AddLinkHeaderForPreloadedAssets::class)->handle( |
| 52 | + Request::create('/'), |
| 53 | + fn () => response('hello world')->withHeaders([ |
| 54 | + 'Link' => '</js/app.js>; rel=modulepreload', |
| 55 | + ]), |
| 56 | + ); |
| 57 | + |
| 58 | + $this->assertEquals($asset = 'http://localhost/css/app-123.css', (string) $tailwindcss); |
| 59 | + $this->assertEquals('hello world', $response->content()); |
| 60 | + $this->assertEquals("</js/app.js>; rel=modulepreload, <{$asset}>; rel=preload; as=style", $response->headers->get('Link', null)); |
| 61 | + } |
| 62 | + |
| 63 | + /** @test */ |
| 64 | + public function adds_link_header_when_preloading_custom_attributes() |
| 65 | + { |
| 66 | + config()->set('tailwindcss.build.manifest_file_path', __DIR__.'/stubs/test-manifest.json'); |
| 67 | + |
| 68 | + $tailwindcss = tailwindcss('css/app.css', ['crossorigin' => 'anonymous']); |
| 69 | + |
| 70 | + $response = resolve(AddLinkHeaderForPreloadedAssets::class)->handle( |
| 71 | + Request::create('/'), |
| 72 | + fn () => response('hello world'), |
| 73 | + ); |
| 74 | + |
| 75 | + $this->assertEquals($asset = 'http://localhost/css/app-123.css', (string) $tailwindcss); |
| 76 | + $this->assertEquals('hello world', $response->content()); |
| 77 | + $this->assertEquals("<{$asset}>; rel=preload; as=style; crossorigin=anonymous", $response->headers->get('Link', null)); |
| 78 | + } |
| 79 | +} |
0 commit comments