|
| 1 | +<?php |
| 2 | +namespace CodeIgniter\Test; |
| 3 | + |
| 4 | +use CodeIgniter\HTTP\Response; |
| 5 | +use CodeIgniter\HTTP\Exceptions\HTTPException; |
| 6 | +use Config\App; |
| 7 | + |
| 8 | +/** |
| 9 | + * This test suite has been created separately from |
| 10 | + * TestCaseTest because it messes with output |
| 11 | + * buffering from PHPUnit, and the individual |
| 12 | + * test cases need to be run as separate processes. |
| 13 | + */ |
| 14 | +class TestCaseEmissionsTest extends \CIUnitTestCase |
| 15 | +{ |
| 16 | + |
| 17 | + //-------------------------------------------------------------------- |
| 18 | + /** |
| 19 | + * This needs to be run as a separate process, since phpunit |
| 20 | + * has already captured the "normal" output, and we will get |
| 21 | + * a "Cannot modify headers" message if we try to change |
| 22 | + * headers or cookies now. |
| 23 | + * |
| 24 | + * Furthermore, this test needs to flush the output buffering |
| 25 | + * that might be in progress, and start our own output buffer |
| 26 | + * capture. |
| 27 | + * |
| 28 | + * This test includes a basic sanity check, to make sure that |
| 29 | + * the body we thought would be sent actually was. |
| 30 | + * |
| 31 | + * @runInSeparateProcess |
| 32 | + * @preserveGlobalState disabled |
| 33 | + */ |
| 34 | + public function testHeadersEmitted() |
| 35 | + { |
| 36 | + |
| 37 | + $response = new Response(new App()); |
| 38 | + $response->pretend(FALSE); |
| 39 | + |
| 40 | + $body = 'Hello'; |
| 41 | + $response->setBody($body); |
| 42 | + |
| 43 | + $response->setCookie('foo', 'bar'); |
| 44 | + $this->assertTrue($response->hasCookie('foo')); |
| 45 | + $this->assertTrue($response->hasCookie('foo', 'bar')); |
| 46 | + |
| 47 | + // send it |
| 48 | + ob_start(); |
| 49 | + $response->send(); |
| 50 | + |
| 51 | + $buffer = ob_clean(); |
| 52 | + if (ob_get_level() > 0) |
| 53 | + ob_end_clean(); |
| 54 | + |
| 55 | + // and what actually got sent?; test both ways |
| 56 | + $this->assertHeaderEmitted("Set-Cookie: foo=bar;"); |
| 57 | + $this->assertHeaderEmitted("set-cookie: FOO=bar", true); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * This needs to be run as a separate process, since phpunit |
| 62 | + * has already captured the "normal" output, and we will get |
| 63 | + * a "Cannot modify headers" message if we try to change |
| 64 | + * headers or cookies now. |
| 65 | + * |
| 66 | + * Furthermore, this test needs to flush the output buffering |
| 67 | + * that might be in progress, and start our own output buffer |
| 68 | + * capture. |
| 69 | + * |
| 70 | + * This test includes a basic sanity check, to make sure that |
| 71 | + * the body we thought would be sent actually was. |
| 72 | + * |
| 73 | + * @runInSeparateProcess |
| 74 | + * @preserveGlobalState disabled |
| 75 | + */ |
| 76 | + public function testHeadersNotEmitted() |
| 77 | + { |
| 78 | + $response = new Response(new App()); |
| 79 | + $response->pretend(FALSE); |
| 80 | + |
| 81 | + $body = 'Hello'; |
| 82 | + $response->setBody($body); |
| 83 | + |
| 84 | + // what do we think we're about to send? |
| 85 | + $response->setCookie('foo', 'bar'); |
| 86 | + $this->assertTrue($response->hasCookie('foo')); |
| 87 | + $this->assertTrue($response->hasCookie('foo', 'bar')); |
| 88 | + |
| 89 | + // send it |
| 90 | + ob_start(); |
| 91 | + $response->send(); |
| 92 | + $output = ob_clean(); // what really was sent |
| 93 | + if (ob_get_level() > 0) |
| 94 | + ob_end_clean(); |
| 95 | + |
| 96 | + $this->assertHeaderNotEmitted("Set-Cookie: pop=corn", true); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments