PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

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