Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Jan 6, 2024
1 parent 218a1eb commit eee0cdf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Http/Middleware/AddLinkHeadersForPreloadedPins.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

class AddLinkHeadersForPreloadedPins
{
public function __construct(private ?AssetResolver $assetsResolver = null)
{
}

/**
* Sets the Link header for preloaded pins.
*
Expand All @@ -17,7 +21,7 @@ class AddLinkHeadersForPreloadedPins
public function handle($request, $next)
{
return tap($next($request), function ($response) {
$resolver = new AssetResolver();
$resolver = $this->assetsResolver ?? new AssetResolver();

if ($preloaded = Importmap::preloadedModulePaths($resolver)) {
$response->header('Link', collect($preloaded)
Expand Down
13 changes: 11 additions & 2 deletions tests/PreloadingWithLinkHeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Tonysm\ImportmapLaravel\AssetResolver;
use Tonysm\ImportmapLaravel\Http\Middleware\AddLinkHeadersForPreloadedPins;
use Tonysm\ImportmapLaravel\Importmap;

Expand Down Expand Up @@ -34,12 +35,20 @@ public function sets_link_header_when_pins_are_preloaded(): void
$map->pin('editor', to: 'js/rich_text.js', preload: false);
$map->pinAllFrom('resources/js/', under: 'controllers', to: 'js/', preload: true);

$response = (new AddLinkHeadersForPreloadedPins())->handle(new Request(), function () {
$resolver = new class extends AssetResolver
{
public function __invoke($module)
{
return 'http://localhost/'.str_replace(['.js'], ['-123123.js'], $module);
}
};

$response = (new AddLinkHeadersForPreloadedPins($resolver))->handle(new Request(), function () {
return new Response('Hello World');
});

$this->assertEquals(
'<http://localhost/js/app.js?digest=da39a3ee5e6b4b0d3255bfef95601890afd80709>; rel="modulepreload", <http://localhost/js/app.js?digest=da39a3ee5e6b4b0d3255bfef95601890afd80709>; rel="modulepreload", <http://localhost/js/controllers/hello_controller.js?digest=da39a3ee5e6b4b0d3255bfef95601890afd80709>; rel="modulepreload", <http://localhost/js/controllers/index.js?digest=da39a3ee5e6b4b0d3255bfef95601890afd80709>; rel="modulepreload", <http://localhost/js/controllers/utilities/md5_controller.js?digest=da39a3ee5e6b4b0d3255bfef95601890afd80709>; rel="modulepreload", <http://localhost/js/helpers/requests/index.js?digest=da39a3ee5e6b4b0d3255bfef95601890afd80709>; rel="modulepreload", <http://localhost/js/libs/vendor/alpine.js?digest=da39a3ee5e6b4b0d3255bfef95601890afd80709>; rel="modulepreload", <http://localhost/js/spina/controllers/another_controller.js?digest=da39a3ee5e6b4b0d3255bfef95601890afd80709>; rel="modulepreload", <http://localhost/js/spina/controllers/deeper/again_controller.js?digest=da39a3ee5e6b4b0d3255bfef95601890afd80709>; rel="modulepreload"',
'<http://localhost/js/app-123123.js>; rel="modulepreload", <http://localhost/js/app-123123.js>; rel="modulepreload", <http://localhost/js/controllers/hello_controller-123123.js>; rel="modulepreload", <http://localhost/js/controllers/index-123123.js>; rel="modulepreload", <http://localhost/js/controllers/utilities/md5_controller-123123.js>; rel="modulepreload", <http://localhost/js/helpers/requests/index-123123.js>; rel="modulepreload", <http://localhost/js/libs/vendor/alpine-123123.js>; rel="modulepreload", <http://localhost/js/spina/controllers/another_controller-123123.js>; rel="modulepreload", <http://localhost/js/spina/controllers/deeper/again_controller-123123.js>; rel="modulepreload"',
$response->headers->get('Link'),
);
}
Expand Down

0 comments on commit eee0cdf

Please sign in to comment.