HttpSocketTest.php 55 KB

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