VerifyCsrfTokenExceptTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Illuminate\Tests\Integration\Http\Middleware;
  3. use Illuminate\Encryption\Encrypter;
  4. use Illuminate\Http\Request;
  5. use Orchestra\Testbench\TestCase;
  6. class VerifyCsrfTokenExceptTest extends TestCase
  7. {
  8. private $stub;
  9. private $request;
  10. protected function setUp(): void
  11. {
  12. parent::setUp();
  13. $this->stub = new VerifyCsrfTokenExceptStub(app(), new Encrypter(Encrypter::generateKey('AES-128-CBC')));
  14. $this->request = Request::create('http://example.com/foo/bar', 'POST');
  15. }
  16. public function testItCanExceptPaths()
  17. {
  18. $this->assertMatchingExcept(['/foo/bar']);
  19. $this->assertMatchingExcept(['foo/bar']);
  20. $this->assertNonMatchingExcept(['/bar/foo']);
  21. }
  22. public function testItCanExceptWildcardPaths()
  23. {
  24. $this->assertMatchingExcept(['/foo/*']);
  25. $this->assertNonMatchingExcept(['/bar*']);
  26. }
  27. public function testItCanExceptFullUrlPaths()
  28. {
  29. $this->assertMatchingExcept(['http://example.com/foo/bar']);
  30. $this->assertMatchingExcept(['http://example.com/foo/bar/']);
  31. $this->assertNonMatchingExcept(['https://example.com/foo/bar/']);
  32. $this->assertNonMatchingExcept(['http://foobar.com/']);
  33. }
  34. public function testItCanExceptFullUrlWildcardPaths()
  35. {
  36. $this->assertMatchingExcept(['http://example.com/*']);
  37. $this->assertMatchingExcept(['*example.com*']);
  38. $this->request = Request::create('https://example.com', 'POST');
  39. $this->assertMatchingExcept(['*example.com']);
  40. }
  41. private function assertMatchingExcept(array $except, $bool = true)
  42. {
  43. $this->assertSame($bool, $this->stub->setExcept($except)->checkInExceptArray($this->request));
  44. }
  45. private function assertNonMatchingExcept(array $except)
  46. {
  47. return $this->assertMatchingExcept($except, false);
  48. }
  49. }