HttpTest.php 22 KB

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