|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Yiisoft\Yii\Web\Tests\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use Nyholm\Psr7\Response; |
|
7
|
|
|
use Nyholm\Psr7\ServerRequest; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
12
|
|
|
use Yiisoft\Yii\Web\Middleware\NetworkResolver; |
|
13
|
|
|
use Yiisoft\Yii\Web\NetworkResolver\BasicNetworkResolver; |
|
14
|
|
|
|
|
15
|
|
|
class NetworkResolverTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
public function simpleDataProvider() |
|
19
|
|
|
{ |
|
20
|
|
|
return [ |
|
21
|
|
|
'httpNotModify' => ['http', [], null, 'http'], |
|
22
|
|
|
'httpsNotModify' => ['https', [], null, 'https'], |
|
23
|
|
|
'httpNotMatchedProtocolHeader' => [ |
|
24
|
|
|
'http', |
|
25
|
|
|
['x-forwarded-proto' => ['https']], |
|
26
|
|
|
['test' => ['https']], |
|
27
|
|
|
'http' |
|
28
|
|
|
], |
|
29
|
|
|
'httpNotMatchedProtocolHeaderValue' => [ |
|
30
|
|
|
'http', |
|
31
|
|
|
['x-forwarded-proto' => ['https']], |
|
32
|
|
|
['x-forwarded-proto' => ['test']], |
|
33
|
|
|
'http' |
|
34
|
|
|
], |
|
35
|
|
|
'httpToHttps' => ['http', ['x-forwarded-proto' => ['https']], ['x-forwarded-proto' => ['https']], 'https'], |
|
36
|
|
|
'httpToHttpsUpperCase' => [ |
|
37
|
|
|
'http', |
|
38
|
|
|
['x-forwarded-proto' => ['https']], |
|
39
|
|
|
['x-forwarded-proto' => ['HTTPS']], |
|
40
|
|
|
'https' |
|
41
|
|
|
], |
|
42
|
|
|
'httpToHttpsMultiValue' => [ |
|
43
|
|
|
'http', |
|
44
|
|
|
['x-forwarded-proto' => ['https']], |
|
45
|
|
|
['x-forwarded-proto' => ['on', 's', 'https']], |
|
46
|
|
|
'https' |
|
47
|
|
|
], |
|
48
|
|
|
// @TODO: implement https -> http |
|
49
|
|
|
'httpsToHttp' => ['https', ['x-forwarded-proto' => ['http']], ['x-forwarded-proto' => ['https']], 'http'], |
|
50
|
|
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @dataProvider simpleDataProvider |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testSimple(string $scheme, array $headers, ?array $protocolHeaders, string $expectedScheme) |
|
57
|
|
|
{ |
|
58
|
|
|
$request = new ServerRequest('GET', '/', $headers); |
|
59
|
|
|
$uri = $request->getUri()->withScheme($scheme); |
|
60
|
|
|
$request = $request->withUri($uri); |
|
61
|
|
|
|
|
62
|
|
|
$requestHandler = new class implements RequestHandlerInterface |
|
63
|
|
|
{ |
|
64
|
|
|
public $request; |
|
65
|
|
|
|
|
66
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
67
|
|
|
{ |
|
68
|
|
|
$this->request = $request; |
|
69
|
|
|
return new Response(200); |
|
70
|
|
|
} |
|
71
|
|
|
}; |
|
72
|
|
|
|
|
73
|
|
|
$nr = new BasicNetworkResolver(); |
|
74
|
|
|
if ($protocolHeaders !== null) { |
|
|
|
|
|
|
75
|
|
|
foreach ($protocolHeaders as $header => $values) { |
|
76
|
|
|
$nr = $nr->withNewSecureProtocolHeader($header, $values); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
$middleware = new NetworkResolver($nr); |
|
80
|
|
|
$middleware->process($request, $requestHandler); |
|
81
|
|
|
$resultRequest = $requestHandler->request; |
|
82
|
|
|
/* @var $resultRequest ServerRequestInterface */ |
|
83
|
|
|
$this->assertSame($expectedScheme, $resultRequest->getUri()->getScheme()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|