HttpSocketTest.php 48 KB

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