PageRenderTime 65ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/omarsmak/html5-cloud-ide
PHP | 1513 lines | 1037 code | 189 blank | 287 comment | 5 complexity | 0c5e78c11ddff6e545cf6fef8e84086c MD5 | raw file

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

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

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