|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Nyholm\Psr7\ServerRequest; |
|
6
|
|
|
use Psr\Http\Message\RequestInterface; |
|
7
|
|
|
use Yiisoft\Yii\Web\Helper\HeaderHelper; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class HeaderHelperTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
public function valueAndParametersDataProvider(): array |
|
13
|
|
|
{ |
|
14
|
|
|
return [ |
|
15
|
|
|
'empty' => ['', []], |
|
16
|
|
|
'noParams' => ['test', ['test']], |
|
17
|
|
|
'withParams' => ['test;q=1.0;version=2', ['test', 'q' => '1.0', 'version' => '2']], |
|
18
|
|
|
'witQuotedParameter' => [ |
|
19
|
|
|
'test;noqoute=test;qoute="test2"', |
|
20
|
|
|
['test', 'noqoute' => 'test', 'qoute' => 'test2'] |
|
21
|
|
|
], |
|
22
|
|
|
]; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @dataProvider valueAndParametersDataProvider |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testValueAndParameters(string $input, array $expected): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->assertSame($expected, HeaderHelper::getValueAndParameters($input)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function sortedValuesAndParametersDataProvider(): array |
|
34
|
|
|
{ |
|
35
|
|
|
return [ |
|
36
|
|
|
'empty' => ['', []], |
|
37
|
|
|
'emptyArray' => [[], []], |
|
38
|
|
|
'noQ' => ['text/html,text/xml', [['text/html', 'q' => 1.0], ['text/xml', 'q' => 1.0]]], |
|
39
|
|
|
'q' => ['text/html;q=0.2,text/xml', [['text/xml', 'q' => 1.0], ['text/html', 'q' => 0.2]]], |
|
40
|
|
|
'qq' => ['text/html;q=0.2,text/xml;q=0.3', [['text/xml', 'q' => 0.3], ['text/html', 'q' => 0.2]]], |
|
41
|
|
|
'qqDigits' => ['text/html;q=0.000,text/xml;q=1.000', [['text/xml', 'q' => 1.0], ['text/html', 'q' => 0.0]]], |
|
42
|
|
|
'qqDigits0.999' => [ |
|
43
|
|
|
'text/html;q=0.999,text/xml;q=1.000', |
|
44
|
|
|
[['text/xml', 'q' => 1.0], ['text/html', 'q' => 0.999]] |
|
45
|
|
|
], |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @dataProvider sortedValuesAndParametersDataProvider |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testSortedValuesAndParameters($input, array $expected): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->assertSame($expected, HeaderHelper::getSortedValueAndParameters($input)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function qFactorSortFailDataProvider(): array |
|
58
|
|
|
{ |
|
59
|
|
|
return [ |
|
60
|
|
|
'qTooBig' => ['text/xml;q=1.001', \InvalidArgumentException::class], |
|
61
|
|
|
'qTooBig2' => ['text/xml;q=2', \InvalidArgumentException::class], |
|
62
|
|
|
'qInvalidDigits' => ['text/xml;q=0.0000', \InvalidArgumentException::class], |
|
63
|
|
|
'qInvalidDigits2' => ['text/xml;q=1.0000', \InvalidArgumentException::class], |
|
64
|
|
|
'int' => [3, \InvalidArgumentException::class], |
|
65
|
|
|
'float' => [3.0, \InvalidArgumentException::class], |
|
66
|
|
|
'request' => [new ServerRequest('get', '/'), \InvalidArgumentException::class], |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @dataProvider qFactorSortFailDataProvider |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testQFactorSortFail($input, string $expected): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->expectException($expected); |
|
76
|
|
|
HeaderHelper::getSortedValueAndParameters($input); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function sortedAcceptTypesDataProvider(): array |
|
80
|
|
|
{ |
|
81
|
|
|
return [ |
|
82
|
|
|
'empty' => ['', []], |
|
83
|
|
|
'emptyArray' => [[], []], |
|
84
|
|
|
'noQ' => ['text/html,text/xml', ['text/html', 'text/xml']], |
|
85
|
|
|
'q1' => ['text/html;q=1,text/xml', ['text/html', 'text/xml']], |
|
86
|
|
|
'q1End' => ['text/html,text/xml;q=1', ['text/html', 'text/xml']], |
|
87
|
|
|
'forward' => ['text/html;q=0.2,text/xml', ['text/xml', 'text/html']], |
|
88
|
|
|
'forward2' => ['text/html;q=0.4,text/xml,text/plain;q=0.8', ['text/xml', 'text/plain', 'text/html']], |
|
89
|
|
|
'specType' => ['text/html,text/html;version=2', ['text/html;version=2', 'text/html']], |
|
90
|
|
|
'specTypeQ' => ['text/html;q=0.3,text/html;version=2;q=0.2', ['text/html', 'text/html;version=2']], |
|
91
|
|
|
'qSame' => ['text/html;q=0.4,text/xml;q=0.4', ['text/html', 'text/xml']], |
|
92
|
|
|
'specFormatOrder' => [ |
|
93
|
|
|
'text/html;version=2;a=b,text/html;version=2;a=a', |
|
94
|
|
|
['text/html;a=b;version=2', 'text/html;a=a;version=2'] |
|
95
|
|
|
], |
|
96
|
|
|
'wildcardRfcExample' => [ // https://tools.ietf.org/html/rfc7231#section-5.3.2 |
|
97
|
|
|
'text/*, text/plain, text/plain;format=flowed, */*', |
|
98
|
|
|
[ |
|
99
|
|
|
'text/plain;format=flowed', |
|
100
|
|
|
'text/plain', |
|
101
|
|
|
'text/*', |
|
102
|
|
|
'*/*', |
|
103
|
|
|
], |
|
104
|
|
|
], |
|
105
|
|
|
]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @dataProvider sortedAcceptTypesDataProvider |
|
110
|
|
|
*/ |
|
111
|
|
|
public function testSortedAcceptTypes($input, array $expected): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->assertSame($expected, HeaderHelper::getSortedAcceptTypes($input)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function sortedAcceptTypesFromRequestDataProvider(): array |
|
117
|
|
|
{ |
|
118
|
|
|
return [ |
|
119
|
|
|
'simple' => [ |
|
120
|
|
|
new ServerRequest('get', '/', ['accept' => ['text/html;q=0.1', 'text/xml']]), |
|
121
|
|
|
['text/xml', 'text/html'], |
|
122
|
|
|
], |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @dataProvider sortedAcceptTypesFromRequestDataProvider |
|
128
|
|
|
*/ |
|
129
|
|
|
public function testSortedAcceptTypesFromRequest(RequestInterface $request, array $expected): void |
|
130
|
|
|
{ |
|
131
|
|
|
$this->assertSame($expected, HeaderHelper::getSortedAcceptTypesFromRequest($request)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function getParametersDataProvider(): array |
|
135
|
|
|
{ |
|
136
|
|
|
return [ |
|
137
|
|
|
'simple' => ['a=test; test=test55', ['a' => 'test', 'test' => 'test55']], |
|
138
|
|
|
'quoted' => ['a="test" ;b="test2;";d ="."', ['a' => 'test', 'b' => 'test2;', 'd' => '.']], |
|
139
|
|
|
'mixed' => ['a = b; c="apple"', ['a' => 'b', 'c' => 'apple']], |
|
140
|
|
|
'one' => ['a=test', ['a' => 'test']], |
|
141
|
|
|
'oneSpace1' => ['a =test', ['a' => 'test']], |
|
142
|
|
|
'oneSpace2' => ['a= test', ['a' => 'test']], |
|
143
|
|
|
'oneSpace3' => ['a = test', ['a' => 'test']], |
|
144
|
|
|
'oneQuoted' => ['a="test"', ['a' => 'test']], |
|
145
|
|
|
'oneQuotedSpace1' => ['a ="test"', ['a' => 'test']], |
|
146
|
|
|
'oneQuotedSpace2' => ['a= "test"', ['a' => 'test']], |
|
147
|
|
|
'oneQuotedSpace3' => ['a = "test"', ['a' => 'test']], |
|
148
|
|
|
'semicolonAtEnd' => ['a = b;', ['a' => 'b']], |
|
149
|
|
|
'semicolonAndSpaceAtEnd' => ['a = b; ', ['a' => 'b']], |
|
150
|
|
|
'mixedQuotes' => ['a="test\'";test = "\'test\'"', ['a' => 'test\'', 'test' => '\'test\'']], |
|
151
|
|
|
'specChars' => ['a=!#$%&\'*+.^`|~-; b=test', ['a' => '!#$%&\'*+.^`|~-', 'b' => 'test']], |
|
152
|
|
|
'numbers' => ['a=8888;b="999"', ['a' => '8888', 'b' => '999']], |
|
153
|
|
|
'invalidQuotes2' => ['a="test', null, \InvalidArgumentException::class], |
|
154
|
|
|
'invalidQuotes3' => ['a=test"', null, \InvalidArgumentException::class], |
|
155
|
|
|
'invalidEmptyQuotes' => ['a=""', null, \InvalidArgumentException::class], |
|
156
|
|
|
'invalidEmptyValue' => ['a=b; c=', null, \InvalidArgumentException::class], |
|
157
|
|
|
]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @dataProvider getParametersDataProvider |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testGetParameters(string $input, ?array $expected, ?string $expectedException = null): void |
|
164
|
|
|
{ |
|
165
|
|
|
if ($expectedException !== null) { |
|
166
|
|
|
$this->expectException($expectedException); |
|
167
|
|
|
} |
|
168
|
|
|
$this->assertSame($expected, HeaderHelper::getParameters($input)); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|