ServerBagTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\HttpFoundation\Tests;
  11. use Symfony\Component\HttpFoundation\ServerBag;
  12. /**
  13. * ServerBagTest
  14. *
  15. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  16. */
  17. class ServerBagTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testShouldExtractHeadersFromServerArray()
  20. {
  21. $server = array(
  22. 'SOME_SERVER_VARIABLE' => 'value',
  23. 'SOME_SERVER_VARIABLE2' => 'value',
  24. 'ROOT' => 'value',
  25. 'HTTP_CONTENT_TYPE' => 'text/html',
  26. 'HTTP_CONTENT_LENGTH' => '0',
  27. 'HTTP_ETAG' => 'asdf',
  28. 'PHP_AUTH_USER' => 'foo',
  29. 'PHP_AUTH_PW' => 'bar',
  30. );
  31. $bag = new ServerBag($server);
  32. $this->assertEquals(array(
  33. 'CONTENT_TYPE' => 'text/html',
  34. 'CONTENT_LENGTH' => '0',
  35. 'ETAG' => 'asdf',
  36. 'AUTHORIZATION' => 'Basic '.base64_encode('foo:bar'),
  37. 'PHP_AUTH_USER' => 'foo',
  38. 'PHP_AUTH_PW' => 'bar',
  39. ), $bag->getHeaders());
  40. }
  41. public function testHttpPasswordIsOptional()
  42. {
  43. $bag = new ServerBag(array('PHP_AUTH_USER' => 'foo'));
  44. $this->assertEquals(array(
  45. 'AUTHORIZATION' => 'Basic '.base64_encode('foo:'),
  46. 'PHP_AUTH_USER' => 'foo',
  47. 'PHP_AUTH_PW' => ''
  48. ), $bag->getHeaders());
  49. }
  50. public function testHttpBasicAuthWithPhpCgi()
  51. {
  52. $bag = new ServerBag(array('HTTP_AUTHORIZATION' => 'Basic '.base64_encode('foo:bar')));
  53. $this->assertEquals(array(
  54. 'AUTHORIZATION' => 'Basic '.base64_encode('foo:bar'),
  55. 'PHP_AUTH_USER' => 'foo',
  56. 'PHP_AUTH_PW' => 'bar'
  57. ), $bag->getHeaders());
  58. }
  59. public function testHttpBasicAuthWithPhpCgiRedirect()
  60. {
  61. $bag = new ServerBag(array('REDIRECT_HTTP_AUTHORIZATION' => 'Basic '.base64_encode('foo:bar')));
  62. $this->assertEquals(array(
  63. 'AUTHORIZATION' => 'Basic '.base64_encode('foo:bar'),
  64. 'PHP_AUTH_USER' => 'foo',
  65. 'PHP_AUTH_PW' => 'bar'
  66. ), $bag->getHeaders());
  67. }
  68. public function testHttpBasicAuthWithPhpCgiEmptyPassword()
  69. {
  70. $bag = new ServerBag(array('HTTP_AUTHORIZATION' => 'Basic '.base64_encode('foo:')));
  71. $this->assertEquals(array(
  72. 'AUTHORIZATION' => 'Basic '.base64_encode('foo:'),
  73. 'PHP_AUTH_USER' => 'foo',
  74. 'PHP_AUTH_PW' => ''
  75. ), $bag->getHeaders());
  76. }
  77. public function testHttpDigestAuthWithPhpCgi()
  78. {
  79. $digest = 'Digest username="foo", realm="acme", nonce="'.md5('secret').'", uri="/protected, qop="auth"';
  80. $bag = new ServerBag(array('HTTP_AUTHORIZATION' => $digest));
  81. $this->assertEquals(array(
  82. 'AUTHORIZATION' => $digest,
  83. 'PHP_AUTH_DIGEST' => $digest,
  84. ), $bag->getHeaders());
  85. }
  86. public function testHttpDigestAuthWithPhpCgiRedirect()
  87. {
  88. $digest = 'Digest username="foo", realm="acme", nonce="'.md5('secret').'", uri="/protected, qop="auth"';
  89. $bag = new ServerBag(array('REDIRECT_HTTP_AUTHORIZATION' => $digest));
  90. $this->assertEquals(array(
  91. 'AUTHORIZATION' => $digest,
  92. 'PHP_AUTH_DIGEST' => $digest,
  93. ), $bag->getHeaders());
  94. }
  95. public function testOAuthBearerAuth()
  96. {
  97. $headerContent = 'Bearer L-yLEOr9zhmUYRkzN1jwwxwQ-PBNiKDc8dgfB4hTfvo';
  98. $bag = new ServerBag(array('HTTP_AUTHORIZATION' => $headerContent));
  99. $this->assertEquals(array(
  100. 'AUTHORIZATION' => $headerContent,
  101. ), $bag->getHeaders());
  102. }
  103. }