PageRenderTime 69ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

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

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