UriSignerTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\UriSigner;
  14. class UriSignerTest extends TestCase
  15. {
  16. public function testSign()
  17. {
  18. $signer = new UriSigner('foobar');
  19. $this->assertStringContainsString('?_hash=', $signer->sign('http://example.com/foo'));
  20. $this->assertStringContainsString('?_hash=', $signer->sign('http://example.com/foo?foo=bar'));
  21. $this->assertStringContainsString('&foo=', $signer->sign('http://example.com/foo?foo=bar'));
  22. }
  23. public function testCheck()
  24. {
  25. $signer = new UriSigner('foobar');
  26. $this->assertFalse($signer->check('http://example.com/foo?_hash=foo'));
  27. $this->assertFalse($signer->check('http://example.com/foo?foo=bar&_hash=foo'));
  28. $this->assertFalse($signer->check('http://example.com/foo?foo=bar&_hash=foo&bar=foo'));
  29. $this->assertTrue($signer->check($signer->sign('http://example.com/foo')));
  30. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar')));
  31. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&0=integer')));
  32. $this->assertSame($signer->sign('http://example.com/foo?foo=bar&bar=foo'), $signer->sign('http://example.com/foo?bar=foo&foo=bar'));
  33. }
  34. public function testCheckWithDifferentArgSeparator()
  35. {
  36. $this->iniSet('arg_separator.output', '&amp;');
  37. $signer = new UriSigner('foobar');
  38. $this->assertSame(
  39. 'http://example.com/foo?_hash=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D&baz=bay&foo=bar',
  40. $signer->sign('http://example.com/foo?foo=bar&baz=bay')
  41. );
  42. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay')));
  43. }
  44. public function testCheckWithRequest()
  45. {
  46. $signer = new UriSigner('foobar');
  47. $this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo'))));
  48. $this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo?foo=bar'))));
  49. $this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo?foo=bar&0=integer'))));
  50. }
  51. public function testCheckWithDifferentParameter()
  52. {
  53. $signer = new UriSigner('foobar', 'qux');
  54. $this->assertSame(
  55. 'http://example.com/foo?baz=bay&foo=bar&qux=rIOcC%2FF3DoEGo%2FvnESjSp7uU9zA9S%2F%2BOLhxgMexoPUM%3D',
  56. $signer->sign('http://example.com/foo?foo=bar&baz=bay')
  57. );
  58. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay')));
  59. }
  60. public function testSignerWorksWithFragments()
  61. {
  62. $signer = new UriSigner('foobar');
  63. $this->assertSame(
  64. 'http://example.com/foo?_hash=EhpAUyEobiM3QTrKxoLOtQq5IsWyWedoXDPqIjzNj5o%3D&bar=foo&foo=bar#foobar',
  65. $signer->sign('http://example.com/foo?bar=foo&foo=bar#foobar')
  66. );
  67. $this->assertTrue($signer->check($signer->sign('http://example.com/foo?bar=foo&foo=bar#foobar')));
  68. }
  69. }