Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Response send testing #1477

Merged
merged 1 commit into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/HTTP/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ public function sendHeaders()

// Per spec, MUST be sent with each request, if possible.
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
if (isset($this->headers['Date']))
if (! isset($this->headers['Date']))
{
$this->setDate(\DateTime::createFromFormat('U', time()));
}
Expand Down
100 changes: 100 additions & 0 deletions tests/system/HTTP/ResponseSendTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
namespace CodeIgniter\HTTP;

use CodeIgniter\HTTP\Response;
use Config\App;

/**
* This test suite has been created separately from
* TestCaseTest because it messes with output
* buffering from PHPUnit, and the individual
* test cases need to be run as separate processes.
*/
class ResponseSendTest extends \CIUnitTestCase
{

/**
* These need to be run as a separate process, since phpunit
* has already captured the "normal" output, and we will get
* a "Cannot modify headers" message if we try to change
* headers or cookies now.
*
* Furthermore, these tests needs to flush the output buffering
* that might be in progress, and start our own output buffer
* capture.
*
* The tests includes a basic sanity check, to make sure that
* the body we thought would be sent actually was.
*/

//--------------------------------------------------------------------
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testHeadersMissingDate()
{
$response = new Response(new App());
$response->pretend(false);

$body = 'Hello';
$response->setBody($body);

$response->setCookie('foo', 'bar');
$this->assertTrue($response->hasCookie('foo'));
$this->assertTrue($response->hasCookie('foo', 'bar'));

// Drop the date header, to make sure it gets put back in
$response->removeHeader('Date');

// send it
ob_start();
$response->send();

$buffer = ob_clean();
if (ob_get_level() > 0)
{
ob_end_clean();
}

// and what actually got sent?
$this->assertHeaderEmitted('Date:');
}

//--------------------------------------------------------------------
/**
* This test does not test that CSP is handled properly -
* it makes sure that sending gives CSP a chance to do its thing.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testHeadersWithCSP()
{
$config = new App();
$config->CSPEnabled = true;
$response = new Response($config);
$response->pretend(false);

$body = 'Hello';
$response->setBody($body);

$response->setCookie('foo', 'bar');
$this->assertTrue($response->hasCookie('foo'));
$this->assertTrue($response->hasCookie('foo', 'bar'));

// send it
ob_start();
$response->send();

$buffer = ob_clean();
if (ob_get_level() > 0)
{
ob_end_clean();
}

// and what actually got sent?; test both ways
$this->assertHeaderEmitted('Content-Security-Policy:');
}

}
25 changes: 8 additions & 17 deletions tests/system/Test/TestCaseEmissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
class TestCaseEmissionsTest extends \CIUnitTestCase
{

//--------------------------------------------------------------------
/**
* This needs to be run as a separate process, since phpunit
* These need to be run as a separate process, since phpunit
* has already captured the "normal" output, and we will get
* a "Cannot modify headers" message if we try to change
* headers or cookies now.
*
* Furthermore, this test needs to flush the output buffering
* Furthermore, these tests needs to flush the output buffering
* that might be in progress, and start our own output buffer
* capture.
*
* This test includes a basic sanity check, to make sure that
* The tests includes a basic sanity check, to make sure that
* the body we thought would be sent actually was.
*
*/

//--------------------------------------------------------------------
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
Expand Down Expand Up @@ -57,19 +59,8 @@ public function testHeadersEmitted()
$this->assertHeaderEmitted('set-cookie: FOO=bar', true);
}

//--------------------------------------------------------------------
/**
* This needs to be run as a separate process, since phpunit
* has already captured the "normal" output, and we will get
* a "Cannot modify headers" message if we try to change
* headers or cookies now.
*
* Furthermore, this test needs to flush the output buffering
* that might be in progress, and start our own output buffer
* capture.
*
* This test includes a basic sanity check, to make sure that
* the body we thought would be sent actually was.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
Expand Down