PageRenderTime 31ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/pmjones/php-framework-benchmarks
PHP | 169 lines | 127 code | 26 blank | 16 comment | 0 complexity | 4f410015f42199fb8d990c83f021b9c8 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, 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\MockRequestType;
  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(array('init' => false));
  16. }
  17. public function testDefaultTypeInitialization() {
  18. $response = new Response(array('request' => new MockRequestType()));
  19. $this->assertEqual('foo', $response->type());
  20. }
  21. public function testTypeManipulation() {
  22. $this->assertEqual('html', $this->response->type());
  23. $this->assertEqual('html', $this->response->type('html'));
  24. $this->assertEqual('json', $this->response->type('json'));
  25. $this->assertEqual('json', $this->response->type());
  26. $this->assertEqual(false, $this->response->type(false));
  27. $this->assertEqual(false, $this->response->type());
  28. }
  29. public function testResponseRendering() {
  30. $this->response->body = 'Document body';
  31. ob_start();
  32. $this->response->render();
  33. $result = ob_get_clean();
  34. $this->assertEqual('Document body', $result);
  35. $this->assertEqual(array('HTTP/1.1 200 OK'), $this->response->testHeaders);
  36. ob_start();
  37. echo $this->response;
  38. $result = ob_get_clean();
  39. $this->assertEqual('Document body', $result);
  40. $this->assertEqual(array('HTTP/1.1 200 OK'), $this->response->testHeaders);
  41. $expires = strtotime('+1 hour');
  42. $this->response->cache($expires);
  43. ob_start();
  44. $this->response->render();
  45. $result = ob_get_clean();
  46. $headers = array (
  47. 'HTTP/1.1 200 OK',
  48. 'Expires: ' . gmdate('D, d M Y H:i:s', $expires) . ' GMT',
  49. 'Cache-Control: max-age=' . ($expires - time()),
  50. );
  51. $this->assertEqual($headers, $this->response->testHeaders);
  52. $expires = '+2 hours';
  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', strtotime($expires)) . ' GMT',
  60. 'Cache-Control: max-age=' . (strtotime($expires) - time()),
  61. );
  62. $this->assertEqual($headers, $this->response->testHeaders);
  63. $this->response->body = 'Created';
  64. $this->response->status(201);
  65. $this->response->disableCache();
  66. ob_start();
  67. $this->response->render();
  68. $result = ob_get_clean();
  69. $this->assertEqual('Created', $result);
  70. $headers = array (
  71. 'HTTP/1.1 201 Created',
  72. 'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
  73. array(
  74. 'Cache-Control: no-store, no-cache, must-revalidate',
  75. 'Cache-Control: post-check=0, pre-check=0',
  76. 'Cache-Control: max-age=0'
  77. ),
  78. 'Pragma: no-cache'
  79. );
  80. $this->assertEqual($headers, $this->response->testHeaders);
  81. }
  82. /**
  83. * Tests various methods of specifying HTTP status codes.
  84. *
  85. * @return void
  86. */
  87. public function testStatusCodes() {
  88. $this->response->status('Created');
  89. ob_start();
  90. $this->response->render();
  91. $result = ob_get_clean();
  92. $this->assertEqual(array('HTTP/1.1 201 Created'), $this->response->testHeaders);
  93. $this->response->status('See Other');
  94. ob_start();
  95. $this->response->render();
  96. $result = ob_get_clean();
  97. $this->assertEqual(array('HTTP/1.1 303 See Other'), $this->response->testHeaders);
  98. $this->expectException('/Invalid status code/');
  99. $this->response->status('foobar');
  100. ob_start();
  101. $this->response->render();
  102. $result = ob_get_clean();
  103. $this->assertFalse($this->response->testHeaders);
  104. }
  105. /**
  106. * Tests location headers and custom header add-ons, like 'download'.
  107. *
  108. * @return void
  109. */
  110. public function testHeaderTypes() {
  111. $this->response->headers('download', 'report.csv');
  112. ob_start();
  113. $this->response->render();
  114. ob_end_clean();
  115. $headers = array(
  116. 'HTTP/1.1 200 OK',
  117. 'Content-Disposition: attachment; filename="report.csv"'
  118. );
  119. $this->assertEqual($headers, $this->response->testHeaders);
  120. $this->response = new MockResponse();
  121. $this->response->headers('location', '/');
  122. ob_start();
  123. $this->response->render();
  124. ob_end_clean();
  125. $headers = array('HTTP/1.1 302 Found', 'Location: /');
  126. $this->assertEqual($headers, $this->response->testHeaders);
  127. }
  128. public function testLocationHeaderStatus() {
  129. $this->response = new MockResponse();
  130. $this->response->status(301);
  131. $this->response->headers('location', '/');
  132. ob_start();
  133. $this->response->render();
  134. ob_end_clean();
  135. $headers = array('HTTP/1.1 301 Moved Permanently', 'Location: /');
  136. $this->assertEqual($headers, $this->response->testHeaders);
  137. $this->response = new Response(array('location' => array(
  138. 'controller' => 'foo_bar', 'action' => 'index'
  139. )));
  140. $this->assertEqual(array('location: /foo_bar'), $this->response->headers());
  141. }
  142. }
  143. ?>