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
|
|
|
|
12
|
|
|
public function testStatusDescriptionStripsNewlines() |
13
|
|
|
{ |
14
|
|
|
$r = new HTTPResponse('my body', 200, "my description \nwith newlines \rand carriage returns"); |
15
|
|
|
$this->assertEquals( |
16
|
|
|
"my description with newlines and carriage returns", |
17
|
|
|
$r->getStatusDescription() |
18
|
|
|
); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testHTTPResponseException() |
22
|
|
|
{ |
23
|
|
|
$response = new HTTPResponse("Test", 200, 'OK'); |
24
|
|
|
|
25
|
|
|
// Confirm that the exception's statusCode and statusDescription take precedence |
26
|
|
|
try { |
27
|
|
|
throw new HTTPResponse_Exception($response, 404, 'not even found'); |
28
|
|
|
} catch (HTTPResponse_Exception $e) { |
29
|
|
|
$this->assertEquals(404, $e->getResponse()->getStatusCode()); |
30
|
|
|
$this->assertEquals('not even found', $e->getResponse()->getStatusDescription()); |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
// Fail if we get to here |
34
|
|
|
$this->assertFalse(true, 'Something went wrong with our test exception'); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testExceptionContentPlainByDefault() |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
// Confirm that the exception's statusCode and statusDescription take precedence |
41
|
|
|
try { |
42
|
|
|
throw new HTTPResponse_Exception("Some content that may be from a hacker", 404, 'not even found'); |
43
|
|
|
} catch (HTTPResponse_Exception $e) { |
44
|
|
|
$this->assertEquals("text/plain", $e->getResponse()->getHeader("Content-Type")); |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
// Fail if we get to here |
48
|
|
|
$this->assertFalse(true, 'Something went wrong with our test exception'); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testRemoveHeader() |
52
|
|
|
{ |
53
|
|
|
$response = new HTTPResponse(); |
54
|
|
|
|
55
|
|
|
$response->addHeader('X-Animal', 'Monkey'); |
56
|
|
|
$this->assertSame('Monkey', $response->getHeader('X-Animal')); |
57
|
|
|
|
58
|
|
|
$response->removeHeader('X-Animal'); |
59
|
|
|
$this->assertEmpty($response->getHeader('X-Animal')); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.