HttpSocketTest.php 48 KB

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