FauxResponseTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Tests for the FauxResponse class
  4. *
  5. * Copyright @ 2011 Alexandre Emsenhuber
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. */
  24. class FauxResponseTest extends MediaWikiTestCase {
  25. /** @var FauxResponse */
  26. protected $response;
  27. protected function setUp() {
  28. parent::setUp();
  29. $this->response = new FauxResponse;
  30. }
  31. /**
  32. * @covers FauxResponse::setCookie
  33. * @covers FauxResponse::getCookie
  34. * @covers FauxResponse::getCookieData
  35. * @covers FauxResponse::getCookies
  36. */
  37. public function testCookie() {
  38. $expire = time() + 100;
  39. $cookie = [
  40. 'value' => 'val',
  41. 'path' => '/path',
  42. 'domain' => 'domain',
  43. 'secure' => true,
  44. 'httpOnly' => false,
  45. 'raw' => false,
  46. 'expire' => $expire,
  47. ];
  48. $this->assertEquals( null, $this->response->getCookie( 'xkey' ), 'Non-existing cookie' );
  49. $this->response->setCookie( 'key', 'val', $expire, [
  50. 'prefix' => 'x',
  51. 'path' => '/path',
  52. 'domain' => 'domain',
  53. 'secure' => 1,
  54. 'httpOnly' => 0,
  55. ] );
  56. $this->assertEquals( 'val', $this->response->getCookie( 'xkey' ), 'Existing cookie' );
  57. $this->assertEquals( $cookie, $this->response->getCookieData( 'xkey' ),
  58. 'Existing cookie (data)' );
  59. $this->assertEquals( [ 'xkey' => $cookie ], $this->response->getCookies(),
  60. 'Existing cookies' );
  61. }
  62. /**
  63. * @covers FauxResponse::getheader
  64. * @covers FauxResponse::header
  65. */
  66. public function testHeader() {
  67. $this->assertEquals( null, $this->response->getHeader( 'Location' ), 'Non-existing header' );
  68. $this->response->header( 'Location: http://localhost/' );
  69. $this->assertEquals(
  70. 'http://localhost/',
  71. $this->response->getHeader( 'Location' ),
  72. 'Set header'
  73. );
  74. $this->response->header( 'Location: http://127.0.0.1/' );
  75. $this->assertEquals(
  76. 'http://127.0.0.1/',
  77. $this->response->getHeader( 'Location' ),
  78. 'Same header'
  79. );
  80. $this->response->header( 'Location: http://127.0.0.2/', false );
  81. $this->assertEquals(
  82. 'http://127.0.0.1/',
  83. $this->response->getHeader( 'Location' ),
  84. 'Same header with override disabled'
  85. );
  86. $this->response->header( 'Location: http://localhost/' );
  87. $this->assertEquals(
  88. 'http://localhost/',
  89. $this->response->getHeader( 'LOCATION' ),
  90. 'Get header case insensitive'
  91. );
  92. }
  93. /**
  94. * @covers FauxResponse::getStatusCode
  95. */
  96. public function testResponseCode() {
  97. $this->response->header( 'HTTP/1.1 200' );
  98. $this->assertEquals( 200, $this->response->getStatusCode(), 'Header with no message' );
  99. $this->response->header( 'HTTP/1.x 201' );
  100. $this->assertEquals(
  101. 201,
  102. $this->response->getStatusCode(),
  103. 'Header with no message and protocol 1.x'
  104. );
  105. $this->response->header( 'HTTP/1.1 202 OK' );
  106. $this->assertEquals( 202, $this->response->getStatusCode(), 'Normal header' );
  107. $this->response->header( 'HTTP/1.x 203 OK' );
  108. $this->assertEquals(
  109. 203,
  110. $this->response->getStatusCode(),
  111. 'Normal header with no message and protocol 1.x'
  112. );
  113. $this->response->header( 'HTTP/1.x 204 OK', false, 205 );
  114. $this->assertEquals(
  115. 205,
  116. $this->response->getStatusCode(),
  117. 'Third parameter overrides the HTTP/... header'
  118. );
  119. $this->response->statusHeader( 210 );
  120. $this->assertEquals(
  121. 210,
  122. $this->response->getStatusCode(),
  123. 'Handle statusHeader method'
  124. );
  125. $this->response->header( 'Location: http://localhost/', false, 206 );
  126. $this->assertEquals(
  127. 206,
  128. $this->response->getStatusCode(),
  129. 'Third parameter with another header'
  130. );
  131. }
  132. }