|
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
|
|
|
]; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @dataProvider valueAndParametersDataProvider |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testValueAndParameters(string $input, array $expected): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->assertSame($expected, HeaderHelper::getValueAndParameters($input)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function sortedValuesAndParametersDataProvider(): array |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
'empty' => ['', []], |
|
33
|
|
|
'emptyArray' => [[], []], |
|
34
|
|
|
'noQ' => ['text/html,text/xml', [['text/html', 'q' => 1.0], ['text/xml', 'q' => 1.0]]], |
|
35
|
|
|
'q' => ['text/html;q=0.2,text/xml', [['text/xml', 'q' => 1.0], ['text/html', 'q' => 0.2]]], |
|
36
|
|
|
'qq' => ['text/html;q=0.2,text/xml;q=0.3', [['text/xml', 'q' => 0.3], ['text/html', 'q' => 0.2]]], |
|
37
|
|
|
'qqDigits' => ['text/html;q=0.000,text/xml;q=1.000', [['text/xml', 'q' => 1.0], ['text/html', 'q' => 0.0]]], |
|
38
|
|
|
'qqDigits0.999' => [ |
|
39
|
|
|
'text/html;q=0.999,text/xml;q=1.000', |
|
40
|
|
|
[['text/xml', 'q' => 1.0], ['text/html', 'q' => 0.999]] |
|
41
|
|
|
], |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @dataProvider sortedValuesAndParametersDataProvider |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testSortedValuesAndParameters($input, array $expected): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->assertSame($expected, HeaderHelper::getSortedValueAndParameters($input)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function qFactorSortFailDataProvider(): array |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
|
|
'qTooBig' => ['text/xml;q=1.001', \InvalidArgumentException::class], |
|
57
|
|
|
'qTooBig2' => ['text/xml;q=2', \InvalidArgumentException::class], |
|
58
|
|
|
'qInvalidDigits' => ['text/xml;q=0.0000', \InvalidArgumentException::class], |
|
59
|
|
|
'qInvalidDigits2' => ['text/xml;q=1.0000', \InvalidArgumentException::class], |
|
60
|
|
|
'int' => [3, \InvalidArgumentException::class], |
|
61
|
|
|
'float' => [3.0, \InvalidArgumentException::class], |
|
62
|
|
|
'request' => [new ServerRequest('get', '/'), \InvalidArgumentException::class], |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @dataProvider qFactorSortFailDataProvider |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testQFactorSortFail($input, string $expected): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->expectException($expected); |
|
72
|
|
|
HeaderHelper::getSortedValueAndParameters($input); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function sortedAcceptTypesDataProvider(): array |
|
76
|
|
|
{ |
|
77
|
|
|
return [ |
|
78
|
|
|
'empty' => ['', []], |
|
79
|
|
|
'emptyArray' => [[], []], |
|
80
|
|
|
'noQ' => ['text/html,text/xml', ['text/html', 'text/xml']], |
|
81
|
|
|
'q1' => ['text/html;q=1,text/xml', ['text/html', 'text/xml']], |
|
82
|
|
|
'q1End' => ['text/html,text/xml;q=1', ['text/html', 'text/xml']], |
|
83
|
|
|
'forward' => ['text/html;q=0.2,text/xml', ['text/xml', 'text/html']], |
|
84
|
|
|
'forward2' => ['text/html;q=0.4,text/xml,text/plain;q=0.8', ['text/xml', 'text/plain', 'text/html']], |
|
85
|
|
|
'specType' => ['text/html,text/html;version=2', ['text/html;version=2', 'text/html']], |
|
86
|
|
|
'specTypeQ' => ['text/html;q=0.3,text/html;version=2;q=0.2', ['text/html', 'text/html;version=2']], |
|
87
|
|
|
'qSame' => ['text/html;q=0.4,text/xml;q=0.4', ['text/html', 'text/xml']], |
|
88
|
|
|
'specFormatOrder' => [ |
|
89
|
|
|
'text/html;version=2;a=b,text/html;version=2;a=a', |
|
90
|
|
|
['text/html;a=b;version=2', 'text/html;a=a;version=2'] |
|
91
|
|
|
], |
|
92
|
|
|
'wildcardRfcExample' => [ // https://tools.ietf.org/html/rfc7231#section-5.3.2 |
|
93
|
|
|
'text/*, text/plain, text/plain;format=flowed, */*', |
|
94
|
|
|
[ |
|
95
|
|
|
'text/plain;format=flowed', |
|
96
|
|
|
'text/plain', |
|
97
|
|
|
'text/*', |
|
98
|
|
|
'*/*', |
|
99
|
|
|
], |
|
100
|
|
|
], |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @dataProvider sortedAcceptTypesDataProvider |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testSortedAcceptTypes($input, array $expected): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->assertSame($expected, HeaderHelper::getSortedAcceptTypes($input)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function sortedAcceptTypesFromRequestDataProvider(): array |
|
113
|
|
|
{ |
|
114
|
|
|
return [ |
|
115
|
|
|
'simple' => [ |
|
116
|
|
|
new ServerRequest('get', '/', ['accept' => ['text/html;q=0.1', 'text/xml']]), |
|
117
|
|
|
['text/xml', 'text/html'], |
|
118
|
|
|
], |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @dataProvider sortedAcceptTypesFromRequestDataProvider |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testSortedAcceptTypesFromRequest(RequestInterface $request, array $expected): void |
|
126
|
|
|
{ |
|
127
|
|
|
$this->assertSame($expected, HeaderHelper::getSortedAcceptTypesFromRequest($request)); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|