|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Koded\Http\AcceptHeaderNegotiator; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
class AcceptEncodingHeaderTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @dataProvider dataWithAsterisk |
|
12
|
|
|
*/ |
|
13
|
|
|
public function test_with_asterisk($accept, $expect, $quality) |
|
14
|
|
|
{ |
|
15
|
|
|
$encoding = (new AcceptHeaderNegotiator('*'))->match($accept); |
|
16
|
|
|
|
|
17
|
|
|
$this->assertSame($expect, $encoding->value(), 'Expects ' . $expect); |
|
18
|
|
|
$this->assertSame($quality, $encoding->quality(), 'Expects q=' . $quality); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @dataProvider dataWithSupportedEncoding |
|
23
|
|
|
*/ |
|
24
|
|
|
public function test_with_preferred_encoding($accept, $expect, $quality) |
|
25
|
|
|
{ |
|
26
|
|
|
$negotiator = (new AcceptHeaderNegotiator('gzip, compress, deflate'))->match($accept); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertSame($expect, $negotiator->value(), 'Expects ' . $expect); |
|
29
|
|
|
$this->assertSame($quality, $negotiator->quality(), 'Expects q=' . $quality); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function test_empty_accept_header() |
|
33
|
|
|
{ |
|
34
|
|
|
$match = (new AcceptHeaderNegotiator('*'))->match(''); |
|
35
|
|
|
$this->assertEquals('', $match->value(), 'Empty accept header returns empty match value'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function dataWithAsterisk() |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
['br;q=1.0, gzip;q=0.8, *;q=0.1', 'br', 1.0], |
|
42
|
|
|
['deflate', 'deflate', 1.0], |
|
43
|
|
|
['compress, gzip', 'compress', 1.0], |
|
44
|
|
|
['*', '*', 1.0], |
|
45
|
|
|
['compress;q=0.5, gzip;q=0.3', 'compress', 0.5], |
|
46
|
|
|
['gzip;q=1.0, identity; q=0.5, *;q=0', 'gzip', 1.0], |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function dataWithSupportedEncoding() |
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
|
|
['br;q=1.0, gzip;q=0.8, *;q=0.1', 'gzip', 0.8], |
|
54
|
|
|
['deflate', 'deflate', 1.0], |
|
55
|
|
|
['compress, gzip', 'compress', 1.0], |
|
56
|
|
|
['*', 'gzip', 1.0], |
|
57
|
|
|
['compress;q=0.5, gzip;q=0.3', 'compress', 0.5], |
|
58
|
|
|
['gzip;q=0.5;var=1, identity; q=0.5, *;q=0', 'gzip', 0.5], |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|