HttpTest.php 21 KB

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