HttpTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. $this->_response->setHeader('Content-Type', 'text/plain');
  173. $this->_response->setBody('Content');
  174. $this->_response->appendBody('; and more content.');
  175. $expected = 'Content; and more content.';
  176. $result = $this->_response->__toString();
  177. $this->assertSame($expected, $result);
  178. return;
  179. // header checking will not work
  180. if (!$skipHeadersTest) {
  181. $this->assertTrue(headers_sent());
  182. $headers = headers_list();
  183. $found = false;
  184. foreach ($headers as $header) {
  185. if ('Content-Type: text/plain' == $header) {
  186. $found = true;
  187. }
  188. }
  189. $this->assertTrue($found, var_export($headers, 1));
  190. }
  191. }
  192. public function testRenderExceptions()
  193. {
  194. $this->assertFalse($this->_response->renderExceptions());
  195. $this->assertTrue($this->_response->renderExceptions(true));
  196. $this->assertTrue($this->_response->renderExceptions());
  197. $this->assertFalse($this->_response->renderExceptions(false));
  198. $this->assertFalse($this->_response->renderExceptions());
  199. }
  200. public function testGetException()
  201. {
  202. $e = new Exception('Test');
  203. $this->_response->setException($e);
  204. $test = $this->_response->getException();
  205. $found = false;
  206. foreach ($test as $t) {
  207. if ($t === $e) {
  208. $found = true;
  209. }
  210. }
  211. $this->assertTrue($found);
  212. }
  213. public function testSendResponseWithExceptions()
  214. {
  215. $e = new Exception('Test exception rendering');
  216. $this->_response->setException($e);
  217. $this->_response->renderExceptions(true);
  218. ob_start();
  219. $this->_response->sendResponse();
  220. $string = ob_get_clean();
  221. $this->assertContains('Test exception rendering', $string);
  222. }
  223. public function testSetResponseCodeThrowsExceptionWithBadCode()
  224. {
  225. try {
  226. $this->_response->setHttpResponseCode(99);
  227. $this->fail('Should not accept response codes < 100');
  228. } catch (Exception $e) {
  229. }
  230. try {
  231. $this->_response->setHttpResponseCode(600);
  232. $this->fail('Should not accept response codes > 599');
  233. } catch (Exception $e) {
  234. }
  235. try {
  236. $this->_response->setHttpResponseCode('bogus');
  237. $this->fail('Should not accept non-integer response codes');
  238. } catch (Exception $e) {
  239. }
  240. }
  241. /**
  242. * Same problem as test__toString()
  243. *
  244. * Specifically for this test, headers_sent will always be false, so canSentHeaders() will
  245. * never actually throw an exception since the conditional exception code will never trigger
  246. */
  247. public function testCanSendHeadersIndicatesFileAndLine()
  248. {
  249. $this->markTestSkipped();
  250. return;
  251. $this->_response->headersSentThrowsException = true;
  252. try {
  253. $this->_response->canSendHeaders(true);
  254. $this->fail('canSendHeaders() should throw exception');
  255. } catch (Exception $e) {
  256. var_dump($e->getMessage());
  257. $this->assertRegExp('/headers already sent in .+, line \d+$/', $e->getMessage());
  258. }
  259. }
  260. public function testAppend()
  261. {
  262. $this->_response->append('some', "some content\n");
  263. $this->_response->append('more', "more content\n");
  264. $content = $this->_response->getBody(true);
  265. $this->assertTrue(is_array($content));
  266. $expected = array(
  267. 'some' => "some content\n",
  268. 'more' => "more content\n"
  269. );
  270. $this->assertEquals($expected, $content);
  271. }
  272. public function testAppendUsingExistingSegmentOverwrites()
  273. {
  274. $this->_response->append('some', "some content\n");
  275. $this->_response->append('some', "more content\n");
  276. $content = $this->_response->getBody(true);
  277. $this->assertTrue(is_array($content));
  278. $expected = array(
  279. 'some' => "more content\n"
  280. );
  281. $this->assertEquals($expected, $content);
  282. }
  283. public function testPrepend()
  284. {
  285. $this->_response->prepend('some', "some content\n");
  286. $this->_response->prepend('more', "more content\n");
  287. $content = $this->_response->getBody(true);
  288. $this->assertTrue(is_array($content));
  289. $expected = array(
  290. 'more' => "more content\n",
  291. 'some' => "some content\n"
  292. );
  293. $this->assertEquals($expected, $content);
  294. }
  295. public function testPrependUsingExistingSegmentOverwrites()
  296. {
  297. $this->_response->prepend('some', "some content\n");
  298. $this->_response->prepend('some', "more content\n");
  299. $content = $this->_response->getBody(true);
  300. $this->assertTrue(is_array($content));
  301. $expected = array(
  302. 'some' => "more content\n"
  303. );
  304. $this->assertEquals($expected, $content);
  305. }
  306. public function testInsert()
  307. {
  308. $this->_response->append('some', "some content\n");
  309. $this->_response->append('more', "more content\n");
  310. $this->_response->insert('foobar', "foobar content\n", 'some');
  311. $content = $this->_response->getBody(true);
  312. $this->assertTrue(is_array($content));
  313. $expected = array(
  314. 'some' => "some content\n",
  315. 'foobar' => "foobar content\n",
  316. 'more' => "more content\n"
  317. );
  318. $this->assertSame($expected, $content);
  319. }
  320. public function testInsertBefore()
  321. {
  322. $this->_response->append('some', "some content\n");
  323. $this->_response->append('more', "more content\n");
  324. $this->_response->insert('foobar', "foobar content\n", 'some', true);
  325. $content = $this->_response->getBody(true);
  326. $this->assertTrue(is_array($content));
  327. $expected = array(
  328. 'foobar' => "foobar content\n",
  329. 'some' => "some content\n",
  330. 'more' => "more content\n"
  331. );
  332. $this->assertSame($expected, $content);
  333. }
  334. public function testInsertWithFalseParent()
  335. {
  336. $this->_response->append('some', "some content\n");
  337. $this->_response->append('more', "more content\n");
  338. $this->_response->insert('foobar', "foobar content\n", 'baz', true);
  339. $content = $this->_response->getBody(true);
  340. $this->assertTrue(is_array($content));
  341. $expected = array(
  342. 'some' => "some content\n",
  343. 'more' => "more content\n",
  344. 'foobar' => "foobar content\n"
  345. );
  346. $this->assertSame($expected, $content);
  347. }
  348. public function testSetBodyNamedSegment()
  349. {
  350. $this->_response->append('some', "some content\n");
  351. $this->_response->setBody("more content\n", 'some');
  352. $content = $this->_response->getBody(true);
  353. $this->assertTrue(is_array($content));
  354. $expected = array(
  355. 'some' => "more content\n"
  356. );
  357. $this->assertEquals($expected, $content);
  358. }
  359. public function testSetBodyOverwritesWithDefaultSegment()
  360. {
  361. $this->_response->append('some', "some content\n");
  362. $this->_response->setBody("more content\n");
  363. $content = $this->_response->getBody(true);
  364. $this->assertTrue(is_array($content));
  365. $expected = array(
  366. 'default' => "more content\n"
  367. );
  368. $this->assertEquals($expected, $content);
  369. }
  370. public function testAppendBodyAppendsDefaultSegment()
  371. {
  372. $this->_response->setBody("some content\n");
  373. $this->_response->appendBody("more content\n");
  374. $content = $this->_response->getBody(true);
  375. $this->assertTrue(is_array($content));
  376. $expected = array(
  377. 'default' => "some content\nmore content\n"
  378. );
  379. $this->assertEquals($expected, $content);
  380. }
  381. public function testAppendBodyAppendsExistingSegment()
  382. {
  383. $this->_response->setBody("some content\n", 'some');
  384. $this->_response->appendBody("more content\n", 'some');
  385. $content = $this->_response->getBody(true);
  386. $this->assertTrue(is_array($content));
  387. $expected = array(
  388. 'some' => "some content\nmore content\n"
  389. );
  390. $this->assertEquals($expected, $content);
  391. }
  392. public function testGetBodyNamedSegment()
  393. {
  394. $this->_response->append('some', "some content\n");
  395. $this->_response->append('more', "more content\n");
  396. $this->assertEquals("more content\n", $this->_response->getBody('more'));
  397. $this->assertEquals("some content\n", $this->_response->getBody('some'));
  398. }
  399. public function testGetBodyAsArray()
  400. {
  401. $string1 = 'content for the response body';
  402. $string2 = 'more content for the response body';
  403. $string3 = 'even more content for the response body';
  404. $this->_response->appendBody($string1, 'string1');
  405. $this->_response->appendBody($string2, 'string2');
  406. $this->_response->appendBody($string3, 'string3');
  407. $expected = array(
  408. 'string1' => $string1,
  409. 'string2' => $string2,
  410. 'string3' => $string3
  411. );
  412. $this->assertEquals($expected, $this->_response->getBody(true));
  413. }
  414. public function testClearBody()
  415. {
  416. $this->_response->append('some', "some content\n");
  417. $this->assertTrue($this->_response->clearBody());
  418. $body = $this->_response->getBody(true);
  419. $this->assertTrue(is_array($body));
  420. $this->assertEquals(0, count($body));
  421. }
  422. public function testClearBodySegment()
  423. {
  424. $this->_response->append('some', "some content\n");
  425. $this->_response->append('more', "more content\n");
  426. $this->_response->append('superfluous', "superfluous content\n");
  427. $this->assertFalse($this->_response->clearBody('many'));
  428. $this->assertTrue($this->_response->clearBody('more'));
  429. $body = $this->_response->getBody(true);
  430. $this->assertTrue(is_array($body));
  431. $this->assertEquals(2, count($body));
  432. $this->assertTrue(isset($body['some']));
  433. $this->assertTrue(isset($body['superfluous']));
  434. }
  435. public function testIsRedirectInitiallyFalse()
  436. {
  437. $this->assertFalse($this->_response->isRedirect());
  438. }
  439. public function testIsRedirectWhenRedirectSet()
  440. {
  441. $this->_response->setRedirect('http://framework.zend.com/');
  442. $this->assertTrue($this->_response->isRedirect());
  443. }
  444. public function testIsRedirectWhenRawLocationHeaderSet()
  445. {
  446. $this->_response->setRawHeader('Location: http://framework.zend.com/');
  447. $this->assertTrue($this->_response->isRedirect());
  448. }
  449. public function testIsRedirectWhen3xxResponseCodeSet()
  450. {
  451. $this->_response->setHttpResponseCode(301);
  452. $this->assertTrue($this->_response->isRedirect());
  453. }
  454. public function testIsNotRedirectWithSufficientlyLarge3xxResponseCodeSet()
  455. {
  456. $this->_response->setHttpResponseCode(309);
  457. $this->assertFalse($this->_response->isRedirect());
  458. }
  459. public function testHasExceptionOfType()
  460. {
  461. $this->assertFalse($this->_response->hasExceptionOfType('Zend_Controller_Response_Exception'));
  462. $this->_response->setException(new Zend_Controller_Response_Exception());
  463. $this->assertTrue($this->_response->hasExceptionOfType('Zend_Controller_Response_Exception'));
  464. }
  465. public function testHasExceptionOfMessage()
  466. {
  467. $this->assertFalse($this->_response->hasExceptionOfMessage('FooBar'));
  468. $this->_response->setException(new Zend_Controller_Response_Exception('FooBar'));
  469. $this->assertTrue($this->_response->hasExceptionOfMessage('FooBar'));
  470. }
  471. public function testHasExceptionOfCode()
  472. {
  473. $this->assertFalse($this->_response->hasExceptionOfCode(200));
  474. $this->_response->setException(new Zend_Controller_Response_Exception('FooBar', 200));
  475. $this->assertTrue($this->_response->hasExceptionOfCode(200));
  476. }
  477. public function testGetExceptionByType()
  478. {
  479. $this->assertFalse($this->_response->getExceptionByType('Zend_Controller_Response_Exception'));
  480. $this->_response->setException(new Zend_Controller_Response_Exception());
  481. $exceptions = $this->_response->getExceptionByType('Zend_Controller_Response_Exception');
  482. $this->assertTrue(0 < count($exceptions));
  483. $this->assertTrue($exceptions[0] instanceof Zend_Controller_Response_Exception);
  484. }
  485. public function testGetExceptionByMessage()
  486. {
  487. $this->assertFalse($this->_response->getExceptionByMessage('FooBar'));
  488. $this->_response->setException(new Zend_Controller_Response_Exception('FooBar'));
  489. $exceptions = $this->_response->getExceptionByMessage('FooBar');
  490. $this->assertTrue(0 < count($exceptions));
  491. $this->assertEquals('FooBar', $exceptions[0]->getMessage());
  492. }
  493. public function testGetExceptionByCode()
  494. {
  495. $this->assertFalse($this->_response->getExceptionByCode(200));
  496. $this->_response->setException(new Zend_Controller_Response_Exception('FooBar', 200));
  497. $exceptions = $this->_response->getExceptionByCode(200);
  498. $this->assertTrue(0 < count($exceptions));
  499. $this->assertEquals(200, $exceptions[0]->getCode());
  500. }
  501. public function testHeaderNamesAreCaseInsensitive()
  502. {
  503. $this->_response->setHeader('X-Foo_Bar-Baz', 'value');
  504. $this->_response->setHeader('X-FOO_bar-bAz', 'bat');
  505. $headers = $this->_response->getHeaders();
  506. $names = array();
  507. foreach ($headers as $header) {
  508. $names[] = $header['name'];
  509. }
  510. $this->assertTrue(in_array('X-Foo-Bar-Baz', $names), var_export($headers, 1));
  511. $this->assertFalse(in_array('X-Foo_Bar-Baz', $names));
  512. $this->assertFalse(in_array('X-FOO_bar-bAz', $names));
  513. }
  514. }
  515. require_once 'Zend/Controller/Action.php';
  516. class Zend_Controller_Response_HttpTest_Action extends Zend_Controller_Action
  517. {}
  518. // Call Zend_Controller_Response_HttpTest::main() if this source file is executed directly.
  519. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Response_HttpTest::main") {
  520. Zend_Controller_Response_HttpTest::main();
  521. }