HttpTestCase.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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-2009 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_Request_Http
  22. */
  23. require_once 'Zend/Controller/Request/Http.php';
  24. /**
  25. * Zend_Controller_Request_HttpTestCase
  26. *
  27. * HTTP request object for use with Zend_Controller family.
  28. *
  29. * @uses Zend_Controller_Request_Http
  30. * @package Zend_Controller
  31. * @subpackage Request
  32. */
  33. class Zend_Controller_Request_HttpTestCase extends Zend_Controller_Request_Http
  34. {
  35. /**
  36. * Request headers
  37. * @var array
  38. */
  39. protected $_headers = array();
  40. /**
  41. * Request method
  42. * @var string
  43. */
  44. protected $_method;
  45. /**
  46. * Raw POST body
  47. * @var string|null
  48. */
  49. protected $_rawBody;
  50. /**
  51. * Valid request method types
  52. * @var array
  53. */
  54. protected $_validMethodTypes = array(
  55. 'DELETE',
  56. 'GET',
  57. 'HEAD',
  58. 'OPTIONS',
  59. 'POST',
  60. 'PUT',
  61. );
  62. /**
  63. * Clear GET values
  64. *
  65. * @return Zend_Controller_Request_HttpTestCase
  66. */
  67. public function clearQuery()
  68. {
  69. $_GET = array();
  70. return $this;
  71. }
  72. /**
  73. * Clear POST values
  74. *
  75. * @return Zend_Controller_Request_HttpTestCase
  76. */
  77. public function clearPost()
  78. {
  79. $_POST = array();
  80. return $this;
  81. }
  82. /**
  83. * Set raw POST body
  84. *
  85. * @param string $content
  86. * @return Zend_Controller_Request_HttpTestCase
  87. */
  88. public function setRawBody($content)
  89. {
  90. $this->_rawBody = (string) $content;
  91. return $this;
  92. }
  93. /**
  94. * Get RAW POST body
  95. *
  96. * @return string|null
  97. */
  98. public function getRawBody()
  99. {
  100. return $this->_rawBody;
  101. }
  102. /**
  103. * Clear raw POST body
  104. *
  105. * @return Zend_Controller_Request_HttpTestCase
  106. */
  107. public function clearRawBody()
  108. {
  109. $this->_rawBody = null;
  110. return $this;
  111. }
  112. /**
  113. * Set a cookie
  114. *
  115. * @param string $key
  116. * @param mixed $value
  117. * @return Zend_Controller_Request_HttpTestCase
  118. */
  119. public function setCookie($key, $value)
  120. {
  121. $_COOKIE[(string) $key] = $value;
  122. return $this;
  123. }
  124. /**
  125. * Set multiple cookies at once
  126. *
  127. * @param array $cookies
  128. * @return void
  129. */
  130. public function setCookies(array $cookies)
  131. {
  132. foreach ($cookies as $key => $value) {
  133. $_COOKIE[$key] = $value;
  134. }
  135. return $this;
  136. }
  137. /**
  138. * Clear all cookies
  139. *
  140. * @return Zend_Controller_Request_HttpTestCase
  141. */
  142. public function clearCookies()
  143. {
  144. $_COOKIE = array();
  145. return $this;
  146. }
  147. /**
  148. * Set request method
  149. *
  150. * @param string $type
  151. * @return Zend_Controller_Request_HttpTestCase
  152. */
  153. public function setMethod($type)
  154. {
  155. $type = strtoupper(trim((string) $type));
  156. if (!in_array($type, $this->_validMethodTypes)) {
  157. require_once 'Zend/Controller/Exception.php';
  158. throw new Zend_Controller_Exception('Invalid request method specified');
  159. }
  160. $this->_method = $type;
  161. return $this;
  162. }
  163. /**
  164. * Get request method
  165. *
  166. * @return string|null
  167. */
  168. public function getMethod()
  169. {
  170. return $this->_method;
  171. }
  172. /**
  173. * Set a request header
  174. *
  175. * @param string $key
  176. * @param string $value
  177. * @return Zend_Controller_Request_HttpTestCase
  178. */
  179. public function setHeader($key, $value)
  180. {
  181. $key = $this->_normalizeHeaderName($key);
  182. $this->_headers[$key] = (string) $value;
  183. return $this;
  184. }
  185. /**
  186. * Set request headers
  187. *
  188. * @param array $headers
  189. * @return Zend_Controller_Request_HttpTestCase
  190. */
  191. public function setHeaders(array $headers)
  192. {
  193. foreach ($headers as $key => $value) {
  194. $this->setHeader($key, $value);
  195. }
  196. return $this;
  197. }
  198. /**
  199. * Get request header
  200. *
  201. * @param string $header
  202. * @param mixed $default
  203. * @return string|null
  204. */
  205. public function getHeader($header, $default = null)
  206. {
  207. $header = $this->_normalizeHeaderName($header);
  208. if (array_key_exists($header, $this->_headers)) {
  209. return $this->_headers[$header];
  210. }
  211. return $default;
  212. }
  213. /**
  214. * Get all request headers
  215. *
  216. * @return array
  217. */
  218. public function getHeaders()
  219. {
  220. return $this->_headers;
  221. }
  222. /**
  223. * Clear request headers
  224. *
  225. * @return Zend_Controller_Request_HttpTestCase
  226. */
  227. public function clearHeaders()
  228. {
  229. $this->_headers = array();
  230. return $this;
  231. }
  232. /**
  233. * Get REQUEST_URI
  234. *
  235. * @return null|string
  236. */
  237. public function getRequestUri()
  238. {
  239. return $this->_requestUri;
  240. }
  241. /**
  242. * Normalize a header name for setting and retrieval
  243. *
  244. * @param string $name
  245. * @return string
  246. */
  247. protected function _normalizeHeaderName($name)
  248. {
  249. $name = strtoupper((string) $name);
  250. $name = str_replace('-', '_', $name);
  251. return $name;
  252. }
  253. }