HttpSocketTest.php 50 KB

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