1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | */ |
---|
31 | |
---|
32 | class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | |
---|
35 | /** |
---|
36 | * @var Solarium_Client_Request |
---|
37 | */ |
---|
38 | protected $_request; |
---|
39 | |
---|
40 | public function setup() |
---|
41 | { |
---|
42 | $this->_request = new Solarium_Client_Request; |
---|
43 | } |
---|
44 | |
---|
45 | public function testConfigMode() |
---|
46 | { |
---|
47 | $options = array( |
---|
48 | 'method' => Solarium_Client_Request::METHOD_POST, |
---|
49 | 'handler' => 'myHandler', |
---|
50 | 'param' => array( |
---|
51 | 'param1' => 1, |
---|
52 | 'param2' => 'test', |
---|
53 | ), |
---|
54 | 'rawdata' => 'raw post data here', |
---|
55 | 'header' => array( |
---|
56 | 'myHeader1' => 'X-myHeader1: value1', |
---|
57 | 'myHeader2' => 'X-myHeader2: value2', |
---|
58 | ), |
---|
59 | ); |
---|
60 | $this->_request->setOptions($options); |
---|
61 | |
---|
62 | $this->assertEquals( |
---|
63 | $options['method'], |
---|
64 | $this->_request->getMethod() |
---|
65 | ); |
---|
66 | |
---|
67 | $this->assertEquals( |
---|
68 | $options['handler'], |
---|
69 | $this->_request->getHandler() |
---|
70 | ); |
---|
71 | |
---|
72 | $this->assertEquals( |
---|
73 | $options['rawdata'], |
---|
74 | $this->_request->getRawData() |
---|
75 | ); |
---|
76 | |
---|
77 | $this->assertEquals( |
---|
78 | $options['param'], |
---|
79 | $this->_request->getParams() |
---|
80 | ); |
---|
81 | |
---|
82 | $this->assertEquals( |
---|
83 | array( |
---|
84 | $options['header']['myHeader1'], |
---|
85 | $options['header']['myHeader2'] |
---|
86 | ), |
---|
87 | $this->_request->getHeaders() |
---|
88 | ); |
---|
89 | } |
---|
90 | |
---|
91 | public function testGetDefaultMethod() |
---|
92 | { |
---|
93 | $this->assertEquals( |
---|
94 | Solarium_Client_Request::METHOD_GET, |
---|
95 | $this->_request->getMethod() |
---|
96 | ); |
---|
97 | } |
---|
98 | |
---|
99 | public function testSetAndGetMethod() |
---|
100 | { |
---|
101 | $this->_request->setMethod(Solarium_Client_Request::METHOD_POST); |
---|
102 | |
---|
103 | $this->assertEquals( |
---|
104 | Solarium_Client_Request::METHOD_POST, |
---|
105 | $this->_request->getMethod() |
---|
106 | ); |
---|
107 | } |
---|
108 | |
---|
109 | public function testSetAndGetHandler() |
---|
110 | { |
---|
111 | $this->_request->setHandler('myhandler'); |
---|
112 | |
---|
113 | $this->assertEquals( |
---|
114 | 'myhandler', |
---|
115 | $this->_request->getHandler() |
---|
116 | ); |
---|
117 | } |
---|
118 | |
---|
119 | public function testSetAndGetParams() |
---|
120 | { |
---|
121 | $params = array( |
---|
122 | 'param1' => 1, |
---|
123 | 'param2' => 2, |
---|
124 | ); |
---|
125 | |
---|
126 | $this->_request->setParams($params); |
---|
127 | |
---|
128 | $this->assertEquals( |
---|
129 | $params, |
---|
130 | $this->_request->getParams() |
---|
131 | ); |
---|
132 | } |
---|
133 | |
---|
134 | public function testSetAndGetParam() |
---|
135 | { |
---|
136 | $params = array( |
---|
137 | 'param1' => 1, |
---|
138 | 'param2' => 2, |
---|
139 | ); |
---|
140 | |
---|
141 | $this->_request->setParams($params); |
---|
142 | |
---|
143 | $this->assertEquals( |
---|
144 | 2, |
---|
145 | $this->_request->getParam('param2') |
---|
146 | ); |
---|
147 | } |
---|
148 | |
---|
149 | public function testGetInvalidParam() |
---|
150 | { |
---|
151 | $this->assertEquals( |
---|
152 | null, |
---|
153 | $this->_request->getParam('invalidname') |
---|
154 | ); |
---|
155 | } |
---|
156 | |
---|
157 | public function testAddParam() |
---|
158 | { |
---|
159 | $params = array( |
---|
160 | 'param1' => 1, |
---|
161 | 'param2' => 2, |
---|
162 | ); |
---|
163 | |
---|
164 | $this->_request->setParams($params); |
---|
165 | $this->_request->addParam('param3', 3); |
---|
166 | |
---|
167 | $params['param3'] = 3; |
---|
168 | |
---|
169 | $this->assertEquals( |
---|
170 | $params, |
---|
171 | $this->_request->getParams() |
---|
172 | ); |
---|
173 | } |
---|
174 | |
---|
175 | public function testAddParamBoolean() |
---|
176 | { |
---|
177 | $params = array( |
---|
178 | 'param1' => true, |
---|
179 | 'param2' => false, |
---|
180 | ); |
---|
181 | |
---|
182 | $this->_request->addParams($params); |
---|
183 | |
---|
184 | $this->assertEquals( |
---|
185 | array( |
---|
186 | 'param1' => 'true', |
---|
187 | 'param2' => 'false', |
---|
188 | ), |
---|
189 | $this->_request->getParams() |
---|
190 | ); |
---|
191 | } |
---|
192 | |
---|
193 | public function testAddParamMultivalue() |
---|
194 | { |
---|
195 | $params = array( |
---|
196 | 'param1' => 1, |
---|
197 | ); |
---|
198 | |
---|
199 | $this->_request->setParams($params); |
---|
200 | $this->_request->addParam('param2', 2); |
---|
201 | $this->_request->addParam('param2', 3); |
---|
202 | |
---|
203 | $params['param2'] = array(2, 3); |
---|
204 | |
---|
205 | $this->assertEquals( |
---|
206 | $params, |
---|
207 | $this->_request->getParams() |
---|
208 | ); |
---|
209 | } |
---|
210 | |
---|
211 | public function testAddParamNoValue() |
---|
212 | { |
---|
213 | $params = array( |
---|
214 | 'param1' => 1, |
---|
215 | 'param2' => 2, |
---|
216 | 'param3' => 3, |
---|
217 | ); |
---|
218 | |
---|
219 | $this->_request->setParams($params); |
---|
220 | $this->_request->addParam('param2', ''); // this should add an empty value to param2 |
---|
221 | $this->_request->addParam('param3', '' , true); // this should overwrite param2 with an empty value |
---|
222 | $this->_request->addParam('param4', ''); // this should add an empty param (for instance "q=" in dismax) |
---|
223 | $this->_request->addParam('param5', null); // this param should be ignored |
---|
224 | |
---|
225 | $this->assertEquals( |
---|
226 | array( |
---|
227 | 'param1' => 1, |
---|
228 | 'param2' => array(2,''), |
---|
229 | 'param3' => '', |
---|
230 | 'param4' => '', |
---|
231 | ), |
---|
232 | $this->_request->getParams() |
---|
233 | ); |
---|
234 | } |
---|
235 | |
---|
236 | public function testAddParamOverwrite() |
---|
237 | { |
---|
238 | $params = array( |
---|
239 | 'param1' => 1, |
---|
240 | ); |
---|
241 | |
---|
242 | $this->_request->setParams($params); |
---|
243 | $this->_request->addParam('param1', 2, true); |
---|
244 | |
---|
245 | |
---|
246 | $this->assertEquals( |
---|
247 | array('param1' => 2), |
---|
248 | $this->_request->getParams() |
---|
249 | ); |
---|
250 | } |
---|
251 | |
---|
252 | public function testAddParams() |
---|
253 | { |
---|
254 | $params = array( |
---|
255 | 'param1' => 1, |
---|
256 | ); |
---|
257 | |
---|
258 | $extraParams = array( |
---|
259 | 'param1' => 2, |
---|
260 | 'param2' => 3, |
---|
261 | ); |
---|
262 | |
---|
263 | $this->_request->setParams($params); |
---|
264 | $this->_request->addParams($extraParams); |
---|
265 | |
---|
266 | |
---|
267 | $this->assertEquals( |
---|
268 | array( |
---|
269 | 'param1' => array(1,2), |
---|
270 | 'param2' => 3, |
---|
271 | ), |
---|
272 | $this->_request->getParams() |
---|
273 | ); |
---|
274 | } |
---|
275 | |
---|
276 | public function testAddParamsOverwrite() |
---|
277 | { |
---|
278 | $params = array( |
---|
279 | 'param1' => 1, |
---|
280 | ); |
---|
281 | |
---|
282 | $extraParams = array( |
---|
283 | 'param1' => 2, |
---|
284 | 'param2' => 3, |
---|
285 | ); |
---|
286 | |
---|
287 | $this->_request->setParams($params); |
---|
288 | $this->_request->addParams($extraParams, true); |
---|
289 | |
---|
290 | |
---|
291 | $this->assertEquals( |
---|
292 | array( |
---|
293 | 'param1' => 2, |
---|
294 | 'param2' => 3, |
---|
295 | ), |
---|
296 | $this->_request->getParams() |
---|
297 | ); |
---|
298 | } |
---|
299 | |
---|
300 | public function testRemoveParam() |
---|
301 | { |
---|
302 | $params = array( |
---|
303 | 'param1' => 1, |
---|
304 | 'param2' => 2, |
---|
305 | ); |
---|
306 | |
---|
307 | $this->_request->setParams($params); |
---|
308 | $this->_request->removeParam('param2'); |
---|
309 | |
---|
310 | $this->assertEquals( |
---|
311 | array('param1' => 1), |
---|
312 | $this->_request->getParams() |
---|
313 | ); |
---|
314 | } |
---|
315 | |
---|
316 | public function testClearParams() |
---|
317 | { |
---|
318 | $params = array( |
---|
319 | 'param1' => 1, |
---|
320 | 'param2' => 2, |
---|
321 | ); |
---|
322 | |
---|
323 | $this->_request->setParams($params); |
---|
324 | $this->_request->clearParams(); |
---|
325 | |
---|
326 | $this->assertEquals( |
---|
327 | array(), |
---|
328 | $this->_request->getParams() |
---|
329 | ); |
---|
330 | } |
---|
331 | |
---|
332 | public function testGetAndSetRawData() |
---|
333 | { |
---|
334 | $data = '1234567890'; |
---|
335 | $this->_request->setRawData($data); |
---|
336 | |
---|
337 | $this->assertEquals( |
---|
338 | $data, |
---|
339 | $this->_request->getRawData() |
---|
340 | ); |
---|
341 | } |
---|
342 | |
---|
343 | public function testSetAndGetHeaders() |
---|
344 | { |
---|
345 | $headers = array( |
---|
346 | 'User-Agent: My Agent', |
---|
347 | 'Cache-Control: no-cache' |
---|
348 | ); |
---|
349 | $this->_request->setHeaders($headers); |
---|
350 | |
---|
351 | $this->assertEquals( |
---|
352 | $headers, |
---|
353 | $this->_request->getHeaders() |
---|
354 | ); |
---|
355 | } |
---|
356 | |
---|
357 | public function testAddHeader() |
---|
358 | { |
---|
359 | $headers = array( |
---|
360 | 'User-Agent: My Agent', |
---|
361 | |
---|
362 | ); |
---|
363 | |
---|
364 | $this->_request->setHeaders($headers); |
---|
365 | $this->_request->addHeader('Cache-Control: no-cache'); |
---|
366 | |
---|
367 | $headers[] = 'Cache-Control: no-cache'; |
---|
368 | |
---|
369 | $this->assertEquals( |
---|
370 | $headers, |
---|
371 | $this->_request->getHeaders() |
---|
372 | ); |
---|
373 | } |
---|
374 | |
---|
375 | public function testAddHeaders() |
---|
376 | { |
---|
377 | $headers = array( |
---|
378 | 'User-Agent: My Agent', |
---|
379 | |
---|
380 | ); |
---|
381 | |
---|
382 | $extraHeaders = array( |
---|
383 | 'Cache-Control: no-cache', |
---|
384 | 'X-custom: 123', |
---|
385 | ); |
---|
386 | |
---|
387 | $this->_request->setHeaders($headers); |
---|
388 | $this->_request->addHeaders($extraHeaders); |
---|
389 | |
---|
390 | $this->assertEquals( |
---|
391 | array_merge($headers, $extraHeaders), |
---|
392 | $this->_request->getHeaders() |
---|
393 | ); |
---|
394 | } |
---|
395 | |
---|
396 | public function testClearHeaders() |
---|
397 | { |
---|
398 | $headers = array( |
---|
399 | 'User-Agent: My Agent', |
---|
400 | 'Cache-Control: no-cache' |
---|
401 | ); |
---|
402 | |
---|
403 | $this->_request->setHeaders($headers); |
---|
404 | |
---|
405 | $this->assertEquals( |
---|
406 | $headers, |
---|
407 | $this->_request->getHeaders() |
---|
408 | ); |
---|
409 | |
---|
410 | $this->_request->clearHeaders(); |
---|
411 | |
---|
412 | $this->assertEquals( |
---|
413 | array(), |
---|
414 | $this->_request->getHeaders() |
---|
415 | ); |
---|
416 | } |
---|
417 | |
---|
418 | public function testGetUri() |
---|
419 | { |
---|
420 | $this->assertEquals( |
---|
421 | '?', |
---|
422 | $this->_request->getUri() |
---|
423 | ); |
---|
424 | } |
---|
425 | |
---|
426 | public function testGetUriWithHandlerAndParams() |
---|
427 | { |
---|
428 | $params = array( |
---|
429 | 'param1' => 1, |
---|
430 | 'param2' => array(2,3), |
---|
431 | ); |
---|
432 | |
---|
433 | $this->_request->setHandler('myHandler'); |
---|
434 | $this->_request->addParams($params); |
---|
435 | |
---|
436 | $this->assertEquals( |
---|
437 | 'myHandler?param1=1¶m2=2¶m2=3', |
---|
438 | $this->_request->getUri() |
---|
439 | ); |
---|
440 | } |
---|
441 | |
---|
442 | public function testToString() |
---|
443 | { |
---|
444 | $options = array( |
---|
445 | 'method' => Solarium_Client_Request::METHOD_POST, |
---|
446 | 'handler' => '/myHandler', |
---|
447 | 'param' => array( |
---|
448 | 'param1' => 1, |
---|
449 | 'param2' => 'test content', |
---|
450 | ), |
---|
451 | 'rawdata' => 'post data', |
---|
452 | 'header' => array( |
---|
453 | 'myHeader1' => 'X-myHeader1: value1', |
---|
454 | 'myHeader2' => 'X-myHeader2: value2', |
---|
455 | ), |
---|
456 | ); |
---|
457 | $this->_request->setOptions($options); |
---|
458 | |
---|
459 | $this->assertEquals( |
---|
460 | 'Solarium_Client_Request::toString |
---|
461 | method: POST |
---|
462 | header: Array |
---|
463 | ( |
---|
464 | [0] => X-myHeader1: value1 |
---|
465 | [1] => X-myHeader2: value2 |
---|
466 | ) |
---|
467 | resource: /myHandler?param1=1¶m2=test+content |
---|
468 | resource urldecoded: /myHandler?param1=1¶m2=test content |
---|
469 | raw data: post data |
---|
470 | ', |
---|
471 | (string)$this->_request |
---|
472 | ); |
---|
473 | } |
---|
474 | |
---|
475 | } |
---|