From 1d5ecd342e417b4707a3bbc34e97949bffd14afb Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Fri, 21 Sep 2018 14:54:26 +1200 Subject: [PATCH] BUG Prevent error on valid response status codes --- src/Control/HTTPResponse.php | 19 +++++++++++ tests/php/Control/HTTPResponseTest.php | 46 +++++++++++++++----------- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/Control/HTTPResponse.php b/src/Control/HTTPResponse.php index 3ec7006c4..1fa010866 100644 --- a/src/Control/HTTPResponse.php +++ b/src/Control/HTTPResponse.php @@ -22,6 +22,8 @@ class HTTPResponse protected static $status_codes = [ 100 => 'Continue', 101 => 'Switching Protocols', + 102 => 'Processing', + 103 => 'Early Hints', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', @@ -29,6 +31,9 @@ class HTTPResponse 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', + 207 => 'Multi-Status', + 208 => 'Already Reported', + 226 => 'IM Used', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', @@ -38,6 +43,7 @@ class HTTPResponse 308 => 'Permanent Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', + 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', @@ -53,14 +59,27 @@ class HTTPResponse 415 => 'Unsupported Media Type', 416 => 'Request Range Not Satisfiable', 417 => 'Expectation Failed', + 418 => 'I\'m a Teapot', + 421 => 'Misdirected Request', 422 => 'Unprocessable Entity', + 423 => 'Locked', + 424 => 'Failed Dependency', + 426 => 'Upgrade Required', + 428 => 'Precondition Required', 429 => 'Too Many Requests', + 431 => 'Request Header Fields Too Large', + 451 => 'Unavailable For Legal Reasons', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', + 506 => 'Variant Also Negotiates', + 507 => 'Unsufficient Storage', + 508 => 'Loop Detected', + 510 => 'Not Extended', + 511 => 'Network Authentication Required', ]; /** diff --git a/tests/php/Control/HTTPResponseTest.php b/tests/php/Control/HTTPResponseTest.php index 3457b6985..18469ec62 100644 --- a/tests/php/Control/HTTPResponseTest.php +++ b/tests/php/Control/HTTPResponseTest.php @@ -8,7 +8,6 @@ use SilverStripe\Control\HTTPResponse_Exception; class HTTPResponseTest extends SapphireTest { - public function testStatusDescriptionStripsNewlines() { $r = new HTTPResponse('my body', 200, "my description \nwith newlines \rand carriage returns"); @@ -23,29 +22,16 @@ class HTTPResponseTest extends SapphireTest $response = new HTTPResponse("Test", 200, 'OK'); // Confirm that the exception's statusCode and statusDescription take precedence - try { - throw new HTTPResponse_Exception($response, 404, 'not even found'); - } catch (HTTPResponse_Exception $e) { - $this->assertEquals(404, $e->getResponse()->getStatusCode()); - $this->assertEquals('not even found', $e->getResponse()->getStatusDescription()); - return; - } - // Fail if we get to here - $this->assertFalse(true, 'Something went wrong with our test exception'); + $e = new HTTPResponse_Exception($response, 404, 'not even found'); + $this->assertEquals(404, $e->getResponse()->getStatusCode()); + $this->assertEquals('not even found', $e->getResponse()->getStatusDescription()); } public function testExceptionContentPlainByDefault() { - // Confirm that the exception's statusCode and statusDescription take precedence - try { - throw new HTTPResponse_Exception("Some content that may be from a hacker", 404, 'not even found'); - } catch (HTTPResponse_Exception $e) { - $this->assertEquals("text/plain", $e->getResponse()->getHeader("Content-Type")); - return; - } - // Fail if we get to here - $this->assertFalse(true, 'Something went wrong with our test exception'); + $e = new HTTPResponse_Exception("Some content that may be from a hacker", 404, 'not even found'); + $this->assertEquals("text/plain", $e->getResponse()->getHeader("Content-Type")); } public function testRemoveHeader() @@ -58,4 +44,26 @@ class HTTPResponseTest extends SapphireTest $response->removeHeader('X-Animal'); $this->assertEmpty($response->getHeader('X-Animal')); } + + public function providerTestValidStatusCodes() + { + return [ + [200, 'OK'], + [226, 'IM Used'], + [426, 'Upgrade Required'], + [451, 'Unavailable For Legal Reasons'], + ]; + } + + /** + * @dataProvider providerTestValidStatusCodes + * @param int $code + * @param string $status + */ + public function testValidStatusCodes($code, $status) + { + $response = new HTTPResponse(); + $response->setStatusCode($code); + $this->assertEquals($status, $response->getStatusDescription()); + } }