HttpTestCase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @see Zend_Controller_Response_Http
  22. */
  23. require_once 'Zend/Controller/Response/Http.php';
  24. /**
  25. * Zend_Controller_Response_HttpTestCase
  26. *
  27. * @uses Zend_Controller_Response_Http
  28. * @package Zend_Controller
  29. * @subpackage Request
  30. */
  31. class Zend_Controller_Response_HttpTestCase extends Zend_Controller_Response_Http
  32. {
  33. /**
  34. * "send" headers by returning array of all headers that would be sent
  35. *
  36. * @return array
  37. */
  38. public function sendHeaders()
  39. {
  40. $headers = array();
  41. foreach ($this->_headersRaw as $header) {
  42. $headers[] = $header;
  43. }
  44. foreach ($this->_headers as $header) {
  45. $name = $header['name'];
  46. $key = strtolower($name);
  47. if (array_key_exists($name, $headers)) {
  48. if ($header['replace']) {
  49. $headers[$key] = $header['name'] . ': ' . $header['value'];
  50. }
  51. } else {
  52. $headers[$key] = $header['name'] . ': ' . $header['value'];
  53. }
  54. }
  55. return $headers;
  56. }
  57. /**
  58. * Can we send headers?
  59. *
  60. * @param bool $throw
  61. * @return void
  62. */
  63. public function canSendHeaders($throw = false)
  64. {
  65. return true;
  66. }
  67. /**
  68. * Return the concatenated body segments
  69. *
  70. * @return string
  71. */
  72. public function outputBody()
  73. {
  74. $fullContent = '';
  75. foreach ($this->_body as $content) {
  76. $fullContent .= $content;
  77. }
  78. return $fullContent;
  79. }
  80. /**
  81. * Get body and/or body segments
  82. *
  83. * @param bool|string $spec
  84. * @return string|array|null
  85. */
  86. public function getBody($spec = false)
  87. {
  88. if (false === $spec) {
  89. return $this->outputBody();
  90. } elseif (true === $spec) {
  91. return $this->_body;
  92. } elseif (is_string($spec) && isset($this->_body[$spec])) {
  93. return $this->_body[$spec];
  94. }
  95. return null;
  96. }
  97. /**
  98. * "send" Response
  99. *
  100. * Concats all response headers, and then final body (separated by two
  101. * newlines)
  102. *
  103. * @return string
  104. */
  105. public function sendResponse()
  106. {
  107. $headers = $this->sendHeaders();
  108. $content = implode("\n", $headers) . "\n\n";
  109. if ($this->isException() && $this->renderExceptions()) {
  110. $exceptions = '';
  111. foreach ($this->getException() as $e) {
  112. $exceptions .= $e->__toString() . "\n";
  113. }
  114. $content .= $exceptions;
  115. } else {
  116. $content .= $this->outputBody();
  117. }
  118. return $content;
  119. }
  120. }