Passed
Push — 4.0 ( a8c156...daac57 )
by Loz
08:13
created

HTTPResponseTest::testRemoveHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 9.6666
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');
0 ignored issues
show
Unused Code introduced by
$this->assertFalse(true,...th our test exception') is not reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
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');
0 ignored issues
show
Unused Code introduced by
$this->assertFalse(true,...th our test exception') is not reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
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