ResponseTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\action;
  9. use lithium\action\Response;
  10. use lithium\tests\mocks\action\MockResponse;
  11. class ResponseTest extends \lithium\test\Unit {
  12. public $response = null;
  13. public function setUp() {
  14. $this->response = new MockResponse(array('init' => false));
  15. }
  16. public function testTypeManipulation() {
  17. $this->assertEqual('html', $this->response->type());
  18. $this->assertEqual('html', $this->response->type('html'));
  19. $this->assertEqual('json', $this->response->type('json'));
  20. $this->assertEqual('json', $this->response->type());
  21. $this->assertEqual(false, $this->response->type(false));
  22. $this->assertEqual(false, $this->response->type());
  23. }
  24. public function testResponseRenderString() {
  25. $this->response->body = 'Document body';
  26. ob_start();
  27. $this->response->render();
  28. $result = ob_get_clean();
  29. $this->assertIdentical('Document body', $result);
  30. $this->assertIdentical(array('HTTP/1.1 200 OK'), $this->response->testHeaders);
  31. }
  32. public function testResponseRenderJson() {
  33. $this->response->type('json');
  34. $this->response->body[] = '{"message": "Hello World"}';
  35. ob_start();
  36. $this->response->render();
  37. $result = ob_get_clean();
  38. $this->assertIdentical('{"message": "Hello World"}', $result);
  39. $this->assertIdentical('HTTP/1.1 200 OK', $this->response->testHeaders[0]);
  40. }
  41. public function testResponseToString() {
  42. $this->response->type(false);
  43. $this->response->body = 'Document body';
  44. ob_start();
  45. echo $this->response;
  46. $result = ob_get_clean();
  47. $this->assertIdentical('Document body', $result);
  48. $this->assertIdentical(array('HTTP/1.1 200 OK'), $this->response->testHeaders);
  49. }
  50. public function testResponseCaching() {
  51. $this->response->body = 'Document body';
  52. $expires = strtotime('+1 hour');
  53. $this->response->cache($expires);
  54. ob_start();
  55. $this->response->render();
  56. $result = ob_get_clean();
  57. $headers = array (
  58. 'HTTP/1.1 200 OK',
  59. 'Expires: ' . gmdate('D, d M Y H:i:s', $expires) . ' GMT',
  60. 'Cache-Control: max-age=' . ($expires - time()),
  61. 'Pragma: cache'
  62. );
  63. $this->assertIdentical($headers, $this->response->testHeaders);
  64. $expires = '+2 hours';
  65. $this->response->cache($expires);
  66. ob_start();
  67. $this->response->render();
  68. $result = ob_get_clean();
  69. $headers = array (
  70. 'HTTP/1.1 200 OK',
  71. 'Expires: ' . gmdate('D, d M Y H:i:s', strtotime($expires)) . ' GMT',
  72. 'Cache-Control: max-age=' . (strtotime($expires) - time()),
  73. 'Pragma: cache'
  74. );
  75. $this->assertIdentical($headers, $this->response->testHeaders);
  76. $this->response->body = 'Created';
  77. $this->response->status(201);
  78. $result = $this->response->cache(false);
  79. $expected = array(
  80. 'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
  81. 'Cache-Control: no-store, no-cache, must-revalidate',
  82. 'Cache-Control: post-check=0, pre-check=0',
  83. 'Cache-Control: max-age=0',
  84. 'Pragma: no-cache'
  85. );
  86. $this->assertIdentical($expected, $result);
  87. ob_start();
  88. $this->response->render();
  89. $result = ob_get_clean();
  90. $this->assertIdentical('Created', $result);
  91. $headers = array (
  92. 'HTTP/1.1 201 Created',
  93. 'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
  94. array(
  95. 'Cache-Control: no-store, no-cache, must-revalidate',
  96. 'Cache-Control: post-check=0, pre-check=0',
  97. 'Cache-Control: max-age=0'
  98. ),
  99. 'Pragma: no-cache'
  100. );
  101. $this->assertIdentical($headers, $this->response->testHeaders);
  102. }
  103. /**
  104. * Tests various methods of specifying HTTP status codes.
  105. *
  106. * @return void
  107. */
  108. public function testStatusCodes() {
  109. $this->response->status('Created');
  110. ob_start();
  111. $this->response->render();
  112. $result = ob_get_clean();
  113. $this->assertEqual(array('HTTP/1.1 201 Created'), $this->response->testHeaders);
  114. $this->response->status('See Other');
  115. ob_start();
  116. $this->response->render();
  117. $result = ob_get_clean();
  118. $this->assertEqual(array('HTTP/1.1 303 See Other'), $this->response->testHeaders);
  119. $this->response->status('foobar');
  120. ob_start();
  121. $this->response->render();
  122. $result = ob_get_clean();
  123. $expected = array('HTTP/1.1 500 Internal Server Error');
  124. $this->assertEqual($expected, $this->response->testHeaders);
  125. }
  126. /**
  127. * Tests location headers and custom header add-ons, like 'download'.
  128. *
  129. * @return void
  130. */
  131. public function testHeaderTypes() {
  132. $this->response->headers('download', 'report.csv');
  133. ob_start();
  134. $this->response->render();
  135. ob_get_clean();
  136. $headers = array(
  137. 'HTTP/1.1 200 OK',
  138. 'Content-Disposition: attachment; filename="report.csv"'
  139. );
  140. $this->assertEqual($headers, $this->response->testHeaders);
  141. $this->response = new MockResponse();
  142. $this->response->headers('location', '/');
  143. ob_start();
  144. $this->response->render();
  145. ob_get_clean();
  146. $headers = array('HTTP/1.1 302 Found', 'Location: /');
  147. $this->assertEqual($headers, $this->response->testHeaders);
  148. }
  149. public function testLocationHeaderStatus() {
  150. $this->response = new MockResponse();
  151. $this->response->status(301);
  152. $this->response->headers('location', '/');
  153. ob_start();
  154. $this->response->render();
  155. ob_get_clean();
  156. $headers = array('HTTP/1.1 301 Moved Permanently', 'Location: /');
  157. $this->assertEqual($headers, $this->response->testHeaders);
  158. $this->response = new Response(array(
  159. 'classes' => array('router' => __CLASS__),
  160. 'location' => array('controller' => 'foo_bar', 'action' => 'index')
  161. ));
  162. $this->assertEqual(array('Location: /foo_bar'), $this->response->headers());
  163. }
  164. /**
  165. * Tests that, when a location is assigned without a status code being set, that the status code
  166. * will be automatically set to 302 when the response is rendered.
  167. */
  168. public function testBrowserRedirection() {
  169. $this->response = new MockResponse(array('location' => '/'));
  170. ob_start();
  171. $this->response->render();
  172. ob_get_clean();
  173. $this->assertEqual('HTTP/1.1 302 Found', $this->response->status());
  174. }
  175. public static function match($url) {
  176. if ($url == array('controller' => 'foo_bar', 'action' => 'index')) {
  177. return '/foo_bar';
  178. }
  179. }
  180. }
  181. ?>