Merge pull request #8394 from littlegiant/pulls/4.2/http-status-codes

BUG Prevent error on valid response status codes
This commit is contained in:
Robbie Averill 2018-09-21 13:19:29 +02:00 committed by GitHub
commit d11911d4dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 19 deletions

View File

@ -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',
];
/**

View File

@ -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());
}
}