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;
|
2024-09-18 03:53:44 +02:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class HTTPResponseTest extends SapphireTest
|
|
|
|
{
|
|
|
|
public function testStatusDescriptionStripsNewlines()
|
|
|
|
{
|
|
|
|
$r = new HTTPResponse('my body', 200, "my description \nwith newlines \rand carriage returns");
|
|
|
|
$this->assertEquals(
|
|
|
|
"my description with newlines and carriage returns",
|
|
|
|
$r->getStatusDescription()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHTTPResponseException()
|
|
|
|
{
|
|
|
|
$response = new HTTPResponse("Test", 200, 'OK');
|
|
|
|
|
|
|
|
// Confirm that the exception's statusCode and statusDescription take precedence
|
2018-09-21 04:54:26 +02:00
|
|
|
$e = new HTTPResponse_Exception($response, 404, 'not even found');
|
|
|
|
$this->assertEquals(404, $e->getResponse()->getStatusCode());
|
|
|
|
$this->assertEquals('not even found', $e->getResponse()->getStatusDescription());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testExceptionContentPlainByDefault()
|
|
|
|
{
|
|
|
|
// Confirm that the exception's statusCode and statusDescription take precedence
|
2018-09-21 04:54:26 +02:00
|
|
|
$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"));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2018-01-16 11:20:52 +01:00
|
|
|
|
|
|
|
public function testRemoveHeader()
|
|
|
|
{
|
|
|
|
$response = new HTTPResponse();
|
|
|
|
|
|
|
|
$response->addHeader('X-Animal', 'Monkey');
|
|
|
|
$this->assertSame('Monkey', $response->getHeader('X-Animal'));
|
|
|
|
|
|
|
|
$response->removeHeader('X-Animal');
|
|
|
|
$this->assertEmpty($response->getHeader('X-Animal'));
|
|
|
|
}
|
2018-09-21 04:54:26 +02:00
|
|
|
|
2024-09-18 03:53:44 +02:00
|
|
|
public static function providerSanitiseHeaders()
|
2022-09-06 05:06:35 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'plain text is retained' => ['some arbitrary value1', 'some arbitrary value1'],
|
|
|
|
'special chars are retained' => ['`~!@#$%^&*()_+-=,./<>?;\':"[]{}\\|', '`~!@#$%^&*()_+-=,./<>?;\':"[]{}\\|'],
|
|
|
|
'line breaks are removed' => ['no line breaks', "n\ro line \nbreaks\r\n"],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-09-18 03:53:44 +02:00
|
|
|
#[DataProvider('providerSanitiseHeaders')]
|
2022-09-06 05:06:35 +02:00
|
|
|
public function testSanitiseHeaders(string $expected, string $value)
|
|
|
|
{
|
|
|
|
$response = new HTTPResponse();
|
|
|
|
|
|
|
|
$response->addHeader('X-Sanitised', $value);
|
|
|
|
$this->assertSame($expected, $response->getHeader('X-Sanitised'));
|
|
|
|
}
|
|
|
|
|
2024-09-18 03:53:44 +02:00
|
|
|
public static function providerTestValidStatusCodes()
|
2018-09-21 04:54:26 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
[200, 'OK'],
|
|
|
|
[226, 'IM Used'],
|
|
|
|
[426, 'Upgrade Required'],
|
|
|
|
[451, 'Unavailable For Legal Reasons'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $code
|
|
|
|
* @param string $status
|
|
|
|
*/
|
2024-09-18 03:53:44 +02:00
|
|
|
#[DataProvider('providerTestValidStatusCodes')]
|
2018-09-21 04:54:26 +02:00
|
|
|
public function testValidStatusCodes($code, $status)
|
|
|
|
{
|
|
|
|
$response = new HTTPResponse();
|
|
|
|
$response->setStatusCode($code);
|
|
|
|
$this->assertEquals($status, $response->getStatusDescription());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|