Completed
Pull Request — master (#125)
by
unknown
05:31
created

NetworkResolverTest::simpleDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 33
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 41
rs 9.392
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
        // @TODO Only relevant tests for middleware
21
        return [
22
            'httpNotModify' => ['http', [], null, 'http'],
23
            'httpsNotModify' => ['https', [], null, 'https'],
24
            'httpNotMatchedProtocolHeader' => [
25
                'http',
26
                ['x-forwarded-proto' => ['https']],
27
                ['test' => ['https' => 'https']],
28
                'http'
29
            ],
30
            'httpNotMatchedProtocolHeaderValue' => [
31
                'http',
32
                ['x-forwarded-proto' => ['https']],
33
                ['x-forwarded-proto' => ['https' => 'test']],
34
                'http'
35
            ],
36
            'httpToHttps' => [
37
                'http',
38
                ['x-forwarded-proto' => ['https']],
39
                ['x-forwarded-proto' => ['https' => 'https']],
40
                'https'
41
            ],
42
            'httpToHttpsUpperCase' => [
43
                'http',
44
                ['x-forwarded-proto' => ['https']],
45
                ['x-forwarded-proto' => ['https' => 'HTTPS']],
46
                'https'
47
            ],
48
            'httpToHttpsMultiValue' => [
49
                'http',
50
                ['x-forwarded-proto' => ['https']],
51
                ['x-forwarded-proto' => ['https' => ['on', 's', 'https']]],
52
                'https'
53
            ],
54
            'httpsToHttp' => [
55
                'https',
56
                ['x-forwarded-proto' => ['http']],
57
                ['x-forwarded-proto' => ['http' => 'http']],
58
                'http'
59
            ],
60
            // @TODO callback test
61
        ];
62
    }
63
64
    /**
65
     * @dataProvider simpleDataProvider
66
     */
67
    public function testSimple(string $scheme, array $headers, ?array $protocolHeaders, string $expectedScheme)
68
    {
69
        $request = new ServerRequest('GET', '/', $headers);
70
        $uri = $request->getUri()->withScheme($scheme);
71
        $request = $request->withUri($uri);
72
73
        $requestHandler = new class implements RequestHandlerInterface
74
        {
75
            public $request;
76
77
            public function handle(ServerRequestInterface $request): ResponseInterface
78
            {
79
                $this->request = $request;
80
                return new Response(200);
81
            }
82
        };
83
84
        $nr = new BasicNetworkResolver();
85
        if ($protocolHeaders !== null) {
0 ignored issues
show
introduced by
The condition $protocolHeaders !== null is always true.
Loading history...
86
            foreach ($protocolHeaders as $header => $values) {
87
                $nr = $nr->withNewProtocolHeader($header, $values);
88
            }
89
        }
90
        $middleware = new NetworkResolver($nr);
91
        $middleware->process($request, $requestHandler);
92
        $resultRequest = $requestHandler->request;
93
        /* @var $resultRequest ServerRequestInterface */
94
        $this->assertSame($expectedScheme, $resultRequest->getUri()->getScheme());
95
    }
96
97
}
98