HttpTestCase.php 5.7 KB

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