mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-03-18 07:29:51 +00:00
Merge pull request #1505 from jameswatts/feature/http-status-codes
Updated the handling of HTTP status codes in the response
This commit is contained in:
commit
cdb05b8bbc
2 changed files with 38 additions and 13 deletions
|
@ -618,7 +618,7 @@ class CakeResponse {
|
||||||
* Sets the HTTP status code to be sent
|
* Sets the HTTP status code to be sent
|
||||||
* if $code is null the current code is returned
|
* if $code is null the current code is returned
|
||||||
*
|
*
|
||||||
* @param integer $code
|
* @param integer $code the HTTP status code
|
||||||
* @return integer current status code
|
* @return integer current status code
|
||||||
* @throws CakeException When an unknown status code is reached.
|
* @throws CakeException When an unknown status code is reached.
|
||||||
*/
|
*/
|
||||||
|
@ -635,31 +635,47 @@ class CakeResponse {
|
||||||
/**
|
/**
|
||||||
* Queries & sets valid HTTP response codes & messages.
|
* Queries & sets valid HTTP response codes & messages.
|
||||||
*
|
*
|
||||||
* @param integer|array $code If $code is an integer, then the corresponding code/message is
|
* @param integer|array $code If $code is an integer, then the corresponding code/message is
|
||||||
* returned if it exists, null if it does not exist. If $code is an array,
|
* returned if it exists, null if it does not exist. If $code is an array, then the
|
||||||
* then the 'code' and 'message' keys of each nested array are added to the default
|
* keys are used as codes and the values as messages to add to the default HTTP
|
||||||
* HTTP codes. Example:
|
* codes. The codes must be integers greater than 99 and less than 1000. Keep in
|
||||||
|
* mind that the HTTP specification outlines that status codes begin with a digit
|
||||||
|
* between 1 and 5, which defines the class of response the client is to expect.
|
||||||
|
* Example:
|
||||||
*
|
*
|
||||||
* httpCodes(404); // returns array(404 => 'Not Found')
|
* httpCodes(404); // returns array(404 => 'Not Found')
|
||||||
*
|
*
|
||||||
* httpCodes(array(
|
* httpCodes(array(
|
||||||
* 701 => 'Unicorn Moved',
|
* 381 => 'Unicorn Moved',
|
||||||
* 800 => 'Unexpected Minotaur'
|
* 555 => 'Unexpected Minotaur'
|
||||||
* )); // sets these new values, and returns true
|
* )); // sets these new values, and returns true
|
||||||
*
|
*
|
||||||
|
* httpCodes(array(
|
||||||
|
* 0 => 'Nothing Here',
|
||||||
|
* -1 => 'Reverse Infinity',
|
||||||
|
* 12345 => 'Universal Password',
|
||||||
|
* 'Hello' => 'World'
|
||||||
|
* )); // throws an exception due to invalid codes
|
||||||
|
*
|
||||||
|
* For more on HTTP status codes see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
|
||||||
|
*
|
||||||
* @return mixed associative array of the HTTP codes as keys, and the message
|
* @return mixed associative array of the HTTP codes as keys, and the message
|
||||||
* strings as values, or null of the given $code does not exist.
|
* strings as values, or null of the given $code does not exist.
|
||||||
|
* @throws CakeException If an attempt is made to add an invalid status code
|
||||||
*/
|
*/
|
||||||
public function httpCodes($code = null) {
|
public function httpCodes($code = null) {
|
||||||
if (empty($code)) {
|
if (empty($code)) {
|
||||||
return $this->_statusCodes;
|
return $this->_statusCodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_array($code)) {
|
if (is_array($code)) {
|
||||||
|
$codes = array_keys($code);
|
||||||
|
$min = min($codes);
|
||||||
|
if (!is_int($min) || $min < 100 || max($codes) > 999) {
|
||||||
|
throw new CakeException(__d('cake_dev', 'Invalid status code'));
|
||||||
|
}
|
||||||
$this->_statusCodes = $code + $this->_statusCodes;
|
$this->_statusCodes = $code + $this->_statusCodes;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($this->_statusCodes[$code])) {
|
if (!isset($this->_statusCodes[$code])) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -361,6 +361,7 @@ class CakeResponseTest extends CakeTestCase {
|
||||||
/**
|
/**
|
||||||
* Tests the httpCodes method
|
* Tests the httpCodes method
|
||||||
*
|
*
|
||||||
|
* @expectedException CakeException
|
||||||
*/
|
*/
|
||||||
public function testHttpCodes() {
|
public function testHttpCodes() {
|
||||||
$response = new CakeResponse();
|
$response = new CakeResponse();
|
||||||
|
@ -372,16 +373,16 @@ class CakeResponseTest extends CakeTestCase {
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
$codes = array(
|
$codes = array(
|
||||||
1337 => 'Undefined Unicorn',
|
381 => 'Unicorn Moved',
|
||||||
1729 => 'Hardy-Ramanujan Located'
|
555 => 'Unexpected Minotaur'
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = $response->httpCodes($codes);
|
$result = $response->httpCodes($codes);
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
$this->assertEquals(42, count($response->httpCodes()));
|
$this->assertEquals(42, count($response->httpCodes()));
|
||||||
|
|
||||||
$result = $response->httpCodes(1337);
|
$result = $response->httpCodes(381);
|
||||||
$expected = array(1337 => 'Undefined Unicorn');
|
$expected = array(381 => 'Unicorn Moved');
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
$codes = array(404 => 'Sorry Bro');
|
$codes = array(404 => 'Sorry Bro');
|
||||||
|
@ -392,6 +393,14 @@ class CakeResponseTest extends CakeTestCase {
|
||||||
$result = $response->httpCodes(404);
|
$result = $response->httpCodes(404);
|
||||||
$expected = array(404 => 'Sorry Bro');
|
$expected = array(404 => 'Sorry Bro');
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
|
//Throws exception
|
||||||
|
$response->httpCodes(array(
|
||||||
|
0 => 'Nothing Here',
|
||||||
|
-1 => 'Reverse Infinity',
|
||||||
|
12345 => 'Universal Password',
|
||||||
|
'Hello' => 'World'
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue