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() : void |
||
20 | { |
||
21 | parent::setUp(); |
||
22 | |||
23 | /** @var CanonicalURLMiddleware $middleware */ |
||
24 | $this->middleware = $this->getMockBuilder(CanonicalURLMiddleware::class) |
||
0 ignored issues
–
show
|
|||
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->assertStringContainsString( |
||
49 | 'https://', |
||
50 | $result->getHeader('Location'), |
||
51 | 'HTTPS is in the redirect location' |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | public function testMiddlewareDelegateIsReturnedWhenBasicAuthRedirectIsDisabled() |
||
56 | { |
||
57 | $this->middleware->expects($this->once())->method('getRedirect'); |
||
58 | $this->middleware->setForceBasicAuthToSSL(false); |
||
59 | |||
60 | $request = new HTTPRequest('GET', '/'); |
||
61 | $mockResponse = (new HTTPResponse) |
||
62 | ->addHeader('WWW-Authenticate', 'basic') |
||
63 | ->setStatusCode(401); |
||
64 | |||
65 | $result = $this->middleware->process($request, function () use ($mockResponse) { |
||
66 | return $mockResponse; |
||
67 | }); |
||
68 | $this->assertSame($mockResponse, $result, 'Response returned verbatim with auto redirect disabled'); |
||
69 | } |
||
70 | |||
71 | public function testMiddlewareDelegateIsReturnedWhenNoBasicAuthIsPresent() |
||
72 | { |
||
73 | $this->middleware->expects($this->once())->method('getRedirect'); |
||
74 | |||
75 | $request = new HTTPRequest('GET', '/'); |
||
76 | $mockResponse = (new HTTPResponse)->addHeader('Foo', 'bar'); |
||
77 | |||
78 | $result = $this->middleware->process($request, function () use ($mockResponse) { |
||
79 | return $mockResponse; |
||
80 | }); |
||
81 | |||
82 | $this->assertSame($mockResponse, $result, 'Non basic-auth responses are returned verbatim'); |
||
83 | } |
||
84 | |||
85 | public function testGetForceBasicAuthToSSL() |
||
86 | { |
||
87 | $this->middleware->setForceBasicAuthToSSL(null); |
||
88 | |||
89 | $this->middleware->setForceSSL(true); |
||
90 | $this->assertTrue($this->middleware->getForceBasicAuthToSSL(), 'Default falls over to forceSSL'); |
||
91 | |||
92 | $this->middleware->setForceSSL(false); |
||
93 | $this->assertFalse($this->middleware->getForceBasicAuthToSSL(), 'Default falls over to forceSSL'); |
||
94 | |||
95 | $this->middleware->setForceBasicAuthToSSL(true); |
||
96 | $this->assertTrue($this->middleware->getForceBasicAuthToSSL(), 'Explicitly set is returned'); |
||
97 | |||
98 | $this->middleware->setForceBasicAuthToSSL(false); |
||
99 | $this->middleware->setForceSSL(true); |
||
100 | $this->assertFalse($this->middleware->getForceBasicAuthToSSL(), 'Explicitly set is returned'); |
||
101 | } |
||
102 | } |
||
103 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.