silverstripe-framework/tests/control/HTTPResponseTest.php
Sam Minnee 39952f4a5c API: Added 'onBeforeHTTPError' and 'onBeforeHTTPError<code>' extension points to RequestHandler::httpError().
These APIs are primarily intended to let developers write custom 404 handlers.  They can define an onBeforeHTTPError404() method on an Extension that gets added to Controller or RequestHandler.

The SS_HTTPResponse_Exception object has also been tidied up to override the status info of any SS_HTTPResponse object that might get passed.  This is mainly to make it easier for callers (such as ContentController and ModelAsController) to use RequestHandler::httpError() more consistently.
2012-09-27 12:26:25 +12:00

65 lines
1.9 KiB
PHP

<?php
/**
* @package framework
* @subpackage tests
*/
class HTTPResponseTest extends SapphireTest {
public function testStatusDescriptionStripsNewlines() {
$r = new SS_HTTPResponse('my body', 200, "my description \nwith newlines \rand carriage returns");
$this->assertEquals(
"my description with newlines and carriage returns",
$r->getStatusDescription()
);
}
public function testContentLengthHeader() {
$r = new SS_HTTPResponse('123ü');
$this->assertNotNull($r->getHeader('Content-Length'), 'Content-length header is added');
$this->assertEquals(
5,
$r->getHeader('Content-Length'),
'Header matches actual content length in bytes'
);
$r->setBody('1234ü');
$this->assertEquals(
6,
$r->getHeader('Content-Length'),
'Header is updated when body is changed'
);
}
public function testHTTPResponseException() {
$response = new SS_HTTPResponse("Test", 200, 'OK');
// Confirm that the exception's statusCode and statusDescription take precedence
try {
throw new SS_HTTPResponse_Exception($response, 404, 'not even found');
} catch(SS_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');
}
public function testExceptionContentPlainByDefault() {
// Confirm that the exception's statusCode and statusDescription take precedence
try {
throw new SS_HTTPResponse_Exception("Some content that may be from a hacker", 404, 'not even found');
} catch(SS_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');
}
}