HttpTest.php 22 KB

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