UriSignerTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpKernel\UriSigner;
  13. class UriSignerTest extends TestCase
  14. {
  15. public function testSign()
  16. {
  17. $signer = new UriSigner('foobar');
  18. $this->assertContains('?_hash=', $signer->sign('http://example.com/foo'));
  19. $this->assertContains('&_hash=', $signer->sign('http://example.com/foo?foo=bar'));
  20. }
  21. public function testCheck()
  22. {
  23. $signer = new UriSigner('foobar');
  24. $this->assertFalse($signer->check('http://example.com/foo?_hash=foo'));
  25. $this->assertFalse($signer->check('http://example.com/foo?foo=bar&_hash=foo'));
  26. $this->assertFalse($signer->check('http://example.com/foo?foo=bar&_hash=foo&bar=foo'));
  27. $this->assertTrue($signer->check($signer->sign('http://example.com/foo')));
  28. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar')));
  29. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&0=integer')));
  30. $this->assertTrue($signer->sign('http://example.com/foo?foo=bar&bar=foo') === $signer->sign('http://example.com/foo?bar=foo&foo=bar'));
  31. }
  32. public function testCheckWithDifferentArgSeparator()
  33. {
  34. $this->iniSet('arg_separator.output', '&amp;');
  35. $signer = new UriSigner('foobar');
  36. $this->assertSame(
  37. 'http://example.com/foo?baz=bay&foo=bar&_hash=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D',
  38. $signer->sign('http://example.com/foo?foo=bar&baz=bay')
  39. );
  40. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay')));
  41. }
  42. public function testCheckWithDifferentParameter()
  43. {
  44. $signer = new UriSigner('foobar', 'qux');
  45. $this->assertSame(
  46. 'http://example.com/foo?baz=bay&foo=bar&qux=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D',
  47. $signer->sign('http://example.com/foo?foo=bar&baz=bay')
  48. );
  49. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay')));
  50. }
  51. }