Completed
Push — master ( 3655f7...92824b )
by
unknown
03:30 queued 12s
created

InitialisationMiddlewareTest::runMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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\SapphireTest;
10
11
class InitialisationMiddlewareTest extends SapphireTest
12
{
13
    /**
14
     * @var HTTPRequest
15
     */
16
    protected $request;
17
18
    /**
19
     * @var InitialisationMiddleware
20
     */
21
    protected $middleware;
22
23
    protected function setUp()
24
    {
25
        parent::setUp();
26
27
        $this->request = new HTTPRequest('GET', '/');
28
        $this->middleware = new InitialisationMiddleware();
29
30
        Environment::setEnv('SS_OUTBOUND_PROXY', '');
31
        Environment::setEnv('SS_OUTBOUND_PROXY_PORT', '');
32
        Environment::setEnv('NO_PROXY', '');
33
    }
34
35
    public function testDoNotConfigureProxyIfNoEnvironmentVarsAreSet()
36
    {
37
        $this->runMiddleware();
38
39
        $this->assertEmpty(
40
            Environment::getEnv('http_proxy'),
41
            'Proxy information is not set if no outbound proxy is configured'
42
        );
43
    }
44
45
    public function testConfigureEgressProxyWhenVarsAreSet()
46
    {
47
        Environment::setEnv('SS_OUTBOUND_PROXY', 'http://example.com');
48
        Environment::setEnv('SS_OUTBOUND_PROXY_PORT', '8024');
49
50
        $this->runMiddleware();
51
52
        $this->assertEquals(
53
            'http://example.com:8024',
54
            Environment::getEnv('http_proxy'),
55
            'Proxy is configured with proxy and port'
56
        );
57
    }
58
59
    public function testDoNotConfigureProxyDomainExclusionsWhenNoneAreDefined()
60
    {
61
        Config::modify()->remove(InitialisationMiddleware::class, 'egress_proxy_exclude_domains');
62
63
        $this->runMiddleware();
64
65
        $this->assertSame(
66
            '',
67
            Environment::getEnv('NO_PROXY'),
68
            'No domain exclusions are set when none are defined'
69
        );
70
    }
71
72
    public function testConfigureEgressProxyDomainExclusions()
73
    {
74
        Config::modify()->set(
75
            InitialisationMiddleware::class,
76
            'egress_proxy_exclude_domains',
77
            'example.com'
78
        );
79
80
        Environment::setEnv('NO_PROXY', 'foo.com,bar.com');
81
82
        $this->runMiddleware();
83
84
        $this->assertSame(
85
            'foo.com,bar.com,example.com',
86
            Environment::getEnv('NO_PROXY'),
87
            'Domain exclusions are combined with existing values and configuration settings'
88
        );
89
    }
90
91
    /**
92
     * Runs the middleware with a stubbed delegate
93
     */
94
    protected function runMiddleware()
95
    {
96
        $this->middleware->process($this->request, function () {
97
            // no op
98
        });
99
    }
100
}
101