⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 http_socket.test.php

📁 Cake Framwork , Excellent
💻 PHP
📖 第 1 页 / 共 3 页
字号:
<?php/* SVN FILE: $Id: http_socket.test.php 7118 2008-06-04 20:49:29Z gwoo $ *//** * Short description for file. * * Long description for file * * PHP versions 4 and 5 * * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite> * Copyright 2005-2008, Cake Software Foundation, Inc. *								1785 E. Sahara Avenue, Suite 490-204 *								Las Vegas, Nevada 89104 * *  Licensed under The Open Group Test Suite License *  Redistributions of files must retain the above copyright notice. * * @filesource * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc. * @link				https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests * @package			cake.tests * @subpackage		cake.tests.cases.libs * @since			CakePHP(tm) v 1.2.0.4206 * @version			$Revision: 7118 $ * @modifiedby		$LastChangedBy: gwoo $ * @lastmodified	$Date: 2008-06-04 13:49:29 -0700 (Wed, 04 Jun 2008) $ * @license			http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */App::import('Core', 'HttpSocket');/** * Short description for class. * * @package		cake.tests * @subpackage	cake.tests.cases.libs */class HttpSocketTest extends UnitTestCase {/** * Socket property *  * @var mixed null * @access public */	var $Socket = null;/** * RequestSocket property *  * @var mixed null * @access public */	var $RequestSocket = null;/** * This function sets up a TestHttpSocket instance we are going to use for testing * */	function setUp() {		if (!class_exists('TestHttpSocket')) {			Mock::generatePartial('HttpSocket', 'TestHttpSocket', array('read', 'write', 'connect'));			Mock::generatePartial('HttpSocket', 'TestHttpSocketRequests', array('read', 'write', 'connect', 'request'));		}		$this->Socket =& new TestHttpSocket();		$this->RequestSocket =& new TestHttpSocketRequests();	}/** * We use this function to clean up after the test case was executed * */	function tearDown() {		unset($this->Socket, $this->RequestSocket);	}/** * Test that HttpSocket::__construct does what one would expect it to do * */	function testConstruct() {		$this->Socket->reset();		$baseConfig = $this->Socket->config;		$this->Socket->expectNever('connect');		$this->Socket->__construct(array('host' => 'foo-bar'));		$baseConfig['host']	 = 'foo-bar';		$baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);		$this->assertIdentical($this->Socket->config, $baseConfig);		$this->Socket->reset();		$baseConfig = $this->Socket->config;		$this->Socket->__construct('http://www.cakephp.org:23/');		$baseConfig['host']	 = 'www.cakephp.org';		$baseConfig['request']['uri']['host'] = 'www.cakephp.org';		$baseConfig['port']	 = 23;		$baseConfig['request']['uri']['port'] = 23;		$baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);		$this->assertIdentical($this->Socket->config, $baseConfig);				$this->Socket->reset();		$this->Socket->__construct(array('request' => array('uri' => 'http://www.cakephp.org:23/')));		$this->assertIdentical($this->Socket->config, $baseConfig);	}/** * Test that HttpSocket::configUri works properly with different types of arguments * */	function testConfigUri() {		$this->Socket->reset();		$r = $this->Socket->configUri('https://bob:secret@www.cakephp.org:23/?query=foo');		$expected = array(			'persistent' => false,			'host' 		 => 'www.cakephp.org',			'protocol'   => 'tcp',			'port' 		 => 23,			'timeout' 	 =>	30,			'request' => array(				'uri' => array(					'scheme' => 'https'					, 'host' => 'www.cakephp.org'					, 'port' => 23				),				'auth' => array(					'method' => 'basic'					, 'user' => 'bob'					, 'pass' => 'secret'				),				'cookies' => array(),			)		);		$this->assertIdentical($this->Socket->config, $expected);		$this->assertIdentical($r, $expected);		$r = $this->Socket->configUri(array('host' => 'www.foo-bar.org'));		$expected['host'] = 'www.foo-bar.org';		$expected['request']['uri']['host'] = 'www.foo-bar.org';		$this->assertIdentical($this->Socket->config, $expected);		$this->assertIdentical($r, $expected);		$r = $this->Socket->configUri('http://www.foo.com');		$expected = array(			'persistent' => false,			'host' 		 => 'www.foo.com',			'protocol'   => 'tcp',			'port' 		 => 80,			'timeout' 	 =>	30,			'request' => array(				'uri' => array(					'scheme' => 'http'					, 'host' => 'www.foo.com'					, 'port' => 80				),				'auth' => array(					'method' => 'basic'					, 'user' => null					, 'pass' => null				),				'cookies' => array()			)		);		$this->assertIdentical($this->Socket->config, $expected);		$this->assertIdentical($r, $expected);		$r = $this->Socket->configUri('/this-is-fuck');		$this->assertIdentical($this->Socket->config, $expected);		$this->assertIdentical($r, false);		$r = $this->Socket->configUri(false);		$this->assertIdentical($this->Socket->config, $expected);		$this->assertIdentical($r, false);	}/** * Tests that HttpSocket::request (the heart of the HttpSocket) is working properly. * */	function testRequest() {		$this->Socket->reset();		$this->Socket->reset();		$response = $this->Socket->request(true);		$this->assertFalse($response);		$tests = array(			0 => array(				'request' => 'http://www.cakephp.org/?foo=bar'				, 'expectation' => array(					'config' => array(						'persistent' => false						, 'host' => 'www.cakephp.org'						, 'protocol' => 'tcp'						, 'port' => 80						, 'timeout' => 30						, 'request' => array(							'uri' => array (								'scheme' => 'http'								, 'host' => 'www.cakephp.org'								, 'port' => 80,							)							, 'auth' => array(								'method' => 'basic'								,'user' => null								,'pass' => null							),							'cookies' => array(),						),					)					, 'request' => array(						'method' => 'GET'						, 'uri' => array(							'scheme' => 'http'							, 'host' => 'www.cakephp.org'							, 'port' => 80							, 'user' => null							, 'pass' => null							, 'path' => '/'							, 'query' => array('foo' => 'bar')							, 'fragment' => null						)						, 'auth' => array(							'method' => 'basic'							, 'user' => null							, 'pass' => null						)						, 'version' => '1.1'						, 'body' => ''						, 'line' => "GET /?foo=bar HTTP/1.1\r\n"						, 'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n"						, 'raw' => ""						, 'cookies' => array(),					)				)			)			, 1 => array(				'request' => array(					'uri' => array(						'host' => 'www.cakephp.org'						, 'query' => '?foo=bar'					)				)			)			, 2 => array(				'request' => 'www.cakephp.org/?foo=bar'			)			, 3 => array(				'request' => array('host' => '192.168.0.1', 'uri' => 'http://www.cakephp.org/?foo=bar')				, 'expectation' => array(					'request' => array(						'uri' => array('host' => 'www.cakephp.org')					)					, 'config' => array(						'request' => array(							'uri' => array('host' => 'www.cakephp.org')						)						, 'host' => '192.168.0.1'					)				)			)			, 'reset4' => array(				'request.uri.query' => array()			)			, 4 => array(				'request' => array('header' => array('Foo@woo' => 'bar-value'))				, 'expectation' => array(					'request' => array(						'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nFoo\"@\"woo: bar-value\r\n"						, 'line' => "GET / HTTP/1.1\r\n"					)				)			)			, 5 => array(				'request' => array('header' => array('Foo@woo' => 'bar-value', 'host' => 'foo.com'), 'uri' => 'http://www.cakephp.org/')				, 'expectation' => array(					'request' => array(						'header' => "Host: foo.com\r\nConnection: close\r\nUser-Agent: CakePHP\r\nFoo\"@\"woo: bar-value\r\n"					)					, 'config' => array(						'host' => 'www.cakephp.org'					)				)			)			, 6 => array(				'request' => array('header' => "Foo: bar\r\n")				, 'expectation' => array(					'request' => array(						'header' => "Foo: bar\r\n"					)				)			)			, 7 => array(				'request' => array('header' => "Foo: bar\r\n", 'uri' => 'http://www.cakephp.org/search?q=http_socket#ignore-me')				, 'expectation' => array(					'request' => array(						'uri' => array(							'path' => '/search'							, 'query' => array('q' => 'http_socket')							, 'fragment' => 'ignore-me'						)						, 'line' => "GET /search?q=http_socket HTTP/1.1\r\n"					)				)			)			, 'reset8' => array(				'request.uri.query' => array()			)			, 8 => array(				'request' => array('method' => 'POST', 'uri' => 'http://www.cakephp.org/posts/add', 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'))				, 'expectation' => array(					'request' => array(						'method' => 'POST'						, 'uri' => array(							'path' => '/posts/add'							, 'fragment' => null						)						, 'body' => "name=HttpSocket-is-released&date=today"						, 'line' => "POST /posts/add HTTP/1.1\r\n"						, '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"						, 'raw' => "name=HttpSocket-is-released&date=today"					)				)			)			, 9 => array(				'request' => array('method' => 'POST', 'uri' => 'https://www.cakephp.org/posts/add', 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'))				, 'expectation' => array(					'config' => array(						'port' => 443						, 'request' => array(							'uri' => array(								'scheme' => 'https'								, 'port' => 443							)						)					)					, 'request' => array(						'uri' => array(							'scheme' => 'https'							, 'port' => 443						)					)				)			)			, 10 => array(				'request' => array(						'method' => 'POST',						'uri' => 'https://www.cakephp.org/posts/add',						'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'),						'cookies' => array('foo' => array('value' => 'bar'))				)				, 'expectation' => array(					'request' => array(						'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",						'cookies' => array(							'foo' => array('value' => 'bar'),						)					)				)			)		);		$expectation = array();		foreach ($tests as $i => $test) {			if (strpos($i, 'reset') === 0) {				foreach ($test as $path => $val) {					$expectation = Set::insert($expectation, $path, $val);				}				continue;			}			if (isset($test['expectation'])) {				$expectation = Set::merge($expectation, $test['expectation']);			}			$this->Socket->request($test['request']);			$raw = $expectation['request']['raw'];			$expectation['request']['raw'] = $expectation['request']['line'].$expectation['request']['header']."\r\n".$raw;			$r = array('config' => $this->Socket->config, 'request' => $this->Socket->request);			$v = $this->assertIdentical($r, $expectation, '%s in test #'.$i.' ');			if (!$v) {				debug('Result:');				debug($r);				debug('Expected:');				debug($expectation);			}			$expectation['request']['raw'] = $raw;		}		$this->Socket->reset();		$request = array('method' => 'POST', 'uri' => 'http://www.cakephp.org/posts/add', 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'));		$response = $this->Socket->request($request);		$this->assertIdentical($this->Socket->request['body'], "name=HttpSocket-is-released&date=today");		$request = array('uri' => '*', 'method' => 'GET');		$this->expectError(new PatternExpectation('/activate quirks mode/i'));		$response = $this->Socket->request($request);		$this->assertFalse($response);		$this->assertFalse($this->Socket->response);		$this->Socket->reset();		$request = array('uri' => 'htpp://www.cakephp.org/');		$this->Socket->setReturnValue('connect', true);		$this->Socket->setReturnValue('read', false);		$this->Socket->_mock->_call_counts['read'] = 0;		$number = rand(0, 9999999);		$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>";		$this->Socket->setReturnValueAt(0, 'read', $serverResponse);		$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"));		$this->Socket->expectCallCount('read', 2);		$response = $this->Socket->request($request);		$this->assertIdentical($response, "<h1>Hello, your lucky number is ".$number."</h1>");		$this->Socket->reset();		$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>";		unset($this->Socket->_mock->_actions->_at['read']);		$this->Socket->_mock->_call_counts['read'] = 0;		$this->Socket->setReturnValueAt(0, 'read', $serverResponse);		$this->Socket->connected = true;		$this->Socket->request($request);		$result = $this->Socket->response['cookies'];		$expect = array(			'foo' => array(				'value' => 'bar'			)		);		$this->assertEqual($result, $expect);		$this->assertEqual($this->Socket->config['request']['cookies'], $expect);		$this->assertFalse($this->Socket->connected);	}/** * testUrl method *  * @access public * @return void */	function testUrl() {		$this->Socket->reset(true);		$this->assertIdentical($this->Socket->url(true), false);		$url = $this->Socket->url('www.cakephp.org');		$this->assertIdentical($url, 'http://www.cakephp.org/');		$url = $this->Socket->url('https://www.cakephp.org/posts/add');		$this->assertIdentical($url, 'https://www.cakephp.org/posts/add');		$url = $this->Socket->url('http://www.cakephp/search?q=socket', '/%path?%query');		$this->assertIdentical($url, '/search?q=socket');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -