PageRenderTime 30ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

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

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