Passed
Pull Request — master (#257)
by Wilmer
23:33 queued 08:42
created

BasicNetworkResolverTest::schemeDataProvider()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 66
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 50
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 66
rs 9.0909

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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