comparison cake/tests/cases/libs/http_socket.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * HttpSocketTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.libs
17 * @since CakePHP(tm) v 1.2.0.4206
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 App::import('Core', 'HttpSocket');
21
22 class TestHttpSocket extends HttpSocket {
23
24 /**
25 * Convenience method for testing protected method
26 *
27 * @param mixed $uri URI (see {@link _parseUri()})
28 * @return array Current configuration settings
29 */
30 function configUri($uri = null) {
31 return parent::_configUri($uri);
32 }
33
34 /**
35 * Convenience method for testing protected method
36 *
37 * @param string $uri URI to parse
38 * @param mixed $base If true use default URI config, otherwise indexed array to set 'scheme', 'host', 'port', etc.
39 * @return array Parsed URI
40 */
41 function parseUri($uri = null, $base = array()) {
42 return parent::_parseUri($uri, $base);
43 }
44
45 /**
46 * Convenience method for testing protected method
47 *
48 * @param array $uri A $uri array, or uses $this->config if left empty
49 * @param string $uriTemplate The Uri template/format to use
50 * @return string A fully qualified URL formated according to $uriTemplate
51 */
52 function buildUri($uri = array(), $uriTemplate = '%scheme://%user:%pass@%host:%port/%path?%query#%fragment') {
53 return parent::_buildUri($uri, $uriTemplate);
54 }
55
56 /**
57 * Convenience method for testing protected method
58 *
59 * @param array $header Header to build
60 * @return string Header built from array
61 */
62 function buildHeader($header, $mode = 'standard') {
63 return parent::_buildHeader($header, $mode);
64 }
65
66 /**
67 * Convenience method for testing protected method
68 *
69 * @param string $message Message to parse
70 * @return array Parsed message (with indexed elements such as raw, status, header, body)
71 */
72 function parseResponse($message) {
73 return parent::_parseResponse($message);
74 }
75
76 /**
77 * Convenience method for testing protected method
78 *
79 * @param array $header Header as an indexed array (field => value)
80 * @return array Parsed header
81 */
82 function parseHeader($header) {
83 return parent::_parseHeader($header);
84 }
85
86 /**
87 * Convenience method for testing protected method
88 *
89 * @param mixed $query A query string to parse into an array or an array to return directly "as is"
90 * @return array The $query parsed into a possibly multi-level array. If an empty $query is given, an empty array is returned.
91 */
92 function parseQuery($query) {
93 return parent::_parseQuery($query);
94 }
95
96 /**
97 * Convenience method for testing protected method
98 *
99 * @param string $body A string continaing the body to decode
100 * @param mixed $encoding Can be false in case no encoding is being used, or a string representing the encoding
101 * @return mixed Array or false
102 */
103 function decodeBody($body, $encoding = 'chunked') {
104 return parent::_decodeBody($body, $encoding);
105 }
106
107 /**
108 * Convenience method for testing protected method
109 *
110 * @param string $body A string continaing the chunked body to decode
111 * @return mixed Array or false
112 */
113 function decodeChunkedBody($body) {
114 return parent::_decodeChunkedBody($body);
115 }
116
117 /**
118 * Convenience method for testing protected method
119 *
120 * @param array $request Needs to contain a 'uri' key. Should also contain a 'method' key, otherwise defaults to GET.
121 * @param string $versionToken The version token to use, defaults to HTTP/1.1
122 * @return string Request line
123 */
124 function buildRequestLine($request = array(), $versionToken = 'HTTP/1.1') {
125 return parent::_buildRequestLine($request, $versionToken);
126 }
127
128 /**
129 * Convenience method for testing protected method
130 *
131 * @param boolean $hex true to get them as HEX values, false otherwise
132 * @return array Escape chars
133 */
134 function tokenEscapeChars($hex = true, $chars = null) {
135 return parent::_tokenEscapeChars($hex, $chars);
136 }
137
138 /**
139 * Convenience method for testing protected method
140 *
141 * @param string $token Token to escape
142 * @return string Escaped token
143 */
144 function EscapeToken($token, $chars = null) {
145 return parent::_escapeToken($token, $chars);
146 }
147
148 /**
149 * Convenience method for testing protected method
150 *
151 * @param string $token Token to unescape
152 * @return string Unescaped token
153 */
154 function unescapeToken($token, $chars = null) {
155 return parent::_unescapeToken($token, $chars);
156 }
157 }
158
159 /**
160 * HttpSocketTest class
161 *
162 * @package cake
163 * @subpackage cake.tests.cases.libs
164 */
165 class HttpSocketTest extends CakeTestCase {
166
167 /**
168 * Socket property
169 *
170 * @var mixed null
171 * @access public
172 */
173 var $Socket = null;
174
175 /**
176 * RequestSocket property
177 *
178 * @var mixed null
179 * @access public
180 */
181 var $RequestSocket = null;
182
183 /**
184 * This function sets up a TestHttpSocket instance we are going to use for testing
185 *
186 * @access public
187 * @return void
188 */
189 function setUp() {
190 if (!class_exists('MockHttpSocket')) {
191 Mock::generatePartial('TestHttpSocket', 'MockHttpSocket', array('read', 'write', 'connect'));
192 Mock::generatePartial('TestHttpSocket', 'MockHttpSocketRequests', array('read', 'write', 'connect', 'request'));
193 }
194
195 $this->Socket =& new MockHttpSocket();
196 $this->RequestSocket =& new MockHttpSocketRequests();
197 }
198
199 /**
200 * We use this function to clean up after the test case was executed
201 *
202 * @access public
203 * @return void
204 */
205 function tearDown() {
206 unset($this->Socket, $this->RequestSocket);
207 }
208
209 /**
210 * Test that HttpSocket::__construct does what one would expect it to do
211 *
212 * @access public
213 * @return void
214 */
215 function testConstruct() {
216 $this->Socket->reset();
217 $baseConfig = $this->Socket->config;
218 $this->Socket->expectNever('connect');
219 $this->Socket->__construct(array('host' => 'foo-bar'));
220 $baseConfig['host'] = 'foo-bar';
221 $baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);
222 $this->assertIdentical($this->Socket->config, $baseConfig);
223 $this->Socket->reset();
224 $baseConfig = $this->Socket->config;
225 $this->Socket->__construct('http://www.cakephp.org:23/');
226 $baseConfig['host'] = 'www.cakephp.org';
227 $baseConfig['request']['uri']['host'] = 'www.cakephp.org';
228 $baseConfig['port'] = 23;
229 $baseConfig['request']['uri']['port'] = 23;
230 $baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);
231 $this->assertIdentical($this->Socket->config, $baseConfig);
232
233 $this->Socket->reset();
234 $this->Socket->__construct(array('request' => array('uri' => 'http://www.cakephp.org:23/')));
235 $this->assertIdentical($this->Socket->config, $baseConfig);
236 }
237
238 /**
239 * Test that HttpSocket::configUri works properly with different types of arguments
240 *
241 * @access public
242 * @return void
243 */
244 function testConfigUri() {
245 $this->Socket->reset();
246 $r = $this->Socket->configUri('https://bob:secret@www.cakephp.org:23/?query=foo');
247 $expected = array(
248 'persistent' => false,
249 'host' => 'www.cakephp.org',
250 'protocol' => 'tcp',
251 'port' => 23,
252 'timeout' => 30,
253 'request' => array(
254 'uri' => array(
255 'scheme' => 'https'
256 , 'host' => 'www.cakephp.org'
257 , 'port' => 23
258 ),
259 'auth' => array(
260 'method' => 'Basic'
261 , 'user' => 'bob'
262 , 'pass' => 'secret'
263 ),
264 'cookies' => array(),
265 )
266 );
267 $this->assertIdentical($this->Socket->config, $expected);
268 $this->assertIdentical($r, $expected);
269 $r = $this->Socket->configUri(array('host' => 'www.foo-bar.org'));
270 $expected['host'] = 'www.foo-bar.org';
271 $expected['request']['uri']['host'] = 'www.foo-bar.org';
272 $this->assertIdentical($this->Socket->config, $expected);
273 $this->assertIdentical($r, $expected);
274
275 $r = $this->Socket->configUri('http://www.foo.com');
276 $expected = array(
277 'persistent' => false,
278 'host' => 'www.foo.com',
279 'protocol' => 'tcp',
280 'port' => 80,
281 'timeout' => 30,
282 'request' => array(
283 'uri' => array(
284 'scheme' => 'http'
285 , 'host' => 'www.foo.com'
286 , 'port' => 80
287 ),
288 'auth' => array(
289 'method' => 'Basic'
290 , 'user' => null
291 , 'pass' => null
292 ),
293 'cookies' => array()
294 )
295 );
296 $this->assertIdentical($this->Socket->config, $expected);
297 $this->assertIdentical($r, $expected);
298 $r = $this->Socket->configUri('/this-is-broken');
299 $this->assertIdentical($this->Socket->config, $expected);
300 $this->assertIdentical($r, false);
301 $r = $this->Socket->configUri(false);
302 $this->assertIdentical($this->Socket->config, $expected);
303 $this->assertIdentical($r, false);
304 }
305
306 /**
307 * Tests that HttpSocket::request (the heart of the HttpSocket) is working properly.
308 *
309 * @access public
310 * @return void
311 */
312 function testRequest() {
313 $this->Socket->reset();
314
315 $this->Socket->reset();
316 $response = $this->Socket->request(true);
317 $this->assertFalse($response);
318
319 $tests = array(
320 0 => array(
321 'request' => 'http://www.cakephp.org/?foo=bar'
322 , 'expectation' => array(
323 'config' => array(
324 'persistent' => false
325 , 'host' => 'www.cakephp.org'
326 , 'protocol' => 'tcp'
327 , 'port' => 80
328 , 'timeout' => 30
329 , 'request' => array(
330 'uri' => array (
331 'scheme' => 'http'
332 , 'host' => 'www.cakephp.org'
333 , 'port' => 80,
334 )
335 , 'auth' => array(
336 'method' => 'Basic'
337 ,'user' => null
338 ,'pass' => null
339 ),
340 'cookies' => array(),
341 ),
342 )
343 , 'request' => array(
344 'method' => 'GET'
345 , 'uri' => array(
346 'scheme' => 'http'
347 , 'host' => 'www.cakephp.org'
348 , 'port' => 80
349 , 'user' => null
350 , 'pass' => null
351 , 'path' => '/'
352 , 'query' => array('foo' => 'bar')
353 , 'fragment' => null
354 )
355 , 'auth' => array(
356 'method' => 'Basic'
357 , 'user' => null
358 , 'pass' => null
359 )
360 , 'version' => '1.1'
361 , 'body' => ''
362 , 'line' => "GET /?foo=bar HTTP/1.1\r\n"
363 , 'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n"
364 , 'raw' => ""
365 , 'cookies' => array(),
366 )
367 )
368 )
369 , 1 => array(
370 'request' => array(
371 'uri' => array(
372 'host' => 'www.cakephp.org'
373 , 'query' => '?foo=bar'
374 )
375 )
376 )
377 , 2 => array(
378 'request' => 'www.cakephp.org/?foo=bar'
379 )
380 , 3 => array(
381 'request' => array('host' => '192.168.0.1', 'uri' => 'http://www.cakephp.org/?foo=bar')
382 , 'expectation' => array(
383 'request' => array(
384 'uri' => array('host' => 'www.cakephp.org')
385 )
386 , 'config' => array(
387 'request' => array(
388 'uri' => array('host' => 'www.cakephp.org')
389 )
390 , 'host' => '192.168.0.1'
391 )
392 )
393 )
394 , 'reset4' => array(
395 'request.uri.query' => array()
396 )
397 , 4 => array(
398 'request' => array('header' => array('Foo@woo' => 'bar-value'))
399 , 'expectation' => array(
400 'request' => array(
401 'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nFoo\"@\"woo: bar-value\r\n"
402 , 'line' => "GET / HTTP/1.1\r\n"
403 )
404 )
405 )
406 , 5 => array(
407 'request' => array('header' => array('Foo@woo' => 'bar-value', 'host' => 'foo.com'), 'uri' => 'http://www.cakephp.org/')
408 , 'expectation' => array(
409 'request' => array(
410 'header' => "Host: foo.com\r\nConnection: close\r\nUser-Agent: CakePHP\r\nFoo\"@\"woo: bar-value\r\n"
411 )
412 , 'config' => array(
413 'host' => 'www.cakephp.org'
414 )
415 )
416 )
417 , 6 => array(
418 'request' => array('header' => "Foo: bar\r\n")
419 , 'expectation' => array(
420 'request' => array(
421 'header' => "Foo: bar\r\n"
422 )
423 )
424 )
425 , 7 => array(
426 'request' => array('header' => "Foo: bar\r\n", 'uri' => 'http://www.cakephp.org/search?q=http_socket#ignore-me')
427 , 'expectation' => array(
428 'request' => array(
429 'uri' => array(
430 'path' => '/search'
431 , 'query' => array('q' => 'http_socket')
432 , 'fragment' => 'ignore-me'
433 )
434 , 'line' => "GET /search?q=http_socket HTTP/1.1\r\n"
435 )
436 )
437 )
438 , 'reset8' => array(
439 'request.uri.query' => array()
440 )
441 , 8 => array(
442 'request' => array('method' => 'POST', 'uri' => 'http://www.cakephp.org/posts/add', 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'))
443 , 'expectation' => array(
444 'request' => array(
445 'method' => 'POST'
446 , 'uri' => array(
447 'path' => '/posts/add'
448 , 'fragment' => null
449 )
450 , 'body' => "name=HttpSocket-is-released&date=today"
451 , 'line' => "POST /posts/add HTTP/1.1\r\n"
452 , 'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 38\r\n"
453 , 'raw' => "name=HttpSocket-is-released&date=today"
454 )
455 )
456 )
457 , 9 => array(
458 'request' => array('method' => 'POST', 'uri' => 'http://www.cakephp.org:8080/posts/add', 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'))
459 , 'expectation' => array(
460 'config' => array(
461 'port' => 8080
462 , 'request' => array(
463 'uri' => array(
464 'port' => 8080
465 )
466 )
467 )
468 , 'request' => array(
469 'uri' => array(
470 'port' => 8080
471 )
472 , 'header' => "Host: www.cakephp.org:8080\r\nConnection: close\r\nUser-Agent: CakePHP\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 38\r\n"
473 )
474 )
475 )
476 , 10 => array(
477 'request' => array('method' => 'POST', 'uri' => 'https://www.cakephp.org/posts/add', 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'))
478 , 'expectation' => array(
479 'config' => array(
480 'port' => 443
481 , 'request' => array(
482 'uri' => array(
483 'scheme' => 'https'
484 , 'port' => 443
485 )
486 )
487 )
488 , 'request' => array(
489 'uri' => array(
490 'scheme' => 'https'
491 , 'port' => 443
492 )
493 , 'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 38\r\n"
494 )
495 )
496 )
497 , 11 => array(
498 'request' => array(
499 'method' => 'POST',
500 'uri' => 'https://www.cakephp.org/posts/add',
501 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'),
502 'cookies' => array('foo' => array('value' => 'bar'))
503 )
504 , 'expectation' => array(
505 'request' => array(
506 'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 38\r\nCookie: foo=bar\r\n",
507 'cookies' => array(
508 'foo' => array('value' => 'bar'),
509 )
510 )
511 )
512 )
513 );
514
515 $expectation = array();
516 foreach ($tests as $i => $test) {
517 if (strpos($i, 'reset') === 0) {
518 foreach ($test as $path => $val) {
519 $expectation = Set::insert($expectation, $path, $val);
520 }
521 continue;
522 }
523
524 if (isset($test['expectation'])) {
525 $expectation = Set::merge($expectation, $test['expectation']);
526 }
527 $this->Socket->request($test['request']);
528
529 $raw = $expectation['request']['raw'];
530 $expectation['request']['raw'] = $expectation['request']['line'].$expectation['request']['header']."\r\n".$raw;
531
532 $r = array('config' => $this->Socket->config, 'request' => $this->Socket->request);
533 $v = $this->assertIdentical($r, $expectation, '%s in test #'.$i.' ');
534 $expectation['request']['raw'] = $raw;
535 }
536
537 $this->Socket->reset();
538 $request = array('method' => 'POST', 'uri' => 'http://www.cakephp.org/posts/add', 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'));
539 $response = $this->Socket->request($request);
540 $this->assertIdentical($this->Socket->request['body'], "name=HttpSocket-is-released&date=today");
541
542 $request = array('uri' => '*', 'method' => 'GET');
543 $this->expectError(new PatternExpectation('/activate quirks mode/i'));
544 $response = $this->Socket->request($request);
545 $this->assertFalse($response);
546 $this->assertFalse($this->Socket->response);
547
548 $this->Socket->reset();
549 $request = array('uri' => 'htpp://www.cakephp.org/');
550 $this->Socket->setReturnValue('connect', true);
551 $this->Socket->setReturnValue('read', false);
552 $this->Socket->_mock->_call_counts['read'] = 0;
553 $number = mt_rand(0, 9999999);
554 $serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>Hello, your lucky number is " . $number . "</h1>";
555 $this->Socket->setReturnValueAt(0, 'read', $serverResponse);
556 $this->Socket->expect('write', array("GET / HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n\r\n"));
557 $this->Socket->expectCallCount('read', 2);
558 $response = $this->Socket->request($request);
559 $this->assertIdentical($response, "<h1>Hello, your lucky number is " . $number . "</h1>");
560
561 $this->Socket->reset();
562 $serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: foo=bar\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a cookie test!</h1>";
563 unset($this->Socket->_mock->_actions->_at['read']);
564 unset($this->Socket->_mock->_return_sequence['read']);
565 $this->Socket->_mock->_call_counts['read'] = 0;
566 $this->Socket->setReturnValueAt(0, 'read', $serverResponse);
567
568 $this->Socket->connected = true;
569 $this->Socket->request($request);
570 $result = $this->Socket->response['cookies'];
571 $expect = array(
572 'foo' => array(
573 'value' => 'bar'
574 )
575 );
576 $this->assertEqual($result, $expect);
577 $this->assertEqual($this->Socket->config['request']['cookies'], $expect);
578 $this->assertFalse($this->Socket->connected);
579 }
580
581 /**
582 * testUrl method
583 *
584 * @access public
585 * @return void
586 */
587 function testUrl() {
588 $this->Socket->reset(true);
589
590 $this->assertIdentical($this->Socket->url(true), false);
591
592 $url = $this->Socket->url('www.cakephp.org');
593 $this->assertIdentical($url, 'http://www.cakephp.org/');
594
595 $url = $this->Socket->url('https://www.cakephp.org/posts/add');
596 $this->assertIdentical($url, 'https://www.cakephp.org/posts/add');
597 $url = $this->Socket->url('http://www.cakephp/search?q=socket', '/%path?%query');
598 $this->assertIdentical($url, '/search?q=socket');
599
600 $this->Socket->config['request']['uri']['host'] = 'bakery.cakephp.org';
601 $url = $this->Socket->url();
602 $this->assertIdentical($url, 'http://bakery.cakephp.org/');
603
604 $this->Socket->configUri('http://www.cakephp.org');
605 $url = $this->Socket->url('/search?q=bar');
606 $this->assertIdentical($url, 'http://www.cakephp.org/search?q=bar');
607
608 $url = $this->Socket->url(array('host' => 'www.foobar.org', 'query' => array('q' => 'bar')));
609 $this->assertIdentical($url, 'http://www.foobar.org/?q=bar');
610
611 $url = $this->Socket->url(array('path' => '/supersearch', 'query' => array('q' => 'bar')));
612 $this->assertIdentical($url, 'http://www.cakephp.org/supersearch?q=bar');
613
614 $this->Socket->configUri('http://www.google.com');
615 $url = $this->Socket->url('/search?q=socket');
616 $this->assertIdentical($url, 'http://www.google.com/search?q=socket');
617
618 $url = $this->Socket->url();
619 $this->assertIdentical($url, 'http://www.google.com/');
620
621 $this->Socket->configUri('https://www.google.com');
622 $url = $this->Socket->url('/search?q=socket');
623 $this->assertIdentical($url, 'https://www.google.com/search?q=socket');
624
625 $this->Socket->reset();
626 $this->Socket->configUri('www.google.com:443');
627 $url = $this->Socket->url('/search?q=socket');
628 $this->assertIdentical($url, 'https://www.google.com/search?q=socket');
629
630 $this->Socket->reset();
631 $this->Socket->configUri('www.google.com:8080');
632 $url = $this->Socket->url('/search?q=socket');
633 $this->assertIdentical($url, 'http://www.google.com:8080/search?q=socket');
634 }
635
636 /**
637 * testGet method
638 *
639 * @access public
640 * @return void
641 */
642 function testGet() {
643 $this->RequestSocket->reset();
644
645 $this->RequestSocket->expect('request', a(array('method' => 'GET', 'uri' => 'http://www.google.com/')));
646 $this->RequestSocket->get('http://www.google.com/');
647
648 $this->RequestSocket->expect('request', a(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar')));
649 $this->RequestSocket->get('http://www.google.com/', array('foo' => 'bar'));
650
651 $this->RequestSocket->expect('request', a(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar')));
652 $this->RequestSocket->get('http://www.google.com/', 'foo=bar');
653
654 $this->RequestSocket->expect('request', a(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=23&foobar=42')));
655 $this->RequestSocket->get('http://www.google.com/?foo=bar', array('foobar' => '42', 'foo' => '23'));
656
657 $this->RequestSocket->expect('request', a(array('method' => 'GET', 'uri' => 'http://www.google.com/', 'auth' => array('user' => 'foo', 'pass' => 'bar'))));
658 $this->RequestSocket->get('http://www.google.com/', null, array('auth' => array('user' => 'foo', 'pass' => 'bar')));
659 }
660
661 /**
662 * test that two consecutive get() calls reset the authentication credentials.
663 *
664 * @return void
665 */
666 function testConsecutiveGetResetsAuthCredentials() {
667 $socket = new MockHttpSocket();
668 $socket->config['request']['auth'] = array(
669 'method' => 'Basic',
670 'user' => 'mark',
671 'pass' => 'secret'
672 );
673 $socket->get('http://mark:secret@example.com/test');
674 $this->assertEqual($socket->request['uri']['user'], 'mark');
675 $this->assertEqual($socket->request['uri']['pass'], 'secret');
676
677 $socket->get('/test2');
678 $this->assertEqual($socket->request['auth']['user'], 'mark');
679 $this->assertEqual($socket->request['auth']['pass'], 'secret');
680
681 $socket->get('/test3');
682 $this->assertEqual($socket->request['auth']['user'], 'mark');
683 $this->assertEqual($socket->request['auth']['pass'], 'secret');
684 }
685
686 /**
687 * testPostPutDelete method
688 *
689 * @access public
690 * @return void
691 */
692 function testPostPutDelete() {
693 $this->RequestSocket->reset();
694
695 foreach (array('POST', 'PUT', 'DELETE') as $method) {
696 $this->RequestSocket->expect('request', a(array('method' => $method, 'uri' => 'http://www.google.com/', 'body' => array())));
697 $this->RequestSocket->{low($method)}('http://www.google.com/');
698
699 $this->RequestSocket->expect('request', a(array('method' => $method, 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar'))));
700 $this->RequestSocket->{low($method)}('http://www.google.com/', array('Foo' => 'bar'));
701
702 $this->RequestSocket->expect('request', a(array('method' => $method, 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server')));
703 $this->RequestSocket->{low($method)}('http://www.google.com/', null, array('line' => 'Hey Server'));
704 }
705 }
706
707 /**
708 * testParseResponse method
709 *
710 * @access public
711 * @return void
712 */
713 function testParseResponse() {
714 $this->Socket->reset();
715
716 $r = $this->Socket->parseResponse(array('foo' => 'bar'));
717 $this->assertIdentical($r, array('foo' => 'bar'));
718
719 $r = $this->Socket->parseResponse(true);
720 $this->assertIdentical($r, false);
721
722 $r = $this->Socket->parseResponse("HTTP Foo\r\nBar: La");
723 $this->assertIdentical($r, false);
724
725 $tests = array(
726 'simple-request' => array(
727 'response' => array(
728 'status-line' => "HTTP/1.x 200 OK\r\n",
729 'header' => "Date: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n",
730 'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>"
731 )
732 , 'expectations' => array(
733 'status.http-version' => 'HTTP/1.x',
734 'status.code' => 200,
735 'status.reason-phrase' => 'OK',
736 'header' => $this->Socket->parseHeader("Date: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n"),
737 'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>"
738 )
739 ),
740 'no-header' => array(
741 'response' => array(
742 'status-line' => "HTTP/1.x 404 OK\r\n",
743 'header' => null,
744 )
745 , 'expectations' => array(
746 'status.code' => 404,
747 'header' => array()
748 )
749 ),
750 'chunked' => array(
751 'response' => array(
752 'header' => "Transfer-Encoding: chunked\r\n",
753 'body' => "19\r\nThis is a chunked message\r\n0\r\n"
754 ),
755 'expectations' => array(
756 'body' => "This is a chunked message",
757 'header' => $this->Socket->parseHeader("Transfer-Encoding: chunked\r\n")
758 )
759 ),
760 'enitity-header' => array(
761 'response' => array(
762 'body' => "19\r\nThis is a chunked message\r\n0\r\nFoo: Bar\r\n"
763 ),
764 'expectations' => array(
765 'header' => $this->Socket->parseHeader("Transfer-Encoding: chunked\r\nFoo: Bar\r\n")
766 )
767 ),
768 'enitity-header-combine' => array(
769 'response' => array(
770 'header' => "Transfer-Encoding: chunked\r\nFoo: Foobar\r\n"
771 ),
772 'expectations' => array(
773 'header' => $this->Socket->parseHeader("Transfer-Encoding: chunked\r\nFoo: Foobar\r\nFoo: Bar\r\n")
774 )
775 )
776 );
777
778 $testResponse = array();
779 $expectations = array();
780
781 foreach ($tests as $name => $test) {
782
783 $testResponse = array_merge($testResponse, $test['response']);
784 $testResponse['response'] = $testResponse['status-line'].$testResponse['header']."\r\n".$testResponse['body'];
785 $r = $this->Socket->parseResponse($testResponse['response']);
786 $expectations = array_merge($expectations, $test['expectations']);
787
788 foreach ($expectations as $property => $expectedVal) {
789 $val = Set::extract($r, $property);
790 $this->assertIdentical($val, $expectedVal, 'Test "'.$name.'": response.'.$property.' - %s');
791 }
792
793 foreach (array('status-line', 'header', 'body', 'response') as $field) {
794 $this->assertIdentical($r['raw'][$field], $testResponse[$field], 'Test response.raw.'.$field.': %s');
795 }
796 }
797 }
798
799 /**
800 * testDecodeBody method
801 *
802 * @access public
803 * @return void
804 */
805 function testDecodeBody() {
806 $this->Socket->reset();
807
808 $r = $this->Socket->decodeBody(true);
809 $this->assertIdentical($r, false);
810
811 $r = $this->Socket->decodeBody('Foobar', false);
812 $this->assertIdentical($r, array('body' => 'Foobar', 'header' => false));
813
814 $encodings = array(
815 'chunked' => array(
816 'encoded' => "19\r\nThis is a chunked message\r\n0\r\n",
817 'decoded' => array('body' => "This is a chunked message", 'header' => false)
818 ),
819 'foo-coded' => array(
820 'encoded' => '!Foobar!',
821 'decoded' => array('body' => '!Foobar!', 'header' => false),
822 'error' => new PatternExpectation('/unknown encoding: foo-coded/i')
823 )
824 );
825
826 foreach ($encodings as $encoding => $sample) {
827 if (isset($sample['error'])) {
828 $this->expectError($sample['error']);
829 }
830
831 $r = $this->Socket->decodeBody($sample['encoded'], $encoding);
832 $this->assertIdentical($r, $sample['decoded']);
833
834 if (isset($sample['error'])) {
835 $this->Socket->quirksMode = true;
836 $r = $this->Socket->decodeBody($sample['encoded'], $encoding);
837 $this->assertIdentical($r, $sample['decoded']);
838 $this->Socket->quirksMode = false;
839 }
840 }
841 }
842
843 /**
844 * testDecodeChunkedBody method
845 *
846 * @access public
847 * @return void
848 */
849 function testDecodeChunkedBody() {
850 $this->Socket->reset();
851
852 $r = $this->Socket->decodeChunkedBody(true);
853 $this->assertIdentical($r, false);
854
855 $encoded = "19\r\nThis is a chunked message\r\n0\r\n";
856 $decoded = "This is a chunked message";
857 $r = $this->Socket->decodeChunkedBody($encoded);
858 $this->assertIdentical($r['body'], $decoded);
859 $this->assertIdentical($r['header'], false);
860
861 $encoded = "19 \r\nThis is a chunked message\r\n0\r\n";
862 $r = $this->Socket->decodeChunkedBody($encoded);
863 $this->assertIdentical($r['body'], $decoded);
864
865 $encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\n";
866 $decoded = "This is a chunked message\nThat is cool\n";
867 $r = $this->Socket->decodeChunkedBody($encoded);
868 $this->assertIdentical($r['body'], $decoded);
869 $this->assertIdentical($r['header'], false);
870
871 $encoded = "19\r\nThis is a chunked message\r\nE;foo-chunk=5\r\n\nThat is cool\n\r\n0\r\n";
872 $r = $this->Socket->decodeChunkedBody($encoded);
873 $this->assertIdentical($r['body'], $decoded);
874 $this->assertIdentical($r['header'], false);
875
876 $encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\nfoo-header: bar\r\ncake: PHP\r\n\r\n";
877 $r = $this->Socket->decodeChunkedBody($encoded);
878 $this->assertIdentical($r['body'], $decoded);
879 $this->assertIdentical($r['header'], array('Foo-Header' => 'bar', 'Cake' => 'PHP'));
880
881 $encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n";
882 $this->expectError(new PatternExpectation('/activate quirks mode/i'));
883 $r = $this->Socket->decodeChunkedBody($encoded);
884 $this->assertIdentical($r, false);
885
886 $this->Socket->quirksMode = true;
887 $r = $this->Socket->decodeChunkedBody($encoded);
888 $this->assertIdentical($r['body'], $decoded);
889 $this->assertIdentical($r['header'], false);
890
891 $encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\nfoo-header: bar\r\ncake: PHP\r\n\r\n";
892 $r = $this->Socket->decodeChunkedBody($encoded);
893 $this->assertIdentical($r['body'], $decoded);
894 $this->assertIdentical($r['header'], array('Foo-Header' => 'bar', 'Cake' => 'PHP'));
895 }
896
897 /**
898 * testBuildRequestLine method
899 *
900 * @access public
901 * @return void
902 */
903 function testBuildRequestLine() {
904 $this->Socket->reset();
905
906 $this->expectError(new PatternExpectation('/activate quirks mode/i'));
907 $r = $this->Socket->buildRequestLine('Foo');
908 $this->assertIdentical($r, false);
909
910 $this->Socket->quirksMode = true;
911 $r = $this->Socket->buildRequestLine('Foo');
912 $this->assertIdentical($r, 'Foo');
913 $this->Socket->quirksMode = false;
914
915 $r = $this->Socket->buildRequestLine(true);
916 $this->assertIdentical($r, false);
917
918 $r = $this->Socket->buildRequestLine(array('foo' => 'bar', 'method' => 'foo'));
919 $this->assertIdentical($r, false);
920
921 $r = $this->Socket->buildRequestLine(array('method' => 'GET', 'uri' => 'http://www.cakephp.org/search?q=socket'));
922 $this->assertIdentical($r, "GET /search?q=socket HTTP/1.1\r\n");
923
924 $request = array(
925 'method' => 'GET',
926 'uri' => array(
927 'path' => '/search',
928 'query' => array('q' => 'socket')
929 )
930 );
931 $r = $this->Socket->buildRequestLine($request);
932 $this->assertIdentical($r, "GET /search?q=socket HTTP/1.1\r\n");
933
934 unset($request['method']);
935 $r = $this->Socket->buildRequestLine($request);
936 $this->assertIdentical($r, "GET /search?q=socket HTTP/1.1\r\n");
937
938 $r = $this->Socket->buildRequestLine($request, 'CAKE-HTTP/0.1');
939 $this->assertIdentical($r, "GET /search?q=socket CAKE-HTTP/0.1\r\n");
940
941 $request = array('method' => 'OPTIONS', 'uri' => '*');
942 $r = $this->Socket->buildRequestLine($request);
943 $this->assertIdentical($r, "OPTIONS * HTTP/1.1\r\n");
944
945 $request['method'] = 'GET';
946 $this->expectError(new PatternExpectation('/activate quirks mode/i'));
947 $r = $this->Socket->buildRequestLine($request);
948 $this->assertIdentical($r, false);
949
950 $this->expectError(new PatternExpectation('/activate quirks mode/i'));
951 $r = $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
952 $this->assertIdentical($r, false);
953
954 $this->Socket->quirksMode = true;
955 $r = $this->Socket->buildRequestLine($request);
956 $this->assertIdentical($r, "GET * HTTP/1.1\r\n");
957
958 $r = $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
959 $this->assertIdentical($r, "GET * HTTP/1.1\r\n");
960 }
961
962 /**
963 * Asserts that HttpSocket::parseUri is working properly
964 *
965 * @access public
966 * @return void
967 */
968 function testParseUri() {
969 $this->Socket->reset();
970
971 $uri = $this->Socket->parseUri(array('invalid' => 'uri-string'));
972 $this->assertIdentical($uri, false);
973
974 $uri = $this->Socket->parseUri(array('invalid' => 'uri-string'), array('host' => 'somehost'));
975 $this->assertIdentical($uri, array('host' => 'somehost', 'invalid' => 'uri-string'));
976
977 $uri = $this->Socket->parseUri(false);
978 $this->assertIdentical($uri, false);
979
980 $uri = $this->Socket->parseUri('/my-cool-path');
981 $this->assertIdentical($uri, array('path' => '/my-cool-path'));
982
983 $uri = $this->Socket->parseUri('http://bob:foo123@www.cakephp.org:40/search?q=dessert#results');
984 $this->assertIdentical($uri, array(
985 'scheme' => 'http',
986 'host' => 'www.cakephp.org',
987 'port' => 40,
988 'user' => 'bob',
989 'pass' => 'foo123',
990 'path' => '/search',
991 'query' => array('q' => 'dessert'),
992 'fragment' => 'results'
993 ));
994
995 $uri = $this->Socket->parseUri('http://www.cakephp.org/');
996 $this->assertIdentical($uri, array(
997 'scheme' => 'http',
998 'host' => 'www.cakephp.org',
999 'path' => '/',
1000 ));
1001
1002 $uri = $this->Socket->parseUri('http://www.cakephp.org', true);
1003 $this->assertIdentical($uri, array(
1004 'scheme' => 'http',
1005 'host' => 'www.cakephp.org',
1006 'port' => 80,
1007 'user' => null,
1008 'pass' => null,
1009 'path' => '/',
1010 'query' => array(),
1011 'fragment' => null
1012 ));
1013
1014 $uri = $this->Socket->parseUri('https://www.cakephp.org', true);
1015 $this->assertIdentical($uri, array(
1016 'scheme' => 'https',
1017 'host' => 'www.cakephp.org',
1018 'port' => 443,
1019 'user' => null,
1020 'pass' => null,
1021 'path' => '/',
1022 'query' => array(),
1023 'fragment' => null
1024 ));
1025
1026 $uri = $this->Socket->parseUri('www.cakephp.org:443/query?foo', true);
1027 $this->assertIdentical($uri, array(
1028 'scheme' => 'https',
1029 'host' => 'www.cakephp.org',
1030 'port' => 443,
1031 'user' => null,
1032 'pass' => null,
1033 'path' => '/query',
1034 'query' => array('foo' => ""),
1035 'fragment' => null
1036 ));
1037
1038 $uri = $this->Socket->parseUri('http://www.cakephp.org', array('host' => 'piephp.org', 'user' => 'bob', 'fragment' => 'results'));
1039 $this->assertIdentical($uri, array(
1040 'host' => 'www.cakephp.org',
1041 'user' => 'bob',
1042 'fragment' => 'results',
1043 'scheme' => 'http'
1044 ));
1045
1046 $uri = $this->Socket->parseUri('https://www.cakephp.org', array('scheme' => 'http', 'port' => 23));
1047 $this->assertIdentical($uri, array(
1048 'scheme' => 'https',
1049 'port' => 23,
1050 'host' => 'www.cakephp.org'
1051 ));
1052
1053 $uri = $this->Socket->parseUri('www.cakephp.org:59', array('scheme' => array('http', 'https'), 'port' => 80));
1054 $this->assertIdentical($uri, array(
1055 'scheme' => 'http',
1056 'port' => 59,
1057 'host' => 'www.cakephp.org'
1058 ));
1059
1060 $uri = $this->Socket->parseUri(array('scheme' => 'http', 'host' => 'www.google.com', 'port' => 8080), array('scheme' => array('http', 'https'), 'host' => 'www.google.com', 'port' => array(80, 443)));
1061 $this->assertIdentical($uri, array(
1062 'scheme' => 'http',
1063 'host' => 'www.google.com',
1064 'port' => 8080,
1065 ));
1066
1067 $uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2%3Dvalue3');
1068 $this->assertIdentical($uri, array(
1069 'scheme' => 'http',
1070 'host' => 'www.cakephp.org',
1071 'path' => '/',
1072 'query' => array(
1073 'param1' => 'value1',
1074 'param2' => 'value2=value3'
1075 )
1076 ));
1077
1078 $uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2=value3');
1079 $this->assertIdentical($uri, array(
1080 'scheme' => 'http',
1081 'host' => 'www.cakephp.org',
1082 'path' => '/',
1083 'query' => array(
1084 'param1' => 'value1',
1085 'param2' => 'value2=value3'
1086 )
1087 ));
1088 }
1089
1090 /**
1091 * Tests that HttpSocket::buildUri can turn all kinds of uri arrays (and strings) into fully or partially qualified URI's
1092 *
1093 * @access public
1094 * @return void
1095 */
1096 function testBuildUri() {
1097 $this->Socket->reset();
1098
1099 $r = $this->Socket->buildUri(true);
1100 $this->assertIdentical($r, false);
1101
1102 $r = $this->Socket->buildUri('foo.com');
1103 $this->assertIdentical($r, 'http://foo.com/');
1104
1105 $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'));
1106 $this->assertIdentical($r, 'http://www.cakephp.org/');
1107
1108 $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https'));
1109 $this->assertIdentical($r, 'https://www.cakephp.org/');
1110
1111 $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'port' => 23));
1112 $this->assertIdentical($r, 'http://www.cakephp.org:23/');
1113
1114 $r = $this->Socket->buildUri(array('path' => 'www.google.com/search', 'query' => 'q=cakephp'));
1115 $this->assertIdentical($r, 'http://www.google.com/search?q=cakephp');
1116
1117 $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https', 'port' => 79));
1118 $this->assertIdentical($r, 'https://www.cakephp.org:79/');
1119
1120 $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => 'foo'));
1121 $this->assertIdentical($r, 'http://www.cakephp.org/foo');
1122
1123 $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/foo'));
1124 $this->assertIdentical($r, 'http://www.cakephp.org/foo');
1125
1126 $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/search', 'query' => array('q' => 'HttpSocket')));
1127 $this->assertIdentical($r, 'http://www.cakephp.org/search?q=HttpSocket');
1128
1129 $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'));
1130 $this->assertIdentical($r, 'http://www.cakephp.org/#bar');
1131
1132 $r = $this->Socket->buildUri(array(
1133 'scheme' => 'https',
1134 'host' => 'www.cakephp.org',
1135 'port' => 25,
1136 'user' => 'bob',
1137 'pass' => 'secret',
1138 'path' => '/cool',
1139 'query' => array('foo' => 'bar'),
1140 'fragment' => 'comment'
1141 ));
1142 $this->assertIdentical($r, 'https://bob:secret@www.cakephp.org:25/cool?foo=bar#comment');
1143
1144 $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'), '%fragment?%host');
1145 $this->assertIdentical($r, 'bar?www.cakephp.org');
1146
1147 $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'), '%fragment???%host');
1148 $this->assertIdentical($r, '???www.cakephp.org');
1149
1150 $r = $this->Socket->buildUri(array('path' => '*'), '/%path?%query');
1151 $this->assertIdentical($r, '*');
1152
1153 $r = $this->Socket->buildUri(array('scheme' => 'foo', 'host' => 'www.cakephp.org'));
1154 $this->assertIdentical($r, 'foo://www.cakephp.org:80/');
1155 }
1156
1157 /**
1158 * Asserts that HttpSocket::parseQuery is working properly
1159 *
1160 * @access public
1161 * @return void
1162 */
1163 function testParseQuery() {
1164 $this->Socket->reset();
1165
1166 $query = $this->Socket->parseQuery(array('framework' => 'cakephp'));
1167 $this->assertIdentical($query, array('framework' => 'cakephp'));
1168
1169 $query = $this->Socket->parseQuery('');
1170 $this->assertIdentical($query, array());
1171
1172 $query = $this->Socket->parseQuery('framework=cakephp');
1173 $this->assertIdentical($query, array('framework' => 'cakephp'));
1174
1175 $query = $this->Socket->parseQuery('?framework=cakephp');
1176 $this->assertIdentical($query, array('framework' => 'cakephp'));
1177
1178 $query = $this->Socket->parseQuery('a&b&c');
1179 $this->assertIdentical($query, array('a' => '', 'b' => '', 'c' => ''));
1180
1181 $query = $this->Socket->parseQuery('value=12345');
1182 $this->assertIdentical($query, array('value' => '12345'));
1183
1184 $query = $this->Socket->parseQuery('a[0]=foo&a[1]=bar&a[2]=cake');
1185 $this->assertIdentical($query, array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')));
1186
1187 $query = $this->Socket->parseQuery('a[]=foo&a[]=bar&a[]=cake');
1188 $this->assertIdentical($query, array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')));
1189
1190 $query = $this->Socket->parseQuery('a]][[=foo&[]=bar&]]][]=cake');
1191 $this->assertIdentical($query, array('a]][[' => 'foo', 0 => 'bar', ']]]' => array('cake')));
1192
1193 $query = $this->Socket->parseQuery('a[][]=foo&a[][]=bar&a[][]=cake');
1194 $expectedQuery = array(
1195 'a' => array(
1196 0 => array(
1197 0 => 'foo'
1198 ),
1199 1 => array(
1200 0 => 'bar'
1201 ),
1202 array(
1203 0 => 'cake'
1204 )
1205 )
1206 );
1207 $this->assertIdentical($query, $expectedQuery);
1208
1209 $query = $this->Socket->parseQuery('a[][]=foo&a[bar]=php&a[][]=bar&a[][]=cake');
1210 $expectedQuery = array(
1211 'a' => array(
1212 0 => array(
1213 0 => 'foo'
1214 ),
1215 'bar' => 'php',
1216 1 => array(
1217 0 => 'bar'
1218 ),
1219 array(
1220 0 => 'cake'
1221 )
1222 )
1223 );
1224 $this->assertIdentical($query, $expectedQuery);
1225
1226 $query = $this->Socket->parseQuery('user[]=jim&user[3]=tom&user[]=bob');
1227 $expectedQuery = array(
1228 'user' => array(
1229 0 => 'jim',
1230 3 => 'tom',
1231 4 => 'bob'
1232 )
1233 );
1234 $this->assertIdentical($query, $expectedQuery);
1235
1236 $queryStr = 'user[0]=foo&user[0][items][]=foo&user[0][items][]=bar&user[][name]=jim&user[1][items][personal][]=book&user[1][items][personal][]=pen&user[1][items][]=ball&user[count]=2&empty';
1237 $query = $this->Socket->parseQuery($queryStr);
1238 $expectedQuery = array(
1239 'user' => array(
1240 0 => array(
1241 'items' => array(
1242 'foo',
1243 'bar'
1244 )
1245 ),
1246 1 => array(
1247 'name' => 'jim',
1248 'items' => array(
1249 'personal' => array(
1250 'book'
1251 , 'pen'
1252 ),
1253 'ball'
1254 )
1255 ),
1256 'count' => '2'
1257 ),
1258 'empty' => ''
1259 );
1260 $this->assertIdentical($query, $expectedQuery);
1261 }
1262
1263 /**
1264 * Tests that HttpSocket::buildHeader can turn a given $header array into a proper header string according to
1265 * HTTP 1.1 specs.
1266 *
1267 * @access public
1268 * @return void
1269 */
1270 function testBuildHeader() {
1271 $this->Socket->reset();
1272
1273 $r = $this->Socket->buildHeader(true);
1274 $this->assertIdentical($r, false);
1275
1276 $r = $this->Socket->buildHeader('My raw header');
1277 $this->assertIdentical($r, 'My raw header');
1278
1279 $r = $this->Socket->buildHeader(array('Host' => 'www.cakephp.org'));
1280 $this->assertIdentical($r, "Host: www.cakephp.org\r\n");
1281
1282 $r = $this->Socket->buildHeader(array('Host' => 'www.cakephp.org', 'Connection' => 'Close'));
1283 $this->assertIdentical($r, "Host: www.cakephp.org\r\nConnection: Close\r\n");
1284
1285 $r = $this->Socket->buildHeader(array('People' => array('Bob', 'Jim', 'John')));
1286 $this->assertIdentical($r, "People: Bob,Jim,John\r\n");
1287
1288 $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\nMulti Line field"));
1289 $this->assertIdentical($r, "Multi-Line-Field: This is my\r\n Multi Line field\r\n");
1290
1291 $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\n Multi Line field"));
1292 $this->assertIdentical($r, "Multi-Line-Field: This is my\r\n Multi Line field\r\n");
1293
1294 $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\n\tMulti Line field"));
1295 $this->assertIdentical($r, "Multi-Line-Field: This is my\r\n\tMulti Line field\r\n");
1296
1297 $r = $this->Socket->buildHeader(array('Test@Field' => "My value"));
1298 $this->assertIdentical($r, "Test\"@\"Field: My value\r\n");
1299
1300 }
1301
1302 /**
1303 * Test that HttpSocket::parseHeader can take apart a given (and valid) $header string and turn it into an array.
1304 *
1305 * @access public
1306 * @return void
1307 */
1308 function testParseHeader() {
1309 $this->Socket->reset();
1310
1311 $r = $this->Socket->parseHeader(array('foo' => 'Bar', 'fOO-bAr' => 'quux'));
1312 $this->assertIdentical($r, array('Foo' => 'Bar', 'Foo-Bar' => 'quux'));
1313
1314 $r = $this->Socket->parseHeader(true);
1315 $this->assertIdentical($r, false);
1316
1317 $header = "Host: cakephp.org\t\r\n";
1318 $r = $this->Socket->parseHeader($header);
1319 $expected = array(
1320 'Host' => 'cakephp.org'
1321 );
1322 $this->assertIdentical($r, $expected);
1323
1324 $header = "Date:Sat, 07 Apr 2007 10:10:25 GMT\r\nX-Powered-By: PHP/5.1.2\r\n";
1325 $r = $this->Socket->parseHeader($header);
1326 $expected = array(
1327 'Date' => 'Sat, 07 Apr 2007 10:10:25 GMT'
1328 , 'X-Powered-By' => 'PHP/5.1.2'
1329 );
1330 $this->assertIdentical($r, $expected);
1331
1332 $header = "people: Jim,John\r\nfoo-LAND: Bar\r\ncAKe-PHP: rocks\r\n";
1333 $r = $this->Socket->parseHeader($header);
1334 $expected = array(
1335 'People' => 'Jim,John'
1336 , 'Foo-Land' => 'Bar'
1337 , 'Cake-Php' => 'rocks'
1338 );
1339 $this->assertIdentical($r, $expected);
1340
1341 $header = "People: Jim,John,Tim\r\nPeople: Lisa,Tina,Chelsea\r\n";
1342 $r = $this->Socket->parseHeader($header);
1343 $expected = array(
1344 'People' => array('Jim,John,Tim', 'Lisa,Tina,Chelsea')
1345 );
1346 $this->assertIdentical($r, $expected);
1347
1348 $header = "Multi-Line: I am a \r\nmulti line\t\r\nfield value.\r\nSingle-Line: I am not\r\n";
1349 $r = $this->Socket->parseHeader($header);
1350 $expected = array(
1351 'Multi-Line' => "I am a\r\nmulti line\r\nfield value."
1352 , 'Single-Line' => 'I am not'
1353 );
1354 $this->assertIdentical($r, $expected);
1355
1356 $header = "Esc\"@\"ped: value\r\n";
1357 $r = $this->Socket->parseHeader($header);
1358 $expected = array(
1359 'Esc@ped' => 'value'
1360 );
1361 $this->assertIdentical($r, $expected);
1362 }
1363
1364 /**
1365 * testParseCookies method
1366 *
1367 * @access public
1368 * @return void
1369 */
1370 function testParseCookies() {
1371 $header = array(
1372 'Set-Cookie' => array(
1373 'foo=bar',
1374 'people=jim,jack,johnny";";Path=/accounts',
1375 'google=not=nice'
1376 ),
1377 'Transfer-Encoding' => 'chunked',
1378 'Date' => 'Sun, 18 Nov 2007 18:57:42 GMT',
1379 );
1380 $cookies = $this->Socket->parseCookies($header);
1381 $expected = array(
1382 'foo' => array(
1383 'value' => 'bar'
1384 ),
1385 'people' => array(
1386 'value' => 'jim,jack,johnny";"',
1387 'path' => '/accounts',
1388 ),
1389 'google' => array(
1390 'value' => 'not=nice',
1391 )
1392 );
1393 $this->assertEqual($cookies, $expected);
1394
1395 $header['Set-Cookie'][] = 'cakephp=great; Secure';
1396 $expected['cakephp'] = array('value' => 'great', 'secure' => true);
1397 $cookies = $this->Socket->parseCookies($header);
1398 $this->assertEqual($cookies, $expected);
1399
1400 $header['Set-Cookie'] = 'foo=bar';
1401 unset($expected['people'], $expected['cakephp'], $expected['google']);
1402 $cookies = $this->Socket->parseCookies($header);
1403 $this->assertEqual($cookies, $expected);
1404 }
1405
1406 /**
1407 * testBuildCookies method
1408 *
1409 * @return void
1410 * @access public
1411 * @todo Test more scenarios
1412 */
1413 function testBuildCookies() {
1414 $cookies = array(
1415 'foo' => array(
1416 'value' => 'bar'
1417 ),
1418 'people' => array(
1419 'value' => 'jim,jack,johnny;',
1420 'path' => '/accounts'
1421 )
1422 );
1423 $expect = "Cookie: foo=bar; people=jim,jack,johnny\";\"\r\n";
1424 $result = $this->Socket->buildCookies($cookies);
1425 $this->assertEqual($result, $expect);
1426 }
1427
1428 /**
1429 * Tests that HttpSocket::_tokenEscapeChars() returns the right characters.
1430 *
1431 * @access public
1432 * @return void
1433 */
1434 function testTokenEscapeChars() {
1435 $this->Socket->reset();
1436
1437 $expected = array(
1438 '\x22','\x28','\x29','\x3c','\x3e','\x40','\x2c','\x3b','\x3a','\x5c','\x2f','\x5b','\x5d','\x3f','\x3d','\x7b',
1439 '\x7d','\x20','\x00','\x01','\x02','\x03','\x04','\x05','\x06','\x07','\x08','\x09','\x0a','\x0b','\x0c','\x0d',
1440 '\x0e','\x0f','\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1a','\x1b','\x1c','\x1d',
1441 '\x1e','\x1f','\x7f'
1442 );
1443 $r = $this->Socket->tokenEscapeChars();
1444 $this->assertEqual($r, $expected);
1445
1446 foreach ($expected as $key => $char) {
1447 $expected[$key] = chr(hexdec(substr($char, 2)));
1448 }
1449
1450 $r = $this->Socket->tokenEscapeChars(false);
1451 $this->assertEqual($r, $expected);
1452 }
1453
1454 /**
1455 * Test that HttpSocket::escapeToken is escaping all characters as descriped in RFC 2616 (HTTP 1.1 specs)
1456 *
1457 * @access public
1458 * @return void
1459 */
1460 function testEscapeToken() {
1461 $this->Socket->reset();
1462
1463 $this->assertIdentical($this->Socket->escapeToken('Foo'), 'Foo');
1464
1465 $escape = $this->Socket->tokenEscapeChars(false);
1466 foreach ($escape as $char) {
1467 $token = 'My-special-'.$char.'-Token';
1468 $escapedToken = $this->Socket->escapeToken($token);
1469 $expectedToken = 'My-special-"'.$char.'"-Token';
1470
1471 $this->assertIdentical($escapedToken, $expectedToken, 'Test token escaping for ASCII '.ord($char));
1472 }
1473
1474 $token = 'Extreme-:Token- -"@-test';
1475 $escapedToken = $this->Socket->escapeToken($token);
1476 $expectedToken = 'Extreme-":"Token-" "-""""@"-test';
1477 $this->assertIdentical($expectedToken, $escapedToken);
1478 }
1479
1480 /**
1481 * Test that escaped token strings are properly unescaped by HttpSocket::unescapeToken
1482 *
1483 * @access public
1484 * @return void
1485 */
1486 function testUnescapeToken() {
1487 $this->Socket->reset();
1488
1489 $this->assertIdentical($this->Socket->unescapeToken('Foo'), 'Foo');
1490
1491 $escape = $this->Socket->tokenEscapeChars(false);
1492 foreach ($escape as $char) {
1493 $token = 'My-special-"'.$char.'"-Token';
1494 $unescapedToken = $this->Socket->unescapeToken($token);
1495 $expectedToken = 'My-special-'.$char.'-Token';
1496
1497 $this->assertIdentical($unescapedToken, $expectedToken, 'Test token unescaping for ASCII '.ord($char));
1498 }
1499
1500 $token = 'Extreme-":"Token-" "-""""@"-test';
1501 $escapedToken = $this->Socket->unescapeToken($token);
1502 $expectedToken = 'Extreme-:Token- -"@-test';
1503 $this->assertIdentical($expectedToken, $escapedToken);
1504 }
1505
1506 /**
1507 * This tests asserts HttpSocket::reset() resets a HttpSocket instance to it's initial state (before Object::__construct
1508 * got executed)
1509 *
1510 * @access public
1511 * @return void
1512 */
1513 function testReset() {
1514 $this->Socket->reset();
1515
1516 $initialState = get_class_vars('HttpSocket');
1517 foreach ($initialState as $property => $value) {
1518 $this->Socket->{$property} = 'Overwritten';
1519 }
1520
1521 $return = $this->Socket->reset();
1522
1523 foreach ($initialState as $property => $value) {
1524 $this->assertIdentical($this->Socket->{$property}, $value);
1525 }
1526
1527 $this->assertIdentical($return, true);
1528 }
1529
1530 /**
1531 * This tests asserts HttpSocket::reset(false) resets certain HttpSocket properties to their initial state (before
1532 * Object::__construct got executed).
1533 *
1534 * @access public
1535 * @return void
1536 */
1537 function testPartialReset() {
1538 $this->Socket->reset();
1539
1540 $partialResetProperties = array('request', 'response');
1541 $initialState = get_class_vars('HttpSocket');
1542
1543 foreach ($initialState as $property => $value) {
1544 $this->Socket->{$property} = 'Overwritten';
1545 }
1546
1547 $return = $this->Socket->reset(false);
1548
1549 foreach ($initialState as $property => $originalValue) {
1550 if (in_array($property, $partialResetProperties)) {
1551 $this->assertIdentical($this->Socket->{$property}, $originalValue);
1552 } else {
1553 $this->assertIdentical($this->Socket->{$property}, 'Overwritten');
1554 }
1555 }
1556 $this->assertIdentical($return, true);
1557 }
1558 }