2010-11-23 20:38:15 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Control\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2010-11-23 20:38:15 +01:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-11-23 20:38:15 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class HTTPResponseTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testStatusDescriptionStripsNewlines() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$r = new HTTPResponse('my body', 200, "my description \nwith newlines \rand carriage returns");
|
2010-11-23 20:38:15 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
"my description with newlines and carriage returns",
|
|
|
|
$r->getStatusDescription()
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-27 02:26:25 +02:00
|
|
|
public function testHTTPResponseException() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$response = new HTTPResponse("Test", 200, 'OK');
|
2012-09-27 02:26:25 +02:00
|
|
|
|
|
|
|
// Confirm that the exception's statusCode and statusDescription take precedence
|
|
|
|
try {
|
2016-09-09 08:43:05 +02:00
|
|
|
throw new HTTPResponse_Exception($response, 404, 'not even found');
|
2012-09-27 02:26:25 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
} catch(HTTPResponse_Exception $e) {
|
2012-09-27 02:26:25 +02:00
|
|
|
$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 {
|
2016-09-09 08:43:05 +02:00
|
|
|
throw new HTTPResponse_Exception("Some content that may be from a hacker", 404, 'not even found');
|
2012-09-27 02:26:25 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
} catch(HTTPResponse_Exception $e) {
|
2012-09-27 02:26:25 +02:00
|
|
|
$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');
|
|
|
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|