PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/cases/action/ResponseTest.php

http://github.com/UnionOfRAD/lithium
PHP | 232 lines | 174 code | 37 blank | 21 comment | 1 complexity | 5b76ac6927ece0301932aede8c36b14a MD5 | raw file
  1. <?php
  2. /**
  3. * li₃: the most RAD framework for PHP (http://li3.me)
  4. *
  5. * Copyright 2009, Union of RAD. All rights reserved. This source
  6. * code is distributed under the terms of the BSD 3-Clause License.
  7. * The full license text can be found in the LICENSE.txt file.
  8. */
  9. namespace lithium\tests\cases\action;
  10. use lithium\action\Response;
  11. use lithium\tests\mocks\action\MockResponse;
  12. class ResponseTest extends \lithium\test\Unit {
  13. public $response = null;
  14. public function setUp() {
  15. $this->response = new MockResponse(['init' => false]);
  16. }
  17. public function testTypeManipulation() {
  18. $this->assertEqual('html', $this->response->type());
  19. $this->assertEqual('html', $this->response->type('html'));
  20. $this->assertEqual('json', $this->response->type('json'));
  21. $this->assertEqual('json', $this->response->type());
  22. $this->assertEqual(false, $this->response->type(false));
  23. $this->assertEqual(false, $this->response->type());
  24. }
  25. public function testResponseRenderString() {
  26. $this->response->body = 'Document body';
  27. ob_start();
  28. $this->response->render();
  29. $result = ob_get_clean();
  30. $this->assertIdentical('Document body', $result);
  31. $this->assertIdentical(['HTTP/1.1 200 OK'], $this->response->testHeaders);
  32. }
  33. public function testResponseRenderJson() {
  34. $this->response->type('json');
  35. $this->response->body[] = '{"message": "Hello World"}';
  36. ob_start();
  37. $this->response->render();
  38. $result = ob_get_clean();
  39. $this->assertIdentical('{"message": "Hello World"}', $result);
  40. $this->assertIdentical('HTTP/1.1 200 OK', $this->response->testHeaders[0]);
  41. }
  42. public function testResponseRenderWithCookies() {
  43. $this->response->cookies([
  44. 'Name' => ['value' => 'Ali', 'domain' => '.li3.me', 'secure' => true],
  45. 'Destination' => ['value' => 'The Future', 'expires' => 'Oct 21 2015 4:29 PM PDT']
  46. ]);
  47. ob_start();
  48. $this->response->render();
  49. $result = ob_get_clean();
  50. $expected = [
  51. 'HTTP/1.1 200 OK',
  52. 'Set-Cookie: Name=Ali; Domain=.li3.me; Secure',
  53. 'Set-Cookie: Destination=The%20Future; Expires=Wed, 21-Oct-2015 23:29:00 GMT',
  54. ];
  55. $this->assertIdentical($expected, $this->response->testHeaders);
  56. }
  57. public function testResponseToString() {
  58. $this->response->type(false);
  59. $this->response->body = 'Document body';
  60. ob_start();
  61. echo $this->response;
  62. $result = ob_get_clean();
  63. $this->assertIdentical('Document body', $result);
  64. $this->assertIdentical(['HTTP/1.1 200 OK'], $this->response->testHeaders);
  65. }
  66. public function testResponseCaching() {
  67. $this->response->body = 'Document body';
  68. $time = time();
  69. $expires = strtotime("@{$time} +1 hour");
  70. $this->response->cache($expires);
  71. ob_start();
  72. $this->response->render();
  73. $result = ob_get_clean();
  74. $headers = [
  75. 'HTTP/1.1 200 OK',
  76. 'Expires: ' . gmdate('D, d M Y H:i:s', $expires) . ' GMT',
  77. 'Cache-Control: max-age=' . ($expires - $time),
  78. 'Pragma: cache'
  79. ];
  80. $this->assertIdentical($headers, $this->response->testHeaders);
  81. $expires = strtotime("@{$time} +2 hours");
  82. $this->response->cache($expires);
  83. ob_start();
  84. $this->response->render();
  85. $result = ob_get_clean();
  86. $headers = [
  87. 'HTTP/1.1 200 OK',
  88. 'Expires: ' . gmdate('D, d M Y H:i:s', $expires) . ' GMT',
  89. 'Cache-Control: max-age=' . ($expires - time()),
  90. 'Pragma: cache'
  91. ];
  92. $this->assertIdentical($headers, $this->response->testHeaders);
  93. $this->response->body = 'Created';
  94. $this->response->status(201);
  95. $this->response->cache(false);
  96. $result = $this->response->headers();
  97. $expected = [
  98. 'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
  99. 'Cache-Control: no-store, no-cache, must-revalidate',
  100. 'Cache-Control: post-check=0, pre-check=0',
  101. 'Cache-Control: max-age=0',
  102. 'Pragma: no-cache'
  103. ];
  104. $this->assertIdentical($expected, $result);
  105. ob_start();
  106. $this->response->render();
  107. $result = ob_get_clean();
  108. $this->assertIdentical('Created', $result);
  109. $headers = [
  110. 'HTTP/1.1 201 Created',
  111. 'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
  112. 'Cache-Control: no-store, no-cache, must-revalidate',
  113. 'Cache-Control: post-check=0, pre-check=0',
  114. 'Cache-Control: max-age=0',
  115. 'Pragma: no-cache'
  116. ];
  117. $this->assertIdentical($headers, $this->response->testHeaders);
  118. }
  119. /**
  120. * Tests various methods of specifying HTTP status codes.
  121. */
  122. public function testStatusCodes() {
  123. $this->response->status('Created');
  124. ob_start();
  125. $this->response->render();
  126. $result = ob_get_clean();
  127. $this->assertEqual(['HTTP/1.1 201 Created'], $this->response->testHeaders);
  128. $this->response->status('See Other');
  129. ob_start();
  130. $this->response->render();
  131. $result = ob_get_clean();
  132. $this->assertEqual(['HTTP/1.1 303 See Other'], $this->response->testHeaders);
  133. $this->response->status('foobar');
  134. ob_start();
  135. $this->response->render();
  136. $result = ob_get_clean();
  137. $expected = ['HTTP/1.1 500 Internal Server Error'];
  138. $this->assertEqual($expected, $this->response->testHeaders);
  139. }
  140. /**
  141. * Tests location headers.
  142. */
  143. public function testLocationHeader() {
  144. $this->response = new MockResponse();
  145. $this->response->status(301);
  146. $this->response->headers('Location', '/');
  147. ob_start();
  148. $this->response->render();
  149. ob_get_clean();
  150. $headers = ['HTTP/1.1 301 Moved Permanently', 'Location: /'];
  151. $this->assertEqual($headers, $this->response->testHeaders);
  152. $this->response = new MockResponse();
  153. $this->response->headers('Location', '/');
  154. ob_start();
  155. $this->response->render();
  156. ob_get_clean();
  157. $headers = ['HTTP/1.1 302 Found', 'Location: /'];
  158. $this->assertEqual($headers, $this->response->testHeaders);
  159. $this->response = new Response([
  160. 'classes' => ['router' => __CLASS__],
  161. 'location' => ['controller' => 'foo_bar', 'action' => 'index']
  162. ]);
  163. $this->assertEqual(['Location: /foo_bar'], $this->response->headers());
  164. }
  165. /**
  166. * Tests that, when a location is assigned without a status code being set, that the status code
  167. * will be automatically set to 302 when the response is rendered.
  168. */
  169. public function testBrowserRedirection() {
  170. $this->response = new MockResponse(['location' => '/']);
  171. ob_start();
  172. $this->response->render();
  173. ob_get_clean();
  174. $this->assertEqual('HTTP/1.1 302 Found', $this->response->status());
  175. }
  176. public static function match($url) {
  177. if ($url === ['controller' => 'foo_bar', 'action' => 'index']) {
  178. return '/foo_bar';
  179. }
  180. }
  181. /* Deprecated / BC */
  182. /**
  183. * Tests custom header add-ons, like 'download'.
  184. */
  185. public function testDownloadMagicHeader() {
  186. $backup = error_reporting();
  187. error_reporting(E_ALL);
  188. $response = $this->response;
  189. $this->assertException('/deprecated/', function() use ($response) {
  190. $response->headers('download', 'report.csv');
  191. });
  192. error_reporting($backup);
  193. }
  194. }
  195. ?>