|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpFoundation\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\UriSigner; |
| 17 | + |
| 18 | +/** |
| 19 | + * @group legacy |
| 20 | + */ |
| 21 | +class UriSignerTest extends TestCase |
| 22 | +{ |
| 23 | + public function testSign() |
| 24 | + { |
| 25 | + $signer = new UriSigner('foobar'); |
| 26 | + |
| 27 | + $this->assertStringContainsString('?_hash=', $signer->sign('http://example.com/foo')); |
| 28 | + $this->assertStringContainsString('?_hash=', $signer->sign('http://example.com/foo?foo=bar')); |
| 29 | + $this->assertStringContainsString('&foo=', $signer->sign('http://example.com/foo?foo=bar')); |
| 30 | + } |
| 31 | + |
| 32 | + public function testCheck() |
| 33 | + { |
| 34 | + $signer = new UriSigner('foobar'); |
| 35 | + |
| 36 | + $this->assertFalse($signer->check('http://example.com/foo?_hash=foo')); |
| 37 | + $this->assertFalse($signer->check('http://example.com/foo?foo=bar&_hash=foo')); |
| 38 | + $this->assertFalse($signer->check('http://example.com/foo?foo=bar&_hash=foo&bar=foo')); |
| 39 | + |
| 40 | + $this->assertTrue($signer->check($signer->sign('http://example.com/foo'))); |
| 41 | + $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar'))); |
| 42 | + $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&0=integer'))); |
| 43 | + |
| 44 | + $this->assertSame($signer->sign('http://example.com/foo?foo=bar&bar=foo'), $signer->sign('http://example.com/foo?bar=foo&foo=bar')); |
| 45 | + } |
| 46 | + |
| 47 | + public function testCheckWithDifferentArgSeparator() |
| 48 | + { |
| 49 | + $this->iniSet('arg_separator.output', '&'); |
| 50 | + $signer = new UriSigner('foobar'); |
| 51 | + |
| 52 | + $this->assertSame( |
| 53 | + 'http://example.com/foo?_hash=rIOcC%2FF3DoEGo%2FvnESjSp
A3E2
7uU9zA9S%2F%2BOLhxgMexoPUM%3D&baz=bay&foo=bar', |
| 54 | + $signer->sign('http://example.com/foo?foo=bar&baz=bay') |
| 55 | + ); |
| 56 | + $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay'))); |
| 57 | + } |
| 58 | + |
| 59 | + public function testCheckWithRequest() |
| 60 | + { |
| 61 | + $signer = new UriSigner('foobar'); |
| 62 | + |
| 63 | + $this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo')))); |
| 64 | + $this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo?foo=bar')))); |
| 65 | + $this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo?foo=bar&0=integer')))); |
| 66 | + } |
| 67 | + |
| 68 | + public function testCheckWithDifferentParameter() |
| 69 | + { |
| 70 | + $signer = new UriSigner('foobar', 'qux'); |
| 71 | + |
| 72 | + $this->assertSame( |
| 73 | + 'http://example.com/foo?baz=bay&foo=bar&qux=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D', |
| 74 | + $signer->sign('http://example.com/foo?foo=bar&baz=bay') |
| 75 | + ); |
| 76 | + $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay'))); |
| 77 | + } |
| 78 | + |
| 79 | + public function testSignerWorksWithFragments() |
| 80 | + { |
| 81 | + $signer = new UriSigner('foobar'); |
| 82 | + |
| 83 | + $this->assertSame( |
| 84 | + 'http://example.com/foo?_hash=EhpAUyEobiM3QTrKxoLOtQq5IsWyWedoXDPqIjzNj5o%3D&bar=foo&foo=bar#foobar', |
| 85 | + $signer->sign('http://example.com/foo?bar=foo&foo=bar#foobar') |
| 86 | + ); |
| 87 | + $this->assertTrue($signer->check($signer->sign('http://example.com/foo?bar=foo&foo=bar#foobar'))); |
| 88 | + } |
| 89 | +} |
0 commit comments