HttpSocketTest.php 53 KB

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