Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Feb 17, 2018
1 parent a91523b commit 321b65b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Exceptions/InvalidCspProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

class InvalidCspProfile extends Exception
{
public static function create(string $class): self
public static function create($class): self
{
return new self("The csp profile `{$class}` is not valid. A valid profile extends ".Profile::class);
$className = get_class($class);

return new self("The csp profile `{$className}` is not valid. A valid profile extends ".Profile::class);
}
}
15 changes: 15 additions & 0 deletions tests/AddCspHeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Route;
use Spatie\Csp\AddCspHeaders;
use Spatie\Csp\Exceptions\InvalidCspProfile;
use Symfony\Component\HttpFoundation\HeaderBag;

class AddCspHeadersTest extends TestCase
Expand Down Expand Up @@ -59,6 +60,20 @@ public function a_report_uri_can_be_set_in_the_config()
->assertCspHeaderContains($headers, 'report-to {"url":"https:\/\/report-uri.com","group-name":"Basic","max-age":18144000};');
}

/** @test */
public function using_an_invalid_profile_class_will_throw_an_exception()
{
$this->withoutExceptionHandling();

$invalidProfileClassName = get_class(new class {});

config(['csp.profile' => $invalidProfileClassName]);

$this->expectException(InvalidCspProfile::class);

$this->getResponseHeaders();
}

protected function assertCspHeaderContains(HeaderBag $headerBag, string $needle): self
{
$this->assertContains($needle, $headerBag->get('Content-Security-Policy'));
Expand Down

0 comments on commit 321b65b

Please sign in to comment.