PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/intelij/restaurant-manager
PHP | 1629 lines | 1127 code | 200 blank | 302 comment | 6 complexity | 3d3ad9b7760a9cfaf8bb1bf53cd0f290 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception

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

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

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