PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Controller/Response/HttpTest.php

https://bitbucket.org/ksekar/campus
PHP | 654 lines | 494 code | 97 blank | 63 comment | 18 complexity | 5d5f5125eda99f9326bbd118a034315d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: HttpTest.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. // Call Zend_Controller_Response_HttpTest::main() if this source file is executed directly.
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_Controller_Response_HttpTest::main');
  25. }
  26. require_once 'Zend/Controller/Response/Http.php';
  27. require_once 'Zend/Controller/Response/Exception.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Controller
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Controller
  35. * @group Zend_Controller_Response
  36. */
  37. class Zend_Controller_Response_HttpTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * @var Zend_Http_Response
  41. */
  42. protected $_response;
  43. /**
  44. * Runs the test methods of this class.
  45. *
  46. * @access public
  47. * @static
  48. */
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Response_HttpTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. public function setUp()
  55. {
  56. $this->_response = new Zend_Controller_Response_Http();
  57. $this->_response->headersSentThrowsException = false;
  58. }
  59. public function tearDown()
  60. {
  61. unset($this->_response);
  62. }
  63. public function testSetHeader()
  64. {
  65. $expected = array(array('name' => 'Content-Type', 'value' => 'text/xml', 'replace' => false));
  66. $this->_response->setHeader('Content-Type', 'text/xml');
  67. $this->assertSame($expected, $this->_response->getHeaders());
  68. $expected[] =array('name' => 'Content-Type', 'value' => 'text/html', 'replace' => false);
  69. $this->_response->setHeader('Content-Type', 'text/html');
  70. $this->assertSame($expected, $this->_response->getHeaders());
  71. $expected = array(array('name' => 'Content-Type', 'value' => 'text/plain', 'replace' => true));
  72. $this->_response->setHeader('Content-Type', 'text/plain', true);
  73. $count = 0;
  74. foreach ($this->_response->getHeaders() as $header) {
  75. if ('Content-Type' == $header['name']) {
  76. if ('text/plain' == $header['value']) {
  77. ++$count;
  78. } else {
  79. $this->fail('Found header, but incorrect value');
  80. }
  81. }
  82. }
  83. $this->assertEquals(1, $count);
  84. }
  85. public function testNoDuplicateLocationHeader()
  86. {
  87. $this->_response->setRedirect('http://www.example.com/foo/bar');
  88. $this->_response->setRedirect('http://www.example.com/bar/baz');
  89. $headers = $this->_response->getHeaders();
  90. $location = 0;
  91. foreach ($headers as $header) {
  92. if ('Location' == $header['name']) {
  93. ++$location;
  94. }
  95. }
  96. $this->assertEquals(1, $location);
  97. }
  98. public function testClearHeaders()
  99. {
  100. $this->_response->setHeader('Content-Type', 'text/xml');
  101. $headers = $this->_response->getHeaders();
  102. $this->assertEquals(1, count($headers));
  103. $this->_response->clearHeaders();
  104. $headers = $this->_response->getHeaders();
  105. $this->assertEquals(0, count($headers));
  106. }
  107. /**
  108. * @group ZF-6038
  109. */
  110. public function testClearHeader()
  111. {
  112. $this->_response->setHeader('Connection', 'keep-alive');
  113. $original_headers = $this->_response->getHeaders();
  114. $this->_response->clearHeader('Connection');
  115. $updated_headers = $this->_response->getHeaders();
  116. $this->assertFalse($original_headers == $updated_headers);
  117. }
  118. public function testSetRawHeader()
  119. {
  120. $this->_response->setRawHeader('HTTP/1.0 404 Not Found');
  121. $headers = $this->_response->getRawHeaders();
  122. $this->assertContains('HTTP/1.0 404 Not Found', $headers);
  123. }
  124. public function testClearRawHeaders()
  125. {
  126. $this->_response->setRawHeader('HTTP/1.0 404 Not Found');
  127. $headers = $this->_response->getRawHeaders();
  128. $this->assertContains('HTTP/1.0 404 Not Found', $headers);
  129. $this->_response->clearRawHeaders();
  130. $headers = $this->_response->getRawHeaders();
  131. $this->assertTrue(empty($headers));
  132. }
  133. /**
  134. * @group ZF-6038
  135. */
  136. public function testClearRawHeader()
  137. {
  138. $this->_response->setRawHeader('HTTP/1.0 404 Not Found');
  139. $this->_response->setRawHeader('HTTP/1.0 401 Unauthorized');
  140. $originalHeadersRaw = $this->_response->getRawHeaders();
  141. $this->_response->clearRawHeader('HTTP/1.0 404 Not Found');
  142. $updatedHeadersRaw = $this->_response->getRawHeaders();
  143. $this->assertFalse($originalHeadersRaw == $updatedHeadersRaw);
  144. }
  145. /**
  146. * @group ZF-6038
  147. */
  148. public function testClearRawHeaderThatDoesNotExist()
  149. {
  150. $this->_response->setRawHeader('HTTP/1.0 404 Not Found');
  151. $this->_response->setRawHeader('HTTP/1.0 401 Unauthorized');
  152. $originalHeadersRaw = $this->_response->getRawHeaders();
  153. $this->_response->clearRawHeader('HTTP/1.0 403 Forbidden');
  154. $updatedHeadersRaw = $this->_response->getRawHeaders();
  155. $this->assertTrue($originalHeadersRaw == $updatedHeadersRaw);
  156. }
  157. public function testClearAllHeaders()
  158. {
  159. $this->_response->setRawHeader('HTTP/1.0 404 Not Found');
  160. $this->_response->setHeader('Content-Type', 'text/xml');
  161. $headers = $this->_response->getHeaders();
  162. $this->assertFalse(empty($headers));
  163. $headers = $this->_response->getRawHeaders();
  164. $this->assertFalse(empty($headers));
  165. $this->_response->clearAllHeaders();
  166. $headers = $this->_response->getHeaders();
  167. $this->assertTrue(empty($headers));
  168. $headers = $this->_response->getRawHeaders();
  169. $this->assertTrue(empty($headers));
  170. }
  171. public function testSetHttpResponseCode()
  172. {
  173. $this->assertEquals(200, $this->_response->getHttpResponseCode());
  174. $this->_response->setHttpResponseCode(302);
  175. $this->assertEquals(302, $this->_response->getHttpResponseCode());
  176. }
  177. public function testSetBody()
  178. {
  179. $expected = 'content for the response body';
  180. $this->_response->setBody($expected);
  181. $this->assertEquals($expected, $this->_response->getBody());
  182. $expected = 'new content';
  183. $this->_response->setBody($expected);
  184. $this->assertEquals($expected, $this->_response->getBody());
  185. }
  186. public function testAppendBody()
  187. {
  188. $expected = 'content for the response body';
  189. $this->_response->setBody($expected);
  190. $additional = '; and then there was more';
  191. $this->_response->appendBody($additional);
  192. $this->assertEquals($expected . $additional, $this->_response->getBody());
  193. }
  194. /**
  195. * SKIPPED - This test is untestable in the CLI environment. PHP ignores all
  196. * header() calls (which are used by Http_Abstract::setHeader()), thus, anything
  197. * that is expected to be found in http headers when inserted via header(), will
  198. * not be found. In addition, headers_sent() should always return false, until
  199. * real output is sent to the console.
  200. */
  201. public function test__toString()
  202. {
  203. $skipHeadersTest = headers_sent();
  204. if ($skipHeadersTest) {
  205. $this->markTestSkipped('Unable to run Zend_Controller_Response_Http::__toString() test as headers have already been sent');
  206. return;
  207. }
  208. $this->_response->setHeader('Content-Type', 'text/plain');
  209. $this->_response->setBody('Content');
  210. $this->_response->appendBody('; and more content.');
  211. $expected = 'Content; and more content.';
  212. $result = $this->_response->__toString();
  213. $this->assertSame($expected, $result);
  214. return;
  215. // header checking will not work
  216. if (!$skipHeadersTest) {
  217. $this->assertTrue(headers_sent());
  218. $headers = headers_list();
  219. $found = false;
  220. foreach ($headers as $header) {
  221. if ('Content-Type: text/plain' == $header) {
  222. $found = true;
  223. }
  224. }
  225. $this->assertTrue($found, var_export($headers, 1));
  226. }
  227. }
  228. public function testRenderExceptions()
  229. {
  230. $this->assertFalse($this->_response->renderExceptions());
  231. $this->assertTrue($this->_response->renderExceptions(true));
  232. $this->assertTrue($this->_response->renderExceptions());
  233. $this->assertFalse($this->_response->renderExceptions(false));
  234. $this->assertFalse($this->_response->renderExceptions());
  235. }
  236. public function testGetException()
  237. {
  238. $e = new Exception('Test');
  239. $this->_response->setException($e);
  240. $test = $this->_response->getException();
  241. $found = false;
  242. foreach ($test as $t) {
  243. if ($t === $e) {
  244. $found = true;
  245. }
  246. }
  247. $this->assertTrue($found);
  248. }
  249. public function testSendResponseWithExceptions()
  250. {
  251. $e = new Exception('Test exception rendering');
  252. $this->_response->setException($e);
  253. $this->_response->renderExceptions(true);
  254. ob_start();
  255. $this->_response->sendResponse();
  256. $string = ob_get_clean();
  257. $this->assertContains('Test exception rendering', $string);
  258. }
  259. public function testSetResponseCodeThrowsExceptionWithBadCode()
  260. {
  261. try {
  262. $this->_response->setHttpResponseCode(99);
  263. $this->fail('Should not accept response codes < 100');
  264. } catch (Exception $e) {
  265. }
  266. try {
  267. $this->_response->setHttpResponseCode(600);
  268. $this->fail('Should not accept response codes > 599');
  269. } catch (Exception $e) {
  270. }
  271. try {
  272. $this->_response->setHttpResponseCode('bogus');
  273. $this->fail('Should not accept non-integer response codes');
  274. } catch (Exception $e) {
  275. }
  276. }
  277. /**
  278. * Same problem as test__toString()
  279. *
  280. * Specifically for this test, headers_sent will always be false, so canSentHeaders() will
  281. * never actually throw an exception since the conditional exception code will never trigger
  282. */
  283. public function testCanSendHeadersIndicatesFileAndLine()
  284. {
  285. $this->markTestSkipped();
  286. return;
  287. $this->_response->headersSentThrowsException = true;
  288. try {
  289. $this->_response->canSendHeaders(true);
  290. $this->fail('canSendHeaders() should throw exception');
  291. } catch (Exception $e) {
  292. var_dump($e->getMessage());
  293. $this->assertRegExp('/headers already sent in .+, line \d+$/', $e->getMessage());
  294. }
  295. }
  296. public function testAppend()
  297. {
  298. $this->_response->append('some', "some content\n");
  299. $this->_response->append('more', "more content\n");
  300. $content = $this->_response->getBody(true);
  301. $this->assertTrue(is_array($content));
  302. $expected = array(
  303. 'some' => "some content\n",
  304. 'more' => "more content\n"
  305. );
  306. $this->assertEquals($expected, $content);
  307. }
  308. public function testAppendUsingExistingSegmentOverwrites()
  309. {
  310. $this->_response->append('some', "some content\n");
  311. $this->_response->append('some', "more content\n");
  312. $content = $this->_response->getBody(true);
  313. $this->assertTrue(is_array($content));
  314. $expected = array(
  315. 'some' => "more content\n"
  316. );
  317. $this->assertEquals($expected, $content);
  318. }
  319. public function testPrepend()
  320. {
  321. $this->_response->prepend('some', "some content\n");
  322. $this->_response->prepend('more', "more content\n");
  323. $content = $this->_response->getBody(true);
  324. $this->assertTrue(is_array($content));
  325. $expected = array(
  326. 'more' => "more content\n",
  327. 'some' => "some content\n"
  328. );
  329. $this->assertEquals($expected, $content);
  330. }
  331. public function testPrependUsingExistingSegmentOverwrites()
  332. {
  333. $this->_response->prepend('some', "some content\n");
  334. $this->_response->prepend('some', "more content\n");
  335. $content = $this->_response->getBody(true);
  336. $this->assertTrue(is_array($content));
  337. $expected = array(
  338. 'some' => "more content\n"
  339. );
  340. $this->assertEquals($expected, $content);
  341. }
  342. public function testInsert()
  343. {
  344. $this->_response->append('some', "some content\n");
  345. $this->_response->append('more', "more content\n");
  346. $this->_response->insert('foobar', "foobar content\n", 'some');
  347. $content = $this->_response->getBody(true);
  348. $this->assertTrue(is_array($content));
  349. $expected = array(
  350. 'some' => "some content\n",
  351. 'foobar' => "foobar content\n",
  352. 'more' => "more content\n"
  353. );
  354. $this->assertSame($expected, $content);
  355. }
  356. public function testInsertBefore()
  357. {
  358. $this->_response->append('some', "some content\n");
  359. $this->_response->append('more', "more content\n");
  360. $this->_response->insert('foobar', "foobar content\n", 'some', true);
  361. $content = $this->_response->getBody(true);
  362. $this->assertTrue(is_array($content));
  363. $expected = array(
  364. 'foobar' => "foobar content\n",
  365. 'some' => "some content\n",
  366. 'more' => "more content\n"
  367. );
  368. $this->assertSame($expected, $content);
  369. }
  370. public function testInsertWithFalseParent()
  371. {
  372. $this->_response->append('some', "some content\n");
  373. $this->_response->append('more', "more content\n");
  374. $this->_response->insert('foobar', "foobar content\n", 'baz', true);
  375. $content = $this->_response->getBody(true);
  376. $this->assertTrue(is_array($content));
  377. $expected = array(
  378. 'some' => "some content\n",
  379. 'more' => "more content\n",
  380. 'foobar' => "foobar content\n"
  381. );
  382. $this->assertSame($expected, $content);
  383. }
  384. public function testSetBodyNamedSegment()
  385. {
  386. $this->_response->append('some', "some content\n");
  387. $this->_response->setBody("more content\n", 'some');
  388. $content = $this->_response->getBody(true);
  389. $this->assertTrue(is_array($content));
  390. $expected = array(
  391. 'some' => "more content\n"
  392. );
  393. $this->assertEquals($expected, $content);
  394. }
  395. public function testSetBodyOverwritesWithDefaultSegment()
  396. {
  397. $this->_response->append('some', "some content\n");
  398. $this->_response->setBody("more content\n");
  399. $content = $this->_response->getBody(true);
  400. $this->assertTrue(is_array($content));
  401. $expected = array(
  402. 'default' => "more content\n"
  403. );
  404. $this->assertEquals($expected, $content);
  405. }
  406. public function testAppendBodyAppendsDefaultSegment()
  407. {
  408. $this->_response->setBody("some content\n");
  409. $this->_response->appendBody("more content\n");
  410. $content = $this->_response->getBody(true);
  411. $this->assertTrue(is_array($content));
  412. $expected = array(
  413. 'default' => "some content\nmore content\n"
  414. );
  415. $this->assertEquals($expected, $content);
  416. }
  417. public function testAppendBodyAppendsExistingSegment()
  418. {
  419. $this->_response->setBody("some content\n", 'some');
  420. $this->_response->appendBody("more content\n", 'some');
  421. $content = $this->_response->getBody(true);
  422. $this->assertTrue(is_array($content));
  423. $expected = array(
  424. 'some' => "some content\nmore content\n"
  425. );
  426. $this->assertEquals($expected, $content);
  427. }
  428. public function testGetBodyNamedSegment()
  429. {
  430. $this->_response->append('some', "some content\n");
  431. $this->_response->append('more', "more content\n");
  432. $this->assertEquals("more content\n", $this->_response->getBody('more'));
  433. $this->assertEquals("some content\n", $this->_response->getBody('some'));
  434. }
  435. public function testGetBodyAsArray()
  436. {
  437. $string1 = 'content for the response body';
  438. $string2 = 'more content for the response body';
  439. $string3 = 'even more content for the response body';
  440. $this->_response->appendBody($string1, 'string1');
  441. $this->_response->appendBody($string2, 'string2');
  442. $this->_response->appendBody($string3, 'string3');
  443. $expected = array(
  444. 'string1' => $string1,
  445. 'string2' => $string2,
  446. 'string3' => $string3
  447. );
  448. $this->assertEquals($expected, $this->_response->getBody(true));
  449. }
  450. public function testClearBody()
  451. {
  452. $this->_response->append('some', "some content\n");
  453. $this->assertTrue($this->_response->clearBody());
  454. $body = $this->_response->getBody(true);
  455. $this->assertTrue(is_array($body));
  456. $this->assertEquals(0, count($body));
  457. }
  458. public function testClearBodySegment()
  459. {
  460. $this->_response->append('some', "some content\n");
  461. $this->_response->append('more', "more content\n");
  462. $this->_response->append('superfluous', "superfluous content\n");
  463. $this->assertFalse($this->_response->clearBody('many'));
  464. $this->assertTrue($this->_response->clearBody('more'));
  465. $body = $this->_response->getBody(true);
  466. $this->assertTrue(is_array($body));
  467. $this->assertEquals(2, count($body));
  468. $this->assertTrue(isset($body['some']));
  469. $this->assertTrue(isset($body['superfluous']));
  470. }
  471. public function testIsRedirectInitiallyFalse()
  472. {
  473. $this->assertFalse($this->_response->isRedirect());
  474. }
  475. public function testIsRedirectWhenRedirectSet()
  476. {
  477. $this->_response->setRedirect('http://framework.zend.com/');
  478. $this->assertTrue($this->_response->isRedirect());
  479. }
  480. public function testIsRedirectWhenRawLocationHeaderSet()
  481. {
  482. $this->_response->setRawHeader('Location: http://framework.zend.com/');
  483. $this->assertTrue($this->_response->isRedirect());
  484. }
  485. public function testIsRedirectWhen3xxResponseCodeSet()
  486. {
  487. $this->_response->setHttpResponseCode(301);
  488. $this->assertTrue($this->_response->isRedirect());
  489. }
  490. public function testIsNotRedirectWithSufficientlyLarge3xxResponseCodeSet()
  491. {
  492. $this->_response->setHttpResponseCode(309);
  493. $this->assertFalse($this->_response->isRedirect());
  494. }
  495. public function testHasExceptionOfType()
  496. {
  497. $this->assertFalse($this->_response->hasExceptionOfType('Zend_Controller_Response_Exception'));
  498. $this->_response->setException(new Zend_Controller_Response_Exception());
  499. $this->assertTrue($this->_response->hasExceptionOfType('Zend_Controller_Response_Exception'));
  500. }
  501. public function testHasExceptionOfMessage()
  502. {
  503. $this->assertFalse($this->_response->hasExceptionOfMessage('FooBar'));
  504. $this->_response->setException(new Zend_Controller_Response_Exception('FooBar'));
  505. $this->assertTrue($this->_response->hasExceptionOfMessage('FooBar'));
  506. }
  507. public function testHasExceptionOfCode()
  508. {
  509. $this->assertFalse($this->_response->hasExceptionOfCode(200));
  510. $this->_response->setException(new Zend_Controller_Response_Exception('FooBar', 200));
  511. $this->assertTrue($this->_response->hasExceptionOfCode(200));
  512. }
  513. public function testGetExceptionByType()
  514. {
  515. $this->assertFalse($this->_response->getExceptionByType('Zend_Controller_Response_Exception'));
  516. $this->_response->setException(new Zend_Controller_Response_Exception());
  517. $exceptions = $this->_response->getExceptionByType('Zend_Controller_Response_Exception');
  518. $this->assertTrue(0 < count($exceptions));
  519. $this->assertTrue($exceptions[0] instanceof Zend_Controller_Response_Exception);
  520. }
  521. public function testGetExceptionByMessage()
  522. {
  523. $this->assertFalse($this->_response->getExceptionByMessage('FooBar'));
  524. $this->_response->setException(new Zend_Controller_Response_Exception('FooBar'));
  525. $exceptions = $this->_response->getExceptionByMessage('FooBar');
  526. $this->assertTrue(0 < count($exceptions));
  527. $this->assertEquals('FooBar', $exceptions[0]->getMessage());
  528. }
  529. public function testGetExceptionByCode()
  530. {
  531. $this->assertFalse($this->_response->getExceptionByCode(200));
  532. $this->_response->setException(new Zend_Controller_Response_Exception('FooBar', 200));
  533. $exceptions = $this->_response->getExceptionByCode(200);
  534. $this->assertTrue(0 < count($exceptions));
  535. $this->assertEquals(200, $exceptions[0]->getCode());
  536. }
  537. public function testHeaderNamesAreCaseInsensitive()
  538. {
  539. $this->_response->setHeader('X-Foo_Bar-Baz', 'value');
  540. $this->_response->setHeader('X-FOO_bar-bAz', 'bat');
  541. $headers = $this->_response->getHeaders();
  542. $names = array();
  543. foreach ($headers as $header) {
  544. $names[] = $header['name'];
  545. }
  546. $this->assertTrue(in_array('X-Foo-Bar-Baz', $names), var_export($headers, 1));
  547. $this->assertFalse(in_array('X-Foo_Bar-Baz', $names));
  548. $this->assertFalse(in_array('X-FOO_bar-bAz', $names));
  549. }
  550. }
  551. require_once 'Zend/Controller/Action.php';
  552. class Zend_Controller_Response_HttpTest_Action extends Zend_Controller_Action
  553. {}
  554. // Call Zend_Controller_Response_HttpTest::main() if this source file is executed directly.
  555. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Response_HttpTest::main") {
  556. Zend_Controller_Response_HttpTest::main();
  557. }