HttpTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. // Call Zend_Controller_Request_HttpTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Request_HttpTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/Controller/Request/Http.php';
  8. class Zend_Controller_Request_HttpTest extends PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var Zend_Controller_Request_Http
  12. */
  13. protected $_request;
  14. /**
  15. * Original $_SERVER
  16. * @var array
  17. */
  18. protected $_origServer;
  19. /**
  20. * Runs the test methods of this class.
  21. *
  22. * @access public
  23. * @static
  24. */
  25. public static function main()
  26. {
  27. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Request_HttpTest");
  28. $result = PHPUnit_TextUI_TestRunner::run($suite);
  29. }
  30. public function setUp()
  31. {
  32. $this->_origServer = $_SERVER;
  33. $_GET = array();
  34. $_POST = array();
  35. $_SERVER = array(
  36. 'SCRIPT_FILENAME' => __FILE__,
  37. 'PHP_SELF' => __FILE__,
  38. );
  39. $this->_request = new Zend_Controller_Request_Http('http://framework.zend.com/news/3?var1=val1&var2=val2#anchor');
  40. }
  41. public function tearDown()
  42. {
  43. unset($this->_request);
  44. $_SERVER = $this->_origServer;
  45. }
  46. public function testSetGetControllerKey()
  47. {
  48. $this->_request->setControllerKey('controller');
  49. $this->assertEquals('controller', $this->_request->getControllerKey());
  50. $this->_request->setControllerKey('foo');
  51. $this->assertEquals('foo', $this->_request->getControllerKey());
  52. }
  53. public function testSetGetActionKey()
  54. {
  55. $this->_request->setActionKey('action');
  56. $this->assertEquals('action', $this->_request->getActionKey());
  57. $this->_request->setActionKey('foo');
  58. $this->assertEquals('foo', $this->_request->getActionKey());
  59. }
  60. public function testSetGetControllerName()
  61. {
  62. $this->_request->setControllerName('foo');
  63. $this->assertEquals('foo', $this->_request->getControllerName());
  64. $this->_request->setControllerName('bar');
  65. $this->assertEquals('bar', $this->_request->getControllerName());
  66. }
  67. public function testSetGetActionName()
  68. {
  69. $this->_request->setActionName('foo');
  70. $this->assertEquals('foo', $this->_request->getActionName());
  71. $this->_request->setActionName('bar');
  72. $this->assertEquals('bar', $this->_request->getActionName());
  73. }
  74. public function test__Get()
  75. {
  76. $_POST['baz'] = 'boo';
  77. $_COOKIE['bal'] = 'peen';
  78. $this->_request->setParam('foo', 'bar');
  79. foreach ($_ENV as $envKey => $expected) {
  80. if (isset($_ENV[$envKey]) && !empty($_ENV[$envKey])) {
  81. $expEnvKey = $envKey;
  82. break;
  83. }
  84. }
  85. $_SERVER['REQUEST_TIME'] = date('Y-m-d H:i:s');
  86. $this->assertEquals('bar', $this->_request->foo);
  87. $this->assertEquals('val1', $this->_request->var1);
  88. $this->assertEquals('boo', $this->_request->baz);
  89. $this->assertEquals('peen', $this->_request->bal);
  90. $this->assertEquals($_SERVER['REQUEST_TIME'], $this->_request->REQUEST_TIME);
  91. $this->assertEquals($this->_request->getPathInfo(), $this->_request->PATH_INFO, $this->_request->PATH_INFO);
  92. $this->assertEquals($this->_request->getRequestUri(), $this->_request->REQUEST_URI, $this->_request->REQUEST_URI);
  93. if (isset($expEnvKey)) {
  94. $this->assertEquals($expected, $this->_request->$expEnvKey);
  95. }
  96. }
  97. public function testGetIsAlias()
  98. {
  99. $this->assertEquals('val1', $this->_request->get('var1'));
  100. }
  101. public function testSetIsAlias()
  102. {
  103. try {
  104. $this->_request->set('foo', 'bar');
  105. $this->fail('set() should alias to __set(), and throw an exception');
  106. } catch (Exception $e) {
  107. // success
  108. }
  109. }
  110. public function test__Isset()
  111. {
  112. $_POST['baz'] = 'boo';
  113. $_COOKIE['bal'] = 'peen';
  114. $this->_request->setParam('foo', 'bar');
  115. foreach ($_ENV as $envKey => $expected) {
  116. if (isset($_ENV[$envKey]) && !empty($_ENV[$envKey])) {
  117. $expEnvKey = $envKey;
  118. break;
  119. }
  120. }
  121. $_SERVER['REQUEST_TIME'] = date('Y-m-d H:i:s');
  122. $this->assertTrue(isset($this->_request->foo));
  123. $this->assertTrue(isset($this->_request->var1));
  124. $this->assertTrue(isset($this->_request->baz));
  125. $this->assertTrue(isset($this->_request->bal));
  126. $this->assertTrue(isset($this->_request->REQUEST_TIME));
  127. $this->assertFalse(isset($this->_request->bogosity));
  128. if (isset($expEnvKey)) {
  129. $this->assertTrue(isset($this->_request->$expEnvKey));
  130. }
  131. }
  132. public function testHasIsAlias()
  133. {
  134. $this->assertTrue($this->_request->has('var1'));
  135. }
  136. public function test__SetThrowsException()
  137. {
  138. try {
  139. $this->_request->foo = 'bar';
  140. $this->fail('__set() should throw an exception');
  141. } catch (Exception $e) {
  142. // success
  143. }
  144. }
  145. public function testSetGetParam()
  146. {
  147. $this->_request->setParam('foo', 'bar');
  148. $this->assertEquals('bar', $this->_request->getParam('foo'));
  149. }
  150. public function testSetGetParams()
  151. {
  152. $params = array(
  153. 'foo' => 'bar',
  154. 'boo' => 'bah',
  155. 'fee' => 'fi'
  156. );
  157. $this->_request->setParams($params);
  158. $received = $this->_request->getParams();
  159. $this->assertSame($params, array_intersect_assoc($params, $received));
  160. }
  161. public function testGetParamsWithNoGetOrPost()
  162. {
  163. unset($_GET, $_POST);
  164. $params = array(
  165. 'foo' => 'bar',
  166. 'boo' => 'bah',
  167. 'fee' => 'fi'
  168. );
  169. $this->_request->setParams($params);
  170. $received = $this->_request->getParams();
  171. $this->assertSame($params, array_intersect_assoc($params, $received));
  172. }
  173. public function testGetParamsWithGetAndPost()
  174. {
  175. $_GET = array(
  176. 'get' => true
  177. );
  178. $_POST = array(
  179. 'post' => true
  180. );
  181. $params = array(
  182. 'foo' => 'bar',
  183. 'boo' => 'bah',
  184. 'fee' => 'fi'
  185. );
  186. $this->_request->setParams($params);
  187. $expected = $params + $_GET + $_POST;
  188. $received = $this->_request->getParams();
  189. $this->assertSame($params, array_intersect_assoc($params, $received));
  190. }
  191. public function testConstructSetsRequestUri()
  192. {
  193. $_SERVER['REQUEST_URI'] = '/mycontroller/myaction?foo=bar';
  194. $request = new Zend_Controller_Request_Http();
  195. $this->assertEquals('/mycontroller/myaction?foo=bar', $request->getRequestUri());
  196. }
  197. public function testSetRequestUriDoesNotPassUriThroughUrldecode()
  198. {
  199. $request = new Zend_Controller_Request_Http();
  200. $request->setRequestUri('/foo/bar?foo=bar%20baz');
  201. $requestUri = $request->getRequestUri();
  202. $this->assertNotEquals('/foo/bar?foo=bar baz', $requestUri);
  203. $this->assertEquals('/foo/bar?foo=bar%20baz', $requestUri);
  204. }
  205. public function testGetMethod()
  206. {
  207. $_SERVER['REQUEST_METHOD'] = 'POST';
  208. $this->assertEquals('POST', $this->_request->getMethod());
  209. $_SERVER['REQUEST_METHOD'] = 'GET';
  210. $this->assertEquals('GET', $this->_request->getMethod());
  211. }
  212. public function testIsPost()
  213. {
  214. $_SERVER['REQUEST_METHOD'] = 'POST';
  215. $this->assertTrue($this->_request->isPost());
  216. $_SERVER['REQUEST_METHOD'] = 'GET';
  217. $this->assertFalse($this->_request->isPost());
  218. }
  219. public function testIsGet()
  220. {
  221. $_SERVER['REQUEST_METHOD'] = 'GET';
  222. $this->assertTrue($this->_request->isGet());
  223. $this->assertFalse($this->_request->isPost());
  224. }
  225. public function testIsPut()
  226. {
  227. $_SERVER['REQUEST_METHOD'] = 'PUT';
  228. $this->assertTrue($this->_request->isPut());
  229. $this->assertFalse($this->_request->isGet());
  230. }
  231. public function testIsDelete()
  232. {
  233. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  234. $this->assertTrue($this->_request->isDelete());
  235. $this->assertFalse($this->_request->isGet());
  236. }
  237. public function testIsHead()
  238. {
  239. $_SERVER['REQUEST_METHOD'] = 'HEAD';
  240. $this->assertTrue($this->_request->isHead());
  241. $this->assertFalse($this->_request->isGet());
  242. }
  243. public function testIsOptions()
  244. {
  245. $_SERVER['REQUEST_METHOD'] = 'OPTIONS';
  246. $this->assertTrue($this->_request->isOptions());
  247. $this->assertFalse($this->_request->isGet());
  248. }
  249. public function testGetRawBodyReturnsFalseWithNoPost()
  250. {
  251. $this->assertFalse($this->_request->getRawBody());
  252. }
  253. public function testGetQuery()
  254. {
  255. $this->assertEquals('val1', $this->_request->getQuery('var1'));
  256. $this->assertEquals('foo', $this->_request->getQuery('BAR', 'foo'));
  257. $expected = array('var1' => 'val1', 'var2' => 'val2');
  258. $this->assertEquals( $expected, $this->_request->getQuery());
  259. }
  260. public function testGetPost()
  261. {
  262. $_POST['post1'] = 'val1';
  263. $this->assertEquals('val1', $this->_request->getPost('post1'));
  264. $this->assertEquals('foo', $this->_request->getPost('BAR', 'foo'));
  265. $_POST['post2'] = 'val2';
  266. $expected = array('post1' => 'val1', 'post2' => 'val2');
  267. $this->assertEquals($expected, $this->_request->getPost());
  268. }
  269. public function testGetPathInfo()
  270. {
  271. $this->assertEquals('/news/3', $this->_request->getPathInfo(), 'Base URL: ' . var_export($this->_request->getBaseUrl(), 1));
  272. }
  273. public function testSetPathInfo()
  274. {
  275. $this->_request->setPathInfo('/archives/past/4');
  276. $this->assertEquals('/archives/past/4', $this->_request->getPathInfo());
  277. }
  278. public function testPathInfoNeedingBaseUrl()
  279. {
  280. $request = new Zend_Controller_Request_Http('http://localhost/test/index.php/ctrl-name/act-name');
  281. $this->assertEquals('/test/index.php/ctrl-name/act-name', $request->getRequestUri());
  282. $request->setBaseUrl('/test/index.php');
  283. $this->assertEquals('/test/index.php', $request->getBaseUrl());
  284. $requestUri = $request->getRequestUri();
  285. $baseUrl = $request->getBaseUrl();
  286. $pathInfo = substr($requestUri, strlen($baseUrl));
  287. $this->assertTrue($pathInfo ? true : false);
  288. $this->assertEquals('/ctrl-name/act-name', $request->getPathInfo(), "Expected $pathInfo;");
  289. }
  290. public function testGetSetAlias()
  291. {
  292. $this->_request->setAlias('controller', 'var1');
  293. $this->assertEquals('var1', $this->_request->getAlias('controller'));
  294. }
  295. public function testGetAliases()
  296. {
  297. $this->_request->setAlias('controller', 'var1');
  298. $this->_request->setAlias('action', 'var2');
  299. $this->assertSame(array('controller' => 'var1', 'action' => 'var2'), $this->_request->getAliases());
  300. }
  301. public function testGetRequestUri()
  302. {
  303. $this->assertEquals('/news/3?var1=val1&var2=val2', $this->_request->getRequestUri());
  304. }
  305. public function testSetRequestUri()
  306. {
  307. $this->_request->setRequestUri('/archives/past/4?set=this&unset=that');
  308. $this->assertEquals('/archives/past/4?set=this&unset=that', $this->_request->getRequestUri());
  309. $this->assertEquals('this', $this->_request->getQuery('set'));
  310. $this->assertEquals('that', $this->_request->getQuery('unset'));
  311. }
  312. public function testGetBaseUrl()
  313. {
  314. $this->assertSame('', $this->_request->getBaseUrl());
  315. }
  316. /*
  317. * Tests if an empty string gets returned when no basepath is set on the request.
  318. * This is important on windows, where before this fix '\' was returned instead of an empty string.
  319. * @group ZF-4810
  320. */
  321. public function testGetBasePathIsEmptyStringIfNoneSet()
  322. {
  323. $request = new Zend_Controller_Request_Http();
  324. $this->assertEquals('', $request->getBasePath());
  325. }
  326. public function testSetBaseUrl()
  327. {
  328. $this->_request->setBaseUrl('/news');
  329. $this->assertEquals('/news', $this->_request->getBaseUrl());
  330. }
  331. public function testSetBaseUrlUsingPhpSelf()
  332. {
  333. $_SERVER['REQUEST_URI'] = '/index.php/news/3?var1=val1&var2=val2';
  334. $_SERVER['SCRIPT_NAME'] = '/home.php';
  335. $_SERVER['PHP_SELF'] = '/index.php/news/3';
  336. $_SERVER['SCRIPT_FILENAME'] = '/var/web/html/index.php';
  337. $_GET = array(
  338. 'var1' => 'val1',
  339. 'var2' => 'val2'
  340. );
  341. $request = new Zend_Controller_Request_Http();
  342. $this->assertEquals('/index.php', $request->getBaseUrl());
  343. }
  344. public function testSetBaseUrlUsingOrigScriptName()
  345. {
  346. $_SERVER['REQUEST_URI'] = '/index.php/news/3?var1=val1&var2=val2';
  347. $_SERVER['SCRIPT_NAME'] = '/home.php';
  348. $_SERVER['PHP_SELF'] = '/home.php';
  349. $_SERVER['ORIG_SCRIPT_NAME']= '/index.php';
  350. $_SERVER['SCRIPT_FILENAME'] = '/var/web/html/index.php';
  351. $_GET = array(
  352. 'var1' => 'val1',
  353. 'var2' => 'val2'
  354. );
  355. $request = new Zend_Controller_Request_Http();
  356. $this->assertEquals('/index.php', $request->getBaseUrl());
  357. }
  358. public function testSetBaseUrlAutoDiscoveryUsingRequestUri()
  359. {
  360. $_SERVER['REQUEST_URI'] = '/index.php/news/3?var1=val1&var2=val2';
  361. $_SERVER['PHP_SELF'] = '/index.php/news/3';
  362. $_SERVER['SCRIPT_FILENAME'] = '/var/web/html/index.php';
  363. $_GET = array(
  364. 'var1' => 'val1',
  365. 'var2' => 'val2'
  366. );
  367. $request = new Zend_Controller_Request_Http();
  368. $this->assertEquals('/index.php', $request->getBaseUrl());
  369. }
  370. public function testSetBaseUrlAutoDiscoveryUsingXRewriteUrl()
  371. {
  372. unset($_SERVER['REQUEST_URI']);
  373. $_SERVER['HTTP_X_REWRITE_URL'] = '/index.php/news/3?var1=val1&var2=val2';
  374. $_SERVER['PHP_SELF'] = '/index.php/news/3';
  375. $_SERVER['SCRIPT_FILENAME'] = '/var/web/html/index.php';
  376. $_GET = array(
  377. 'var1' => 'val1',
  378. 'var2' => 'val2'
  379. );
  380. $request = new Zend_Controller_Request_Http();
  381. $this->assertEquals('/index.php', $request->getBaseUrl());
  382. }
  383. public function testSetBaseUrlAutoDiscoveryUsingOrigPathInfo()
  384. {
  385. unset($_SERVER['REQUEST_URI']);
  386. $_SERVER['ORIG_PATH_INFO'] = '/index.php/news/3';
  387. $_SERVER['QUERY_STRING'] = 'var1=val1&var2=val2';
  388. $_SERVER['PHP_SELF'] = '/index.php/news/3';
  389. $_SERVER['SCRIPT_FILENAME'] = '/var/web/html/index.php';
  390. $_GET = array(
  391. 'var1' => 'val1',
  392. 'var2' => 'val2'
  393. );
  394. $request = new Zend_Controller_Request_Http();
  395. $this->assertEquals('/index.php', $request->getBaseUrl());
  396. }
  397. public function testGetSetBasePath()
  398. {
  399. $this->_request->setBasePath('/news');
  400. $this->assertEquals('/news', $this->_request->getBasePath());
  401. }
  402. public function testBasePathAutoDiscovery()
  403. {
  404. $_SERVER['REQUEST_URI'] = '/html/index.php/news/3?var1=val1&var2=val2';
  405. $_SERVER['PHP_SELF'] = '/html/index.php/news/3';
  406. $_SERVER['SCRIPT_FILENAME'] = '/var/web/html/index.php';
  407. $_GET = array(
  408. 'var1' => 'val1',
  409. 'var2' => 'val2'
  410. );
  411. $request = new Zend_Controller_Request_Http();
  412. $this->assertEquals('/html', $request->getBasePath(), $request->getBaseUrl());
  413. }
  414. public function testBasePathAutoDiscoveryWithPhpFile()
  415. {
  416. $_SERVER['REQUEST_URI'] = '/dir/action';
  417. $_SERVER['PHP_SELF'] = '/dir/index.php';
  418. $_SERVER['SCRIPT_FILENAME'] = '/var/web/dir/index.php';
  419. $request = new Zend_Controller_Request_Http();
  420. $this->assertEquals('/dir', $request->getBasePath(), $request->getBaseUrl());
  421. }
  422. public function testGetCookie()
  423. {
  424. $_COOKIE['foo'] = 'bar';
  425. $this->assertSame('bar', $this->_request->getCookie('foo'));
  426. $this->assertEquals('foo', $this->_request->getCookie('BAR', 'foo'));
  427. $this->assertEquals($_COOKIE, $this->_request->getCookie());
  428. }
  429. public function testGetServer()
  430. {
  431. if (isset($_SERVER['REQUEST_METHOD'])) {
  432. $this->assertEquals($_SERVER['REQUEST_METHOD'], $this->_request->getServer('REQUEST_METHOD'));
  433. }
  434. $this->assertEquals('foo', $this->_request->getServer('BAR', 'foo'));
  435. $this->assertEquals($_SERVER, $this->_request->getServer());
  436. }
  437. public function testGetEnv()
  438. {
  439. if (isset($_ENV['PATH'])) {
  440. $this->assertEquals($_ENV['PATH'], $this->_request->getEnv('PATH'));
  441. }
  442. $this->assertEquals('foo', $this->_request->getEnv('BAR', 'foo'));
  443. $this->assertEquals($_ENV, $this->_request->getEnv());
  444. }
  445. public function testGetHeader()
  446. {
  447. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'UTF-8';
  448. $_SERVER['HTTP_CONTENT_TYPE'] = 'text/json';
  449. $this->assertEquals('UTF-8', $this->_request->getHeader('Accept-Encoding'));
  450. $this->assertEquals('text/json', $this->_request->getHeader('Content-Type'));
  451. $this->assertFalse($this->_request->getHeader('X-No-Such-Thing'));
  452. }
  453. public function testGetHeaderThrowsExceptionWithNoInput()
  454. {
  455. try {
  456. // Suppressing warning
  457. $header = @$this->_request->getHeader();
  458. $this->fail('getHeader() should fail with no arguments)');
  459. } catch (Exception $e) {
  460. // success
  461. }
  462. }
  463. public function testIsXmlHttpRequest()
  464. {
  465. $this->assertFalse($this->_request->isXmlHttpRequest());
  466. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  467. $this->assertTrue($this->_request->isXmlHttpRequest());
  468. }
  469. public function testSetNullParamUnsetsKey()
  470. {
  471. $this->_request->setParam('foo', 'bar');
  472. $this->assertEquals('bar', $this->_request->getParam('foo'));
  473. $this->_request->setParam('foo', null);
  474. $params = $this->_request->getParams();
  475. $this->assertFalse(isset($params['foo']));
  476. }
  477. public function testSetNullParamsUnsetsKeys()
  478. {
  479. $this->_request->setParams(array('foo' => 'bar', 'bar' => 'baz'));
  480. $this->assertEquals('bar', $this->_request->getParam('foo'));
  481. $this->assertEquals('baz', $this->_request->getParam('bar'));
  482. $this->_request->setParams(array('foo' => null));
  483. $params = $this->_request->getParams();
  484. $this->assertFalse(isset($params['foo']));
  485. $this->assertTrue(isset($params['bar']));
  486. }
  487. public function testGetAliasedParamZF2455()
  488. {
  489. $this->_request->setParam('controller', 'value');
  490. $this->_request->setAlias('var1', 'controller');
  491. $this->assertEquals('value', $this->_request->getParam('controller'));
  492. $this->assertEquals('value', $this->_request->getParam('var1'));
  493. }
  494. public function testCanDetectFlashRequests()
  495. {
  496. $this->assertFalse($this->_request->isFlashRequest());
  497. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  498. $this->assertTrue($this->_request->isFlashRequest());
  499. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 10';
  500. $this->assertTrue($this->_request->isFlashRequest());
  501. }
  502. /**
  503. * @group ZF-1798
  504. */
  505. public function testGetAndPostBothInDefaultParamSources()
  506. {
  507. $this->assertEquals(array('_GET', '_POST'), $this->_request->getParamSources());
  508. }
  509. /**
  510. * @group ZF-1798
  511. */
  512. public function testCanSetParamSources()
  513. {
  514. $this->testGetAndPostBothInDefaultParamSources();
  515. $this->_request->setParamSources(array());
  516. $this->assertSame(array(), $this->_request->getParamSources());
  517. $this->_request->setParamSources(array('_GET'));
  518. $this->assertSame(array('_GET'), $this->_request->getParamSources());
  519. }
  520. /**
  521. * @group ZF-1798
  522. */
  523. public function testParamSourcesHonoredByGetParam()
  524. {
  525. $_GET = array('foo' => 'bar');
  526. $_POST = array('foo' => 'baz');
  527. $this->_request->setParamSources(array('_POST'));
  528. $this->assertEquals('baz', $this->_request->getParam('foo'));
  529. }
  530. /**
  531. * @group ZF-3161
  532. * @group ZFI-233
  533. * @group ZF-5818
  534. */
  535. public function testStrippingHttpProtocolAndHostFromRequestUriOnlyWhenPresentAtBeginningOfUri()
  536. {
  537. $_SERVER['REQUEST_URI'] = 'http://foo.example.com/foo/bar?r=http://foo.example.com/bar/baz';
  538. $_SERVER['HTTP_HOST'] = 'foo.example.com';
  539. $request = new Zend_Controller_Request_Http();
  540. $test = $request->getRequestUri();
  541. $this->assertEquals('/foo/bar?r=http://foo.example.com/bar/baz', $test);
  542. $_SERVER['REQUEST_URI'] = '/foo/bar?r=http://foo.example.com/bar/baz';
  543. $_SERVER['HTTP_HOST'] = 'foo.example.com';
  544. $request = new Zend_Controller_Request_Http();
  545. $test = $request->getRequestUri();
  546. $this->assertEquals('/foo/bar?r=http://foo.example.com/bar/baz', $test);
  547. }
  548. /**
  549. * @group ZF-3161
  550. * @group ZFI-233
  551. * @group ZF-5818
  552. */
  553. public function testStrippingHttpsProtocolAndHostFromRequestUriOnlyWhenPresentAtBeginningOfUri()
  554. {
  555. $_SERVER['REQUEST_URI'] = 'https://foo.example.com/foo/bar?r=https://foo.example.com/bar/baz';
  556. $_SERVER['HTTP_HOST'] = 'foo.example.com';
  557. $_SERVER['HTTPS'] = 'on';
  558. $request = new Zend_Controller_Request_Http();
  559. $test = $request->getRequestUri();
  560. $this->assertEquals('/foo/bar?r=https://foo.example.com/bar/baz', $test);
  561. $_SERVER['REQUEST_URI'] = '/foo/bar?r=https://foo.example.com/bar/baz';
  562. $_SERVER['HTTP_HOST'] = 'foo.example.com';
  563. $_SERVER['HTTPS'] = 'on';
  564. $request = new Zend_Controller_Request_Http();
  565. $test = $request->getRequestUri();
  566. $this->assertEquals('/foo/bar?r=https://foo.example.com/bar/baz', $test);
  567. }
  568. /**
  569. * @group ZF-3161
  570. * @group ZFI-233
  571. * @group ZF-5818
  572. */
  573. public function testStrippingHttpProtocolHostAndNonStandardPortFromRequestUriOnlyWhenPresentAtBeginningOfUri()
  574. {
  575. $_SERVER['REQUEST_URI'] = 'http://foo.example.com:8888/foo/bar?r=http://foo.example.com:8888/bar/baz';
  576. $_SERVER['HTTP_HOST'] = '';
  577. $_SERVER['SERVER_NAME'] = 'foo.example.com';
  578. $_SERVER['SERVER_PORT'] = '8888';
  579. $request = new Zend_Controller_Request_Http();
  580. $test = $request->getRequestUri();
  581. $this->assertEquals('/foo/bar?r=http://foo.example.com:8888/bar/baz', $test);
  582. $_SERVER['REQUEST_URI'] = '/foo/bar?r=https://foo.example.com:8888/bar/baz';
  583. $_SERVER['HTTP_HOST'] = '';
  584. $_SERVER['SERVER_NAME'] = 'foo.example.com';
  585. $_SERVER['SERVER_PORT'] = '8888';
  586. $request = new Zend_Controller_Request_Http();
  587. $test = $request->getRequestUri();
  588. $this->assertEquals('/foo/bar?r=https://foo.example.com:8888/bar/baz', $test);
  589. }
  590. /**
  591. * @group ZF-7092
  592. */
  593. public function testGetClientIp()
  594. {
  595. $request = new Zend_Controller_Request_Http();
  596. $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.10';
  597. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.11';
  598. $_SERVER['REMOTE_ADDR'] = '192.168.1.12';
  599. $this->assertEquals('192.168.1.10', $request->getClientIp());
  600. $_SERVER['HTTP_CLIENT_IP'] = '';
  601. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.11';
  602. $_SERVER['REMOTE_ADDR'] = '192.168.1.12';
  603. $this->assertEquals('192.168.1.11', $request->getClientIp());
  604. $_SERVER['HTTP_CLIENT_IP'] = '';
  605. $_SERVER['HTTP_X_FORWARDED_FOR'] = '';
  606. $_SERVER['REMOTE_ADDR'] = '192.168.1.12';
  607. $this->assertEquals('192.168.1.12', $request->getClientIp());
  608. }
  609. /**
  610. * @group ZF-7117
  611. */
  612. public function testGetClientIpNoProxyCheck()
  613. {
  614. $request = new Zend_Controller_Request_Http();
  615. $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.10';
  616. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.11';
  617. $_SERVER['REMOTE_ADDR'] = '192.168.1.12';
  618. $this->assertEquals('192.168.1.12', $request->getClientIp(false));
  619. }
  620. }
  621. // Call Zend_Controller_Request_HttpTest::main() if this source file is executed directly.
  622. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Request_HttpTest::main") {
  623. Zend_Controller_Request_HttpTest::main();
  624. }