Merge #75 - send HTTP status code

This commit is contained in:
filp
2013-07-15 21:30:48 +01:00
2 changed files with 50 additions and 0 deletions
+36
View File
@@ -22,6 +22,7 @@ class Run
protected $isRegistered;
protected $allowQuit = true;
protected $sendOutput = true;
protected $sendHttpCode = 500;
/**
* @var HandlerInterface[]
@@ -142,6 +143,7 @@ class Run
}
/**
<<<<<<< HEAD
* Silence particular errors in particular files
* @param array|string $patterns List or a single regex pattern to match
* @param integer $levels Defaults to E_STRICT | E_DEPRECATED
@@ -164,6 +166,36 @@ class Run
return $this;
}
/*
* Should Whoops send HTTP error code to the browser if possible?
* Whoops will by default send HTTP code 500, but you may wish to
* use 502, 503, or another 5xx family code.
* @param bool|int $code
* @return bool
*/
public function sendHttpCode($code = null)
{
if(func_num_args() == 0) {
return $this->sendHttpCode;
}
if(!$code) {
return $this->sendHttpCode = false;
}
if($code === true) {
$code = 500;
}
if(!is_numeric($code) || $code < 500 || 600 <= $code) {
throw new \Exception(
'Only status codes 500-599 should be used in case of a server error'
);
}
return $this->sendHttpCode = $code;
}
/**
* Should Whoops push output directly to the client?
* If this is false, output will be returned by handleException
@@ -231,6 +263,10 @@ class Run
while (ob_get_level() > 0) ob_end_clean();
}
if ($this->sendHttpCode() && isset($_SERVER['REQUEST_URI']) && !headers_sent()) {
header(' ', true, $this->sendHttpCode());
}
echo $output;
}
+14
View File
@@ -360,4 +360,18 @@ class RunTest extends TestCase
$this->assertEquals("hello there", $run->handleException(new RuntimeException));
$this->assertEquals("", ob_get_clean());
}
public function testSendHttpCode()
{
$run = $this->getRunInstance();
$run->sendHttpCode(true);
$this->assertEquals(500, $run->sendHttpCode());
}
public function testSendHttpCodeWrongCode()
{
$run = $this->getRunInstance();
$this->setExpectedException('Exception');
$run->sendHttpCode(403);
}
}