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

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

https://bitbucket.org/gencer/cakephp
PHP | 1815 lines | 1257 code | 218 blank | 340 comment | 5 complexity | d53e8bbacf3d48fb6258012f2c6b1d82 MD5 | raw 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(), $versionToken = 'HTTP/1.1') {
  126. return parent::_buildRequestLine($request, $versionToken);
  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. $baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);
  196. $this->assertEquals($this->Socket->config, $baseConfig);
  197. $this->Socket->reset();
  198. $baseConfig = $this->Socket->config;
  199. $this->Socket->__construct('http://www.cakephp.org:23/');
  200. $baseConfig['host'] = $baseConfig['request']['uri']['host'] = 'www.cakephp.org';
  201. $baseConfig['port'] = $baseConfig['request']['uri']['port'] = 23;
  202. $baseConfig['request']['uri']['scheme'] = 'http';
  203. $baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);
  204. $this->assertEquals($this->Socket->config, $baseConfig);
  205. $this->Socket->reset();
  206. $this->Socket->__construct(array('request' => array('uri' => 'http://www.cakephp.org:23/')));
  207. $this->assertEquals($this->Socket->config, $baseConfig);
  208. }
  209. /**
  210. * Test that HttpSocket::configUri works properly with different types of arguments
  211. *
  212. * @return void
  213. */
  214. public function testConfigUri() {
  215. $this->Socket->reset();
  216. $r = $this->Socket->configUri('https://bob:secret@www.cakephp.org:23/?query=foo');
  217. $expected = array(
  218. 'persistent' => false,
  219. 'host' => 'www.cakephp.org',
  220. 'protocol' => 'tcp',
  221. 'port' => 23,
  222. 'timeout' => 30,
  223. 'ssl_verify_peer' => true,
  224. 'ssl_allow_self_signed' => false,
  225. 'ssl_verify_depth' => 5,
  226. 'ssl_verify_host' => true,
  227. 'request' => array(
  228. 'uri' => array(
  229. 'scheme' => 'https',
  230. 'host' => 'www.cakephp.org',
  231. 'port' => 23
  232. ),
  233. 'redirect' => false,
  234. 'cookies' => array(),
  235. )
  236. );
  237. $this->assertEquals($expected, $this->Socket->config);
  238. $this->assertTrue($r);
  239. $r = $this->Socket->configUri(array('host' => 'www.foo-bar.org'));
  240. $expected['host'] = 'www.foo-bar.org';
  241. $expected['request']['uri']['host'] = 'www.foo-bar.org';
  242. $this->assertEquals($expected, $this->Socket->config);
  243. $this->assertTrue($r);
  244. $r = $this->Socket->configUri('http://www.foo.com');
  245. $expected = array(
  246. 'persistent' => false,
  247. 'host' => 'www.foo.com',
  248. 'protocol' => 'tcp',
  249. 'port' => 80,
  250. 'timeout' => 30,
  251. 'ssl_verify_peer' => true,
  252. 'ssl_allow_self_signed' => false,
  253. 'ssl_verify_depth' => 5,
  254. 'ssl_verify_host' => true,
  255. 'request' => array(
  256. 'uri' => array(
  257. 'scheme' => 'http',
  258. 'host' => 'www.foo.com',
  259. 'port' => 80
  260. ),
  261. 'redirect' => false,
  262. 'cookies' => array(),
  263. )
  264. );
  265. $this->assertEquals($expected, $this->Socket->config);
  266. $this->assertTrue($r);
  267. $r = $this->Socket->configUri('/this-is-broken');
  268. $this->assertEquals($expected, $this->Socket->config);
  269. $this->assertFalse($r);
  270. $r = $this->Socket->configUri(false);
  271. $this->assertEquals($expected, $this->Socket->config);
  272. $this->assertFalse($r);
  273. }
  274. /**
  275. * Tests that HttpSocket::request (the heart of the HttpSocket) is working properly.
  276. *
  277. * @return void
  278. */
  279. public function testRequest() {
  280. $this->Socket->reset();
  281. $response = $this->Socket->request(true);
  282. $this->assertFalse($response);
  283. $context = array(
  284. 'ssl' => array(
  285. 'verify_peer' => true,
  286. 'allow_self_signed' => false,
  287. 'verify_depth' => 5,
  288. 'CN_match' => 'www.cakephp.org',
  289. 'cafile' => CAKE . 'Config' . DS . 'cacert.pem'
  290. )
  291. );
  292. $tests = array(
  293. array(
  294. 'request' => 'http://www.cakephp.org/?foo=bar',
  295. 'expectation' => array(
  296. 'config' => array(
  297. 'persistent' => false,
  298. 'host' => 'www.cakephp.org',
  299. 'protocol' => 'tcp',
  300. 'port' => 80,
  301. 'timeout' => 30,
  302. 'context' => $context,
  303. 'request' => array(
  304. 'uri' => array(
  305. 'scheme' => 'http',
  306. 'host' => 'www.cakephp.org',
  307. 'port' => 80
  308. ),
  309. 'redirect' => false,
  310. 'cookies' => array()
  311. )
  312. ),
  313. 'request' => array(
  314. 'method' => 'GET',
  315. 'uri' => array(
  316. 'scheme' => 'http',
  317. 'host' => 'www.cakephp.org',
  318. 'port' => 80,
  319. 'user' => null,
  320. 'pass' => null,
  321. 'path' => '/',
  322. 'query' => array('foo' => 'bar'),
  323. 'fragment' => null
  324. ),
  325. 'version' => '1.1',
  326. 'body' => '',
  327. 'line' => "GET /?foo=bar HTTP/1.1\r\n",
  328. 'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n",
  329. 'raw' => "",
  330. 'redirect' => false,
  331. 'cookies' => array(),
  332. 'proxy' => array(),
  333. 'auth' => array()
  334. )
  335. )
  336. ),
  337. array(
  338. 'request' => array(
  339. 'uri' => array(
  340. 'host' => 'www.cakephp.org',
  341. 'query' => '?foo=bar'
  342. )
  343. )
  344. ),
  345. array(
  346. 'request' => 'www.cakephp.org/?foo=bar'
  347. ),
  348. array(
  349. 'request' => array(
  350. 'host' => '192.168.0.1',
  351. 'uri' => 'http://www.cakephp.org/?foo=bar'
  352. ),
  353. 'expectation' => array(
  354. 'request' => array(
  355. 'uri' => array('host' => 'www.cakephp.org')
  356. ),
  357. 'config' => array(
  358. 'request' => array(
  359. 'uri' => array('host' => 'www.cakephp.org')
  360. ),
  361. 'host' => '192.168.0.1'
  362. )
  363. )
  364. ),
  365. 'reset4' => array(
  366. 'request.uri.query' => array()
  367. ),
  368. array(
  369. 'request' => array(
  370. 'header' => array('Foo@woo' => 'bar-value')
  371. ),
  372. 'expectation' => array(
  373. 'request' => array(
  374. 'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nFoo\"@\"woo: bar-value\r\n",
  375. 'line' => "GET / HTTP/1.1\r\n"
  376. )
  377. )
  378. ),
  379. array(
  380. 'request' => array('header' => array('Foo@woo' => 'bar-value', 'host' => 'foo.com'), 'uri' => 'http://www.cakephp.org/'),
  381. 'expectation' => array(
  382. 'request' => array(
  383. 'header' => "Host: foo.com\r\nConnection: close\r\nUser-Agent: CakePHP\r\nFoo\"@\"woo: bar-value\r\n"
  384. ),
  385. 'config' => array(
  386. 'host' => 'www.cakephp.org'
  387. )
  388. )
  389. ),
  390. array(
  391. 'request' => array('header' => "Foo: bar\r\n"),
  392. 'expectation' => array(
  393. 'request' => array(
  394. 'header' => "Foo: bar\r\n"
  395. )
  396. )
  397. ),
  398. array(
  399. 'request' => array('header' => "Foo: bar\r\n", 'uri' => 'http://www.cakephp.org/search?q=http_socket#ignore-me'),
  400. 'expectation' => array(
  401. 'request' => array(
  402. 'uri' => array(
  403. 'path' => '/search',
  404. 'query' => array('q' => 'http_socket'),
  405. 'fragment' => 'ignore-me'
  406. ),
  407. 'line' => "GET /search?q=http_socket HTTP/1.1\r\n"
  408. )
  409. )
  410. ),
  411. 'reset8' => array(
  412. 'request.uri.query' => array()
  413. ),
  414. array(
  415. 'request' => array(
  416. 'method' => 'POST',
  417. 'uri' => 'http://www.cakephp.org/posts/add',
  418. 'body' => array(
  419. 'name' => 'HttpSocket-is-released',
  420. 'date' => 'today'
  421. )
  422. ),
  423. 'expectation' => array(
  424. 'request' => array(
  425. 'method' => 'POST',
  426. 'uri' => array(
  427. 'path' => '/posts/add',
  428. 'fragment' => null
  429. ),
  430. 'body' => "name=HttpSocket-is-released&date=today",
  431. 'line' => "POST /posts/add HTTP/1.1\r\n",
  432. '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",
  433. 'raw' => "name=HttpSocket-is-released&date=today"
  434. )
  435. )
  436. ),
  437. array(
  438. 'request' => array(
  439. 'method' => 'POST',
  440. 'uri' => 'http://www.cakephp.org:8080/posts/add',
  441. 'body' => array(
  442. 'name' => 'HttpSocket-is-released',
  443. 'date' => 'today'
  444. )
  445. ),
  446. 'expectation' => array(
  447. 'config' => array(
  448. 'port' => 8080,
  449. 'request' => array(
  450. 'uri' => array(
  451. 'port' => 8080
  452. )
  453. )
  454. ),
  455. 'request' => array(
  456. 'uri' => array(
  457. 'port' => 8080
  458. ),
  459. '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"
  460. )
  461. )
  462. ),
  463. array(
  464. 'request' => array(
  465. 'method' => 'POST',
  466. 'uri' => 'https://www.cakephp.org/posts/add',
  467. 'body' => array(
  468. 'name' => 'HttpSocket-is-released',
  469. 'date' => 'today'
  470. )
  471. ),
  472. 'expectation' => array(
  473. 'config' => array(
  474. 'port' => 443,
  475. 'request' => array(
  476. 'uri' => array(
  477. 'scheme' => 'https',
  478. 'port' => 443
  479. )
  480. )
  481. ),
  482. 'request' => array(
  483. 'uri' => array(
  484. 'scheme' => 'https',
  485. 'port' => 443
  486. ),
  487. '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"
  488. )
  489. )
  490. ),
  491. array(
  492. 'request' => array(
  493. 'method' => 'POST',
  494. 'uri' => 'https://www.cakephp.org/posts/add',
  495. 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'),
  496. 'cookies' => array('foo' => array('value' => 'bar'))
  497. ),
  498. 'expectation' => array(
  499. 'request' => array(
  500. '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",
  501. 'cookies' => array(
  502. 'foo' => array('value' => 'bar'),
  503. )
  504. )
  505. )
  506. )
  507. );
  508. $expectation = array();
  509. foreach ($tests as $i => $test) {
  510. if (strpos($i, 'reset') === 0) {
  511. foreach ($test as $path => $val) {
  512. $expectation = Hash::insert($expectation, $path, $val);
  513. }
  514. continue;
  515. }
  516. if (isset($test['expectation'])) {
  517. $expectation = Hash::merge($expectation, $test['expectation']);
  518. }
  519. $this->Socket->request($test['request']);
  520. $raw = $expectation['request']['raw'];
  521. $expectation['request']['raw'] = $expectation['request']['line'] . $expectation['request']['header'] . "\r\n" . $raw;
  522. $r = array('config' => $this->Socket->config, 'request' => $this->Socket->request);
  523. $this->assertEquals($r, $expectation, 'Failed test #' . $i . ' ');
  524. $expectation['request']['raw'] = $raw;
  525. }
  526. $this->Socket->reset();
  527. $request = array('method' => 'POST', 'uri' => 'http://www.cakephp.org/posts/add', 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'));
  528. $response = $this->Socket->request($request);
  529. $this->assertEquals("name=HttpSocket-is-released&date=today", $this->Socket->request['body']);
  530. }
  531. /**
  532. * Test the scheme + port keys
  533. *
  534. * @return void
  535. */
  536. public function testGetWithSchemeAndPort() {
  537. $this->Socket->reset();
  538. $request = array(
  539. 'uri' => array(
  540. 'scheme' => 'http',
  541. 'host' => 'cakephp.org',
  542. 'port' => 8080,
  543. 'path' => '/',
  544. ),
  545. 'method' => 'GET'
  546. );
  547. $this->Socket->request($request);
  548. $this->assertContains('Host: cakephp.org:8080', $this->Socket->request['header']);
  549. }
  550. /**
  551. * Test URLs like http://cakephp.org/index.php?somestring without key/value pair for query
  552. *
  553. * @return void
  554. */
  555. public function testRequestWithStringQuery() {
  556. $this->Socket->reset();
  557. $request = array(
  558. 'uri' => array(
  559. 'scheme' => 'http',
  560. 'host' => 'cakephp.org',
  561. 'path' => 'index.php',
  562. 'query' => 'somestring'
  563. ),
  564. 'method' => 'GET'
  565. );
  566. $this->Socket->request($request);
  567. $this->assertContains("GET /index.php?somestring HTTP/1.1", $this->Socket->request['line']);
  568. }
  569. /**
  570. * The "*" asterisk character is only allowed for the following methods: OPTIONS.
  571. *
  572. * @expectedException SocketException
  573. * @return void
  574. */
  575. public function testRequestNotAllowedUri() {
  576. $this->Socket->reset();
  577. $request = array('uri' => '*', 'method' => 'GET');
  578. $this->Socket->request($request);
  579. }
  580. /**
  581. * testRequest2 method
  582. *
  583. * @return void
  584. */
  585. public function testRequest2() {
  586. $this->Socket->reset();
  587. $request = array('uri' => 'htpp://www.cakephp.org/');
  588. $number = mt_rand(0, 9999999);
  589. $this->Socket->expects($this->any())->method('connect')->will($this->returnValue(true));
  590. $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>";
  591. $this->Socket->expects($this->at(0))->method('write')
  592. ->with("GET / HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n\r\n");
  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. $response = (string)$this->Socket->request($request);
  596. $this->assertEquals($response, "<h1>Hello, your lucky number is " . $number . "</h1>");
  597. }
  598. /**
  599. * testRequest3 method
  600. *
  601. * @return void
  602. */
  603. public function testRequest3() {
  604. $request = array('uri' => 'htpp://www.cakephp.org/');
  605. $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>";
  606. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  607. $this->Socket->connected = true;
  608. $this->Socket->request($request);
  609. $result = $this->Socket->response['cookies'];
  610. $expected = array(
  611. 'foo' => array(
  612. 'value' => 'bar'
  613. )
  614. );
  615. $this->assertEquals($expected, $result);
  616. $this->assertEquals($expected, $this->Socket->config['request']['cookies']['www.cakephp.org']);
  617. $this->assertFalse($this->Socket->connected);
  618. }
  619. /**
  620. * testRequestWithConstructor method
  621. *
  622. * @return void
  623. */
  624. public function testRequestWithConstructor() {
  625. $request = array(
  626. 'request' => array(
  627. 'uri' => array(
  628. 'scheme' => 'http',
  629. 'host' => 'localhost',
  630. 'port' => '5984',
  631. 'user' => null,
  632. 'pass' => null
  633. )
  634. )
  635. );
  636. $http = $this->getMock('TestHttpSocket', array('read', 'write', 'connect', 'request'), array($request));
  637. $expected = array('method' => 'GET', 'uri' => '/_test');
  638. $http->expects($this->at(0))->method('request')->with($expected);
  639. $http->get('/_test');
  640. $expected = array('method' => 'GET', 'uri' => 'http://localhost:5984/_test?count=4');
  641. $http->expects($this->at(0))->method('request')->with($expected);
  642. $http->get('/_test', array('count' => 4));
  643. }
  644. /**
  645. * testRequestWithResource
  646. *
  647. * @return void
  648. */
  649. public function testRequestWithResource() {
  650. $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>";
  651. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  652. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  653. $this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse));
  654. $this->Socket->connected = true;
  655. $f = fopen(TMP . 'download.txt', 'w');
  656. if (!$f) {
  657. $this->markTestSkipped('Can not write in TMP directory.');
  658. }
  659. $this->Socket->setContentResource($f);
  660. $result = (string)$this->Socket->request('http://www.cakephp.org/');
  661. $this->assertEquals('', $result);
  662. $this->assertEquals('CakeHttp Server', $this->Socket->response['header']['Server']);
  663. fclose($f);
  664. $this->assertEquals(file_get_contents(TMP . 'download.txt'), '<h1>This is a test!</h1>');
  665. unlink(TMP . 'download.txt');
  666. $this->Socket->setContentResource(false);
  667. $result = (string)$this->Socket->request('http://www.cakephp.org/');
  668. $this->assertEquals('<h1>This is a test!</h1>', $result);
  669. }
  670. /**
  671. * testRequestWithCrossCookie
  672. *
  673. * @return void
  674. */
  675. public function testRequestWithCrossCookie() {
  676. $this->Socket->connected = true;
  677. $this->Socket->config['request']['cookies'] = array();
  678. $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>";
  679. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  680. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  681. $expected = array('www.cakephp.org' => array('foo' => array('value' => 'bar')));
  682. $this->Socket->request('http://www.cakephp.org/');
  683. $this->assertEquals($expected, $this->Socket->config['request']['cookies']);
  684. $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>";
  685. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  686. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  687. $this->Socket->request('http://www.cakephp.org/other');
  688. $this->assertEquals(array('foo' => array('value' => 'bar')), $this->Socket->request['cookies']);
  689. $expected['www.cakephp.org'] += array('bar' => array('value' => 'foo'));
  690. $this->assertEquals($expected, $this->Socket->config['request']['cookies']);
  691. $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>";
  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('/other2');
  695. $this->assertEquals($expected, $this->Socket->config['request']['cookies']);
  696. $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>";
  697. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  698. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  699. $this->Socket->request('http://www.cake.com');
  700. $this->assertTrue(empty($this->Socket->request['cookies']));
  701. $expected['www.cake.com'] = array('foobar' => array('value' => 'ok'));
  702. $this->assertEquals($expected, $this->Socket->config['request']['cookies']);
  703. }
  704. /**
  705. * testRequestCustomResponse
  706. *
  707. * @return void
  708. */
  709. public function testRequestCustomResponse() {
  710. $this->Socket->connected = true;
  711. $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>";
  712. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  713. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  714. $this->Socket->responseClass = 'CustomResponse';
  715. $response = $this->Socket->request('http://www.cakephp.org/');
  716. $this->assertInstanceOf('CustomResponse', $response);
  717. $this->assertEquals('HTTP/1.x 2', $response->first10);
  718. }
  719. /**
  720. * Test that redirect URLs are urldecoded
  721. *
  722. * @return void
  723. */
  724. public function testRequestWithRedirectUrlEncoded() {
  725. $request = array(
  726. 'uri' => 'http://localhost/oneuri',
  727. 'redirect' => 1
  728. );
  729. $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";
  730. $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>";
  731. $this->Socket->expects($this->at(1))
  732. ->method('read')
  733. ->will($this->returnValue($serverResponse1));
  734. $this->Socket->expects($this->at(3))
  735. ->method('write')
  736. ->with($this->logicalAnd(
  737. $this->stringContains('Host: i.cmpnet.com'),
  738. $this->stringContains('GET /techonline/pdf/a.pdf')
  739. ));
  740. $this->Socket->expects($this->at(4))
  741. ->method('read')
  742. ->will($this->returnValue($serverResponse2));
  743. $response = $this->Socket->request($request);
  744. $this->assertEquals('<h1>You have been redirected</h1>', $response->body());
  745. }
  746. /**
  747. * testRequestWithRedirect method
  748. *
  749. * @return void
  750. */
  751. public function testRequestWithRedirectAsTrue() {
  752. $request = array(
  753. 'uri' => 'http://localhost/oneuri',
  754. 'redirect' => true
  755. );
  756. $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";
  757. $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>";
  758. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
  759. $this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
  760. $response = $this->Socket->request($request);
  761. $this->assertEquals('<h1>You have been redirected</h1>', $response->body());
  762. }
  763. /**
  764. * Test that redirects with a count limit are decremented.
  765. *
  766. * @return void
  767. */
  768. public function testRequestWithRedirectAsInt() {
  769. $request = array(
  770. 'uri' => 'http://localhost/oneuri',
  771. 'redirect' => 2
  772. );
  773. $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";
  774. $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>";
  775. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
  776. $this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
  777. $this->Socket->request($request);
  778. $this->assertEquals(1, $this->Socket->request['redirect']);
  779. }
  780. /**
  781. * Test that redirects after the redirect count reaches 9 are not followed.
  782. *
  783. * @return void
  784. */
  785. public function testRequestWithRedirectAsIntReachingZero() {
  786. $request = array(
  787. 'uri' => 'http://localhost/oneuri',
  788. 'redirect' => 1
  789. );
  790. $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";
  791. $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";
  792. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
  793. $this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
  794. $response = $this->Socket->request($request);
  795. $this->assertEquals(0, $this->Socket->request['redirect']);
  796. $this->assertEquals(302, $response->code);
  797. $this->assertEquals('http://localhost/anotheruri', $response->getHeader('Location'));
  798. }
  799. /**
  800. * testProxy method
  801. *
  802. * @return void
  803. */
  804. public function testProxy() {
  805. $this->Socket->reset();
  806. $this->Socket->expects($this->any())->method('connect')->will($this->returnValue(true));
  807. $this->Socket->expects($this->any())->method('read')->will($this->returnValue(false));
  808. $this->Socket->configProxy('proxy.server', 123);
  809. $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";
  810. $this->Socket->request('http://www.cakephp.org/');
  811. $this->assertEquals($expected, $this->Socket->request['raw']);
  812. $this->assertEquals('proxy.server', $this->Socket->config['host']);
  813. $this->assertEquals(123, $this->Socket->config['port']);
  814. $expected = array(
  815. 'host' => 'proxy.server',
  816. 'port' => 123,
  817. 'method' => null,
  818. 'user' => null,
  819. 'pass' => null
  820. );
  821. $this->assertEquals($expected, $this->Socket->request['proxy']);
  822. $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";
  823. $this->Socket->request('/bakery');
  824. $this->assertEquals($expected, $this->Socket->request['raw']);
  825. $this->assertEquals('proxy.server', $this->Socket->config['host']);
  826. $this->assertEquals(123, $this->Socket->config['port']);
  827. $expected = array(
  828. 'host' => 'proxy.server',
  829. 'port' => 123,
  830. 'method' => null,
  831. 'user' => null,
  832. 'pass' => null
  833. );
  834. $this->assertEquals($expected, $this->Socket->request['proxy']);
  835. $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";
  836. $this->Socket->configProxy('proxy.server', 123, 'Test', 'mark', 'secret');
  837. $this->Socket->request('http://www.cakephp.org/');
  838. $this->assertEquals($expected, $this->Socket->request['raw']);
  839. $this->assertEquals('proxy.server', $this->Socket->config['host']);
  840. $this->assertEquals(123, $this->Socket->config['port']);
  841. $expected = array(
  842. 'host' => 'proxy.server',
  843. 'port' => 123,
  844. 'method' => 'Test',
  845. 'user' => 'mark',
  846. 'pass' => 'secret'
  847. );
  848. $this->assertEquals($expected, $this->Socket->request['proxy']);
  849. $this->Socket->configAuth('Test', 'login', 'passwd');
  850. $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";
  851. $this->Socket->request('http://www.cakephp.org/');
  852. $this->assertEquals($expected, $this->Socket->request['raw']);
  853. $expected = array(
  854. 'host' => 'proxy.server',
  855. 'port' => 123,
  856. 'method' => 'Test',
  857. 'user' => 'mark',
  858. 'pass' => 'secret'
  859. );
  860. $this->assertEquals($expected, $this->Socket->request['proxy']);
  861. $expected = array(
  862. 'Test' => array(
  863. 'user' => 'login',
  864. 'pass' => 'passwd'
  865. )
  866. );
  867. $this->assertEquals($expected, $this->Socket->request['auth']);
  868. }
  869. /**
  870. * testUrl method
  871. *
  872. * @return void
  873. */
  874. public function testUrl() {
  875. $this->Socket->reset(true);
  876. $this->assertEquals(false, $this->Socket->url(true));
  877. $url = $this->Socket->url('www.cakephp.org');
  878. $this->assertEquals('http://www.cakephp.org/', $url);
  879. $url = $this->Socket->url('https://www.cakephp.org/posts/add');
  880. $this->assertEquals('https://www.cakephp.org/posts/add', $url);
  881. $url = $this->Socket->url('http://www.cakephp/search?q=socket', '/%path?%query');
  882. $this->assertEquals('/search?q=socket', $url);
  883. $this->Socket->config['request']['uri']['host'] = 'bakery.cakephp.org';
  884. $url = $this->Socket->url();
  885. $this->assertEquals('http://bakery.cakephp.org/', $url);
  886. $this->Socket->configUri('http://www.cakephp.org');
  887. $url = $this->Socket->url('/search?q=bar');
  888. $this->assertEquals('http://www.cakephp.org/search?q=bar', $url);
  889. $url = $this->Socket->url(array('host' => 'www.foobar.org', 'query' => array('q' => 'bar')));
  890. $this->assertEquals('http://www.foobar.org/?q=bar', $url);
  891. $url = $this->Socket->url(array('path' => '/supersearch', 'query' => array('q' => 'bar')));
  892. $this->assertEquals('http://www.cakephp.org/supersearch?q=bar', $url);
  893. $this->Socket->configUri('http://www.google.com');
  894. $url = $this->Socket->url('/search?q=socket');
  895. $this->assertEquals('http://www.google.com/search?q=socket', $url);
  896. $url = $this->Socket->url();
  897. $this->assertEquals('http://www.google.com/', $url);
  898. $this->Socket->configUri('https://www.google.com');
  899. $url = $this->Socket->url('/search?q=socket');
  900. $this->assertEquals('https://www.google.com/search?q=socket', $url);
  901. $this->Socket->reset();
  902. $this->Socket->configUri('www.google.com:443');
  903. $url = $this->Socket->url('/search?q=socket');
  904. $this->assertEquals('https://www.google.com/search?q=socket', $url);
  905. $this->Socket->reset();
  906. $this->Socket->configUri('www.google.com:8080');
  907. $url = $this->Socket->url('/search?q=socket');
  908. $this->assertEquals('http://www.google.com:8080/search?q=socket', $url);
  909. }
  910. /**
  911. * testGet method
  912. *
  913. * @return void
  914. */
  915. public function testGet() {
  916. $this->RequestSocket->reset();
  917. $this->RequestSocket->expects($this->at(0))
  918. ->method('request')
  919. ->with(array('method' => 'GET', 'uri' => 'http://www.google.com/'));
  920. $this->RequestSocket->expects($this->at(1))
  921. ->method('request')
  922. ->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar'));
  923. $this->RequestSocket->expects($this->at(2))
  924. ->method('request')
  925. ->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar'));
  926. $this->RequestSocket->expects($this->at(3))
  927. ->method('request')
  928. ->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=23&foobar=42'));
  929. $this->RequestSocket->expects($this->at(4))
  930. ->method('request')
  931. ->with(array('method' => 'GET', 'uri' => 'http://www.google.com/', 'version' => '1.0'));
  932. $this->RequestSocket->expects($this->at(5))
  933. ->method('request')
  934. ->with(array('method' => 'GET', 'uri' => 'https://secure.example.com/test.php?one=two'));
  935. $this->RequestSocket->expects($this->at(6))
  936. ->method('request')
  937. ->with(array('method' => 'GET', 'uri' => 'https://example.com/oauth/access?clientid=123&redirect_uri=http%3A%2F%2Fexample.com&code=456'));
  938. $this->RequestSocket->get('http://www.google.com/');
  939. $this->RequestSocket->get('http://www.google.com/', array('foo' => 'bar'));
  940. $this->RequestSocket->get('http://www.google.com/', 'foo=bar');
  941. $this->RequestSocket->get('http://www.google.com/?foo=bar', array('foobar' => '42', 'foo' => '23'));
  942. $this->RequestSocket->get('http://www.google.com/', null, array('version' => '1.0'));
  943. $this->RequestSocket->get('https://secure.example.com/test.php', array('one' => 'two'));
  944. $this->RequestSocket->get('https://example.com/oauth/access', array(
  945. 'clientid' => '123',
  946. 'redirect_uri' => 'http://example.com',
  947. 'code' => 456
  948. ));
  949. }
  950. /**
  951. * Test authentication
  952. *
  953. * @return void
  954. */
  955. public function testAuth() {
  956. $this->Socket->get('http://mark:secret@example.com/test');
  957. $this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  958. $this->Socket->configAuth(false);
  959. $this->Socket->get('http://example.com/test');
  960. $this->assertFalse(strpos($this->Socket->request['header'], 'Authorization:'));
  961. $this->Socket->configAuth('Test', 'mark', 'passwd');
  962. $this->Socket->get('http://example.com/test');
  963. $this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Test mark.passwd') !== false);
  964. $this->Socket->configAuth(false);
  965. $this->Socket->request(array(
  966. 'method' => 'GET',
  967. 'uri' => 'http://example.com/test',
  968. 'auth' => array(
  969. 'method' => 'Basic',
  970. 'user' => 'joel',
  971. 'pass' => 'hunter2'
  972. )
  973. ));
  974. $this->assertEquals($this->Socket->request['auth'], array('Basic' => array('user' => 'joel', 'pass' => 'hunter2')));
  975. $this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic am9lbDpodW50ZXIy') !== false);
  976. $this->Socket->configAuth('Basic', 'mark', 'password');
  977. $this->Socket->request(array(
  978. 'method' => 'GET',
  979. 'uri' => 'http://example.com/test',
  980. 'header' => array(
  981. 'Authorization' => 'OtherAuth Hi.There'
  982. )
  983. ));
  984. $this->assertPattern('/Authorization: OtherAuth Hi\.There/m', $this->Socket->request['header']);
  985. }
  986. /**
  987. * test that two consecutive get() calls reset the authentication credentials.
  988. *
  989. * @return void
  990. */
  991. public function testConsecutiveGetResetsAuthCredentials() {
  992. $this->Socket->get('http://mark:secret@example.com/test');
  993. $this->assertEquals('mark', $this->Socket->request['uri']['user']);
  994. $this->assertEquals('secret', $this->Socket->request['uri']['pass']);
  995. $this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  996. $this->Socket->get('/test2');
  997. $this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  998. $this->Socket->request(array(
  999. 'method' => 'GET',
  1000. 'uri' => 'http://example.com/test',
  1001. 'header' => array(
  1002. 'Authorization' => 'OtherAuth Hi.There'
  1003. )
  1004. ));
  1005. $this->assertPattern('/Authorization: OtherAuth Hi\.There/m', $this->Socket->request['header']);
  1006. $this->Socket->get('/test3');
  1007. $this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  1008. }
  1009. /**
  1010. * testPostPutDelete method
  1011. *
  1012. * @return void
  1013. */
  1014. public function testPost() {
  1015. $this->RequestSocket->reset();
  1016. $this->RequestSocket->expects($this->at(0))
  1017. ->method('request')
  1018. ->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => array()));
  1019. $this->RequestSocket->expects($this->at(1))
  1020. ->method('request')
  1021. ->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  1022. $this->RequestSocket->expects($this->at(2))
  1023. ->method('request')
  1024. ->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  1025. $this->RequestSocket->post('http://www.google.com/');
  1026. $this->RequestSocket->post('http://www.google.com/', array('Foo' => 'bar'));
  1027. $this->RequestSocket->post('http://www.google.com/', null, array('line' => 'Hey Server'));
  1028. }
  1029. /**
  1030. * testPut
  1031. *
  1032. * @return void
  1033. */
  1034. public function testPut() {
  1035. $this->RequestSocket->reset();
  1036. $this->RequestSocket->expects($this->at(0))
  1037. ->method('request')
  1038. ->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => array()));
  1039. $this->RequestSocket->expects($this->at(1))
  1040. ->method('request')
  1041. ->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  1042. $this->RequestSocket->expects($this->at(2))
  1043. ->method('request')
  1044. ->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  1045. $this->RequestSocket->put('http://www.google.com/');
  1046. $this->RequestSocket->put('http://www.google.com/', array('Foo' => 'bar'));
  1047. $this->RequestSocket->put('http://www.google.com/', null, array('line' => 'Hey Server'));
  1048. }
  1049. /**
  1050. * testPatch
  1051. *
  1052. * @return void
  1053. */
  1054. public function testPatch() {
  1055. $this->RequestSocket->reset();
  1056. $this->RequestSocket->expects($this->at(0))
  1057. ->method('request')
  1058. ->with(array('method' => 'PATCH', 'uri' => 'http://www.google.com/', 'body' => array()));
  1059. $this->RequestSocket->expects($this->at(1))
  1060. ->method('request')
  1061. ->with(array('method' => 'PATCH', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  1062. $this->RequestSocket->expects($this->at(2))
  1063. ->method('request')
  1064. ->with(array('method' => 'PATCH', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  1065. $this->RequestSocket->patch('http://www.google.com/');
  1066. $this->RequestSocket->patch('http://www.google.com/', array('Foo' => 'bar'));
  1067. $this->RequestSocket->patch('http://www.google.com/', null, array('line' => 'Hey Server'));
  1068. }
  1069. /**
  1070. * testDelete
  1071. *
  1072. * @return void
  1073. */
  1074. public function testDelete() {
  1075. $this->RequestSocket->reset();
  1076. $this->RequestSocket->expects($this->at(0))
  1077. ->method('request')
  1078. ->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => array()));
  1079. $this->RequestSocket->expects($this->at(1))
  1080. ->method('request')
  1081. ->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  1082. $this->RequestSocket->expects($this->at(2))
  1083. ->method('request')
  1084. ->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  1085. $this->RequestSocket->delete('http://www.google.com/');
  1086. $this->RequestSocket->delete('http://www.google.com/', array('Foo' => 'bar'));
  1087. $this->RequestSocket->delete('http://www.google.com/', null, array('line' => 'Hey Server'));
  1088. }
  1089. /**
  1090. * testBuildRequestLine method
  1091. *
  1092. * @return void
  1093. */
  1094. public function testBuildRequestLine() {
  1095. $this->Socket->reset();
  1096. $this->Socket->quirksMode = true;
  1097. $r = $this->Socket->buildRequestLine('Foo');
  1098. $this->assertEquals('Foo', $r);
  1099. $this->Socket->quirksMode = false;
  1100. $r = $this->Socket->buildRequestLine(true);
  1101. $this->assertEquals(false, $r);
  1102. $r = $this->Socket->buildRequestLine(array('foo' => 'bar', 'method' => 'foo'));
  1103. $this->assertEquals(false, $r);
  1104. $r = $this->Socket->buildRequestLine(array('method' => 'GET', 'uri' => 'http://www.cakephp.org/search?q=socket'));
  1105. $this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
  1106. $request = array(
  1107. 'method' => 'GET',
  1108. 'uri' => array(
  1109. 'path' => '/search',
  1110. 'query' => array('q' => 'socket')
  1111. )
  1112. );
  1113. $r = $this->Socket->buildRequestLine($request);
  1114. $this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
  1115. unset($request['method']);
  1116. $r = $this->Socket->buildRequestLine($request);
  1117. $this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
  1118. $r = $this->Socket->buildRequestLine($request, 'CAKE-HTTP/0.1');
  1119. $this->assertEquals("GET /search?q=socket CAKE-HTTP/0.1\r\n", $r);
  1120. $request = array('method' => 'OPTIONS', 'uri' => '*');
  1121. $r = $this->Socket->buildRequestLine($request);
  1122. $this->assertEquals("OPTIONS * HTTP/1.1\r\n", $r);
  1123. $request['method'] = 'GET';
  1124. $this->Socket->quirksMode = true;
  1125. $r = $this->Socket->buildRequestLine($request);
  1126. $this->assertEquals("GET * HTTP/1.1\r\n", $r);
  1127. $r = $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
  1128. $this->assertEquals("GET * HTTP/1.1\r\n", $r);
  1129. }
  1130. /**
  1131. * testBadBuildRequestLine method
  1132. *
  1133. * @expectedException SocketException
  1134. * @return void
  1135. */
  1136. public function testBadBuildRequestLine() {
  1137. $this->Socket->buildRequestLine('Foo');
  1138. }
  1139. /**
  1140. * testBadBuildRequestLine2 method
  1141. *
  1142. * @expectedException SocketException
  1143. * @return void
  1144. */
  1145. public function testBadBuildRequestLine2() {
  1146. $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
  1147. }
  1148. /**
  1149. * Asserts that HttpSocket::parseUri is working properly
  1150. *
  1151. * @return void
  1152. */
  1153. public function testParseUri() {
  1154. $this->Socket->reset();
  1155. $uri = $this->Socket->parseUri(array('invalid' => 'uri-string'));
  1156. $this->assertEquals(false, $uri);
  1157. $uri = $this->Socket->parseUri(array('invalid' => 'uri-string'), array('host' => 'somehost'));
  1158. $this->assertEquals(array('host' => 'somehost', 'invalid' => 'uri-string'), $uri);
  1159. $uri = $this->Socket->parseUri(false);
  1160. $this->assertEquals(false, $uri);
  1161. $uri = $this->Socket->parseUri('/my-cool-path');
  1162. $this->assertEquals(array('path' => '/my-cool-path'), $uri);
  1163. $uri = $this->Socket->parseUri('http://bob:foo123@www.cakephp.org:40/search?q=dessert#results');
  1164. $this->assertEquals($uri, array(
  1165. 'scheme' => 'http',
  1166. 'host' => 'www.cakephp.org',
  1167. 'port' => 40,
  1168. 'user' => 'bob',
  1169. 'pass' => 'foo123',
  1170. 'path' => '/search',
  1171. 'query' => array('q' => 'dessert'),
  1172. 'fragment' => 'results'
  1173. ));
  1174. $uri = $this->Socket->parseUri('http://www.cakephp.org/');
  1175. $this->assertEquals($uri, array(
  1176. 'scheme' => 'http',
  1177. 'host' => 'www.cakephp.org',
  1178. 'path' => '/'
  1179. ));
  1180. $uri = $this->Socket->parseUri('http://www.cakephp.org', true);
  1181. $this->assertEquals($uri, array(
  1182. 'scheme' => 'http',
  1183. 'host' => 'www.cakephp.org',
  1184. 'port' => 80,
  1185. 'user' => null,
  1186. 'pass' => null,
  1187. 'path' => '/',
  1188. 'query' => array(),
  1189. 'fragment' => null
  1190. ));
  1191. $uri = $this->Socket->parseUri('https://www.cakephp.org', true);
  1192. $this->assertEquals($uri, array(
  1193. 'scheme' => 'https',
  1194. 'host' => 'www.cakephp.org',
  1195. 'port' => 443,
  1196. 'user' => null,
  1197. 'pass' => null,
  1198. 'path' => '/',
  1199. 'query' => array(),
  1200. 'fragment' => null
  1201. ));
  1202. $uri = $this->Socket->parseUri('www.cakephp.org:443/query?foo', true);
  1203. $this->assertEquals($uri, array(
  1204. 'scheme' => 'https',
  1205. 'host' => 'www.cakephp.org',
  1206. 'port' => 443,
  1207. 'user' => null,
  1208. 'pass' => null,
  1209. 'path' => '/query',
  1210. 'query' => array('foo' => ""),
  1211. 'fragment' => null
  1212. ));
  1213. $uri = $this->Socket->parseUri('http://www.cakephp.org', array('host' => 'piephp.org', 'user' => 'bob', 'fragment' => 'results'));
  1214. $this->assertEquals($uri, array(
  1215. 'host' => 'www.cakephp.org',
  1216. 'user' => 'bob',
  1217. 'fragment' => 'results',
  1218. 'scheme' => 'http'
  1219. ));
  1220. $uri = $this->Socket->parseUri('https://www.cakephp.org', array('scheme' => 'http', 'port' => 23));
  1221. $this->assertEquals($uri, array(
  1222. 'scheme' => 'https',
  1223. 'port' => 23,
  1224. 'host' => 'www.cakephp.org'
  1225. ));
  1226. $uri = $this->Socket->parseUri('www.cakephp.org:59', array('scheme' => array('http', 'https'), 'port' => 80));
  1227. $this->assertEquals($uri, array(
  1228. 'scheme' => 'http',
  1229. 'port' => 59,
  1230. 'host' => 'www.cakephp.org'
  1231. ));
  1232. $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)));
  1233. $this->assertEquals($uri, array(
  1234. 'scheme' => 'http',
  1235. 'host' => 'www.google.com',
  1236. 'port' => 8080
  1237. ));
  1238. $uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2%3Dvalue3');
  1239. $this->assertEquals($uri, array(
  1240. 'scheme' => 'http',
  1241. 'host' => 'www.cakephp.org',
  1242. 'path' => '/',
  1243. 'query' => array(
  1244. 'param1' => 'value1',
  1245. 'param2' => 'value2=value3'
  1246. )
  1247. ));
  1248. $uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2=value3');
  1249. $this->assertEquals($uri, array(
  1250. 'scheme' => 'http',
  1251. 'host' => 'www.cakephp.org',
  1252. 'path' => '/',
  1253. 'query' => array(
  1254. 'param1' => 'value1',
  1255. 'param2' => 'value2=value3'
  1256. )
  1257. ));
  1258. }
  1259. /**
  1260. * Tests that HttpSocket::buildUri can turn all kinds of uri arrays (and strings) into fully or partially qualified URI's
  1261. *
  1262. * @return void
  1263. */
  1264. public function testBuildUri() {
  1265. $this->Socket->reset();
  1266. $r = $this->Socket->buildUri(true);
  1267. $this->assertEquals(false, $r);
  1268. $r = $this->Socket->buildUri('foo.com');
  1269. $this->assertEquals('http://foo.com/', $r);
  1270. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'));
  1271. $this->assertEquals('http://www.cakephp.org/', $r);
  1272. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https'));
  1273. $this->assertEquals('https://www.cakephp.org/', $r);
  1274. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'port' => 23));
  1275. $this->assertEquals('http://www.cakephp.org:23/', $r);
  1276. $r = $this->Socket->buildUri(array('path' => 'www.google.com/search', 'query' => 'q=cakephp'));
  1277. $this->assertEquals('http://www.google.com/search?q=cakephp', $r);
  1278. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https', 'port' => 79));
  1279. $this->assertEquals('https://www.cakephp.org:79/', $r);
  1280. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => 'foo'));
  1281. $this->assertEquals('http://www.cakephp.org/foo', $r);
  1282. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/foo'));
  1283. $this->assertEquals('http://www.cakephp.org/foo', $r);
  1284. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/search', 'query' => array('q' => 'HttpSocket')));
  1285. $this->assertEquals('http://www.cakephp.org/search?q=HttpSocket', $r);
  1286. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'));
  1287. $this->assertEquals('http://www.cakephp.org/#bar', $r);
  1288. $r = $this->Socket->buildUri(array(
  1289. 'scheme' => 'https',
  1290. 'host' => 'www.cakephp.org',
  1291. 'port' => 25,
  1292. 'user' => 'bob',
  1293. 'pass' => 'secret',
  1294. 'path' => '/cool',
  1295. 'query' => array('foo' => 'bar'),
  1296. 'fragment' => 'comment'
  1297. ));
  1298. $this->assertEquals('https://bob:secret@www.cakephp.org:25/cool?foo=bar#comment', $r);
  1299. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'), '%fragment?%host');
  1300. $this->assertEquals('bar?www.cakephp.org', $r);
  1301. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'), '%fragment???%host');
  1302. $this->assertEquals('???www.cakephp.org', $r);
  1303. $r = $this->Socket->buildUri(array('path' => '*'), '/%path?%query');
  1304. $this->assertEquals('*', $r);
  1305. $r = $this->Socket->buildUri(array('scheme' => 'foo', 'host' => 'www.cakephp.org'));
  1306. $this->assertEquals('foo://www.cakephp.org:80/', $r);
  1307. }
  1308. /**
  1309. * Asserts that HttpSocket::parseQuery is working properly
  1310. *
  1311. * @return void
  1312. */
  1313. public function testParseQuery() {
  1314. $this->Socket->reset();
  1315. $query = $this->Socket->parseQuery(array('framework' => 'cakephp'));
  1316. $this->assertEquals(array('framework' => 'cakephp'), $query);
  1317. $query = $this->Socket->parseQuery('');
  1318. $this->assertEquals(array(), $query);
  1319. $query = $this->Socket->parseQuery('framework=cakephp');
  1320. $this->assertEquals(array('framework' => 'cakephp'), $query);
  1321. $query = $this->Socket->parseQuery('?framework=cakephp');
  1322. $this->assertEquals(array('framework' => 'cakephp'), $query);
  1323. $query = $this->Socket->parseQuery('a&b&c');
  1324. $this->assertEquals(array('a' => '', 'b' => '', 'c' => ''), $query);
  1325. $query = $this->Socket->parseQuery('value=12345');
  1326. $this->assertEquals(array('value' => '12345'), $query);
  1327. $query = $this->Socket->parseQuery('a[0]=foo&a[1]=bar&a[2]=cake');
  1328. $this->assertEquals(array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')), $query);
  1329. $query = $this->Socket->parseQuery('a[]=foo&a[]=bar&a[]=cake');
  1330. $this->assertEquals(array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')), $query);
  1331. $query = $this->Socket->parseQuery('a[][]=foo&a[][]=bar&a[][]=cake');
  1332. $expectedQuery = array(
  1333. 'a' => array(
  1334. 0 => array(
  1335. 0 => 'foo'
  1336. ),
  1337. 1 => array(
  1338. 0 => 'bar'
  1339. ),
  1340. array(
  1341. 0 => 'cake'
  1342. )
  1343. )
  1344. );
  1345. $this->assertEquals($expectedQuery, $query);
  1346. $query = $this->Socket->parseQuery('a[][]=foo&a[bar]=php&a[][]=bar&a[][]=cake');
  1347. $expectedQuery = array(
  1348. 'a' => array(
  1349. array('foo'),
  1350. 'bar' => 'php',
  1351. array('bar'),
  1352. array('cake')
  1353. )
  1354. );
  1355. $this->assertEquals($expectedQuery, $query);
  1356. $query = $this->Socket->parseQuery('user[]=jim&user[3]=tom&user[]=bob');
  1357. $expectedQuery = array(
  1358. 'user' => array(
  1359. 0 => 'jim',
  1360. 3 => 'tom',
  1361. 4 => 'bob'
  1362. )
  1363. );
  1364. $this->assertEquals($expectedQuery, $query);
  1365. $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';
  1366. $query = $this->Socket->parseQuery($queryStr);
  1367. $expectedQuery = array(
  1368. 'user' => array(
  1369. 0 => array(
  1370. 'items' => array(
  1371. 'foo',
  1372. 'bar'
  1373. )
  1374. ),
  1375. 1 => array(
  1376. 'name' => 'jim',
  1377. 'items' => array(
  1378. 'personal' => array(
  1379. 'book',
  1380. 'pen'
  1381. ),
  1382. 'ball'
  1383. )
  1384. ),
  1385. 'count' => '2'
  1386. ),
  1387. 'empty' => ''
  1388. );
  1389. $this->assertEquals($expectedQuery, $query);
  1390. $query = 'openid.ns=example.com&foo=bar&foo=baz';
  1391. $result = $this->Socket->parseQuery($query);
  1392. $expected = array(
  1393. 'openid.ns' => 'example.com',
  1394. 'foo' => array('bar', 'baz')
  1395. );
  1396. $this->assertEquals($expected, $result);
  1397. }
  1398. /**
  1399. * Tests that HttpSocket::buildHeader can turn a given $header array into a proper header string according to
  1400. * HTTP 1.1 specs.
  1401. *
  1402. * @return void
  1403. */
  1404. public function testBuildHeader() {
  1405. $this->Socket->reset();
  1406. $r = $this->Socket->buildHeader(true);
  1407. $this->assertEquals(false, $r);
  1408. $r = $this->Socket->buildHeader('My raw header');
  1409. $this->assertEquals('My raw header', $r);
  1410. $r = $this->Socket->buildHeader(array('Host' => 'www.cakephp.org'));
  1411. $this->assertEquals("Host: www.cakephp.org\r\n", $r);
  1412. $r = $this->Socket->buildHeader(array('Host' => 'www.cakephp.org', 'Connection' => 'Close'));
  1413. $this->assertEquals("Host: www.cakephp.org\r\nConnection: Close\r\n", $r);
  1414. $r = $this->Socket->buildHeader(array('People' => array('Bob', 'Jim', 'John')));
  1415. $this->assertEquals("People: Bob,Jim,John\r\n", $r);
  1416. $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\nMulti Line field"));
  1417. $this->assertEquals("Multi-Line-Field: This is my\r\n Multi Line field\r\n", $r);
  1418. $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\n Multi Line field"));
  1419. $this->assertEquals("Multi-Line-Field: This is my\r\n Multi Line field\r\n", $r);
  1420. $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\n\tMulti Line field"));
  1421. $this->assertEquals("Multi-Line-Field: This is my\r\n\tMulti Line field\r\n", $r);
  1422. $r = $this->Socket->buildHeader(array('Test@Field' => "My value"));
  1423. $this->assertEquals("Test\"@\"Field: My value\r\n", $r);
  1424. }
  1425. /**
  1426. * testBuildCookies method
  1427. *
  1428. * @return void
  1429. */
  1430. public function testBuildCookies() {
  1431. $cookies = array(
  1432. 'foo' => array(
  1433. 'value' => 'bar'
  1434. ),
  1435. 'people' => array(
  1436. 'value' => 'jim,jack,johnny;',
  1437. 'path' => '/accounts'
  1438. ),
  1439. 'key' => 'value'
  1440. );
  1441. $expect = "Cookie: foo=bar; people=jim,jack,johnny\";\"; key=value\r\n";
  1442. $result = $this->Socket->buildCookies($cookies);
  1443. $this->assertEquals($expect, $result);
  1444. }
  1445. /**
  1446. * Tests that HttpSocket::_tokenEscapeChars() returns the right characters.
  1447. *
  1448. * @return void
  1449. */
  1450. public function testTokenEscapeChars() {
  1451. $this->Socket->reset();
  1452. $expected = array(
  1453. '\x22', '\x28', '\x29', '\x3c', '\x3e', '\x40', '\x2c', '\x3b', '\x3a', '\x5c', '\x2f', '\x5b', '\x5d', '\x3f', '\x3d', '\x7b',
  1454. '\x7d', '\x20', '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d',
  1455. '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d',
  1456. '\x1e', '\x1f', '\x7f'
  1457. );
  1458. $r = $this->Socket->tokenEscapeChars();
  1459. $this->assertEquals($expected, $r);
  1460. foreach ($expected as $key => $char) {
  1461. $expected[$key] = chr(hexdec(substr($char, 2)));
  1462. }
  1463. $r = $this->Socket->tokenEscapeChars(false);
  1464. $this->assertEquals($expected, $r);
  1465. }
  1466. /**
  1467. * Test that HttpSocket::escapeToken is escaping all characters as described in RFC 2616 (HTTP 1.1 specs)
  1468. *
  1469. * @return void
  1470. */
  1471. public function testEscapeToken() {
  1472. $this->Socket->reset();
  1473. $this->assertEquals('Foo', $this->Socket->escapeToken('Foo'));
  1474. $escape = $this->Socket->tokenEscapeChars(false);
  1475. foreach ($escape as $char) {
  1476. $token = 'My-special-' . $char . '-Token';
  1477. $escapedToken = $this->Socket->escapeToken($token);
  1478. $expectedToken = 'My-special-"' . $char . '"-Token';
  1479. $this->assertEquals($expectedToken, $escapedToken, 'Test token escaping for ASCII ' . ord($char));
  1480. }
  1481. $token = 'Extreme-:Token- -"@-test';
  1482. $escapedToken = $this->Socket->escapeToken($token);
  1483. $expectedToken = 'Extreme-":"Token-" "-""""@"-test';
  1484. $this->assertEquals($expectedToken, $escapedToken);
  1485. }
  1486. /**
  1487. * This tests asserts HttpSocket::reset() resets a HttpSocket instance to it's initial state (before Object::__construct
  1488. * got executed)
  1489. *
  1490. * @return void
  1491. */
  1492. public function testReset() {
  1493. $this->Socket->reset();
  1494. $initialState = get_class_vars('HttpSocket');
  1495. foreach ($initialState as $property => $value) {
  1496. $this->Socket->{$property} = 'Overwritten';
  1497. }
  1498. $return = $this->Socket->reset();
  1499. foreach ($initialState as $property => $value) {
  1500. $this->assertEquals($this->Socket->{$property}, $value);
  1501. }
  1502. $this->assertEquals(true, $return);
  1503. }
  1504. /**
  1505. * This tests asserts HttpSocket::reset(false) resets certain HttpSocket properties to their initial state (before
  1506. * Object::__construct got executed).
  1507. *
  1508. * @return void
  1509. */
  1510. public function testPartialReset() {
  1511. $this->Socket->reset();
  1512. $partialResetProperties = array('request', 'response');
  1513. $initialState = get_class_vars('HttpSocket');
  1514. foreach ($initialState as $property => $value) {
  1515. $this->Socket->{$property} = 'Overwritten';
  1516. }
  1517. $return = $this->Socket->reset(false);
  1518. foreach ($initialState as $property => $originalValue) {
  1519. if (in_array($property, $partialResetProperties)) {
  1520. $this->assertEquals($this->Socket->{$property}, $originalValue);
  1521. } else {
  1522. $this->assertEquals('Overwritten', $this->Socket->{$property});
  1523. }
  1524. }
  1525. $this->assertEquals(true, $return);
  1526. }
  1527. /**
  1528. * test configuring the context from the flat keys.
  1529. *
  1530. * @return void
  1531. */
  1532. public function testConfigContext() {
  1533. $this->Socket->reset();
  1534. $this->Socket->request('http://example.com');
  1535. $this->assertTrue($this->Socket->config['context']['ssl']['verify_peer']);
  1536. $this->assertFalse($this->Socket->config['context']['ssl']['allow_self_signed']);
  1537. $this->assertEquals(5, $this->Socket->config['context']['ssl']['verify_depth']);
  1538. $this->assertEquals('example.com', $this->Socket->config['context']['ssl']['CN_match']);
  1539. $this->assertArrayNotHasKey('ssl_verify_peer', $this->Socket->config);
  1540. $this->assertArrayNotHasKey('ssl_allow_self_signed', $this->Socket->config);
  1541. $this->assertArrayNotHasKey('ssl_verify_host', $this->Socket->config);
  1542. $this->assertArrayNotHasKey('ssl_verify_depth', $this->Socket->config);
  1543. }
  1544. /**
  1545. * Test that requests fail when peer verification fails.
  1546. *
  1547. * @return void
  1548. */
  1549. public function testVerifyPeer() {
  1550. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  1551. $socket = new HttpSocket();
  1552. try {
  1553. $socket->get('https://tv.eurosport.com/');
  1554. $this->markTestSkipped('Found valid certificate, was expecting invalid certificate.');
  1555. } catch (SocketException $e) {
  1556. $message = $e->getMessage();
  1557. $this->skipIf(strpos($message, 'Invalid HTTP') !== false, 'Invalid HTTP Response received, skipping.');
  1558. $this->assertContains('Peer certificate CN', $message);
  1559. $this->assertContains('Failed to enable crypto', $message);
  1560. }
  1561. }
  1562. /**
  1563. * Data provider for status codes.
  1564. *
  1565. * @return array
  1566. */
  1567. public function statusProvider() {
  1568. return array(
  1569. array('HTTP/1.1 200 ', '200'),
  1570. array('HTTP/1.1 200 ', '200'),
  1571. array('HTTP/1.1 200', '200'),
  1572. array('HTTP/1.1 200 OK', '200', 'OK'),
  1573. array('HTTP/1.1 404 Not Found', '404', 'Not Found'),
  1574. array('HTTP/1.1 404 Not Found', '404', 'Not Found'),
  1575. );
  1576. }
  1577. /**
  1578. * test response status parsing
  1579. *
  1580. * @dataProvider statusProvider
  1581. * @return void
  1582. */
  1583. public function testResponseStatusParsing($status, $code, $msg = '') {
  1584. $this->Socket->connected = true;
  1585. $serverResponse = $status . "\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n\r\n<h1>This is a test!</h1>";
  1586. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  1587. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  1588. $response = $this->Socket->request('http://www.cakephp.org/');
  1589. $this->assertInstanceOf('HttpSocketResponse', $response);
  1590. $expected = array(
  1591. 'http-version' => 'HTTP/1.1',
  1592. 'code' => $code,
  1593. 'reason-phrase' => $msg
  1594. );
  1595. $this->assertEquals($expected, $response['status']);
  1596. }
  1597. }