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 = [ protected static $status_codes = [
100 => 'Continue', 100 => 'Continue',
101 => 'Switching Protocols', 101 => 'Switching Protocols',
102 => 'Processing',
103 => 'Early Hints',
200 => 'OK', 200 => 'OK',
201 => 'Created', 201 => 'Created',
202 => 'Accepted', 202 => 'Accepted',
@ -29,6 +31,9 @@ class HTTPResponse
204 => 'No Content', 204 => 'No Content',
205 => 'Reset Content', 205 => 'Reset Content',
206 => 'Partial Content', 206 => 'Partial Content',
207 => 'Multi-Status',
208 => 'Already Reported',
226 => 'IM Used',
301 => 'Moved Permanently', 301 => 'Moved Permanently',
302 => 'Found', 302 => 'Found',
303 => 'See Other', 303 => 'See Other',
@ -38,6 +43,7 @@ class HTTPResponse
308 => 'Permanent Redirect', 308 => 'Permanent Redirect',
400 => 'Bad Request', 400 => 'Bad Request',
401 => 'Unauthorized', 401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden', 403 => 'Forbidden',
404 => 'Not Found', 404 => 'Not Found',
405 => 'Method Not Allowed', 405 => 'Method Not Allowed',
@ -53,14 +59,27 @@ class HTTPResponse
415 => 'Unsupported Media Type', 415 => 'Unsupported Media Type',
416 => 'Request Range Not Satisfiable', 416 => 'Request Range Not Satisfiable',
417 => 'Expectation Failed', 417 => 'Expectation Failed',
418 => 'I\'m a Teapot',
421 => 'Misdirected Request',
422 => 'Unprocessable Entity', 422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests', 429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
451 => 'Unavailable For Legal Reasons',
500 => 'Internal Server Error', 500 => 'Internal Server Error',
501 => 'Not Implemented', 501 => 'Not Implemented',
502 => 'Bad Gateway', 502 => 'Bad Gateway',
503 => 'Service Unavailable', 503 => 'Service Unavailable',
504 => 'Gateway Timeout', 504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported', 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 class HTTPResponseTest extends SapphireTest
{ {
public function testStatusDescriptionStripsNewlines() public function testStatusDescriptionStripsNewlines()
{ {
$r = new HTTPResponse('my body', 200, "my description \nwith newlines \rand carriage returns"); $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'); $response = new HTTPResponse("Test", 200, 'OK');
// Confirm that the exception's statusCode and statusDescription take precedence // Confirm that the exception's statusCode and statusDescription take precedence
try { $e = new HTTPResponse_Exception($response, 404, 'not even found');
throw new HTTPResponse_Exception($response, 404, 'not even found'); $this->assertEquals(404, $e->getResponse()->getStatusCode());
} catch (HTTPResponse_Exception $e) { $this->assertEquals('not even found', $e->getResponse()->getStatusDescription());
$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');
} }
public function testExceptionContentPlainByDefault() public function testExceptionContentPlainByDefault()
{ {
// Confirm that the exception's statusCode and statusDescription take precedence // Confirm that the exception's statusCode and statusDescription take precedence
try { $e = new HTTPResponse_Exception("Some content that may be from a hacker", 404, 'not even found');
throw new HTTPResponse_Exception("Some content that may be from a hacker", 404, 'not even found'); $this->assertEquals("text/plain", $e->getResponse()->getHeader("Content-Type"));
} 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');
} }
public function testRemoveHeader() public function testRemoveHeader()
@ -58,4 +44,26 @@ class HTTPResponseTest extends SapphireTest
$response->removeHeader('X-Animal'); $response->removeHeader('X-Animal');
$this->assertEmpty($response->getHeader('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());
}
} }