PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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

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