PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/westonkjones/westonkjones
PHP | 1794 lines | 1238 code | 216 blank | 340 comment | 5 complexity | ce6be42856ba3b948d5ff8a5c41f9fce 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 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. $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. }
  977. /**
  978. * test that two consecutive get() calls reset the authentication credentials.
  979. *
  980. * @return void
  981. */
  982. public function testConsecutiveGetResetsAuthCredentials() {
  983. $this->Socket->get('http://mark:secret@example.com/test');
  984. $this->assertEquals('mark', $this->Socket->request['uri']['user']);
  985. $this->assertEquals('secret', $this->Socket->request['uri']['pass']);
  986. $this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  987. $this->Socket->get('/test2');
  988. $this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  989. $this->Socket->get('/test3');
  990. $this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  991. }
  992. /**
  993. * testPostPutDelete method
  994. *
  995. * @return void
  996. */
  997. public function testPost() {
  998. $this->RequestSocket->reset();
  999. $this->RequestSocket->expects($this->at(0))
  1000. ->method('request')
  1001. ->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => array()));
  1002. $this->RequestSocket->expects($this->at(1))
  1003. ->method('request')
  1004. ->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  1005. $this->RequestSocket->expects($this->at(2))
  1006. ->method('request')
  1007. ->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  1008. $this->RequestSocket->post('http://www.google.com/');
  1009. $this->RequestSocket->post('http://www.google.com/', array('Foo' => 'bar'));
  1010. $this->RequestSocket->post('http://www.google.com/', null, array('line' => 'Hey Server'));
  1011. }
  1012. /**
  1013. * testPut
  1014. *
  1015. * @return void
  1016. */
  1017. public function testPut() {
  1018. $this->RequestSocket->reset();
  1019. $this->RequestSocket->expects($this->at(0))
  1020. ->method('request')
  1021. ->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => array()));
  1022. $this->RequestSocket->expects($this->at(1))
  1023. ->method('request')
  1024. ->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  1025. $this->RequestSocket->expects($this->at(2))
  1026. ->method('request')
  1027. ->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  1028. $this->RequestSocket->put('http://www.google.com/');
  1029. $this->RequestSocket->put('http://www.google.com/', array('Foo' => 'bar'));
  1030. $this->RequestSocket->put('http://www.google.com/', null, array('line' => 'Hey Server'));
  1031. }
  1032. /**
  1033. * testPatch
  1034. *
  1035. * @return void
  1036. */
  1037. public function testPatch() {
  1038. $this->RequestSocket->reset();
  1039. $this->RequestSocket->expects($this->at(0))
  1040. ->method('request')
  1041. ->with(array('method' => 'PATCH', 'uri' => 'http://www.google.com/', 'body' => array()));
  1042. $this->RequestSocket->expects($this->at(1))
  1043. ->method('request')
  1044. ->with(array('method' => 'PATCH', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  1045. $this->RequestSocket->expects($this->at(2))
  1046. ->method('request')
  1047. ->with(array('method' => 'PATCH', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  1048. $this->RequestSocket->patch('http://www.google.com/');
  1049. $this->RequestSocket->patch('http://www.google.com/', array('Foo' => 'bar'));
  1050. $this->RequestSocket->patch('http://www.google.com/', null, array('line' => 'Hey Server'));
  1051. }
  1052. /**
  1053. * testDelete
  1054. *
  1055. * @return void
  1056. */
  1057. public function testDelete() {
  1058. $this->RequestSocket->reset();
  1059. $this->RequestSocket->expects($this->at(0))
  1060. ->method('request')
  1061. ->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => array()));
  1062. $this->RequestSocket->expects($this->at(1))
  1063. ->method('request')
  1064. ->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  1065. $this->RequestSocket->expects($this->at(2))
  1066. ->method('request')
  1067. ->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  1068. $this->RequestSocket->delete('http://www.google.com/');
  1069. $this->RequestSocket->delete('http://www.google.com/', array('Foo' => 'bar'));
  1070. $this->RequestSocket->delete('http://www.google.com/', null, array('line' => 'Hey Server'));
  1071. }
  1072. /**
  1073. * testBuildRequestLine method
  1074. *
  1075. * @return void
  1076. */
  1077. public function testBuildRequestLine() {
  1078. $this->Socket->reset();
  1079. $this->Socket->quirksMode = true;
  1080. $r = $this->Socket->buildRequestLine('Foo');
  1081. $this->assertEquals('Foo', $r);
  1082. $this->Socket->quirksMode = false;
  1083. $r = $this->Socket->buildRequestLine(true);
  1084. $this->assertEquals(false, $r);
  1085. $r = $this->Socket->buildRequestLine(array('foo' => 'bar', 'method' => 'foo'));
  1086. $this->assertEquals(false, $r);
  1087. $r = $this->Socket->buildRequestLine(array('method' => 'GET', 'uri' => 'http://www.cakephp.org/search?q=socket'));
  1088. $this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
  1089. $request = array(
  1090. 'method' => 'GET',
  1091. 'uri' => array(
  1092. 'path' => '/search',
  1093. 'query' => array('q' => 'socket')
  1094. )
  1095. );
  1096. $r = $this->Socket->buildRequestLine($request);
  1097. $this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
  1098. unset($request['method']);
  1099. $r = $this->Socket->buildRequestLine($request);
  1100. $this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
  1101. $r = $this->Socket->buildRequestLine($request, 'CAKE-HTTP/0.1');
  1102. $this->assertEquals("GET /search?q=socket CAKE-HTTP/0.1\r\n", $r);
  1103. $request = array('method' => 'OPTIONS', 'uri' => '*');
  1104. $r = $this->Socket->buildRequestLine($request);
  1105. $this->assertEquals("OPTIONS * HTTP/1.1\r\n", $r);
  1106. $request['method'] = 'GET';
  1107. $this->Socket->quirksMode = true;
  1108. $r = $this->Socket->buildRequestLine($request);
  1109. $this->assertEquals("GET * HTTP/1.1\r\n", $r);
  1110. $r = $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
  1111. $this->assertEquals("GET * HTTP/1.1\r\n", $r);
  1112. }
  1113. /**
  1114. * testBadBuildRequestLine method
  1115. *
  1116. * @expectedException SocketException
  1117. * @return void
  1118. */
  1119. public function testBadBuildRequestLine() {
  1120. $this->Socket->buildRequestLine('Foo');
  1121. }
  1122. /**
  1123. * testBadBuildRequestLine2 method
  1124. *
  1125. * @expectedException SocketException
  1126. * @return void
  1127. */
  1128. public function testBadBuildRequestLine2() {
  1129. $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
  1130. }
  1131. /**
  1132. * Asserts that HttpSocket::parseUri is working properly
  1133. *
  1134. * @return void
  1135. */
  1136. public function testParseUri() {
  1137. $this->Socket->reset();
  1138. $uri = $this->Socket->parseUri(array('invalid' => 'uri-string'));
  1139. $this->assertEquals(false, $uri);
  1140. $uri = $this->Socket->parseUri(array('invalid' => 'uri-string'), array('host' => 'somehost'));
  1141. $this->assertEquals(array('host' => 'somehost', 'invalid' => 'uri-string'), $uri);
  1142. $uri = $this->Socket->parseUri(false);
  1143. $this->assertEquals(false, $uri);
  1144. $uri = $this->Socket->parseUri('/my-cool-path');
  1145. $this->assertEquals(array('path' => '/my-cool-path'), $uri);
  1146. $uri = $this->Socket->parseUri('http://bob:foo123@www.cakephp.org:40/search?q=dessert#results');
  1147. $this->assertEquals($uri, array(
  1148. 'scheme' => 'http',
  1149. 'host' => 'www.cakephp.org',
  1150. 'port' => 40,
  1151. 'user' => 'bob',
  1152. 'pass' => 'foo123',
  1153. 'path' => '/search',
  1154. 'query' => array('q' => 'dessert'),
  1155. 'fragment' => 'results'
  1156. ));
  1157. $uri = $this->Socket->parseUri('http://www.cakephp.org/');
  1158. $this->assertEquals($uri, array(
  1159. 'scheme' => 'http',
  1160. 'host' => 'www.cakephp.org',
  1161. 'path' => '/'
  1162. ));
  1163. $uri = $this->Socket->parseUri('http://www.cakephp.org', true);
  1164. $this->assertEquals($uri, array(
  1165. 'scheme' => 'http',
  1166. 'host' => 'www.cakephp.org',
  1167. 'port' => 80,
  1168. 'user' => null,
  1169. 'pass' => null,
  1170. 'path' => '/',
  1171. 'query' => array(),
  1172. 'fragment' => null
  1173. ));
  1174. $uri = $this->Socket->parseUri('https://www.cakephp.org', true);
  1175. $this->assertEquals($uri, array(
  1176. 'scheme' => 'https',
  1177. 'host' => 'www.cakephp.org',
  1178. 'port' => 443,
  1179. 'user' => null,
  1180. 'pass' => null,
  1181. 'path' => '/',
  1182. 'query' => array(),
  1183. 'fragment' => null
  1184. ));
  1185. $uri = $this->Socket->parseUri('www.cakephp.org:443/query?foo', true);
  1186. $this->assertEquals($uri, array(
  1187. 'scheme' => 'https',
  1188. 'host' => 'www.cakephp.org',
  1189. 'port' => 443,
  1190. 'user' => null,
  1191. 'pass' => null,
  1192. 'path' => '/query',
  1193. 'query' => array('foo' => ""),
  1194. 'fragment' => null
  1195. ));
  1196. $uri = $this->Socket->parseUri('http://www.cakephp.org', array('host' => 'piephp.org', 'user' => 'bob', 'fragment' => 'results'));
  1197. $this->assertEquals($uri, array(
  1198. 'host' => 'www.cakephp.org',
  1199. 'user' => 'bob',
  1200. 'fragment' => 'results',
  1201. 'scheme' => 'http'
  1202. ));
  1203. $uri = $this->Socket->parseUri('https://www.cakephp.org', array('scheme' => 'http', 'port' => 23));
  1204. $this->assertEquals($uri, array(
  1205. 'scheme' => 'https',
  1206. 'port' => 23,
  1207. 'host' => 'www.cakephp.org'
  1208. ));
  1209. $uri = $this->Socket->parseUri('www.cakephp.org:59', array('scheme' => array('http', 'https'), 'port' => 80));
  1210. $this->assertEquals($uri, array(
  1211. 'scheme' => 'http',
  1212. 'port' => 59,
  1213. 'host' => 'www.cakephp.org'
  1214. ));
  1215. $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)));
  1216. $this->assertEquals($uri, array(
  1217. 'scheme' => 'http',
  1218. 'host' => 'www.google.com',
  1219. 'port' => 8080
  1220. ));
  1221. $uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2%3Dvalue3');
  1222. $this->assertEquals($uri, array(
  1223. 'scheme' => 'http',
  1224. 'host' => 'www.cakephp.org',
  1225. 'path' => '/',
  1226. 'query' => array(
  1227. 'param1' => 'value1',
  1228. 'param2' => 'value2=value3'
  1229. )
  1230. ));
  1231. $uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2=value3');
  1232. $this->assertEquals($uri, array(
  1233. 'scheme' => 'http',
  1234. 'host' => 'www.cakephp.org',
  1235. 'path' => '/',
  1236. 'query' => array(
  1237. 'param1' => 'value1',
  1238. 'param2' => 'value2=value3'
  1239. )
  1240. ));
  1241. }
  1242. /**
  1243. * Tests that HttpSocket::buildUri can turn all kinds of uri arrays (and strings) into fully or partially qualified URI's
  1244. *
  1245. * @return void
  1246. */
  1247. public function testBuildUri() {
  1248. $this->Socket->reset();
  1249. $r = $this->Socket->buildUri(true);
  1250. $this->assertEquals(false, $r);
  1251. $r = $this->Socket->buildUri('foo.com');
  1252. $this->assertEquals('http://foo.com/', $r);
  1253. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'));
  1254. $this->assertEquals('http://www.cakephp.org/', $r);
  1255. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https'));
  1256. $this->assertEquals('https://www.cakephp.org/', $r);
  1257. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'port' => 23));
  1258. $this->assertEquals('http://www.cakephp.org:23/', $r);
  1259. $r = $this->Socket->buildUri(array('path' => 'www.google.com/search', 'query' => 'q=cakephp'));
  1260. $this->assertEquals('http://www.google.com/search?q=cakephp', $r);
  1261. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https', 'port' => 79));
  1262. $this->assertEquals('https://www.cakephp.org:79/', $r);
  1263. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => 'foo'));
  1264. $this->assertEquals('http://www.cakephp.org/foo', $r);
  1265. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/foo'));
  1266. $this->assertEquals('http://www.cakephp.org/foo', $r);
  1267. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/search', 'query' => array('q' => 'HttpSocket')));
  1268. $this->assertEquals('http://www.cakephp.org/search?q=HttpSocket', $r);
  1269. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'));
  1270. $this->assertEquals('http://www.cakephp.org/#bar', $r);
  1271. $r = $this->Socket->buildUri(array(
  1272. 'scheme' => 'https',
  1273. 'host' => 'www.cakephp.org',
  1274. 'port' => 25,
  1275. 'user' => 'bob',
  1276. 'pass' => 'secret',
  1277. 'path' => '/cool',
  1278. 'query' => array('foo' => 'bar'),
  1279. 'fragment' => 'comment'
  1280. ));
  1281. $this->assertEquals('https://bob:secret@www.cakephp.org:25/cool?foo=bar#comment', $r);
  1282. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'), '%fragment?%host');
  1283. $this->assertEquals('bar?www.cakephp.org', $r);
  1284. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'), '%fragment???%host');
  1285. $this->assertEquals('???www.cakephp.org', $r);
  1286. $r = $this->Socket->buildUri(array('path' => '*'), '/%path?%query');
  1287. $this->assertEquals('*', $r);
  1288. $r = $this->Socket->buildUri(array('scheme' => 'foo', 'host' => 'www.cakephp.org'));
  1289. $this->assertEquals('foo://www.cakephp.org:80/', $r);
  1290. }
  1291. /**
  1292. * Asserts that HttpSocket::parseQuery is working properly
  1293. *
  1294. * @return void
  1295. */
  1296. public function testParseQuery() {
  1297. $this->Socket->reset();
  1298. $query = $this->Socket->parseQuery(array('framework' => 'cakephp'));
  1299. $this->assertEquals(array('framework' => 'cakephp'), $query);
  1300. $query = $this->Socket->parseQuery('');
  1301. $this->assertEquals(array(), $query);
  1302. $query = $this->Socket->parseQuery('framework=cakephp');
  1303. $this->assertEquals(array('framework' => 'cakephp'), $query);
  1304. $query = $this->Socket->parseQuery('?framework=cakephp');
  1305. $this->assertEquals(array('framework' => 'cakephp'), $query);
  1306. $query = $this->Socket->parseQuery('a&b&c');
  1307. $this->assertEquals(array('a' => '', 'b' => '', 'c' => ''), $query);
  1308. $query = $this->Socket->parseQuery('value=12345');
  1309. $this->assertEquals(array('value' => '12345'), $query);
  1310. $query = $this->Socket->parseQuery('a[0]=foo&a[1]=bar&a[2]=cake');
  1311. $this->assertEquals(array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')), $query);
  1312. $query = $this->Socket->parseQuery('a[]=foo&a[]=bar&a[]=cake');
  1313. $this->assertEquals(array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')), $query);
  1314. $query = $this->Socket->parseQuery('a[][]=foo&a[][]=bar&a[][]=cake');
  1315. $expectedQuery = array(
  1316. 'a' => array(
  1317. 0 => array(
  1318. 0 => 'foo'
  1319. ),
  1320. 1 => array(
  1321. 0 => 'bar'
  1322. ),
  1323. array(
  1324. 0 => 'cake'
  1325. )
  1326. )
  1327. );
  1328. $this->assertEquals($expectedQuery, $query);
  1329. $query = $this->Socket->parseQuery('a[][]=foo&a[bar]=php&a[][]=bar&a[][]=cake');
  1330. $expectedQuery = array(
  1331. 'a' => array(
  1332. array('foo'),
  1333. 'bar' => 'php',
  1334. array('bar'),
  1335. array('cake')
  1336. )
  1337. );
  1338. $this->assertEquals($expectedQuery, $query);
  1339. $query = $this->Socket->parseQuery('user[]=jim&user[3]=tom&user[]=bob');
  1340. $expectedQuery = array(
  1341. 'user' => array(
  1342. 0 => 'jim',
  1343. 3 => 'tom',
  1344. 4 => 'bob'
  1345. )
  1346. );
  1347. $this->assertEquals($expectedQuery, $query);
  1348. $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';
  1349. $query = $this->Socket->parseQuery($queryStr);
  1350. $expectedQuery = array(
  1351. 'user' => array(
  1352. 0 => array(
  1353. 'items' => array(
  1354. 'foo',
  1355. 'bar'
  1356. )
  1357. ),
  1358. 1 => array(
  1359. 'name' => 'jim',
  1360. 'items' => array(
  1361. 'personal' => array(
  1362. 'book',
  1363. 'pen'
  1364. ),
  1365. 'ball'
  1366. )
  1367. ),
  1368. 'count' => '2'
  1369. ),
  1370. 'empty' => ''
  1371. );
  1372. $this->assertEquals($expectedQuery, $query);
  1373. $query = 'openid.ns=example.com&foo=bar&foo=baz';
  1374. $result = $this->Socket->parseQuery($query);
  1375. $expected = array(
  1376. 'openid.ns' => 'example.com',
  1377. 'foo' => array('bar', 'baz')
  1378. );
  1379. $this->assertEquals($expected, $result);
  1380. }
  1381. /**
  1382. * Tests that HttpSocket::buildHeader can turn a given $header array into a proper header string according to
  1383. * HTTP 1.1 specs.
  1384. *
  1385. * @return void
  1386. */
  1387. public function testBuildHeader() {
  1388. $this->Socket->reset();
  1389. $r = $this->Socket->buildHeader(true);
  1390. $this->assertEquals(false, $r);
  1391. $r = $this->Socket->buildHeader('My raw header');
  1392. $this->assertEquals('My raw header', $r);
  1393. $r = $this->Socket->buildHeader(array('Host' => 'www.cakephp.org'));
  1394. $this->assertEquals("Host: www.cakephp.org\r\n", $r);
  1395. $r = $this->Socket->buildHeader(array('Host' => 'www.cakephp.org', 'Connection' => 'Close'));
  1396. $this->assertEquals("Host: www.cakephp.org\r\nConnection: Close\r\n", $r);
  1397. $r = $this->Socket->buildHeader(array('People' => array('Bob', 'Jim', 'John')));
  1398. $this->assertEquals("People: Bob,Jim,John\r\n", $r);
  1399. $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\nMulti Line field"));
  1400. $this->assertEquals("Multi-Line-Field: This is my\r\n Multi Line field\r\n", $r);
  1401. $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\n Multi Line field"));
  1402. $this->assertEquals("Multi-Line-Field: This is my\r\n Multi Line field\r\n", $r);
  1403. $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\n\tMulti Line field"));
  1404. $this->assertEquals("Multi-Line-Field: This is my\r\n\tMulti Line field\r\n", $r);
  1405. $r = $this->Socket->buildHeader(array('Test@Field' => "My value"));
  1406. $this->assertEquals("Test\"@\"Field: My value\r\n", $r);
  1407. }
  1408. /**
  1409. * testBuildCookies method
  1410. *
  1411. * @return void
  1412. */
  1413. public function testBuildCookies() {
  1414. $cookies = array(
  1415. 'foo' => array(
  1416. 'value' => 'bar'
  1417. ),
  1418. 'people' => array(
  1419. 'value' => 'jim,jack,johnny;',
  1420. 'path' => '/accounts'
  1421. ),
  1422. 'key' => 'value'
  1423. );
  1424. $expect = "Cookie: foo=bar; people=jim,jack,johnny\";\"; key=value\r\n";
  1425. $result = $this->Socket->buildCookies($cookies);
  1426. $this->assertEquals($expect, $result);
  1427. }
  1428. /**
  1429. * Tests that HttpSocket::_tokenEscapeChars() returns the right characters.
  1430. *
  1431. * @return void
  1432. */
  1433. public function testTokenEscapeChars() {
  1434. $this->Socket->reset();
  1435. $expected = array(
  1436. '\x22', '\x28', '\x29', '\x3c', '\x3e', '\x40', '\x2c', '\x3b', '\x3a', '\x5c', '\x2f', '\x5b', '\x5d', '\x3f', '\x3d', '\x7b',
  1437. '\x7d', '\x20', '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d',
  1438. '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d',
  1439. '\x1e', '\x1f', '\x7f'
  1440. );
  1441. $r = $this->Socket->tokenEscapeChars();
  1442. $this->assertEquals($expected, $r);
  1443. foreach ($expected as $key => $char) {
  1444. $expected[$key] = chr(hexdec(substr($char, 2)));
  1445. }
  1446. $r = $this->Socket->tokenEscapeChars(false);
  1447. $this->assertEquals($expected, $r);
  1448. }
  1449. /**
  1450. * Test that HttpSocket::escapeToken is escaping all characters as described in RFC 2616 (HTTP 1.1 specs)
  1451. *
  1452. * @return void
  1453. */
  1454. public function testEscapeToken() {
  1455. $this->Socket->reset();
  1456. $this->assertEquals('Foo', $this->Socket->escapeToken('Foo'));
  1457. $escape = $this->Socket->tokenEscapeChars(false);
  1458. foreach ($escape as $char) {
  1459. $token = 'My-special-' . $char . '-Token';
  1460. $escapedToken = $this->Socket->escapeToken($token);
  1461. $expectedToken = 'My-special-"' . $char . '"-Token';
  1462. $this->assertEquals($expectedToken, $escapedToken, 'Test token escaping for ASCII ' . ord($char));
  1463. }
  1464. $token = 'Extreme-:Token- -"@-test';
  1465. $escapedToken = $this->Socket->escapeToken($token);
  1466. $expectedToken = 'Extreme-":"Token-" "-""""@"-test';
  1467. $this->assertEquals($expectedToken, $escapedToken);
  1468. }
  1469. /**
  1470. * This tests asserts HttpSocket::reset() resets a HttpSocket instance to it's initial state (before Object::__construct
  1471. * got executed)
  1472. *
  1473. * @return void
  1474. */
  1475. public function testReset() {
  1476. $this->Socket->reset();
  1477. $initialState = get_class_vars('HttpSocket');
  1478. foreach ($initialState as $property => $value) {
  1479. $this->Socket->{$property} = 'Overwritten';
  1480. }
  1481. $return = $this->Socket->reset();
  1482. foreach ($initialState as $property => $value) {
  1483. $this->assertEquals($this->Socket->{$property}, $value);
  1484. }
  1485. $this->assertEquals(true, $return);
  1486. }
  1487. /**
  1488. * This tests asserts HttpSocket::reset(false) resets certain HttpSocket properties to their initial state (before
  1489. * Object::__construct got executed).
  1490. *
  1491. * @return void
  1492. */
  1493. public function testPartialReset() {
  1494. $this->Socket->reset();
  1495. $partialResetProperties = array('request', 'response');
  1496. $initialState = get_class_vars('HttpSocket');
  1497. foreach ($initialState as $property => $value) {
  1498. $this->Socket->{$property} = 'Overwritten';
  1499. }
  1500. $return = $this->Socket->reset(false);
  1501. foreach ($initialState as $property => $originalValue) {
  1502. if (in_array($property, $partialResetProperties)) {
  1503. $this->assertEquals($this->Socket->{$property}, $originalValue);
  1504. } else {
  1505. $this->assertEquals('Overwritten', $this->Socket->{$property});
  1506. }
  1507. }
  1508. $this->assertEquals(true, $return);
  1509. }
  1510. /**
  1511. * test configuring the context from the flat keys.
  1512. *
  1513. * @return void
  1514. */
  1515. public function testConfigContext() {
  1516. $this->Socket->reset();
  1517. $this->Socket->request('http://example.com');
  1518. $this->assertTrue($this->Socket->config['context']['ssl']['verify_peer']);
  1519. $this->assertFalse($this->Socket->config['context']['ssl']['allow_self_signed']);
  1520. $this->assertEquals(5, $this->Socket->config['context']['ssl']['verify_depth']);
  1521. $this->assertEquals('example.com', $this->Socket->config['context']['ssl']['CN_match']);
  1522. $this->assertArrayNotHasKey('ssl_verify_peer', $this->Socket->config);
  1523. $this->assertArrayNotHasKey('ssl_allow_self_signed', $this->Socket->config);
  1524. $this->assertArrayNotHasKey('ssl_verify_host', $this->Socket->config);
  1525. $this->assertArrayNotHasKey('ssl_verify_depth', $this->Socket->config);
  1526. }
  1527. /**
  1528. * Test that requests fail when peer verification fails.
  1529. *
  1530. * @return void
  1531. */
  1532. public function testVerifyPeer() {
  1533. $this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
  1534. $socket = new HttpSocket();
  1535. try {
  1536. $socket->get('https://typography.com');
  1537. $this->markTestSkipped('Found valid certificate, was expecting invalid certificate.');
  1538. } catch (SocketException $e) {
  1539. $message = $e->getMessage();
  1540. $this->skipIf(strpos($message, 'Invalid HTTP') !== false, 'Invalid HTTP Response received, skipping.');
  1541. $this->assertContains('Peer certificate CN', $message);
  1542. $this->assertContains('Failed to enable crypto', $message);
  1543. }
  1544. }
  1545. /**
  1546. * Data provider for status codes.
  1547. *
  1548. * @return array
  1549. */
  1550. public function statusProvider() {
  1551. return array(
  1552. array('HTTP/1.1 200 '),
  1553. array('HTTP/1.1 200 '),
  1554. array('HTTP/1.1 200'),
  1555. array('HTTP/1.1 200 OK', 'OK'),
  1556. );
  1557. }
  1558. /**
  1559. * test response status parsing
  1560. *
  1561. * @dataProvider statusProvider
  1562. * @return void
  1563. */
  1564. public function testResponseStatusParsing($status, $msg = '') {
  1565. $this->Socket->connected = true;
  1566. $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>";
  1567. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  1568. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  1569. $response = $this->Socket->request('http://www.cakephp.org/');
  1570. $this->assertInstanceOf('HttpSocketResponse', $response);
  1571. $expected = array(
  1572. 'http-version' => 'HTTP/1.1',
  1573. 'code' => '200',
  1574. 'reason-phrase' => $msg
  1575. );
  1576. $this->assertEquals($expected, $response['status']);
  1577. }
  1578. }