HttpSocketTest.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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. * The "*" asterisk character is only allowed for the following methods: OPTIONS.
  514. *
  515. * @expectedException SocketException
  516. * @return void
  517. */
  518. public function testRequestNotAllowedUri() {
  519. $this->Socket->reset();
  520. $request = array('uri' => '*', 'method' => 'GET');
  521. $response = $this->Socket->request($request);
  522. }
  523. /**
  524. * testRequest2 method
  525. *
  526. * @return void
  527. */
  528. public function testRequest2() {
  529. $this->Socket->reset();
  530. $request = array('uri' => 'htpp://www.cakephp.org/');
  531. $number = mt_rand(0, 9999999);
  532. $this->Socket->expects($this->once())->method('connect')->will($this->returnValue(true));
  533. $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>";
  534. $this->Socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
  535. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  536. $this->Socket->expects($this->once())->method('write')
  537. ->with("GET / HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n\r\n");
  538. $response = (string)$this->Socket->request($request);
  539. $this->assertEquals($response, "<h1>Hello, your lucky number is " . $number . "</h1>");
  540. }
  541. /**
  542. * testRequest3 method
  543. *
  544. * @return void
  545. */
  546. public function testRequest3() {
  547. $request = array('uri' => 'htpp://www.cakephp.org/');
  548. $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>";
  549. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  550. $this->Socket->connected = true;
  551. $this->Socket->request($request);
  552. $result = $this->Socket->response['cookies'];
  553. $expect = array(
  554. 'foo' => array(
  555. 'value' => 'bar'
  556. )
  557. );
  558. $this->assertEquals($result, $expect);
  559. $this->assertEquals($this->Socket->config['request']['cookies']['www.cakephp.org'], $expect);
  560. $this->assertFalse($this->Socket->connected);
  561. }
  562. /**
  563. * testRequestWithConstructor method
  564. *
  565. * @return void
  566. */
  567. public function testRequestWithConstructor() {
  568. $request = array(
  569. 'request' => array(
  570. 'uri' => array(
  571. 'scheme' => 'http',
  572. 'host' => 'localhost',
  573. 'port' => '5984',
  574. 'user' => null,
  575. 'pass' => null
  576. )
  577. )
  578. );
  579. $http = new MockHttpSocketRequests($request);
  580. $expected = array('method' => 'GET', 'uri' => '/_test');
  581. $http->expects($this->at(0))->method('request')->with($expected);
  582. $http->get('/_test');
  583. $expected = array('method' => 'GET', 'uri' => 'http://localhost:5984/_test?count=4');
  584. $http->expects($this->at(0))->method('request')->with($expected);
  585. $http->get('/_test', array('count' => 4));
  586. }
  587. /**
  588. * testRequestWithResource
  589. *
  590. * @return void
  591. */
  592. public function testRequestWithResource() {
  593. $serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
  594. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  595. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  596. $this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse));
  597. $this->Socket->connected = true;
  598. $f = fopen(TMP . 'download.txt', 'w');
  599. if (!$f) {
  600. $this->markTestSkipped('Can not write in TMP directory.');
  601. }
  602. $this->Socket->setContentResource($f);
  603. $result = (string)$this->Socket->request('http://www.cakephp.org/');
  604. $this->assertEquals($result, '');
  605. $this->assertEquals($this->Socket->response['header']['Server'], 'CakeHttp Server');
  606. fclose($f);
  607. $this->assertEquals(file_get_contents(TMP . 'download.txt'), '<h1>This is a test!</h1>');
  608. unlink(TMP . 'download.txt');
  609. $this->Socket->setContentResource(false);
  610. $result = (string)$this->Socket->request('http://www.cakephp.org/');
  611. $this->assertEquals($result, '<h1>This is a test!</h1>');
  612. }
  613. /**
  614. * testRequestWithCrossCookie
  615. *
  616. * @return void
  617. */
  618. public function testRequestWithCrossCookie() {
  619. $this->Socket->connected = true;
  620. $this->Socket->config['request']['cookies'] = array();
  621. $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>";
  622. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  623. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  624. $expected = array('www.cakephp.org' => array('foo' => array('value' => 'bar')));
  625. $this->Socket->request('http://www.cakephp.org/');
  626. $this->assertEquals($this->Socket->config['request']['cookies'], $expected);
  627. $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>";
  628. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  629. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  630. $this->Socket->request('http://www.cakephp.org/other');
  631. $this->assertEquals($this->Socket->request['cookies'], array('foo' => array('value' => 'bar')));
  632. $expected['www.cakephp.org'] += array('bar' => array('value' => 'foo'));
  633. $this->assertEquals($this->Socket->config['request']['cookies'], $expected);
  634. $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>";
  635. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  636. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  637. $this->Socket->request('/other2');
  638. $this->assertEquals($this->Socket->config['request']['cookies'], $expected);
  639. $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>";
  640. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  641. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  642. $this->Socket->request('http://www.cake.com');
  643. $this->assertTrue(empty($this->Socket->request['cookies']));
  644. $expected['www.cake.com'] = array('foobar' => array('value' => 'ok'));
  645. $this->assertEquals($this->Socket->config['request']['cookies'], $expected);
  646. }
  647. /**
  648. * testRequestCustomResponse
  649. *
  650. * @return void
  651. */
  652. public function testRequestCustomResponse() {
  653. $this->Socket->connected = true;
  654. $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>";
  655. $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
  656. $this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
  657. $this->Socket->responseClass = 'CustomResponse';
  658. $response = $this->Socket->request('http://www.cakephp.org/');
  659. $this->assertInstanceOf('CustomResponse', $response);
  660. $this->assertEquals($response->first10, 'HTTP/1.x 2');
  661. }
  662. /**
  663. * testProxy method
  664. *
  665. * @return void
  666. */
  667. public function testProxy() {
  668. $this->Socket->reset();
  669. $this->Socket->expects($this->any())->method('connect')->will($this->returnValue(true));
  670. $this->Socket->expects($this->any())->method('read')->will($this->returnValue(false));
  671. $this->Socket->configProxy('proxy.server', 123);
  672. $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";
  673. $this->Socket->request('http://www.cakephp.org/');
  674. $this->assertEquals($this->Socket->request['raw'], $expected);
  675. $this->assertEquals($this->Socket->config['host'], 'proxy.server');
  676. $this->assertEquals($this->Socket->config['port'], 123);
  677. $expected = array(
  678. 'host' => 'proxy.server',
  679. 'port' => 123,
  680. 'method' => null,
  681. 'user' => null,
  682. 'pass' => null
  683. );
  684. $this->assertEquals($this->Socket->request['proxy'], $expected);
  685. $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";
  686. $this->Socket->request('/bakery');
  687. $this->assertEquals($this->Socket->request['raw'], $expected);
  688. $this->assertEquals($this->Socket->config['host'], 'proxy.server');
  689. $this->assertEquals($this->Socket->config['port'], 123);
  690. $expected = array(
  691. 'host' => 'proxy.server',
  692. 'port' => 123,
  693. 'method' => null,
  694. 'user' => null,
  695. 'pass' => null
  696. );
  697. $this->assertEquals($this->Socket->request['proxy'], $expected);
  698. $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";
  699. $this->Socket->configProxy('proxy.server', 123, 'Test', 'mark', 'secret');
  700. $this->Socket->request('http://www.cakephp.org/');
  701. $this->assertEquals($this->Socket->request['raw'], $expected);
  702. $this->assertEquals($this->Socket->config['host'], 'proxy.server');
  703. $this->assertEquals($this->Socket->config['port'], 123);
  704. $expected = array(
  705. 'host' => 'proxy.server',
  706. 'port' => 123,
  707. 'method' => 'Test',
  708. 'user' => 'mark',
  709. 'pass' => 'secret'
  710. );
  711. $this->assertEquals($this->Socket->request['proxy'], $expected);
  712. $this->Socket->configAuth('Test', 'login', 'passwd');
  713. $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";
  714. $this->Socket->request('http://www.cakephp.org/');
  715. $this->assertEquals($this->Socket->request['raw'], $expected);
  716. $expected = array(
  717. 'host' => 'proxy.server',
  718. 'port' => 123,
  719. 'method' => 'Test',
  720. 'user' => 'mark',
  721. 'pass' => 'secret'
  722. );
  723. $this->assertEquals($this->Socket->request['proxy'], $expected);
  724. $expected = array(
  725. 'Test' => array(
  726. 'user' => 'login',
  727. 'pass' => 'passwd'
  728. )
  729. );
  730. $this->assertEquals($this->Socket->request['auth'], $expected);
  731. }
  732. /**
  733. * testUrl method
  734. *
  735. * @return void
  736. */
  737. public function testUrl() {
  738. $this->Socket->reset(true);
  739. $this->assertEquals($this->Socket->url(true), false);
  740. $url = $this->Socket->url('www.cakephp.org');
  741. $this->assertEquals($url, 'http://www.cakephp.org/');
  742. $url = $this->Socket->url('https://www.cakephp.org/posts/add');
  743. $this->assertEquals($url, 'https://www.cakephp.org/posts/add');
  744. $url = $this->Socket->url('http://www.cakephp/search?q=socket', '/%path?%query');
  745. $this->assertEquals($url, '/search?q=socket');
  746. $this->Socket->config['request']['uri']['host'] = 'bakery.cakephp.org';
  747. $url = $this->Socket->url();
  748. $this->assertEquals($url, 'http://bakery.cakephp.org/');
  749. $this->Socket->configUri('http://www.cakephp.org');
  750. $url = $this->Socket->url('/search?q=bar');
  751. $this->assertEquals($url, 'http://www.cakephp.org/search?q=bar');
  752. $url = $this->Socket->url(array('host' => 'www.foobar.org', 'query' => array('q' => 'bar')));
  753. $this->assertEquals($url, 'http://www.foobar.org/?q=bar');
  754. $url = $this->Socket->url(array('path' => '/supersearch', 'query' => array('q' => 'bar')));
  755. $this->assertEquals($url, 'http://www.cakephp.org/supersearch?q=bar');
  756. $this->Socket->configUri('http://www.google.com');
  757. $url = $this->Socket->url('/search?q=socket');
  758. $this->assertEquals($url, 'http://www.google.com/search?q=socket');
  759. $url = $this->Socket->url();
  760. $this->assertEquals($url, 'http://www.google.com/');
  761. $this->Socket->configUri('https://www.google.com');
  762. $url = $this->Socket->url('/search?q=socket');
  763. $this->assertEquals($url, 'https://www.google.com/search?q=socket');
  764. $this->Socket->reset();
  765. $this->Socket->configUri('www.google.com:443');
  766. $url = $this->Socket->url('/search?q=socket');
  767. $this->assertEquals($url, 'https://www.google.com/search?q=socket');
  768. $this->Socket->reset();
  769. $this->Socket->configUri('www.google.com:8080');
  770. $url = $this->Socket->url('/search?q=socket');
  771. $this->assertEquals($url, 'http://www.google.com:8080/search?q=socket');
  772. }
  773. /**
  774. * testGet method
  775. *
  776. * @return void
  777. */
  778. public function testGet() {
  779. $this->RequestSocket->reset();
  780. $this->RequestSocket->expects($this->at(0))
  781. ->method('request')
  782. ->with(array('method' => 'GET', 'uri' => 'http://www.google.com/'));
  783. $this->RequestSocket->expects($this->at(1))
  784. ->method('request')
  785. ->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar'));
  786. $this->RequestSocket->expects($this->at(2))
  787. ->method('request')
  788. ->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar'));
  789. $this->RequestSocket->expects($this->at(3))
  790. ->method('request')
  791. ->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=23&foobar=42'));
  792. $this->RequestSocket->expects($this->at(4))
  793. ->method('request')
  794. ->with(array('method' => 'GET', 'uri' => 'http://www.google.com/', 'version' => '1.0'));
  795. $this->RequestSocket->get('http://www.google.com/');
  796. $this->RequestSocket->get('http://www.google.com/', array('foo' => 'bar'));
  797. $this->RequestSocket->get('http://www.google.com/', 'foo=bar');
  798. $this->RequestSocket->get('http://www.google.com/?foo=bar', array('foobar' => '42', 'foo' => '23'));
  799. $this->RequestSocket->get('http://www.google.com/', null, array('version' => '1.0'));
  800. }
  801. /**
  802. * Test authentication
  803. *
  804. * @return void
  805. */
  806. public function testAuth() {
  807. $socket = new MockHttpSocket();
  808. $socket->get('http://mark:secret@example.com/test');
  809. $this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  810. $socket->configAuth(false);
  811. $socket->get('http://example.com/test');
  812. $this->assertFalse(strpos($socket->request['header'], 'Authorization:'));
  813. $socket->configAuth('Test', 'mark', 'passwd');
  814. $socket->get('http://example.com/test');
  815. $this->assertTrue(strpos($socket->request['header'], 'Authorization: Test mark.passwd') !== false);
  816. }
  817. /**
  818. * test that two consecutive get() calls reset the authentication credentials.
  819. *
  820. * @return void
  821. */
  822. public function testConsecutiveGetResetsAuthCredentials() {
  823. $socket = new MockHttpSocket();
  824. $socket->get('http://mark:secret@example.com/test');
  825. $this->assertEquals($socket->request['uri']['user'], 'mark');
  826. $this->assertEquals($socket->request['uri']['pass'], 'secret');
  827. $this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  828. $socket->get('/test2');
  829. $this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  830. $socket->get('/test3');
  831. $this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
  832. }
  833. /**
  834. * testPostPutDelete method
  835. *
  836. * @return void
  837. */
  838. public function testPost() {
  839. $this->RequestSocket->reset();
  840. $this->RequestSocket->expects($this->at(0))
  841. ->method('request')
  842. ->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => array()));
  843. $this->RequestSocket->expects($this->at(1))
  844. ->method('request')
  845. ->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  846. $this->RequestSocket->expects($this->at(2))
  847. ->method('request')
  848. ->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  849. $this->RequestSocket->post('http://www.google.com/');
  850. $this->RequestSocket->post('http://www.google.com/', array('Foo' => 'bar'));
  851. $this->RequestSocket->post('http://www.google.com/', null, array('line' => 'Hey Server'));
  852. }
  853. /**
  854. * testPut
  855. *
  856. * @return void
  857. */
  858. public function testPut() {
  859. $this->RequestSocket->reset();
  860. $this->RequestSocket->expects($this->at(0))
  861. ->method('request')
  862. ->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => array()));
  863. $this->RequestSocket->expects($this->at(1))
  864. ->method('request')
  865. ->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  866. $this->RequestSocket->expects($this->at(2))
  867. ->method('request')
  868. ->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  869. $this->RequestSocket->put('http://www.google.com/');
  870. $this->RequestSocket->put('http://www.google.com/', array('Foo' => 'bar'));
  871. $this->RequestSocket->put('http://www.google.com/', null, array('line' => 'Hey Server'));
  872. }
  873. /**
  874. * testDelete
  875. *
  876. * @return void
  877. */
  878. public function testDelete() {
  879. $this->RequestSocket->reset();
  880. $this->RequestSocket->expects($this->at(0))
  881. ->method('request')
  882. ->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => array()));
  883. $this->RequestSocket->expects($this->at(1))
  884. ->method('request')
  885. ->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
  886. $this->RequestSocket->expects($this->at(2))
  887. ->method('request')
  888. ->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
  889. $this->RequestSocket->delete('http://www.google.com/');
  890. $this->RequestSocket->delete('http://www.google.com/', array('Foo' => 'bar'));
  891. $this->RequestSocket->delete('http://www.google.com/', null, array('line' => 'Hey Server'));
  892. }
  893. /**
  894. * testBuildRequestLine method
  895. *
  896. * @return void
  897. */
  898. public function testBuildRequestLine() {
  899. $this->Socket->reset();
  900. $this->Socket->quirksMode = true;
  901. $r = $this->Socket->buildRequestLine('Foo');
  902. $this->assertEquals($r, 'Foo');
  903. $this->Socket->quirksMode = false;
  904. $r = $this->Socket->buildRequestLine(true);
  905. $this->assertEquals($r, false);
  906. $r = $this->Socket->buildRequestLine(array('foo' => 'bar', 'method' => 'foo'));
  907. $this->assertEquals($r, false);
  908. $r = $this->Socket->buildRequestLine(array('method' => 'GET', 'uri' => 'http://www.cakephp.org/search?q=socket'));
  909. $this->assertEquals($r, "GET /search?q=socket HTTP/1.1\r\n");
  910. $request = array(
  911. 'method' => 'GET',
  912. 'uri' => array(
  913. 'path' => '/search',
  914. 'query' => array('q' => 'socket')
  915. )
  916. );
  917. $r = $this->Socket->buildRequestLine($request);
  918. $this->assertEquals($r, "GET /search?q=socket HTTP/1.1\r\n");
  919. unset($request['method']);
  920. $r = $this->Socket->buildRequestLine($request);
  921. $this->assertEquals($r, "GET /search?q=socket HTTP/1.1\r\n");
  922. $r = $this->Socket->buildRequestLine($request, 'CAKE-HTTP/0.1');
  923. $this->assertEquals($r, "GET /search?q=socket CAKE-HTTP/0.1\r\n");
  924. $request = array('method' => 'OPTIONS', 'uri' => '*');
  925. $r = $this->Socket->buildRequestLine($request);
  926. $this->assertEquals($r, "OPTIONS * HTTP/1.1\r\n");
  927. $request['method'] = 'GET';
  928. $this->Socket->quirksMode = true;
  929. $r = $this->Socket->buildRequestLine($request);
  930. $this->assertEquals($r, "GET * HTTP/1.1\r\n");
  931. $r = $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
  932. $this->assertEquals($r, "GET * HTTP/1.1\r\n");
  933. }
  934. /**
  935. * testBadBuildRequestLine method
  936. *
  937. * @expectedException SocketException
  938. * @return void
  939. */
  940. public function testBadBuildRequestLine() {
  941. $r = $this->Socket->buildRequestLine('Foo');
  942. }
  943. /**
  944. * testBadBuildRequestLine2 method
  945. *
  946. * @expectedException SocketException
  947. * @return void
  948. */
  949. public function testBadBuildRequestLine2() {
  950. $r = $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
  951. }
  952. /**
  953. * Asserts that HttpSocket::parseUri is working properly
  954. *
  955. * @return void
  956. */
  957. public function testParseUri() {
  958. $this->Socket->reset();
  959. $uri = $this->Socket->parseUri(array('invalid' => 'uri-string'));
  960. $this->assertEquals($uri, false);
  961. $uri = $this->Socket->parseUri(array('invalid' => 'uri-string'), array('host' => 'somehost'));
  962. $this->assertEquals($uri, array('host' => 'somehost', 'invalid' => 'uri-string'));
  963. $uri = $this->Socket->parseUri(false);
  964. $this->assertEquals($uri, false);
  965. $uri = $this->Socket->parseUri('/my-cool-path');
  966. $this->assertEquals($uri, array('path' => '/my-cool-path'));
  967. $uri = $this->Socket->parseUri('http://bob:foo123@www.cakephp.org:40/search?q=dessert#results');
  968. $this->assertEquals($uri, array(
  969. 'scheme' => 'http',
  970. 'host' => 'www.cakephp.org',
  971. 'port' => 40,
  972. 'user' => 'bob',
  973. 'pass' => 'foo123',
  974. 'path' => '/search',
  975. 'query' => array('q' => 'dessert'),
  976. 'fragment' => 'results'
  977. ));
  978. $uri = $this->Socket->parseUri('http://www.cakephp.org/');
  979. $this->assertEquals($uri, array(
  980. 'scheme' => 'http',
  981. 'host' => 'www.cakephp.org',
  982. 'path' => '/'
  983. ));
  984. $uri = $this->Socket->parseUri('http://www.cakephp.org', true);
  985. $this->assertEquals($uri, array(
  986. 'scheme' => 'http',
  987. 'host' => 'www.cakephp.org',
  988. 'port' => 80,
  989. 'user' => null,
  990. 'pass' => null,
  991. 'path' => '/',
  992. 'query' => array(),
  993. 'fragment' => null
  994. ));
  995. $uri = $this->Socket->parseUri('https://www.cakephp.org', true);
  996. $this->assertEquals($uri, array(
  997. 'scheme' => 'https',
  998. 'host' => 'www.cakephp.org',
  999. 'port' => 443,
  1000. 'user' => null,
  1001. 'pass' => null,
  1002. 'path' => '/',
  1003. 'query' => array(),
  1004. 'fragment' => null
  1005. ));
  1006. $uri = $this->Socket->parseUri('www.cakephp.org:443/query?foo', true);
  1007. $this->assertEquals($uri, array(
  1008. 'scheme' => 'https',
  1009. 'host' => 'www.cakephp.org',
  1010. 'port' => 443,
  1011. 'user' => null,
  1012. 'pass' => null,
  1013. 'path' => '/query',
  1014. 'query' => array('foo' => ""),
  1015. 'fragment' => null
  1016. ));
  1017. $uri = $this->Socket->parseUri('http://www.cakephp.org', array('host' => 'piephp.org', 'user' => 'bob', 'fragment' => 'results'));
  1018. $this->assertEquals($uri, array(
  1019. 'host' => 'www.cakephp.org',
  1020. 'user' => 'bob',
  1021. 'fragment' => 'results',
  1022. 'scheme' => 'http'
  1023. ));
  1024. $uri = $this->Socket->parseUri('https://www.cakephp.org', array('scheme' => 'http', 'port' => 23));
  1025. $this->assertEquals($uri, array(
  1026. 'scheme' => 'https',
  1027. 'port' => 23,
  1028. 'host' => 'www.cakephp.org'
  1029. ));
  1030. $uri = $this->Socket->parseUri('www.cakephp.org:59', array('scheme' => array('http', 'https'), 'port' => 80));
  1031. $this->assertEquals($uri, array(
  1032. 'scheme' => 'http',
  1033. 'port' => 59,
  1034. 'host' => 'www.cakephp.org'
  1035. ));
  1036. $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)));
  1037. $this->assertEquals($uri, array(
  1038. 'scheme' => 'http',
  1039. 'host' => 'www.google.com',
  1040. 'port' => 8080
  1041. ));
  1042. $uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2%3Dvalue3');
  1043. $this->assertEquals($uri, array(
  1044. 'scheme' => 'http',
  1045. 'host' => 'www.cakephp.org',
  1046. 'path' => '/',
  1047. 'query' => array(
  1048. 'param1' => 'value1',
  1049. 'param2' => 'value2=value3'
  1050. )
  1051. ));
  1052. $uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2=value3');
  1053. $this->assertEquals($uri, array(
  1054. 'scheme' => 'http',
  1055. 'host' => 'www.cakephp.org',
  1056. 'path' => '/',
  1057. 'query' => array(
  1058. 'param1' => 'value1',
  1059. 'param2' => 'value2=value3'
  1060. )
  1061. ));
  1062. }
  1063. /**
  1064. * Tests that HttpSocket::buildUri can turn all kinds of uri arrays (and strings) into fully or partially qualified URI's
  1065. *
  1066. * @return void
  1067. */
  1068. public function testBuildUri() {
  1069. $this->Socket->reset();
  1070. $r = $this->Socket->buildUri(true);
  1071. $this->assertEquals($r, false);
  1072. $r = $this->Socket->buildUri('foo.com');
  1073. $this->assertEquals($r, 'http://foo.com/');
  1074. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'));
  1075. $this->assertEquals($r, 'http://www.cakephp.org/');
  1076. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https'));
  1077. $this->assertEquals($r, 'https://www.cakephp.org/');
  1078. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'port' => 23));
  1079. $this->assertEquals($r, 'http://www.cakephp.org:23/');
  1080. $r = $this->Socket->buildUri(array('path' => 'www.google.com/search', 'query' => 'q=cakephp'));
  1081. $this->assertEquals($r, 'http://www.google.com/search?q=cakephp');
  1082. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https', 'port' => 79));
  1083. $this->assertEquals($r, 'https://www.cakephp.org:79/');
  1084. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => 'foo'));
  1085. $this->assertEquals($r, 'http://www.cakephp.org/foo');
  1086. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/foo'));
  1087. $this->assertEquals($r, 'http://www.cakephp.org/foo');
  1088. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/search', 'query' => array('q' => 'HttpSocket')));
  1089. $this->assertEquals($r, 'http://www.cakephp.org/search?q=HttpSocket');
  1090. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'));
  1091. $this->assertEquals($r, 'http://www.cakephp.org/#bar');
  1092. $r = $this->Socket->buildUri(array(
  1093. 'scheme' => 'https',
  1094. 'host' => 'www.cakephp.org',
  1095. 'port' => 25,
  1096. 'user' => 'bob',
  1097. 'pass' => 'secret',
  1098. 'path' => '/cool',
  1099. 'query' => array('foo' => 'bar'),
  1100. 'fragment' => 'comment'
  1101. ));
  1102. $this->assertEquals($r, 'https://bob:secret@www.cakephp.org:25/cool?foo=bar#comment');
  1103. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'), '%fragment?%host');
  1104. $this->assertEquals($r, 'bar?www.cakephp.org');
  1105. $r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'), '%fragment???%host');
  1106. $this->assertEquals($r, '???www.cakephp.org');
  1107. $r = $this->Socket->buildUri(array('path' => '*'), '/%path?%query');
  1108. $this->assertEquals($r, '*');
  1109. $r = $this->Socket->buildUri(array('scheme' => 'foo', 'host' => 'www.cakephp.org'));
  1110. $this->assertEquals($r, 'foo://www.cakephp.org:80/');
  1111. }
  1112. /**
  1113. * Asserts that HttpSocket::parseQuery is working properly
  1114. *
  1115. * @return void
  1116. */
  1117. public function testParseQuery() {
  1118. $this->Socket->reset();
  1119. $query = $this->Socket->parseQuery(array('framework' => 'cakephp'));
  1120. $this->assertEquals($query, array('framework' => 'cakephp'));
  1121. $query = $this->Socket->parseQuery('');
  1122. $this->assertEquals($query, array());
  1123. $query = $this->Socket->parseQuery('framework=cakephp');
  1124. $this->assertEquals($query, array('framework' => 'cakephp'));
  1125. $query = $this->Socket->parseQuery('?framework=cakephp');
  1126. $this->assertEquals($query, array('framework' => 'cakephp'));
  1127. $query = $this->Socket->parseQuery('a&b&c');
  1128. $this->assertEquals($query, array('a' => '', 'b' => '', 'c' => ''));
  1129. $query = $this->Socket->parseQuery('value=12345');
  1130. $this->assertEquals($query, array('value' => '12345'));
  1131. $query = $this->Socket->parseQuery('a[0]=foo&a[1]=bar&a[2]=cake');
  1132. $this->assertEquals($query, array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')));
  1133. $query = $this->Socket->parseQuery('a[]=foo&a[]=bar&a[]=cake');
  1134. $this->assertEquals($query, array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')));
  1135. $query = $this->Socket->parseQuery('a]][[=foo&[]=bar&]]][]=cake');
  1136. $this->assertEquals($query, array('a]][[' => 'foo', 0 => 'bar', ']]]' => array('cake')));
  1137. $query = $this->Socket->parseQuery('a[][]=foo&a[][]=bar&a[][]=cake');
  1138. $expectedQuery = array(
  1139. 'a' => array(
  1140. 0 => array(
  1141. 0 => 'foo'
  1142. ),
  1143. 1 => array(
  1144. 0 => 'bar'
  1145. ),
  1146. array(
  1147. 0 => 'cake'
  1148. )
  1149. )
  1150. );
  1151. $this->assertEquals($query, $expectedQuery);
  1152. $query = $this->Socket->parseQuery('a[][]=foo&a[bar]=php&a[][]=bar&a[][]=cake');
  1153. $expectedQuery = array(
  1154. 'a' => array(
  1155. array('foo'),
  1156. 'bar' => 'php',
  1157. array('bar'),
  1158. array('cake')
  1159. )
  1160. );
  1161. $this->assertEquals($query, $expectedQuery);
  1162. $query = $this->Socket->parseQuery('user[]=jim&user[3]=tom&user[]=bob');
  1163. $expectedQuery = array(
  1164. 'user' => array(
  1165. 0 => 'jim',
  1166. 3 => 'tom',
  1167. 4 => 'bob'
  1168. )
  1169. );
  1170. $this->assertEquals($query, $expectedQuery);
  1171. $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';
  1172. $query = $this->Socket->parseQuery($queryStr);
  1173. $expectedQuery = array(
  1174. 'user' => array(
  1175. 0 => array(
  1176. 'items' => array(
  1177. 'foo',
  1178. 'bar'
  1179. )
  1180. ),
  1181. 1 => array(
  1182. 'name' => 'jim',
  1183. 'items' => array(
  1184. 'personal' => array(
  1185. 'book'
  1186. , 'pen'
  1187. ),
  1188. 'ball'
  1189. )
  1190. ),
  1191. 'count' => '2'
  1192. ),
  1193. 'empty' => ''
  1194. );
  1195. $this->assertEquals($query, $expectedQuery);
  1196. }
  1197. /**
  1198. * Tests that HttpSocket::buildHeader can turn a given $header array into a proper header string according to
  1199. * HTTP 1.1 specs.
  1200. *
  1201. * @return void
  1202. */
  1203. public function testBuildHeader() {
  1204. $this->Socket->reset();
  1205. $r = $this->Socket->buildHeader(true);
  1206. $this->assertEquals($r, false);
  1207. $r = $this->Socket->buildHeader('My raw header');
  1208. $this->assertEquals($r, 'My raw header');
  1209. $r = $this->Socket->buildHeader(array('Host' => 'www.cakephp.org'));
  1210. $this->assertEquals($r, "Host: www.cakephp.org\r\n");
  1211. $r = $this->Socket->buildHeader(array('Host' => 'www.cakephp.org', 'Connection' => 'Close'));
  1212. $this->assertEquals($r, "Host: www.cakephp.org\r\nConnection: Close\r\n");
  1213. $r = $this->Socket->buildHeader(array('People' => array('Bob', 'Jim', 'John')));
  1214. $this->assertEquals($r, "People: Bob,Jim,John\r\n");
  1215. $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\nMulti Line field"));
  1216. $this->assertEquals($r, "Multi-Line-Field: This is my\r\n Multi Line field\r\n");
  1217. $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\n Multi Line field"));
  1218. $this->assertEquals($r, "Multi-Line-Field: This is my\r\n Multi Line field\r\n");
  1219. $r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\n\tMulti Line field"));
  1220. $this->assertEquals($r, "Multi-Line-Field: This is my\r\n\tMulti Line field\r\n");
  1221. $r = $this->Socket->buildHeader(array('Test@Field' => "My value"));
  1222. $this->assertEquals($r, "Test\"@\"Field: My value\r\n");
  1223. }
  1224. /**
  1225. * testBuildCookies method
  1226. *
  1227. * @return void
  1228. * @todo Test more scenarios
  1229. */
  1230. public function testBuildCookies() {
  1231. $cookies = array(
  1232. 'foo' => array(
  1233. 'value' => 'bar'
  1234. ),
  1235. 'people' => array(
  1236. 'value' => 'jim,jack,johnny;',
  1237. 'path' => '/accounts'
  1238. )
  1239. );
  1240. $expect = "Cookie: foo=bar; people=jim,jack,johnny\";\"\r\n";
  1241. $result = $this->Socket->buildCookies($cookies);
  1242. $this->assertEquals($result, $expect);
  1243. }
  1244. /**
  1245. * Tests that HttpSocket::_tokenEscapeChars() returns the right characters.
  1246. *
  1247. * @return void
  1248. */
  1249. public function testTokenEscapeChars() {
  1250. $this->Socket->reset();
  1251. $expected = array(
  1252. '\x22','\x28','\x29','\x3c','\x3e','\x40','\x2c','\x3b','\x3a','\x5c','\x2f','\x5b','\x5d','\x3f','\x3d','\x7b',
  1253. '\x7d','\x20','\x00','\x01','\x02','\x03','\x04','\x05','\x06','\x07','\x08','\x09','\x0a','\x0b','\x0c','\x0d',
  1254. '\x0e','\x0f','\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1a','\x1b','\x1c','\x1d',
  1255. '\x1e','\x1f','\x7f'
  1256. );
  1257. $r = $this->Socket->tokenEscapeChars();
  1258. $this->assertEquals($r, $expected);
  1259. foreach ($expected as $key => $char) {
  1260. $expected[$key] = chr(hexdec(substr($char, 2)));
  1261. }
  1262. $r = $this->Socket->tokenEscapeChars(false);
  1263. $this->assertEquals($r, $expected);
  1264. }
  1265. /**
  1266. * Test that HttpSocket::escapeToken is escaping all characters as descriped in RFC 2616 (HTTP 1.1 specs)
  1267. *
  1268. * @return void
  1269. */
  1270. public function testEscapeToken() {
  1271. $this->Socket->reset();
  1272. $this->assertEquals($this->Socket->escapeToken('Foo'), 'Foo');
  1273. $escape = $this->Socket->tokenEscapeChars(false);
  1274. foreach ($escape as $char) {
  1275. $token = 'My-special-' . $char . '-Token';
  1276. $escapedToken = $this->Socket->escapeToken($token);
  1277. $expectedToken = 'My-special-"' . $char . '"-Token';
  1278. $this->assertEquals($escapedToken, $expectedToken, 'Test token escaping for ASCII '.ord($char));
  1279. }
  1280. $token = 'Extreme-:Token- -"@-test';
  1281. $escapedToken = $this->Socket->escapeToken($token);
  1282. $expectedToken = 'Extreme-":"Token-" "-""""@"-test';
  1283. $this->assertEquals($expectedToken, $escapedToken);
  1284. }
  1285. /**
  1286. * This tests asserts HttpSocket::reset() resets a HttpSocket instance to it's initial state (before Object::__construct
  1287. * got executed)
  1288. *
  1289. * @return void
  1290. */
  1291. public function testReset() {
  1292. $this->Socket->reset();
  1293. $initialState = get_class_vars('HttpSocket');
  1294. foreach ($initialState as $property => $value) {
  1295. $this->Socket->{$property} = 'Overwritten';
  1296. }
  1297. $return = $this->Socket->reset();
  1298. foreach ($initialState as $property => $value) {
  1299. $this->assertEquals($this->Socket->{$property}, $value);
  1300. }
  1301. $this->assertEquals($return, true);
  1302. }
  1303. /**
  1304. * This tests asserts HttpSocket::reset(false) resets certain HttpSocket properties to their initial state (before
  1305. * Object::__construct got executed).
  1306. *
  1307. * @return void
  1308. */
  1309. public function testPartialReset() {
  1310. $this->Socket->reset();
  1311. $partialResetProperties = array('request', 'response');
  1312. $initialState = get_class_vars('HttpSocket');
  1313. foreach ($initialState as $property => $value) {
  1314. $this->Socket->{$property} = 'Overwritten';
  1315. }
  1316. $return = $this->Socket->reset(false);
  1317. foreach ($initialState as $property => $originalValue) {
  1318. if (in_array($property, $partialResetProperties)) {
  1319. $this->assertEquals($this->Socket->{$property}, $originalValue);
  1320. } else {
  1321. $this->assertEquals($this->Socket->{$property}, 'Overwritten');
  1322. }
  1323. }
  1324. $this->assertEquals($return, true);
  1325. }
  1326. }