1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Yiisoft\Yii\Web\Tests\Middleware; |
5
|
|
|
|
6
|
|
|
use Nyholm\Psr7\ServerRequest; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Yiisoft\Yii\Web\Middleware\BasicNetworkResolver; |
10
|
|
|
use Yiisoft\Yii\Web\Tests\Middleware\Mock\MockRequestHandler; |
11
|
|
|
|
12
|
|
|
class BasicNetworkResolverTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function schemeDataProvider(): array |
15
|
|
|
{ |
16
|
|
|
return [ |
17
|
|
|
'httpNotModify' => ['http', [], null, 'http'], |
18
|
|
|
'httpsNotModify' => ['https', [], null, 'https'], |
19
|
|
|
'httpNotMatchedProtocolHeader' => [ |
20
|
|
|
'http', |
21
|
|
|
['x-forwarded-proto' => ['https']], |
22
|
|
|
['test' => ['https' => 'https']], |
23
|
|
|
'http' |
24
|
|
|
], |
25
|
|
|
'httpNotMatchedProtocolHeaderValue' => [ |
26
|
|
|
'http', |
27
|
|
|
['x-forwarded-proto' => ['https']], |
28
|
|
|
['x-forwarded-proto' => ['https' => 'test']], |
29
|
|
|
'http' |
30
|
|
|
], |
31
|
|
|
'httpToHttps' => [ |
32
|
|
|
'http', |
33
|
|
|
['x-forwarded-proto' => ['https']], |
34
|
|
|
['x-forwarded-proto' => ['https' => 'https']], |
35
|
|
|
'https' |
36
|
|
|
], |
37
|
|
|
'httpToHttpsDefault' => [ |
38
|
|
|
'http', |
39
|
|
|
['x-forwarded-proto' => ['https']], |
40
|
|
|
['x-forwarded-proto' => null], |
41
|
|
|
'https' |
42
|
|
|
], |
43
|
|
|
'httpToHttpsUpperCase' => [ |
44
|
|
|
'http', |
45
|
|
|
['x-forwarded-proto' => ['https']], |
46
|
|
|
['x-forwarded-proto' => ['https' => 'HTTPS']], |
47
|
|
|
'https' |
48
|
|
|
], |
49
|
|
|
'httpToHttpsMultiValue' => [ |
50
|
|
|
'http', |
51
|
|
|
['x-forwarded-proto' => ['https']], |
52
|
|
|
['x-forwarded-proto' => ['https' => ['on', 's', 'https']]], |
53
|
|
|
'https' |
54
|
|
|
], |
55
|
|
|
'httpsToHttp' => [ |
56
|
|
|
'https', |
57
|
|
|
['x-forwarded-proto' => 'http'], |
58
|
|
|
['x-forwarded-proto' => ['http' => 'http']], |
59
|
|
|
'http' |
60
|
|
|
], |
61
|
|
|
'httpToHttpsWithCallback' => [ |
62
|
|
|
'http', |
63
|
|
|
['x-forwarded-proto' => 'test any-https **'], |
64
|
|
|
[ |
65
|
|
|
'x-forwarded-proto' => function (array $values, String $header, ServerRequestInterface $request) { |
66
|
|
|
return stripos($values[0], 'https') !== false ? 'https' : 'http'; |
67
|
|
|
}, |
68
|
|
|
], |
69
|
|
|
'https', |
70
|
|
|
], |
71
|
|
|
'httpWithCallbackNull' => [ |
72
|
|
|
'http', |
73
|
|
|
['x-forwarded-proto' => 'test any-https **'], |
74
|
|
|
[ |
75
|
|
|
'x-forwarded-proto' => function (array $values, String $header, ServerRequestInterface $request) { |
76
|
|
|
return null; |
77
|
|
|
}, |
78
|
|
|
], |
79
|
|
|
'http', |
80
|
|
|
] |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function newRequestWithSchemaAndHeaders( |
85
|
|
|
string $scheme = 'http', |
86
|
|
|
array $headers = [] |
87
|
|
|
): ServerRequestInterface { |
88
|
|
|
$request = new ServerRequest('GET', '/', $headers); |
89
|
|
|
$uri = $request->getUri()->withScheme($scheme); |
90
|
|
|
return $request->withUri($uri); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @dataProvider schemeDataProvider |
95
|
|
|
*/ |
96
|
|
|
public function testScheme(string $scheme, array $headers, ?array $protocolHeaders, string $expectedScheme): void |
97
|
|
|
{ |
98
|
|
|
$request = $this->newRequestWithSchemaAndHeaders($scheme, $headers); |
99
|
|
|
$requestHandler = new MockRequestHandler(); |
100
|
|
|
|
101
|
|
|
$middleware = new BasicNetworkResolver(); |
102
|
|
|
if ($protocolHeaders !== null) { |
103
|
|
|
foreach ($protocolHeaders as $header => $values) { |
104
|
|
|
$middleware = $middleware->withAddedProtocolHeader($header, $values); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
$middleware->process($request, $requestHandler); |
108
|
|
|
$resultRequest = $requestHandler->processedRequest; |
109
|
|
|
/* @var $resultRequest ServerRequestInterface */ |
110
|
|
|
$this->assertSame($expectedScheme, $resultRequest->getUri()->getScheme()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testWithoutProtocolHeaders(): void |
114
|
|
|
{ |
115
|
|
|
$request = $this->newRequestWithSchemaAndHeaders('http', [ |
116
|
|
|
'x-forwarded-proto' => ['https'], |
117
|
|
|
]); |
118
|
|
|
$requestHandler = new MockRequestHandler(); |
119
|
|
|
|
120
|
|
|
$middleware = (new BasicNetworkResolver()) |
121
|
|
|
->withAddedProtocolHeader('x-forwarded-proto') |
122
|
|
|
->withoutProtocolHeaders(); |
123
|
|
|
$middleware->process($request, $requestHandler); |
124
|
|
|
$resultRequest = $requestHandler->processedRequest; |
125
|
|
|
/* @var $resultRequest ServerRequestInterface */ |
126
|
|
|
$this->assertSame('http', $resultRequest->getUri()->getScheme()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testWithoutProtocolHeadersMulti(): void |
130
|
|
|
{ |
131
|
|
|
$request = $this->newRequestWithSchemaAndHeaders('http', [ |
132
|
|
|
'x-forwarded-proto' => ['https'], |
133
|
|
|
'x-forwarded-proto-2' => ['https'], |
134
|
|
|
]); |
135
|
|
|
$requestHandler = new MockRequestHandler(); |
136
|
|
|
|
137
|
|
|
$middleware = (new BasicNetworkResolver()) |
138
|
|
|
->withAddedProtocolHeader('x-forwarded-proto') |
139
|
|
|
->withAddedProtocolHeader('x-forwarded-proto-2') |
140
|
|
|
->withoutProtocolHeaders([ |
141
|
|
|
'x-forwarded-proto', |
142
|
|
|
'x-forwarded-proto-2', |
143
|
|
|
]); |
144
|
|
|
$middleware->process($request, $requestHandler); |
145
|
|
|
$resultRequest = $requestHandler->processedRequest; |
146
|
|
|
/* @var $resultRequest ServerRequestInterface */ |
147
|
|
|
$this->assertSame('http', $resultRequest->getUri()->getScheme()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testWithoutProtocolHeader(): void |
151
|
|
|
{ |
152
|
|
|
$request = $this->newRequestWithSchemaAndHeaders('https', [ |
153
|
|
|
'x-forwarded-proto' => ['https'], |
154
|
|
|
'x-forwarded-proto-2' => ['http'], |
155
|
|
|
]); |
156
|
|
|
$requestHandler = new MockRequestHandler(); |
157
|
|
|
|
158
|
|
|
$middleware = (new BasicNetworkResolver()) |
159
|
|
|
->withAddedProtocolHeader('x-forwarded-proto') |
160
|
|
|
->withAddedProtocolHeader('x-forwarded-proto-2') |
161
|
|
|
->withoutProtocolHeader('x-forwarded-proto'); |
162
|
|
|
$middleware->process($request, $requestHandler); |
163
|
|
|
$resultRequest = $requestHandler->processedRequest; |
164
|
|
|
/* @var $resultRequest ServerRequestInterface */ |
165
|
|
|
$this->assertSame('http', $resultRequest->getUri()->getScheme()); |
166
|
|
|
|
167
|
|
|
$middleware = $middleware->withoutProtocolHeader('x-forwarded-proto-2'); |
168
|
|
|
$middleware->process($request, $requestHandler); |
169
|
|
|
$resultRequest = $requestHandler->processedRequest; |
170
|
|
|
/* @var $resultRequest ServerRequestInterface */ |
171
|
|
|
$this->assertSame('https', $resultRequest->getUri()->getScheme()); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|