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