HttpTest.php 21 KB

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