|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Yiisoft\Yii\Web\Tests\NetworkResolver; |
|
5
|
|
|
|
|
6
|
|
|
use Nyholm\Psr7\ServerRequest; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use Yiisoft\Yii\Web\NetworkResolver\BasicNetworkResolver; |
|
10
|
|
|
|
|
11
|
|
|
class BasicNetworkResolverTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public function simpleDataProvider() |
|
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
|
|
|
'httpToHttpsUpperCase' => [ |
|
38
|
|
|
'http', |
|
39
|
|
|
['x-forwarded-proto' => ['https']], |
|
40
|
|
|
['x-forwarded-proto' => ['https' => 'HTTPS']], |
|
41
|
|
|
'https' |
|
42
|
|
|
], |
|
43
|
|
|
'httpToHttpsMultiValue' => [ |
|
44
|
|
|
'http', |
|
45
|
|
|
['x-forwarded-proto' => ['https']], |
|
46
|
|
|
['x-forwarded-proto' => ['https' => ['on', 's', 'https']]], |
|
47
|
|
|
'https' |
|
48
|
|
|
], |
|
49
|
|
|
'httpsToHttp' => [ |
|
50
|
|
|
'https', |
|
51
|
|
|
['x-forwarded-proto' => 'http'], |
|
52
|
|
|
['x-forwarded-proto' => ['http' => 'http']], |
|
53
|
|
|
'http' |
|
54
|
|
|
], |
|
55
|
|
|
'httpToHttpsWithCallback' => [ |
|
56
|
|
|
'http', |
|
57
|
|
|
['x-forwarded-proto' => 'test any-https **'], |
|
58
|
|
|
[ |
|
59
|
|
|
'x-forwarded-proto' => function (array $values, String $header, ServerRequestInterface $request) { |
|
|
|
|
|
|
60
|
|
|
return stripos($values[0], 'https') !== false ? 'https' : 'http'; |
|
61
|
|
|
} |
|
62
|
|
|
], |
|
63
|
|
|
'https', |
|
64
|
|
|
] |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @dataProvider simpleDataProvider |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testScheme(string $scheme, array $headers, ?array $protocolHeaders, string $expectedScheme) |
|
72
|
|
|
{ |
|
73
|
|
|
$request = new ServerRequest('GET', '/', $headers); |
|
74
|
|
|
$uri = $request->getUri()->withScheme($scheme); |
|
75
|
|
|
$request = $request->withUri($uri); |
|
76
|
|
|
|
|
77
|
|
|
$nr = (new BasicNetworkResolver())->withServerRequest($request); |
|
78
|
|
|
if ($protocolHeaders !== null) { |
|
|
|
|
|
|
79
|
|
|
foreach ($protocolHeaders as $header => $values) { |
|
80
|
|
|
$nr = $nr->withNewProtocolHeader($header, $values); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
$this->assertSame($expectedScheme, $nr->getServerRequest()->getUri()->getScheme()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function ipsDataProvider() |
|
87
|
|
|
{ |
|
88
|
|
|
return [ |
|
89
|
|
|
'ipv4' => ['9.9.9.9', '9.9.9.9'], |
|
90
|
|
|
'ipv6' => ['684D:1111:222:3333:4444:5555:6:77', '684D:1111:222:3333:4444:5555:6:77'], |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @dataProvider ipsDataProvider |
|
96
|
|
|
*/ |
|
97
|
|
|
public function testIps(string $remoteAddress, string $expectedIp) |
|
98
|
|
|
{ |
|
99
|
|
|
$request = new ServerRequest('GET', '/', [], null, '1.1', [ |
|
100
|
|
|
'REMOTE_ADDR' => $remoteAddress, |
|
101
|
|
|
]); |
|
102
|
|
|
$nr = (new BasicNetworkResolver())->withServerRequest($request); |
|
103
|
|
|
$this->assertSame($expectedIp, $nr->getUserIp()); |
|
104
|
|
|
$this->assertSame($expectedIp, $nr->getRemoteIp()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.