Skip to content

Commit

Permalink
Fixes #5 : Handle Response::sendHeaders() to not silently returns whe…
Browse files Browse the repository at this point in the history
…n header already sent

Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
samsonasik committed Aug 9, 2020
1 parent c314b20 commit 7e96d25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/PhpEnvironment/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Laminas\Http\PhpEnvironment;

use Laminas\Http\Exception\RuntimeException;
use Laminas\Http\Header\MultipleHeaderInterface;
use Laminas\Http\Response as HttpResponse;

Expand Down Expand Up @@ -82,7 +83,7 @@ public function contentSent()
public function sendHeaders()
{
if ($this->headersSent()) {
return $this;
throw new RuntimeException('Cannot send headers, headers already sent');
}

$status = $this->renderStatusLine();
Expand Down
24 changes: 24 additions & 0 deletions test/PhpEnvironment/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace LaminasTest\Http\PhpEnvironment;

use Laminas\Http\Exception\InvalidArgumentException;
use Laminas\Http\Exception\RuntimeException;
use Laminas\Http\PhpEnvironment\Response;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -100,4 +101,27 @@ public function testCanExplicitlySetVersion()
$this->expectException(InvalidArgumentException::class);
$response->setVersion('laminas/2.0');
}

/**
* @runInSeparateProcess
*/
public function testSendHeadersHeadersNotAlreadySent()
{
$response = new Response();
$this->assertInstanceOf(Response::class, $response->sendHeaders());
}

/**
* @runInSeparateProcesses
*/
public function testSendHeadersHeadersAlreadySent()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cannot send headers, headers already sent');

echo 'test';

$response = new Response();
$response->sendHeaders();
}
}

0 comments on commit 7e96d25

Please sign in to comment.