1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
use SilverStripe\Control\HTTPResponse; |
7
|
|
|
use SilverStripe\Control\HTTPResponse_Exception; |
8
|
|
|
|
9
|
|
|
class HTTPResponseTest extends SapphireTest |
10
|
|
|
{ |
11
|
|
|
public function testStatusDescriptionStripsNewlines() |
12
|
|
|
{ |
13
|
|
|
$r = new HTTPResponse('my body', 200, "my description \nwith newlines \rand carriage returns"); |
14
|
|
|
$this->assertEquals( |
15
|
|
|
"my description with newlines and carriage returns", |
16
|
|
|
$r->getStatusDescription() |
17
|
|
|
); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testHTTPResponseException() |
21
|
|
|
{ |
22
|
|
|
$response = new HTTPResponse("Test", 200, 'OK'); |
23
|
|
|
|
24
|
|
|
// Confirm that the exception's statusCode and statusDescription take precedence |
25
|
|
|
$e = new HTTPResponse_Exception($response, 404, 'not even found'); |
26
|
|
|
$this->assertEquals(404, $e->getResponse()->getStatusCode()); |
27
|
|
|
$this->assertEquals('not even found', $e->getResponse()->getStatusDescription()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testExceptionContentPlainByDefault() |
31
|
|
|
{ |
32
|
|
|
// Confirm that the exception's statusCode and statusDescription take precedence |
33
|
|
|
$e = new HTTPResponse_Exception("Some content that may be from a hacker", 404, 'not even found'); |
34
|
|
|
$this->assertEquals("text/plain", $e->getResponse()->getHeader("Content-Type")); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testRemoveHeader() |
38
|
|
|
{ |
39
|
|
|
$response = new HTTPResponse(); |
40
|
|
|
|
41
|
|
|
$response->addHeader('X-Animal', 'Monkey'); |
42
|
|
|
$this->assertSame('Monkey', $response->getHeader('X-Animal')); |
43
|
|
|
|
44
|
|
|
$response->removeHeader('X-Animal'); |
45
|
|
|
$this->assertEmpty($response->getHeader('X-Animal')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function providerTestValidStatusCodes() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
[200, 'OK'], |
52
|
|
|
[226, 'IM Used'], |
53
|
|
|
[426, 'Upgrade Required'], |
54
|
|
|
[451, 'Unavailable For Legal Reasons'], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @dataProvider providerTestValidStatusCodes |
60
|
|
|
* @param int $code |
61
|
|
|
* @param string $status |
62
|
|
|
*/ |
63
|
|
|
public function testValidStatusCodes($code, $status) |
64
|
|
|
{ |
65
|
|
|
$response = new HTTPResponse(); |
66
|
|
|
$response->setStatusCode($code); |
67
|
|
|
$this->assertEquals($status, $response->getStatusDescription()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|