Skip to content

Commit

Permalink
Return 404 if a custom script or style is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
wsamoht committed Oct 26, 2024
1 parent 8b73209 commit 71a774a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Http/Controllers/ScriptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ class ScriptController extends Controller
*/
public function __invoke(Request $request)
{
$scriptPath = LaRecipe::allScripts()[$request->script] ?? abort(404);

return response(
file_get_contents(LaRecipe::allScripts()[$request->script]),
200, ['Content-Type' => 'application/javascript']
file_get_contents($scriptPath),
200,
['Content-Type' => 'application/javascript']
);
}
}
7 changes: 5 additions & 2 deletions src/Http/Controllers/StyleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ class StyleController extends Controller
*/
public function __invoke(Request $request)
{
$cssPath = LaRecipe::allStyles()[$request->style] ?? abort(404);

return response(
file_get_contents(LaRecipe::allStyles()[$request->style]),
200, ['Content-Type' => 'text/css']
file_get_contents($cssPath),
200,
['Content-Type' => 'text/css']
);
}
}
27 changes: 27 additions & 0 deletions tests/Feature/CustomScriptsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace BinaryTorch\LaRecipe\Tests\Feature;

use BinaryTorch\LaRecipe\LaRecipe;
use BinaryTorch\LaRecipe\Tests\TestCase;

class CustomScriptsTest extends TestCase
{
/** @test */
public function a_script_not_found_returns_404()
{
$this->get('/docs/scripts/not-found')
->assertNotFound();
}

/** @test */
public function a_script_is_returned()
{
LaRecipe::script('custom-script', __DIR__ . '/../theme/resources/js/custom.js');

$this->get('/docs/scripts/custom-script')
->assertOk()
->assertHeader('Content-Type', 'application/javascript')
->assertSee('Custom JS');
}
}
27 changes: 27 additions & 0 deletions tests/Feature/CustomStylesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace BinaryTorch\LaRecipe\Tests\Feature;

use BinaryTorch\LaRecipe\LaRecipe;
use BinaryTorch\LaRecipe\Tests\TestCase;

class CustomStylesTest extends TestCase
{
/** @test */
public function a_script_not_found_returns_404()
{
$this->get('/docs/styles/not-found')
->assertNotFound();
}

/** @test */
public function a_script_is_returned()
{
LaRecipe::style('custom-style', __DIR__ . '/../theme/resources/css/custom.css');

$this->get('/docs/styles/custom-style')
->assertOk()
->assertHeader('Content-Type', 'text/css; charset=UTF-8')
->assertSee('Custom CSS');
}
}
1 change: 1 addition & 0 deletions tests/theme/resources/css/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/** Custom CSS **/
1 change: 1 addition & 0 deletions tests/theme/resources/js/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Custom JS

0 comments on commit 71a774a

Please sign in to comment.