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