|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Nette\Http; |
| 6 | +use Tester\Assert; |
| 7 | + |
| 8 | +require __DIR__ . '/../bootstrap.php'; |
| 9 | + |
| 10 | + |
| 11 | +test('matches both headers', function () { |
| 12 | + $request = new Http\Request(new Http\UrlScript, headers: [ |
| 13 | + 'Sec-Fetch-Site' => 'same-origin', |
| 14 | + 'Sec-Fetch-Dest' => 'document', |
| 15 | + ]); |
| 16 | + |
| 17 | + Assert::true($request->isFrom('same-origin', 'document')); |
| 18 | +}); |
| 19 | + |
| 20 | + |
| 21 | +test('fails when expected header missing', function () { |
| 22 | + $request = new Http\Request(new Http\UrlScript, headers: [ |
| 23 | + 'Sec-Fetch-Site' => 'same-origin', |
| 24 | + ]); |
| 25 | + |
| 26 | + Assert::false($request->isFrom('same-origin', 'document')); |
| 27 | +}); |
| 28 | + |
| 29 | + |
| 30 | +test('accepts multiple expected values', function () { |
| 31 | + $request = new Http\Request(new Http\UrlScript, headers: [ |
| 32 | + 'Sec-Fetch-Site' => 'cross-site', |
| 33 | + 'Sec-Fetch-Dest' => 'image', |
| 34 | + ]); |
| 35 | + |
| 36 | + Assert::true($request->isFrom(['same-origin', 'cross-site'], ['document', 'image'])); |
| 37 | + Assert::false($request->isFrom(['cross-site'], ['Document'])); |
| 38 | + Assert::false($request->isFrom(['Cross-Site'], ['image'])); |
| 39 | +}); |
| 40 | + |
| 41 | + |
| 42 | +test('fallback same-origin from Origin header', function () { |
| 43 | + $url = new Http\UrlScript('https://nette.org/app/'); |
| 44 | + $request = new Http\Request($url, headers: [ |
| 45 | + 'Origin' => 'https://nette.org', |
| 46 | + ]); |
| 47 | + |
| 48 | + Assert::true($request->isFrom('same-origin')); |
| 49 | +}); |
| 50 | + |
| 51 | + |
| 52 | +test('fallback cross-site from Origin header', function () { |
| 53 | + $url = new Http\UrlScript('https://nette.org/'); |
| 54 | + $request = new Http\Request($url, headers: [ |
| 55 | + 'Origin' => 'https://example.com', |
| 56 | + ]); |
| 57 | + |
| 58 | + Assert::true($request->isFrom('cross-site')); |
| 59 | +}); |
| 60 | + |
| 61 | + |
| 62 | +test('fallback missing without Origin header', function () { |
| 63 | + $url = new Http\UrlScript('https://nette.org/'); |
| 64 | + $request = new Http\Request($url); |
| 65 | + |
| 66 | + Assert::false($request->isFrom('same-origin')); |
| 67 | +}); |
| 68 | + |
| 69 | + |
| 70 | +test('fallback not used when header present', function () { |
| 71 | + $url = new Http\UrlScript('https://nette.org/'); |
| 72 | + $request = new Http\Request($url, headers: [ |
| 73 | + 'Sec-Fetch-Site' => 'none', |
| 74 | + 'Origin' => 'https://nette.org', |
| 75 | + ]); |
| 76 | + |
| 77 | + Assert::false($request->isFrom('same-origin')); |
| 78 | +}); |
| 79 | + |
| 80 | + |
| 81 | +test('fallback cross-site when port differs', function () { |
| 82 | + $url = new Http\UrlScript('https://nette.org:443'); |
| 83 | + $request = new Http\Request($url, headers: [ |
| 84 | + 'Origin' => 'https://nette.org:444', |
| 85 | + ]); |
| 86 | + |
| 87 | + Assert::true($request->isFrom('cross-site')); |
| 88 | +}); |
| 89 | + |
| 90 | + |
| 91 | +test('fallback ignored for invalid Origin', function () { |
| 92 | + $url = new Http\UrlScript('https://nette.org/'); |
| 93 | + $request = new Http\Request($url, headers: [ |
| 94 | + 'Origin' => 'null', |
| 95 | + ]); |
| 96 | + |
| 97 | + Assert::false($request->isFrom('same-origin')); |
| 98 | +}); |
0 commit comments