CustomHeaderPolicyTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\ControllerPolicy\Tests;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Control\HTTPResponse;
7
use SilverStripe\ControllerPolicy\Policies\CustomHeaderPolicy;
8
use SilverStripe\Dev\SapphireTest;
9
10
class CustomHeaderPolicyTest extends SapphireTest
11
{
12
    /**
13
     * @var HTTPRequest
14
     */
15
    protected $request;
16
17
    /**
18
     * @var HTTPResponse
19
     */
20
    protected $response;
21
22
    /**
23
     * @var CustomHeaderPolicy
24
     */
25
    protected $policy;
26
27
    protected function setUp()
28
    {
29
        parent::setUp();
30
31
        $this->policy = new CustomHeaderPolicy();
32
        $this->request = new HTTPRequest('GET', '/');
33
        $this->response = new HTTPResponse();
34
    }
35
36
    public function testCustomHeadersAreAddedToResponse()
37
    {
38
        $this->policy->setHeaders(['X-Vegetable' => 'Banana']);
39
        $this->policy->addHeader('X-Animal', 'Monkey');
40
        $this->policy->applyToResponse(null, $this->request, $this->response);
41
42
        $this->assertSame('Banana', $this->response->getHeader('X-Vegetable'));
43
        $this->assertSame('Monkey', $this->response->getHeader('X-Animal'));
44
    }
45
46
    public function testEmptyValuesUnsetHeaders()
47
    {
48
        $this->policy->setHeaders([
49
            'X-Vary' => 'Cookie, X-Forwarded-Protocol',
50
            'X-Animal' => 'Monkey',
51
        ]);
52
        $this->policy->applyToResponse(null, $this->request, $this->response);
53
54
        $this->policy->addHeader('X-Animal', '');
55
        $this->policy->applyToResponse(null, $this->request, $this->response);
56
57
        $this->assertSame('Cookie, X-Forwarded-Protocol', $this->response->getHeader('X-Vary'));
58
        $this->assertEmpty($this->response->getHeader('X-Animal'));
59
    }
60
}
61