/libraries/lithium/tests/cases/action/ResponseTest.php

https://github.com/kiranatama/sagalaya · PHP · 181 lines · 138 code · 27 blank · 16 comment · 2 complexity · 2e65147270db63ef9a1c42167783d7e2 MD5 · raw file

  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, 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 testResponseRendering() {
  25. $this->response->body = 'Document body';
  26. ob_start();
  27. $this->response->render();
  28. $result = ob_get_clean();
  29. $this->assertEqual('Document body', $result);
  30. $this->assertEqual(array('HTTP/1.1 200 OK'), $this->response->testHeaders);
  31. ob_start();
  32. echo $this->response;
  33. $result = ob_get_clean();
  34. $this->assertEqual('Document body', $result);
  35. $this->assertEqual(array('HTTP/1.1 200 OK'), $this->response->testHeaders);
  36. $expires = strtotime('+1 hour');
  37. $this->response->cache($expires);
  38. ob_start();
  39. $this->response->render();
  40. $result = ob_get_clean();
  41. $headers = array (
  42. 'HTTP/1.1 200 OK',
  43. 'Expires: ' . gmdate('D, d M Y H:i:s', $expires) . ' GMT',
  44. 'Cache-Control: max-age=' . ($expires - time()),
  45. 'Pragma: cache'
  46. );
  47. $this->assertEqual($headers, $this->response->testHeaders);
  48. $expires = '+2 hours';
  49. $this->response->cache($expires);
  50. ob_start();
  51. $this->response->render();
  52. $result = ob_get_clean();
  53. $headers = array (
  54. 'HTTP/1.1 200 OK',
  55. 'Expires: ' . gmdate('D, d M Y H:i:s', strtotime($expires)) . ' GMT',
  56. 'Cache-Control: max-age=' . (strtotime($expires) - time()),
  57. 'Pragma: cache'
  58. );
  59. $this->assertEqual($headers, $this->response->testHeaders);
  60. $this->response->body = 'Created';
  61. $this->response->status(201);
  62. $result = $this->response->cache(false);
  63. $expected = array(
  64. 'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
  65. 'Cache-Control: no-store, no-cache, must-revalidate',
  66. 'Cache-Control: post-check=0, pre-check=0',
  67. 'Cache-Control: max-age=0',
  68. 'Pragma: no-cache'
  69. );
  70. $this->assertEqual($expected, $result);
  71. ob_start();
  72. $this->response->render();
  73. $result = ob_get_clean();
  74. $this->assertEqual('Created', $result);
  75. $headers = array (
  76. 'HTTP/1.1 201 Created',
  77. 'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
  78. array(
  79. 'Cache-Control: no-store, no-cache, must-revalidate',
  80. 'Cache-Control: post-check=0, pre-check=0',
  81. 'Cache-Control: max-age=0'
  82. ),
  83. 'Pragma: no-cache'
  84. );
  85. $this->assertEqual($headers, $this->response->testHeaders);
  86. }
  87. /**
  88. * Tests various methods of specifying HTTP status codes.
  89. *
  90. * @return void
  91. */
  92. public function testStatusCodes() {
  93. $this->response->status('Created');
  94. ob_start();
  95. $this->response->render();
  96. $result = ob_get_clean();
  97. $this->assertEqual(array('HTTP/1.1 201 Created'), $this->response->testHeaders);
  98. $this->response->status('See Other');
  99. ob_start();
  100. $this->response->render();
  101. $result = ob_get_clean();
  102. $this->assertEqual(array('HTTP/1.1 303 See Other'), $this->response->testHeaders);
  103. $this->response->status('foobar');
  104. ob_start();
  105. $this->response->render();
  106. $result = ob_get_clean();
  107. $expected = array('HTTP/1.1 500 Internal Server Error');
  108. $this->assertEqual($expected, $this->response->testHeaders);
  109. }
  110. /**
  111. * Tests location headers and custom header add-ons, like 'download'.
  112. *
  113. * @return void
  114. */
  115. public function testHeaderTypes() {
  116. $this->response->headers('download', 'report.csv');
  117. ob_start();
  118. $this->response->render();
  119. ob_end_clean();
  120. $headers = array(
  121. 'HTTP/1.1 200 OK',
  122. 'Content-Disposition: attachment; filename="report.csv"'
  123. );
  124. $this->assertEqual($headers, $this->response->testHeaders);
  125. $this->response = new MockResponse();
  126. $this->response->headers('location', '/');
  127. ob_start();
  128. $this->response->render();
  129. ob_end_clean();
  130. $headers = array('HTTP/1.1 302 Found', 'Location: /');
  131. $this->assertEqual($headers, $this->response->testHeaders);
  132. }
  133. public function testLocationHeaderStatus() {
  134. $this->response = new MockResponse();
  135. $this->response->status(301);
  136. $this->response->headers('location', '/');
  137. ob_start();
  138. $this->response->render();
  139. ob_end_clean();
  140. $headers = array('HTTP/1.1 301 Moved Permanently', 'Location: /');
  141. $this->assertEqual($headers, $this->response->testHeaders);
  142. $this->response = new Response(array(
  143. 'classes' => array('router' => __CLASS__),
  144. 'location' => array('controller' => 'foo_bar', 'action' => 'index')
  145. ));
  146. $this->assertEqual(array('location: /foo_bar'), $this->response->headers());
  147. }
  148. public static function match($url) {
  149. if ($url == array('controller' => 'foo_bar', 'action' => 'index')) {
  150. return '/foo_bar';
  151. }
  152. }
  153. }
  154. ?>