InitialisationMiddlewareTest::runMiddleware()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace CWP\Core\Tests\Control;
4
5
use CWP\Core\Control\InitialisationMiddleware;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Environment;
9
use SilverStripe\Dev\FunctionalTest;
10
11
class InitialisationMiddlewareTest extends FunctionalTest
12
{
13
    /**
14
     * @var HTTPRequest
15
     */
16
    protected $request;
17
18
    /**
19
     * @var InitialisationMiddleware
20
     */
21
    protected $middleware;
22
23
    protected $usesDatabase = true;
24
25
    protected function setUp()
26
    {
27
        parent::setUp();
28
29
        $this->request = new HTTPRequest('GET', '/');
30
        $this->middleware = new InitialisationMiddleware();
31
32
        Environment::setEnv('SS_OUTBOUND_PROXY', '');
33
        Environment::setEnv('SS_OUTBOUND_PROXY_PORT', '');
34
        putenv('NO_PROXY=');
35
    }
36
37
    public function testDoNotConfigureProxyIfNoEnvironmentVarsAreSet()
38
    {
39
        $this->runMiddleware();
40
41
        $this->assertEmpty(
42
            Environment::getEnv('http_proxy'),
43
            'Proxy information is not set if no outbound proxy is configured'
44
        );
45
    }
46
47
    public function testConfigureEgressProxyWhenVarsAreSet()
48
    {
49
        Environment::setEnv('SS_OUTBOUND_PROXY', 'http://example.com');
50
        Environment::setEnv('SS_OUTBOUND_PROXY_PORT', '8024');
51
52
        $this->runMiddleware();
53
54
        $this->assertEquals(
55
            'http://example.com:8024',
56
            Environment::getEnv('http_proxy'),
57
            'Proxy is configured with proxy and port'
58
        );
59
    }
60
61
    public function testDoNotConfigureProxyDomainExclusionsWhenNoneAreDefined()
62
    {
63
        Config::modify()->remove(InitialisationMiddleware::class, 'egress_proxy_exclude_domains');
64
65
        $this->runMiddleware();
66
67
        $this->assertSame(
68
            '',
69
            Environment::getEnv('NO_PROXY'),
70
            'No domain exclusions are set when none are defined'
71
        );
72
    }
73
74
    public function testConfigureEgressProxyDomainExclusions()
75
    {
76
        Config::modify()->set(
77
            InitialisationMiddleware::class,
78
            'egress_proxy_exclude_domains',
79
            'example.com'
80
        );
81
82
        putenv('NO_PROXY=foo.com,bar.com');
83
        $this->runMiddleware();
84
85
        $this->assertSame(
86
            'foo.com,bar.com,example.com',
87
            Environment::getEnv('NO_PROXY'),
88
            'Domain exclusions are combined with existing values and configuration settings'
89
        );
90
    }
91
92
    public function testSecurityHeadersAddedByDefault()
93
    {
94
        $response = $this->get('Security/login');
95
        $this->assertArrayHasKey('x-xss-protection', $response->getHeaders());
96
        $this->assertSame('1; mode=block', $response->getHeader('x-xss-protection'));
97
    }
98
99
    public function testXSSProtectionHeaderNotAdded()
100
    {
101
        Config::modify()->set(InitialisationMiddleware::class, 'xss_protection_enabled', false);
102
        $response = $this->get('Security/login');
103
        $this->assertArrayNotHasKey('x-xss-protection', $response->getHeaders());
104
    }
105
106
    public function testHstsNotAddedByDefault()
107
    {
108
        Config::modify()->remove(InitialisationMiddleware::class, 'strict_transport_security');
109
        $response = $this->get('Security/login');
110
        $this->assertArrayNotHasKey('strict-transport-security', $response->getHeaders());
111
    }
112
113
    public function testHstsAddedWhenConfigured()
114
    {
115
        Config::modify()->update(InitialisationMiddleware::class, 'strict_transport_security', 'max-age=1');
116
        $response = $this->get('Security/login');
117
        $this->assertArrayHasKey('strict-transport-security', $response->getHeaders());
118
    }
119
120
    /**
121
     * Runs the middleware with a stubbed delegate
122
     */
123
    protected function runMiddleware()
124
    {
125
        $this->middleware->process($this->request, function () {
126
            // no op
127
        });
128
    }
129
}
130