|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
6
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
7
|
|
|
use SilverStripe\Control\Middleware\CanonicalURLMiddleware; |
|
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
9
|
|
|
|
|
10
|
|
|
class CanonicalURLMiddlewareTest extends SapphireTest |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Stub middleware class, partially mocked |
|
14
|
|
|
* |
|
15
|
|
|
* @var CanonicalURLMiddleware |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $middleware; |
|
18
|
|
|
|
|
19
|
|
|
protected function setUp() |
|
20
|
|
|
{ |
|
21
|
|
|
parent::setUp(); |
|
22
|
|
|
|
|
23
|
|
|
/** @var CanonicalURLMiddleware $middleware */ |
|
24
|
|
|
$this->middleware = $this->getMockBuilder(CanonicalURLMiddleware::class) |
|
|
|
|
|
|
25
|
|
|
->setMethods(['getRedirect']) |
|
26
|
|
|
->getMock(); |
|
27
|
|
|
|
|
28
|
|
|
$this->middleware->expects($this->any())->method('getRedirect')->willReturn(false); |
|
29
|
|
|
|
|
30
|
|
|
$this->middleware->setForceBasicAuthToSSL(true); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testHttpsIsForcedForBasicAuth() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->middleware->expects($this->once())->method('getRedirect'); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$request = new HTTPRequest('GET', '/'); |
|
38
|
|
|
$mockResponse = (new HTTPResponse) |
|
39
|
|
|
->addHeader('WWW-Authenticate', 'basic') |
|
40
|
|
|
->setStatusCode(401); |
|
41
|
|
|
|
|
42
|
|
|
$result = $this->middleware->process($request, function () use ($mockResponse) { |
|
43
|
|
|
return $mockResponse; |
|
44
|
|
|
}); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertNotSame($mockResponse, $result, 'New response is created and returned'); |
|
47
|
|
|
$this->assertEquals(301, $result->getStatusCode(), 'Basic auth responses are redirected'); |
|
48
|
|
|
$this->assertContains('https://', $result->getHeader('Location'), 'HTTPS is in the redirect location'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testMiddlewareDelegateIsReturnedWhenBasicAuthRedirectIsDisabled() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->middleware->expects($this->once())->method('getRedirect'); |
|
54
|
|
|
$this->middleware->setForceBasicAuthToSSL(false); |
|
55
|
|
|
|
|
56
|
|
|
$request = new HTTPRequest('GET', '/'); |
|
57
|
|
|
$mockResponse = (new HTTPResponse) |
|
58
|
|
|
->addHeader('WWW-Authenticate', 'basic') |
|
59
|
|
|
->setStatusCode(401); |
|
60
|
|
|
|
|
61
|
|
|
$result = $this->middleware->process($request, function () use ($mockResponse) { |
|
62
|
|
|
return $mockResponse; |
|
63
|
|
|
}); |
|
64
|
|
|
$this->assertSame($mockResponse, $result, 'Response returned verbatim with auto redirect disabled'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testMiddlewareDelegateIsReturnedWhenNoBasicAuthIsPresent() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->middleware->expects($this->once())->method('getRedirect'); |
|
70
|
|
|
|
|
71
|
|
|
$request = new HTTPRequest('GET', '/'); |
|
72
|
|
|
$mockResponse = (new HTTPResponse)->addHeader('Foo', 'bar'); |
|
73
|
|
|
|
|
74
|
|
|
$result = $this->middleware->process($request, function () use ($mockResponse) { |
|
75
|
|
|
return $mockResponse; |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertSame($mockResponse, $result, 'Non basic-auth responses are returned verbatim'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testGetForceBasicAuthToSSL() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->middleware->setForceBasicAuthToSSL(null); |
|
84
|
|
|
|
|
85
|
|
|
$this->middleware->setForceSSL(true); |
|
86
|
|
|
$this->assertTrue($this->middleware->getForceBasicAuthToSSL(), 'Default falls over to forceSSL'); |
|
87
|
|
|
|
|
88
|
|
|
$this->middleware->setForceSSL(false); |
|
89
|
|
|
$this->assertFalse($this->middleware->getForceBasicAuthToSSL(), 'Default falls over to forceSSL'); |
|
90
|
|
|
|
|
91
|
|
|
$this->middleware->setForceBasicAuthToSSL(true); |
|
92
|
|
|
$this->assertTrue($this->middleware->getForceBasicAuthToSSL(), 'Explicitly set is returned'); |
|
93
|
|
|
|
|
94
|
|
|
$this->middleware->setForceBasicAuthToSSL(false); |
|
95
|
|
|
$this->middleware->setForceSSL(true); |
|
96
|
|
|
$this->assertFalse($this->middleware->getForceBasicAuthToSSL(), 'Explicitly set is returned'); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..