HttpSocketTest.php 49 KB

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