Completed
Push — master ( b2599f...653121 )
by Alexander
14:59 queued 04:10
created

testWithoutProtocolHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9666
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
15
    public function schemeDataProvider(): array
16
    {
17
        return [
18
            'httpNotModify' => ['http', [], null, 'http'],
19
            'httpsNotModify' => ['https', [], null, 'https'],
20
            'httpNotMatchedProtocolHeader' => [
21
                'http',
22
                ['x-forwarded-proto' => ['https']],
23
                ['test' => ['https' => 'https']],
24
                'http'
25
            ],
26
            'httpNotMatchedProtocolHeaderValue' => [
27
                'http',
28
                ['x-forwarded-proto' => ['https']],
29
                ['x-forwarded-proto' => ['https' => 'test']],
30
                'http'
31
            ],
32
            'httpToHttps' => [
33
                'http',
34
                ['x-forwarded-proto' => ['https']],
35
                ['x-forwarded-proto' => ['https' => 'https']],
36
                'https'
37
            ],
38
            'httpToHttpsDefault' => [
39
                'http',
40
                ['x-forwarded-proto' => ['https']],
41
                ['x-forwarded-proto' => null],
42
                'https'
43
            ],
44
            'httpToHttpsUpperCase' => [
45
                'http',
46
                ['x-forwarded-proto' => ['https']],
47
                ['x-forwarded-proto' => ['https' => 'HTTPS']],
48
                'https'
49
            ],
50
            'httpToHttpsMultiValue' => [
51
                'http',
52
                ['x-forwarded-proto' => ['https']],
53
                ['x-forwarded-proto' => ['https' => ['on', 's', 'https']]],
54
                'https'
55
            ],
56
            'httpsToHttp' => [
57
                'https',
58
                ['x-forwarded-proto' => 'http'],
59
                ['x-forwarded-proto' => ['http' => 'http']],
60
                'http'
61
            ],
62
            'httpToHttpsWithCallback' => [
63
                'http',
64
                ['x-forwarded-proto' => 'test any-https **'],
65
                [
66
                    'x-forwarded-proto' => function (array $values, String $header, ServerRequestInterface $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

66
                    'x-forwarded-proto' => function (array $values, String $header, /** @scrutinizer ignore-unused */ ServerRequestInterface $request) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $header is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

66
                    'x-forwarded-proto' => function (array $values, /** @scrutinizer ignore-unused */ String $header, ServerRequestInterface $request) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
                        return stripos($values[0], 'https') !== false ? 'https' : 'http';
68
                    },
69
                ],
70
                'https',
71
            ],
72
            'httpWithCallbackNull' => [
73
                'http',
74
                ['x-forwarded-proto' => 'test any-https **'],
75
                [
76
                    'x-forwarded-proto' => function (array $values, String $header, ServerRequestInterface $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $header is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
                    'x-forwarded-proto' => function (array $values, /** @scrutinizer ignore-unused */ String $header, ServerRequestInterface $request) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $values is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
                    'x-forwarded-proto' => function (/** @scrutinizer ignore-unused */ array $values, String $header, ServerRequestInterface $request) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
                    'x-forwarded-proto' => function (array $values, String $header, /** @scrutinizer ignore-unused */ ServerRequestInterface $request) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
                        return null;
78
                    },
79
                ],
80
                'http',
81
            ]
82
        ];
83
    }
84
85
    protected function newRequestWithSchemaAndHeaders(
86
        string $scheme = 'http',
87
        array $headers = []
88
    ): ServerRequestInterface {
89
        $request = new ServerRequest('GET', '/', $headers);
90
        $uri = $request->getUri()->withScheme($scheme);
91
        return $request->withUri($uri);
92
    }
93
94
    /**
95
     * @dataProvider schemeDataProvider
96
     */
97
    public function testScheme(string $scheme, array $headers, ?array $protocolHeaders, string $expectedScheme): void
98
    {
99
        $request = $this->newRequestWithSchemaAndHeaders($scheme, $headers);
100
        $requestHandler = new MockRequestHandler();
101
102
        $middleware = new BasicNetworkResolver();
103
        if ($protocolHeaders !== null) {
0 ignored issues
show
introduced by
The condition $protocolHeaders !== null is always true.
Loading history...
104
            foreach ($protocolHeaders as $header => $values) {
105
                $middleware = $middleware->withAddedProtocolHeader($header, $values);
106
            }
107
        }
108
        $middleware->process($request, $requestHandler);
109
        $resultRequest = $requestHandler->processedRequest;
110
        /* @var $resultRequest ServerRequestInterface */
111
        $this->assertSame($expectedScheme, $resultRequest->getUri()->getScheme());
112
    }
113
114
    public function testWithoutProtocolHeaders(): void
115
    {
116
        $request = $this->newRequestWithSchemaAndHeaders('http', [
117
            'x-forwarded-proto' => ['https'],
118
        ]);
119
        $requestHandler = new MockRequestHandler();
120
121
        $middleware = (new BasicNetworkResolver())
122
            ->withAddedProtocolHeader('x-forwarded-proto')
123
            ->withoutProtocolHeaders();
124
        $middleware->process($request, $requestHandler);
125
        $resultRequest = $requestHandler->processedRequest;
126
        /* @var $resultRequest ServerRequestInterface */
127
        $this->assertSame('http', $resultRequest->getUri()->getScheme());
128
    }
129
130
    public function testWithoutProtocolHeadersMulti(): void
131
    {
132
        $request = $this->newRequestWithSchemaAndHeaders('http', [
133
            'x-forwarded-proto' => ['https'],
134
            'x-forwarded-proto-2' => ['https'],
135
        ]);
136
        $requestHandler = new MockRequestHandler();
137
138
        $middleware = (new BasicNetworkResolver())
139
            ->withAddedProtocolHeader('x-forwarded-proto')
140
            ->withAddedProtocolHeader('x-forwarded-proto-2')
141
            ->withoutProtocolHeaders([
142
                'x-forwarded-proto',
143
                'x-forwarded-proto-2',
144
            ]);
145
        $middleware->process($request, $requestHandler);
146
        $resultRequest = $requestHandler->processedRequest;
147
        /* @var $resultRequest ServerRequestInterface */
148
        $this->assertSame('http', $resultRequest->getUri()->getScheme());
149
    }
150
151
    public function testWithoutProtocolHeader(): void
152
    {
153
        $request = $this->newRequestWithSchemaAndHeaders('https', [
154
            'x-forwarded-proto' => ['https'],
155
            'x-forwarded-proto-2' => ['http'],
156
        ]);
157
        $requestHandler = new MockRequestHandler();
158
159
        $middleware = (new BasicNetworkResolver())
160
            ->withAddedProtocolHeader('x-forwarded-proto')
161
            ->withAddedProtocolHeader('x-forwarded-proto-2')
162
            ->withoutProtocolHeader('x-forwarded-proto');
163
        $middleware->process($request, $requestHandler);
164
        $resultRequest = $requestHandler->processedRequest;
165
        /* @var $resultRequest ServerRequestInterface */
166
        $this->assertSame('http', $resultRequest->getUri()->getScheme());
167
168
        $middleware = $middleware->withoutProtocolHeader('x-forwarded-proto-2');
169
        $middleware->process($request, $requestHandler);
170
        $resultRequest = $requestHandler->processedRequest;
171
        /* @var $resultRequest ServerRequestInterface */
172
        $this->assertSame('https', $resultRequest->getUri()->getScheme());
173
    }
174
175
}
176