PageRenderTime 203ms CodeModel.GetById 15ms app.highlight 60ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/cases/libs/http_socket.test.php

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