PageRenderTime 58ms CodeModel.GetById 2ms app.highlight 45ms RepoModel.GetById 1ms app.codeStats 1ms

/tirecore/lib/Cake/Test/Case/Network/Http/HttpSocketTest.php

https://bitbucket.org/browsewire/symfony-project-code
PHP | 1721 lines | 1187 code | 208 blank | 326 comment | 6 complexity | 09437d75d686338f84ebe5938cb72a74 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/2.0/en/development/testing.html>
   8 * Copyright 2005-2012, 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-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14 * @link          http://book.cakephp.org/2.0/en/development/testing.html 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 string|array $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|array $uri URI to parse
  98 * @param boolean|array $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 formatted 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 string|array $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['request']['uri']['scheme'] = 'http';
 234		$baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);
 235		$this->assertEquals($this->Socket->config, $baseConfig);
 236
 237		$this->Socket->reset();
 238		$this->Socket->__construct(array('request' => array('uri' => 'http://www.cakephp.org:23/')));
 239		$this->assertEquals($this->Socket->config, $baseConfig);
 240	}
 241
 242/**
 243 * Test that HttpSocket::configUri works properly with different types of arguments
 244 *
 245 * @return void
 246 */
 247	public function testConfigUri() {
 248		$this->Socket->reset();
 249		$r = $this->Socket->configUri('https://bob:secret@www.cakephp.org:23/?query=foo');
 250		$expected = array(
 251			'persistent' => false,
 252			'host' => 'www.cakephp.org',
 253			'protocol' => 'tcp',
 254			'port' => 23,
 255			'timeout' => 30,
 256			'ssl_verify_peer' => true,
 257			'ssl_verify_depth' => 5,
 258			'ssl_verify_host' => true,
 259			'request' => array(
 260				'uri' => array(
 261					'scheme' => 'https',
 262					'host' => 'www.cakephp.org',
 263					'port' => 23
 264				),
 265				'redirect' => false,
 266				'cookies' => array(),
 267			)
 268		);
 269		$this->assertEquals($expected, $this->Socket->config);
 270		$this->assertTrue($r);
 271		$r = $this->Socket->configUri(array('host' => 'www.foo-bar.org'));
 272		$expected['host'] = 'www.foo-bar.org';
 273		$expected['request']['uri']['host'] = 'www.foo-bar.org';
 274		$this->assertEquals($expected, $this->Socket->config);
 275		$this->assertTrue($r);
 276
 277		$r = $this->Socket->configUri('http://www.foo.com');
 278		$expected = array(
 279			'persistent' => false,
 280			'host' => 'www.foo.com',
 281			'protocol' => 'tcp',
 282			'port' => 80,
 283			'timeout' => 30,
 284			'ssl_verify_peer' => true,
 285			'ssl_verify_depth' => 5,
 286			'ssl_verify_host' => true,
 287			'request' => array(
 288				'uri' => array(
 289					'scheme' => 'http',
 290					'host' => 'www.foo.com',
 291					'port' => 80
 292				),
 293				'redirect' => false,
 294				'cookies' => array(),
 295			)
 296		);
 297		$this->assertEquals($expected, $this->Socket->config);
 298		$this->assertTrue($r);
 299
 300		$r = $this->Socket->configUri('/this-is-broken');
 301		$this->assertEquals($expected, $this->Socket->config);
 302		$this->assertFalse($r);
 303
 304		$r = $this->Socket->configUri(false);
 305		$this->assertEquals($expected, $this->Socket->config);
 306		$this->assertFalse($r);
 307	}
 308
 309/**
 310 * Tests that HttpSocket::request (the heart of the HttpSocket) is working properly.
 311 *
 312 * @return void
 313 */
 314	public function testRequest() {
 315		$this->Socket->reset();
 316
 317		$response = $this->Socket->request(true);
 318		$this->assertFalse($response);
 319
 320		$context = array(
 321			'ssl' => array(
 322				'verify_peer' => true,
 323				'verify_depth' => 5,
 324				'CN_match' => 'www.cakephp.org',
 325				'cafile' => CAKE . 'Config' . DS . 'cacert.pem'
 326			)
 327		);
 328
 329		$tests = array(
 330			array(
 331				'request' => 'http://www.cakephp.org/?foo=bar',
 332				'expectation' => array(
 333					'config' => array(
 334						'persistent' => false,
 335						'host' => 'www.cakephp.org',
 336						'protocol' => 'tcp',
 337						'port' => 80,
 338						'timeout' => 30,
 339						'context' => $context,
 340						'request' => array(
 341							'uri' => array(
 342								'scheme' => 'http',
 343								'host' => 'www.cakephp.org',
 344								'port' => 80
 345							),
 346							'redirect' => false,
 347							'cookies' => array()
 348						)
 349					),
 350					'request' => array(
 351						'method' => 'GET',
 352						'uri' => array(
 353							'scheme' => 'http',
 354							'host' => 'www.cakephp.org',
 355							'port' => 80,
 356							'user' => null,
 357							'pass' => null,
 358							'path' => '/',
 359							'query' => array('foo' => 'bar'),
 360							'fragment' => null
 361						),
 362						'version' => '1.1',
 363						'body' => '',
 364						'line' => "GET /?foo=bar HTTP/1.1\r\n",
 365						'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n",
 366						'raw' => "",
 367						'redirect' => false,
 368						'cookies' => array(),
 369						'proxy' => array(),
 370						'auth' => array()
 371					)
 372				)
 373			),
 374			array(
 375				'request' => array(
 376					'uri' => array(
 377						'host' => 'www.cakephp.org',
 378						'query' => '?foo=bar'
 379					)
 380				)
 381			),
 382			array(
 383				'request' => 'www.cakephp.org/?foo=bar'
 384			),
 385			array(
 386				'request' => array(
 387					'host' => '192.168.0.1',
 388					'uri' => 'http://www.cakephp.org/?foo=bar'
 389				),
 390				'expectation' => array(
 391					'request' => array(
 392						'uri' => array('host' => 'www.cakephp.org')
 393					),
 394					'config' => array(
 395						'request' => array(
 396							'uri' => array('host' => 'www.cakephp.org')
 397						),
 398						'host' => '192.168.0.1'
 399					)
 400				)
 401			),
 402			'reset4' => array(
 403				'request.uri.query' => array()
 404			),
 405			array(
 406				'request' => array(
 407					'header' => array('Foo@woo' => 'bar-value')
 408				),
 409				'expectation' => array(
 410					'request' => array(
 411						'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nFoo\"@\"woo: bar-value\r\n",
 412						'line' => "GET / HTTP/1.1\r\n"
 413					)
 414				)
 415			),
 416			array(
 417				'request' => array('header' => array('Foo@woo' => 'bar-value', 'host' => 'foo.com'), 'uri' => 'http://www.cakephp.org/'),
 418				'expectation' => array(
 419					'request' => array(
 420						'header' => "Host: foo.com\r\nConnection: close\r\nUser-Agent: CakePHP\r\nFoo\"@\"woo: bar-value\r\n"
 421					),
 422					'config' => array(
 423						'host' => 'www.cakephp.org'
 424					)
 425				)
 426			),
 427			array(
 428				'request' => array('header' => "Foo: bar\r\n"),
 429				'expectation' => array(
 430					'request' => array(
 431						'header' => "Foo: bar\r\n"
 432					)
 433				)
 434			),
 435			array(
 436				'request' => array('header' => "Foo: bar\r\n", 'uri' => 'http://www.cakephp.org/search?q=http_socket#ignore-me'),
 437				'expectation' => array(
 438					'request' => array(
 439						'uri' => array(
 440							'path' => '/search',
 441							'query' => array('q' => 'http_socket'),
 442							'fragment' => 'ignore-me'
 443						),
 444						'line' => "GET /search?q=http_socket HTTP/1.1\r\n"
 445					)
 446				)
 447			),
 448			'reset8' => array(
 449				'request.uri.query' => array()
 450			),
 451			array(
 452				'request' => array(
 453					'method' => 'POST',
 454					'uri' => 'http://www.cakephp.org/posts/add',
 455					'body' => array(
 456						'name' => 'HttpSocket-is-released',
 457						'date' => 'today'
 458					)
 459				),
 460				'expectation' => array(
 461					'request' => array(
 462						'method' => 'POST',
 463						'uri' => array(
 464							'path' => '/posts/add',
 465							'fragment' => null
 466						),
 467						'body' => "name=HttpSocket-is-released&date=today",
 468						'line' => "POST /posts/add HTTP/1.1\r\n",
 469						'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",
 470						'raw' => "name=HttpSocket-is-released&date=today"
 471					)
 472				)
 473			),
 474			array(
 475				'request' => array(
 476					'method' => 'POST',
 477					'uri' => 'http://www.cakephp.org:8080/posts/add',
 478					'body' => array(
 479						'name' => 'HttpSocket-is-released',
 480						'date' => 'today'
 481					)
 482				),
 483				'expectation' => array(
 484					'config' => array(
 485						'port' => 8080,
 486						'request' => array(
 487							'uri' => array(
 488								'port' => 8080
 489							)
 490						)
 491					),
 492					'request' => array(
 493						'uri' => array(
 494							'port' => 8080
 495						),
 496						'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"
 497					)
 498				)
 499			),
 500			array(
 501				'request' => array(
 502					'method' => 'POST',
 503					'uri' => 'https://www.cakephp.org/posts/add',
 504					'body' => array(
 505						'name' => 'HttpSocket-is-released',
 506						'date' => 'today'
 507					)
 508				),
 509				'expectation' => array(
 510					'config' => array(
 511						'port' => 443,
 512						'request' => array(
 513							'uri' => array(
 514								'scheme' => 'https',
 515								'port' => 443
 516							)
 517						)
 518					),
 519					'request' => array(
 520						'uri' => array(
 521							'scheme' => 'https',
 522							'port' => 443
 523						),
 524						'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"
 525					)
 526				)
 527			),
 528			array(
 529				'request' => array(
 530					'method' => 'POST',
 531					'uri' => 'https://www.cakephp.org/posts/add',
 532					'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'),
 533					'cookies' => array('foo' => array('value' => 'bar'))
 534				),
 535				'expectation' => array(
 536					'request' => array(
 537						'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",
 538						'cookies' => array(
 539							'foo' => array('value' => 'bar'),
 540						)
 541					)
 542				)
 543			)
 544		);
 545
 546		$expectation = array();
 547		foreach ($tests as $i => $test) {
 548			if (strpos($i, 'reset') === 0) {
 549				foreach ($test as $path => $val) {
 550					$expectation = Hash::insert($expectation, $path, $val);
 551				}
 552				continue;
 553			}
 554
 555			if (isset($test['expectation'])) {
 556				$expectation = Hash::merge($expectation, $test['expectation']);
 557			}
 558			$this->Socket->request($test['request']);
 559
 560			$raw = $expectation['request']['raw'];
 561			$expectation['request']['raw'] = $expectation['request']['line'] . $expectation['request']['header'] . "\r\n" . $raw;
 562
 563			$r = array('config' => $this->Socket->config, 'request' => $this->Socket->request);
 564			$this->assertEquals($r, $expectation, 'Failed test #' . $i . ' ');
 565			$expectation['request']['raw'] = $raw;
 566		}
 567
 568		$this->Socket->reset();
 569		$request = array('method' => 'POST', 'uri' => 'http://www.cakephp.org/posts/add', 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'));
 570		$response = $this->Socket->request($request);
 571		$this->assertEquals("name=HttpSocket-is-released&date=today", $this->Socket->request['body']);
 572	}
 573
 574/**
 575 * Test the scheme + port keys
 576 *
 577 * @return void
 578 */
 579	public function testGetWithSchemeAndPort() {
 580		$this->Socket->reset();
 581		$request = array(
 582			'uri' => array(
 583				'scheme' => 'http',
 584				'host' => 'cakephp.org',
 585				'port' => 8080,
 586				'path' => '/',
 587			),
 588			'method' => 'GET'
 589		);
 590		$this->Socket->request($request);
 591		$this->assertContains('Host: cakephp.org:8080', $this->Socket->request['header']);
 592	}
 593
 594/**
 595 * Test urls like http://cakephp.org/index.php?somestring without key/value pair for query
 596 *
 597 * @return void
 598 */
 599	public function testRequestWithStringQuery() {
 600		$this->Socket->reset();
 601		$request = array(
 602			'uri' => array(
 603				'scheme' => 'http',
 604				'host' => 'cakephp.org',
 605				'path' => 'index.php',
 606				'query' => 'somestring'
 607			),
 608			'method' => 'GET'
 609		);
 610		$this->Socket->request($request);
 611		$this->assertContains("GET /index.php?somestring HTTP/1.1", $this->Socket->request['line']);
 612	}
 613
 614/**
 615 * The "*" asterisk character is only allowed for the following methods: OPTIONS.
 616 *
 617 * @expectedException SocketException
 618 * @return void
 619 */
 620	public function testRequestNotAllowedUri() {
 621		$this->Socket->reset();
 622		$request = array('uri' => '*', 'method' => 'GET');
 623		$this->Socket->request($request);
 624	}
 625
 626/**
 627 * testRequest2 method
 628 *
 629 * @return void
 630 */
 631	public function testRequest2() {
 632		$this->Socket->reset();
 633		$request = array('uri' => 'htpp://www.cakephp.org/');
 634		$number = mt_rand(0, 9999999);
 635		$this->Socket->expects($this->once())->method('connect')->will($this->returnValue(true));
 636		$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>";
 637		$this->Socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
 638		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
 639		$this->Socket->expects($this->once())->method('write')
 640			->with("GET / HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n\r\n");
 641		$response = (string)$this->Socket->request($request);
 642		$this->assertEquals($response, "<h1>Hello, your lucky number is " . $number . "</h1>");
 643	}
 644
 645/**
 646 * testRequest3 method
 647 *
 648 * @return void
 649 */
 650	public function testRequest3() {
 651		$request = array('uri' => 'htpp://www.cakephp.org/');
 652		$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>";
 653		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
 654		$this->Socket->connected = true;
 655		$this->Socket->request($request);
 656		$result = $this->Socket->response['cookies'];
 657		$expect = array(
 658			'foo' => array(
 659				'value' => 'bar'
 660			)
 661		);
 662		$this->assertEquals($expect, $result);
 663		$this->assertEquals($this->Socket->config['request']['cookies']['www.cakephp.org'], $expect);
 664		$this->assertFalse($this->Socket->connected);
 665	}
 666
 667/**
 668 * testRequestWithConstructor method
 669 *
 670 * @return void
 671 */
 672	public function testRequestWithConstructor() {
 673		$request = array(
 674			'request' => array(
 675				'uri' => array(
 676					'scheme' => 'http',
 677					'host' => 'localhost',
 678					'port' => '5984',
 679					'user' => null,
 680					'pass' => null
 681				)
 682			)
 683		);
 684		$http = new MockHttpSocketRequests($request);
 685
 686		$expected = array('method' => 'GET', 'uri' => '/_test');
 687		$http->expects($this->at(0))->method('request')->with($expected);
 688		$http->get('/_test');
 689
 690		$expected = array('method' => 'GET', 'uri' => 'http://localhost:5984/_test?count=4');
 691		$http->expects($this->at(0))->method('request')->with($expected);
 692		$http->get('/_test', array('count' => 4));
 693	}
 694
 695/**
 696 * testRequestWithResource
 697 *
 698 * @return void
 699 */
 700	public function testRequestWithResource() {
 701		$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>";
 702		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
 703		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
 704		$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse));
 705		$this->Socket->connected = true;
 706
 707		$f = fopen(TMP . 'download.txt', 'w');
 708		if (!$f) {
 709			$this->markTestSkipped('Can not write in TMP directory.');
 710		}
 711
 712		$this->Socket->setContentResource($f);
 713		$result = (string)$this->Socket->request('http://www.cakephp.org/');
 714		$this->assertEquals('', $result);
 715		$this->assertEquals('CakeHttp Server', $this->Socket->response['header']['Server']);
 716		fclose($f);
 717		$this->assertEquals(file_get_contents(TMP . 'download.txt'), '<h1>This is a test!</h1>');
 718		unlink(TMP . 'download.txt');
 719
 720		$this->Socket->setContentResource(false);
 721		$result = (string)$this->Socket->request('http://www.cakephp.org/');
 722		$this->assertEquals('<h1>This is a test!</h1>', $result);
 723	}
 724
 725/**
 726 * testRequestWithCrossCookie
 727 *
 728 * @return void
 729 */
 730	public function testRequestWithCrossCookie() {
 731		$this->Socket->connected = true;
 732		$this->Socket->config['request']['cookies'] = array();
 733
 734		$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>";
 735		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
 736		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
 737		$expected = array('www.cakephp.org' => array('foo' => array('value' => 'bar')));
 738		$this->Socket->request('http://www.cakephp.org/');
 739		$this->assertEquals($expected, $this->Socket->config['request']['cookies']);
 740
 741		$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>";
 742		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
 743		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
 744		$this->Socket->request('http://www.cakephp.org/other');
 745		$this->assertEquals(array('foo' => array('value' => 'bar')), $this->Socket->request['cookies']);
 746		$expected['www.cakephp.org'] += array('bar' => array('value' => 'foo'));
 747		$this->assertEquals($expected, $this->Socket->config['request']['cookies']);
 748
 749		$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>";
 750		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
 751		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
 752		$this->Socket->request('/other2');
 753		$this->assertEquals($expected, $this->Socket->config['request']['cookies']);
 754
 755		$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>";
 756		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
 757		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
 758		$this->Socket->request('http://www.cake.com');
 759		$this->assertTrue(empty($this->Socket->request['cookies']));
 760		$expected['www.cake.com'] = array('foobar' => array('value' => 'ok'));
 761		$this->assertEquals($expected, $this->Socket->config['request']['cookies']);
 762	}
 763
 764/**
 765 * testRequestCustomResponse
 766 *
 767 * @return void
 768 */
 769	public function testRequestCustomResponse() {
 770		$this->Socket->connected = true;
 771		$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>";
 772		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
 773		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
 774
 775		$this->Socket->responseClass = 'CustomResponse';
 776		$response = $this->Socket->request('http://www.cakephp.org/');
 777		$this->assertInstanceOf('CustomResponse', $response);
 778		$this->assertEquals('HTTP/1.x 2', $response->first10);
 779	}
 780
 781/**
 782 * Test that redirect urls are urldecoded
 783 *
 784 * @return void
 785 */
 786	public function testRequestWithRedirectUrlEncoded() {
 787		$request = array(
 788			'uri' => 'http://localhost/oneuri',
 789			'redirect' => 1
 790		);
 791		$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://i.cmpnet.com%2Ftechonline%2Fpdf%2Fa.pdf=\r\n\r\n";
 792		$serverResponse2 = "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>You have been redirected</h1>";
 793
 794		$this->Socket->expects($this->at(1))
 795			->method('read')
 796			->will($this->returnValue($serverResponse1));
 797
 798		$this->Socket->expects($this->at(3))
 799			->method('write')
 800			->with($this->logicalAnd(
 801				$this->stringContains('Host: i.cmpnet.com'),
 802				$this->stringContains('GET /techonline/pdf/a.pdf')
 803			));
 804
 805		$this->Socket->expects($this->at(4))
 806			->method('read')
 807			->will($this->returnValue($serverResponse2));
 808
 809		$response = $this->Socket->request($request);
 810		$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
 811	}
 812
 813/**
 814 * testRequestWithRedirect method
 815 *
 816 * @return void
 817 */
 818	public function testRequestWithRedirectAsTrue() {
 819		$request = array(
 820			'uri' => 'http://localhost/oneuri',
 821			'redirect' => true
 822		);
 823		$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/anotheruri\r\n\r\n";
 824		$serverResponse2 = "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>You have been redirected</h1>";
 825		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
 826		$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
 827
 828		$response = $this->Socket->request($request);
 829		$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
 830	}
 831
 832/**
 833 * Test that redirects with a count limit are decremented.
 834 *
 835 * @return void
 836 */
 837	public function testRequestWithRedirectAsInt() {
 838		$request = array(
 839			'uri' => 'http://localhost/oneuri',
 840			'redirect' => 2
 841		);
 842		$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/anotheruri\r\n\r\n";
 843		$serverResponse2 = "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>You have been redirected</h1>";
 844		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
 845		$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
 846
 847		$this->Socket->request($request);
 848		$this->assertEquals(1, $this->Socket->request['redirect']);
 849	}
 850
 851/**
 852 * Test that redirects after the redirect count reaches 9 are not followed.
 853 *
 854 * @return void
 855 */
 856	public function testRequestWithRedirectAsIntReachingZero() {
 857		$request = array(
 858			'uri' => 'http://localhost/oneuri',
 859			'redirect' => 1
 860		);
 861		$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/oneruri\r\n\r\n";
 862		$serverResponse2 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/anotheruri\r\n\r\n";
 863		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
 864		$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
 865
 866		$response = $this->Socket->request($request);
 867		$this->assertEquals(0, $this->Socket->request['redirect']);
 868		$this->assertEquals(302, $response->code);
 869		$this->assertEquals('http://localhost/anotheruri', $response->getHeader('Location'));
 870	}
 871
 872/**
 873 * testProxy method
 874 *
 875 * @return void
 876 */
 877	public function testProxy() {
 878		$this->Socket->reset();
 879		$this->Socket->expects($this->any())->method('connect')->will($this->returnValue(true));
 880		$this->Socket->expects($this->any())->method('read')->will($this->returnValue(false));
 881
 882		$this->Socket->configProxy('proxy.server', 123);
 883		$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";
 884		$this->Socket->request('http://www.cakephp.org/');
 885		$this->assertEquals($expected, $this->Socket->request['raw']);
 886		$this->assertEquals('proxy.server', $this->Socket->config['host']);
 887		$this->assertEquals(123, $this->Socket->config['port']);
 888		$expected = array(
 889			'host' => 'proxy.server',
 890			'port' => 123,
 891			'method' => null,
 892			'user' => null,
 893			'pass' => null
 894		);
 895		$this->assertEquals($expected, $this->Socket->request['proxy']);
 896
 897		$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";
 898		$this->Socket->request('/bakery');
 899		$this->assertEquals($expected, $this->Socket->request['raw']);
 900		$this->assertEquals('proxy.server', $this->Socket->config['host']);
 901		$this->assertEquals(123, $this->Socket->config['port']);
 902		$expected = array(
 903			'host' => 'proxy.server',
 904			'port' => 123,
 905			'method' => null,
 906			'user' => null,
 907			'pass' => null
 908		);
 909		$this->assertEquals($expected, $this->Socket->request['proxy']);
 910
 911		$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";
 912		$this->Socket->configProxy('proxy.server', 123, 'Test', 'mark', 'secret');
 913		$this->Socket->request('http://www.cakephp.org/');
 914		$this->assertEquals($expected, $this->Socket->request['raw']);
 915		$this->assertEquals('proxy.server', $this->Socket->config['host']);
 916		$this->assertEquals(123, $this->Socket->config['port']);
 917		$expected = array(
 918			'host' => 'proxy.server',
 919			'port' => 123,
 920			'method' => 'Test',
 921			'user' => 'mark',
 922			'pass' => 'secret'
 923		);
 924		$this->assertEquals($expected, $this->Socket->request['proxy']);
 925
 926		$this->Socket->configAuth('Test', 'login', 'passwd');
 927		$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";
 928		$this->Socket->request('http://www.cakephp.org/');
 929		$this->assertEquals($expected, $this->Socket->request['raw']);
 930		$expected = array(
 931			'host' => 'proxy.server',
 932			'port' => 123,
 933			'method' => 'Test',
 934			'user' => 'mark',
 935			'pass' => 'secret'
 936		);
 937		$this->assertEquals($expected, $this->Socket->request['proxy']);
 938		$expected = array(
 939			'Test' => array(
 940				'user' => 'login',
 941				'pass' => 'passwd'
 942			)
 943		);
 944		$this->assertEquals($expected, $this->Socket->request['auth']);
 945	}
 946
 947/**
 948 * testUrl method
 949 *
 950 * @return void
 951 */
 952	public function testUrl() {
 953		$this->Socket->reset(true);
 954
 955		$this->assertEquals(false, $this->Socket->url(true));
 956
 957		$url = $this->Socket->url('www.cakephp.org');
 958		$this->assertEquals('http://www.cakephp.org/', $url);
 959
 960		$url = $this->Socket->url('https://www.cakephp.org/posts/add');
 961		$this->assertEquals('https://www.cakephp.org/posts/add', $url);
 962		$url = $this->Socket->url('http://www.cakephp/search?q=socket', '/%path?%query');
 963		$this->assertEquals('/search?q=socket', $url);
 964
 965		$this->Socket->config['request']['uri']['host'] = 'bakery.cakephp.org';
 966		$url = $this->Socket->url();
 967		$this->assertEquals('http://bakery.cakephp.org/', $url);
 968
 969		$this->Socket->configUri('http://www.cakephp.org');
 970		$url = $this->Socket->url('/search?q=bar');
 971		$this->assertEquals('http://www.cakephp.org/search?q=bar', $url);
 972
 973		$url = $this->Socket->url(array('host' => 'www.foobar.org', 'query' => array('q' => 'bar')));
 974		$this->assertEquals('http://www.foobar.org/?q=bar', $url);
 975
 976		$url = $this->Socket->url(array('path' => '/supersearch', 'query' => array('q' => 'bar')));
 977		$this->assertEquals('http://www.cakephp.org/supersearch?q=bar', $url);
 978
 979		$this->Socket->configUri('http://www.google.com');
 980		$url = $this->Socket->url('/search?q=socket');
 981		$this->assertEquals('http://www.google.com/search?q=socket', $url);
 982
 983		$url = $this->Socket->url();
 984		$this->assertEquals('http://www.google.com/', $url);
 985
 986		$this->Socket->configUri('https://www.google.com');
 987		$url = $this->Socket->url('/search?q=socket');
 988		$this->assertEquals('https://www.google.com/search?q=socket', $url);
 989
 990		$this->Socket->reset();
 991		$this->Socket->configUri('www.google.com:443');
 992		$url = $this->Socket->url('/search?q=socket');
 993		$this->assertEquals('https://www.google.com/search?q=socket', $url);
 994
 995		$this->Socket->reset();
 996		$this->Socket->configUri('www.google.com:8080');
 997		$url = $this->Socket->url('/search?q=socket');
 998		$this->assertEquals('http://www.google.com:8080/search?q=socket', $url);
 999	}
1000
1001/**
1002 * testGet method
1003 *
1004 * @return void
1005 */
1006	public function testGet() {
1007		$this->RequestSocket->reset();
1008
1009		$this->RequestSocket->expects($this->at(0))
1010			->method('request')
1011			->with(array('method' => 'GET', 'uri' => 'http://www.google.com/'));
1012
1013		$this->RequestSocket->expects($this->at(1))
1014			->method('request')
1015			->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar'));
1016
1017		$this->RequestSocket->expects($this->at(2))
1018			->method('request')
1019			->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar'));
1020
1021		$this->RequestSocket->expects($this->at(3))
1022			->method('request')
1023			->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=23&foobar=42'));
1024
1025		$this->RequestSocket->expects($this->at(4))
1026			->method('request')
1027			->with(array('method' => 'GET', 'uri' => 'http://www.google.com/', 'version' => '1.0'));
1028
1029		$this->RequestSocket->expects($this->at(5))
1030			->method('request')
1031			->with(array('method' => 'GET', 'uri' => 'https://secure.example.com/test.php?one=two'));
1032
1033		$this->RequestSocket->expects($this->at(6))
1034			->method('request')
1035			->with(array('method' => 'GET', 'uri' => 'https://example.com/oauth/access?clientid=123&redirect_uri=http%3A%2F%2Fexample.com&code=456'));
1036
1037		$this->RequestSocket->get('http://www.google.com/');
1038		$this->RequestSocket->get('http://www.google.com/', array('foo' => 'bar'));
1039		$this->RequestSocket->get('http://www.google.com/', 'foo=bar');
1040		$this->RequestSocket->get('http://www.google.com/?foo=bar', array('foobar' => '42', 'foo' => '23'));
1041		$this->RequestSocket->get('http://www.google.com/', null, array('version' => '1.0'));
1042		$this->RequestSocket->get('https://secure.example.com/test.php', array('one' => 'two'));
1043		$this->RequestSocket->get('https://example.com/oauth/access', array(
1044			'clientid' => '123',
1045			'redirect_uri' => 'http://example.com',
1046			'code' => 456
1047		));
1048	}
1049
1050/**
1051 * Test authentication
1052 *
1053 * @return void
1054 */
1055	public function testAuth() {
1056		$socket = new MockHttpSocket();
1057		$socket->get('http://mark:secret@example.com/test');
1058		$this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
1059
1060		$socket->configAuth(false);
1061		$socket->get('http://example.com/test');
1062		$this->assertFalse(strpos($socket->request['header'], 'Authorization:'));
1063
1064		$socket->configAuth('Test', 'mark', 'passwd');
1065		$socket->get('http://example.com/test');
1066		$this->assertTrue(strpos($socket->request['header'], 'Authorization: Test mark.passwd') !== false);
1067	}
1068
1069/**
1070 * test that two consecutive get() calls reset the authentication credentials.
1071 *
1072 * @return void
1073 */
1074	public function testConsecutiveGetResetsAuthCredentials() {
1075		$socket = new MockHttpSocket();
1076		$socket->get('http://mark:secret@example.com/test');
1077		$this->assertEquals('mark', $socket->request['uri']['user']);
1078		$this->assertEquals('secret', $socket->request['uri']['pass']);
1079		$this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
1080
1081		$socket->get('/test2');
1082		$this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
1083
1084		$socket->get('/test3');
1085		$this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
1086	}
1087
1088/**
1089 * testPostPutDelete method
1090 *
1091 * @return void
1092 */
1093	public function testPost() {
1094		$this->RequestSocket->reset();
1095		$this->RequestSocket->expects($this->at(0))
1096			->method('request')
1097			->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => array()));
1098
1099		$this->RequestSocket->expects($this->at(1))
1100			->method('request')
1101			->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
1102
1103		$this->RequestSocket->expects($this->at(2))
1104			->method('request')
1105			->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
1106
1107		$this->RequestSocket->post('http://www.google.com/');
1108		$this->RequestSocket->post('http://www.google.com/', array('Foo' => 'bar'));
1109		$this->RequestSocket->post('http://www.google.com/', null, array('line' => 'Hey Server'));
1110	}
1111
1112/**
1113 * testPut
1114 *
1115 * @return void
1116 */
1117	public function testPut() {
1118		$this->RequestSocket->reset();
1119		$this->RequestSocket->expects($this->at(0))
1120			->method('request')
1121			->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => array()));
1122
1123		$this->RequestSocket->expects($this->at(1))
1124			->method('request')
1125			->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
1126
1127		$this->RequestSocket->expects($this->at(2))
1128			->method('request')
1129			->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
1130
1131		$this->RequestSocket->put('http://www.google.com/');
1132		$this->RequestSocket->put('http://www.google.com/', array('Foo' => 'bar'));
1133		$this->RequestSocket->put('http://www.google.com/', null, array('line' => 'Hey Server'));
1134	}
1135
1136/**
1137 * testDelete
1138 *
1139 * @return void
1140 */
1141	public function testDelete() {
1142		$this->RequestSocket->reset();
1143		$this->RequestSocket->expects($this->at(0))
1144			->method('request')
1145			->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => array()));
1146
1147		$this->RequestSocket->expects($this->at(1))
1148			->method('request')
1149			->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
1150
1151		$this->RequestSocket->expects($this->at(2))
1152			->method('request')
1153			->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
1154
1155		$this->RequestSocket->delete('http://www.google.com/');
1156		$this->RequestSocket->delete('http://www.google.com/', array('Foo' => 'bar'));
1157		$this->RequestSocket->delete('http://www.google.com/', null, array('line' => 'Hey Server'));
1158	}
1159
1160/**
1161 * testBuildRequestLine method
1162 *
1163 * @return void
1164 */
1165	public function testBuildRequestLine() {
1166		$this->Socket->reset();
1167
1168		$this->Socket->quirksMode = true;
1169		$r = $this->Socket->buildRequestLine('Foo');
1170		$this->assertEquals('Foo', $r);
1171		$this->Socket->quirksMode = false;
1172
1173		$r = $this->Socket->buildRequestLine(true);
1174		$this->assertEquals(false, $r);
1175
1176		$r = $this->Socket->buildRequestLine(array('foo' => 'bar', 'method' => 'foo'));
1177		$this->assertEquals(false, $r);
1178
1179		$r = $this->Socket->buildRequestLine(array('method' => 'GET', 'uri' => 'http://www.cakephp.org/search?q=socket'));
1180		$this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
1181
1182		$request = array(
1183			'method' => 'GET',
1184			'uri' => array(
1185				'path' => '/search',
1186				'query' => array('q' => 'socket')
1187			)
1188		);
1189		$r = $this->Socket->buildRequestLine($request);
1190		$this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
1191
1192		unset($request['method']);
1193		$r = $this->Socket->buildRequestLine($request);
1194		$this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
1195
1196		$r = $this->Socket->buildRequestLine($request, 'CAKE-HTTP/0.1');
1197		$this->assertEquals("GET /search?q=socket CAKE-HTTP/0.1\r\n", $r);
1198
1199		$request = array('method' => 'OPTIONS', 'uri' => '*');
1200		$r = $this->Socket->buildRequestLine($request);
1201		$this->assertEquals("OPTIONS * HTTP/1.1\r\n", $r);
1202
1203		$request['method'] = 'GET';
1204		$this->Socket->quirksMode = true;
1205		$r = $this->Socket->buildRequestLine($request);
1206		$this->assertEquals("GET * HTTP/1.1\r\n", $r);
1207
1208		$r = $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
1209		$this->assertEquals("GET * HTTP/1.1\r\n", $r);
1210	}
1211
1212/**
1213 * testBadBuildRequestLine method
1214 *
1215 * @expectedException SocketException
1216 * @return void
1217 */
1218	public function testBadBuildRequestLine() {
1219		$this->Socket->buildRequestLine('Foo');
1220	}
1221
1222/**
1223 * testBadBuildRequestLine2 method
1224 *
1225 * @expectedException SocketException
1226 * @return void
1227 */
1228	public function testBadBuildRequestLine2() {
1229		$this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
1230	}
1231
1232/**
1233 * Asserts that HttpSocket::parseUri is working properly
1234 *
1235 * @return void
1236 */
1237	public function testParseUri() {
1238		$this->Socket->reset();
1239
1240		$uri = $this->Socket->parseUri(array('invalid' => 'uri-string'));
1241		$this->assertEquals(false, $uri);
1242
1243		$uri = $this->Socket->parseUri(array('invalid' => 'uri-string'), array('host' => 'somehost'));
1244		$this->assertEquals(array('host' => 'somehost', 'invalid' => 'uri-string'), $uri);
1245
1246		$uri = $this->Socket->parseUri(false);
1247		$this->assertEquals(false, $uri);
1248
1249		$uri = $this->Socket->parseUri('/my-cool-path');
1250		$this->assertEquals(array('path' => '/my-cool-path'), $uri);
1251
1252		$uri = $this->Socket->parseUri('http://bob:foo123@www.cakephp.org:40/search?q=dessert#results');
1253		$this->assertEquals($uri, array(
1254			'scheme' => 'http',
1255			'host' => 'www.cakephp.org',
1256			'port' => 40,
1257			'user' => 'bob',
1258			'pass' => 'foo123',
1259			'path' => '/search',
1260			'query' => array('q' => 'dessert'),
1261			'fragment' => 'results'
1262		));
1263
1264		$uri = $this->Socket->parseUri('http://www.cakephp.org/');
1265		$this->assertEquals($uri, array(
1266			'scheme' => 'http',
1267			'host' => 'www.cakephp.org',
1268			'path' => '/'
1269		));
1270
1271		$uri = $this->Socket->parseUri('http://www.cakephp.org', true);
1272		$this->assertEquals($uri, array(
1273			'scheme' => 'http',
1274			'host' => 'www.cakephp.org',
1275			'port' => 80,
1276			'user' => null,
1277			'pass' => null,
1278			'path' => '/',
1279			'query' => array(),
1280			'fragment' => null
1281		));
1282
1283		$uri = $this->Socket->parseUri('https://www.cakephp.org', true);
1284		$this->assertEquals($uri, array(
1285			'scheme' => 'https',
1286			'host' => 'www.cakephp.org',
1287			'port' => 443,
1288			'user' => null,
1289			'pass' => null,
1290			'path' => '/',
1291			'query' => array(),
1292			'fragment' => null
1293		));
1294
1295		$uri = $this->Socket->parseUri('www.cakephp.org:443/query?foo', true);
1296		$this->assertEquals($uri, array(
1297			'scheme' => 'https',
1298			'host' => 'www.cakephp.org',
1299			'port' => 443,
1300			'user' => null,
1301			'pass' => null,
1302			'path' => '/query',
1303			'query' => array('foo' => ""),
1304			'fragment' => null
1305		));
1306
1307		$uri = $this->Socket->parseUri('http://www.cakephp.org', array('host' => 'piephp.org', 'user' => 'bob', 'fragment' => 'results'));
1308		$this->assertEquals($uri, array(
1309			'host' => 'www.cakephp.org',
1310			'user' => 'bob',
1311			'fragment' => 'results',
1312			'scheme' => 'http'
1313		));
1314
1315		$uri = $this->Socket->parseUri('https://www.cakephp.org', array('scheme' => 'http', 'port' => 23));
1316		$this->assertEquals($uri, array(
1317			'scheme' => 'https',
1318			'port' => 23,
1319			'host' => 'www.cakephp.org'
1320		));
1321
1322		$uri = $this->Socket->parseUri('www.cakephp.org:59', array('scheme' => array('http', 'https'), 'port' => 80));
1323		$this->assertEquals($uri, array(
1324			'scheme' => 'http',
1325			'port' => 59,
1326			'host' => 'www.cakephp.org'
1327		));
1328
1329		$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)));
1330		$this->assertEquals($uri, array(
1331			'scheme' => 'http',
1332			'host' => 'www.google.com',
1333			'port' => 8080
1334		));
1335
1336		$uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2%3Dvalue3');
1337		$this->assertEquals($uri, array(
1338			'scheme' => 'http',
1339			'host' => 'www.cakephp.org',
1340			'path' => '/',
1341			'query' => array(
1342				'param1' => 'value1',
1343				'param2' => 'value2=value3'
1344			)
1345		));
1346
1347		$uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2=value3');
1348		$this->assertEquals($uri, array(
1349			'scheme' => 'http',
1350			'host' => 'www.cakephp.org',
1351			'path' => '/',
1352			'query' => array(
1353				'param1' => 'value1',
1354				'param2' => 'value2=value3'
1355			)
1356		));
1357	}
1358
1359/**
1360 * Tests that HttpSocket::buildUri can turn all kinds of uri arrays (and strings) into fully or partially qualified URI's
1361 *
1362 * @return void
1363 */
1364	public function testBuildUri() {
1365		$this->Socket->reset();
1366
1367		$r = $this->Socket->buildUri(true);
1368		$this->assertEquals(false, $r);
1369
1370		$r = $this->Socket->buildUri('foo.com');
1371		$this->assertEquals('http://foo.com/', $r);
1372
1373		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'));
1374		$this->assertEquals('http://www.cakephp.org/', $r);
1375
1376		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https'));
1377		$this->assertEquals('https://www.cakephp.org/', $r);
1378
1379		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'port' => 23));
1380		$this->assertEquals('http://www.cakephp.org:23/', $r);
1381
1382		$r = $this->Socket->buildUri(array('path' => 'www.google.com/search', 'query' => 'q=cakephp'));
1383		$this->assertEquals('http://www.google.com/search?q=cakephp', $r);
1384
1385		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https', 'port' => 79));
1386		$this->assertEquals('https://www.cakephp.org:79/', $r);
1387
1388		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => 'foo'));
1389		$this->assertEquals('http://www.cakephp.org/foo', $r);
1390
1391		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/foo'));
1392		$this->assertEquals('http://www.cakephp.org/foo', $r);
1393
1394		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/search', 'query' => array('q' => 'HttpSocket')));
1395		$this->assertEquals('http://www.cakephp.org/search?q=HttpSocket', $r);
1396
1397		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'));
1398		$this->assertEquals('http://www.cakephp.org/#bar', $r);
1399
1400		$r = $this->Socket->buildUri(array(
1401			'scheme' => 'https',
1402			'host' => 'www.cakephp.org',
1403			'port' => 25,
1404			'user' => 'bob',
1405			'pass' => 'secret',
1406			'path' => '/cool',
1407			'query' => array('foo' => 'bar'),
1408			'fragment' => 'comment'
1409		));
1410		$this->assertEquals('https://bob:secret@www.cakephp.org:25/cool?foo=bar#comment', $r);
1411
1412		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'), '%fragment?%host');
1413		$this->assertEquals('bar?www.cakephp.org', $r);
1414
1415		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'), '%fragment???%host');
1416		$this->assertEquals('???www.cakephp.org', $r);
1417
1418		$r = $this->Socket->buildUri(array('path' => '*'), '/%path?%query');
1419		$this->assertEquals('*', $r);
1420
1421		$r = $this->Socket->buildUri(array('scheme' => 'foo', 'host' => 'www.cakephp.org'));
1422		$this->assertEquals('foo://www.cakephp.org:80/', $r);
1423	}
1424
1425/**
1426 * Asserts that HttpSocket::parseQuery is working properly
1427 *
1428 * @return void
1429 */
1430	public function testParseQuery() {
1431		$this->Socket->reset();
1432
1433		$query = $this->Socket->parseQuery(array('framework' => 'cakephp'));
1434		$this->assertEquals(array('framework' => 'cakephp'), $query);
1435
1436		$query = $this->Socket->parseQuery('');
1437		$this->assertEquals(array(), $query);
1438
1439		$query = $this->Socket->parseQuery('framework=cakephp');
1440		$this->assertEquals(array('framework' => 'cakephp'), $query);
1441
1442		$query = $this->Socket->parseQuery('?framework=cakephp');
1443		$this->assertEquals(array('framework' => 'cakephp'), $query);
1444
1445		$query = $this->Socket->parseQuery('a&b&c');
1446		$this->assertEquals(array('a' => '', 'b' => '', 'c' => ''), $query);
1447
1448		$query = $this->Socket->parseQuery('value=12345');
1449		$this->assertEquals(array('value' => '12345'), $query);
1450
1451		$query = $this->Socket->parseQuery('a[0]=foo&a[1]=bar&a[2]=cake');
1452		$this->assertEquals(array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')), $query);
1453
1454		$query = $this->Socket->parseQuery('a[]=foo&a[]=bar&a[]=cake');
1455		$this->assertEquals(array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')), $query);
1456
1457		$query = $this->Socket->parseQuery('a[][]=foo&a[][]=bar&a[][]=cake');
1458		$expectedQuery = array(
1459			'a' => array(
1460				0 => array(
1461					0 => 'foo'
1462				),
1463				1 => array(
1464					0 => 'bar'
1465				),
1466				array(
1467					0 => 'cake'
1468				)
1469			)
1470		);
1471		$this->assertEquals($expectedQuery, $query);
1472
1473		$query = $this->Socket->parseQuery('a[][]=foo&a[bar]=php&a[][]=bar&a[][]=cake');
1474		$expectedQuery = array(
1475			'a' => array(
1476				array('foo'),
1477				'bar' => 'php',
1478				array('bar'),
1479				array('cake')
1480			)
1481		);
1482		$this->assertEquals($expectedQuery, $query);
1483
1484		$query = $this->Socket->parseQuery('user[]=jim&user[3]=tom&user[]=bob');
1485		$expectedQuery = array(
1486			'user' => array(
1487				0 => 'jim',
1488				3 => 'tom',
1489				4 => 'bob'
1490			)
1491		);
1492		$this->assertEquals($expectedQuery, $query);
1493
1494		$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';
1495		$query = $this->Socket->parseQuery($queryStr);
1496		$expectedQuery = array(
1497			'user' => array(
1498				0 => array(
1499					'items' => array(
1500						'foo',
1501						'bar'
1502					)
1503				),
1504				1 => array(
1505					'name' => 'jim',
1506					'items' => array(
1507						'personal' => array(
1508							'book'
1509							, 'pen'
1510						),
1511						'ball'
1512					)
1513				),
1514				'count' => '2'
1515			),
1516			'empty' => ''
1517		);
1518		$this->assertEquals($expectedQuery, $query);
1519
1520		$query = 'openid.ns=example.com&foo=bar&foo=baz';
1521		$result = $this->Socket->parseQuery($query);
1522		$expected = array(
1523			'openid.ns' => 'example.com',
1524			'foo' => array('bar', 'baz')
1525		);
1526		$this->assertEquals($expected, $result);
1527	}
1528
1529/**
1530 * Tests that HttpSocket::buildHeader can turn a given $header array into a proper header string according to
1531 * H…

Large files files are truncated, but you can click here to view the full file