UriSignerTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. $this->assertContains('&foo=', $signer->sign('http://example.com/foo?foo=bar'));
  21. }
  22. public function testCheck()
  23. {
  24. $signer = new UriSigner('foobar');
  25. $this->assertFalse($signer->check('http://example.com/foo?_hash=foo'));
  26. $this->assertFalse($signer->check('http://example.com/foo?foo=bar&_hash=foo'));
  27. $this->assertFalse($signer->check('http://example.com/foo?foo=bar&_hash=foo&bar=foo'));
  28. $this->assertTrue($signer->check($signer->sign('http://example.com/foo')));
  29. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar')));
  30. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&0=integer')));
  31. $this->assertSame($signer->sign('http://example.com/foo?foo=bar&bar=foo'), $signer->sign('http://example.com/foo?bar=foo&foo=bar'));
  32. }
  33. public function testCheckWithDifferentArgSeparator()
  34. {
  35. $this->iniSet('arg_separator.output', '&amp;');
  36. $signer = new UriSigner('foobar');
  37. $this->assertSame(
  38. 'http://example.com/foo?_hash=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D&baz=bay&foo=bar',
  39. $signer->sign('http://example.com/foo?foo=bar&baz=bay')
  40. );
  41. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay')));
  42. }
  43. public function testCheckWithDifferentParameter()
  44. {
  45. $signer = new UriSigner('foobar', 'qux');
  46. $this->assertSame(
  47. 'http://example.com/foo?baz=bay&foo=bar&qux=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D',
  48. $signer->sign('http://example.com/foo?foo=bar&baz=bay')
  49. );
  50. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay')));
  51. }
  52. public function testSignerWorksWithFragments()
  53. {
  54. $signer = new UriSigner('foobar');
  55. $this->assertSame(
  56. 'http://example.com/foo?_hash=EhpAUyEobiM3QTrKxoLOtQq5IsWyWedoXDPqIjzNj5o%3D&bar=foo&foo=bar#foobar',
  57. $signer->sign('http://example.com/foo?bar=foo&foo=bar#foobar')
  58. );
  59. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?bar=foo&foo=bar#foobar')));
  60. }
  61. }