forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNegotiateTest.php
More file actions
130 lines (95 loc) · 4.43 KB
/
Copy pathNegotiateTest.php
File metadata and controls
130 lines (95 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
class NegotiateTest extends CIUnitTestCase
{
/**
* @var CodeIgniter\HTTP\Request
*/
protected $request;
/**
* @var \CodeIgniter\HTTP\Negotiate
*/
protected $negotiate;
public function setUp()
{
$this->request = new \CodeIgniter\HTTP\Request(new \App\Config\AppConfig());
$this->negotiate = new \CodeIgniter\HTTP\Negotiate($this->request);
}
//--------------------------------------------------------------------
public function tearDown()
{
$this->request = $this->negotiate = null;
unset($this->request, $this->negotiate);
}
//--------------------------------------------------------------------
public function testNegotiateMediaFindsHighestMatch()
{
$this->request->setHeader('Accept', 'text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c');
$this->assertEquals('text/html', $this->negotiate->media(['text/html', 'text/x-c', 'text/x-dvi', 'text/plain']));
$this->assertEquals('text/x-c', $this->negotiate->media(['text/x-c', 'text/x-dvi', 'text/plain']));
$this->assertEquals('text/x-dvi', $this->negotiate->media(['text/plain', 'text/x-dvi']));
$this->assertEquals('text/x-dvi', $this->negotiate->media(['text/x-dvi']));
// No matches? Return the first that we support...
$this->assertEquals('text/md', $this->negotiate->media(['text/md']));
}
//--------------------------------------------------------------------
public function testParseHeaderDeterminesCorrectPrecedence()
{
$header =$this->negotiate->parseHeader('text/*, text/plain, text/plain;format=flowed, */*');
$this->assertEquals('text/plain', $header[0]['value']);
$this->assertEquals('flowed', $header[0]['params']['format']);
$this->assertEquals('text/plain', $header[1]['value']);
$this->assertEquals('*/*', $header[3]['value']);
}
//--------------------------------------------------------------------
public function testNegotiateMediaReturnsSupportedMatchWhenAsterisksInAvailable()
{
$this->request->setHeader('Accept', 'image/*, text/*');
$this->assertEquals('text/plain', $this->negotiate->media(['text/plain']));
}
//--------------------------------------------------------------------
public function testNegotiateMediaRecognizesMediaTypes()
{
// Image has a higher specificity, but is the wrong type...
$this->request->setHeader('Accept', 'text/*, image/jpeg');
$this->assertEquals('text/plain', $this->negotiate->media(['text/plain']));
}
//--------------------------------------------------------------------
public function testNegotiateMediaSupportsStrictMatching()
{
// Image has a higher specificity, but is the wrong type...
$this->request->setHeader('Accept', 'text/md, image/jpeg');
$this->assertEquals('text/plain', $this->negotiate->media(['text/plain']));
$this->assertEquals('', $this->negotiate->media(['text/plain'], true));
}
//--------------------------------------------------------------------
public function testAcceptCharsetMatchesBasics()
{
$this->request->setHeader('Accept-Charset', 'iso-8859-5, unicode-1-1;q=0.8');
$this->assertEquals('iso-8859-5', $this->negotiate->charset(['iso-8859-5', 'unicode-1-1']));
$this->assertEquals('unicode-1-1', $this->negotiate->charset(['utf-8', 'unicode-1-1']));
// No match will default to utf-8
$this->assertEquals('utf-8', $this->negotiate->charset(['iso-8859', 'unicode-1-2']));
}
//--------------------------------------------------------------------
public function testNegotiateEncodingReturnsFirstIfNoAcceptHeaderExists()
{
$this->assertEquals('compress', $this->negotiate->encoding(['compress', 'gzip']));
}
//--------------------------------------------------------------------
public function testNegotiatesEncodingBasics()
{
$this->request->setHeader('Accept-Encoding', 'gzip;q=1.0, identity; q=0.4, compress;q=0.5');
$this->assertEquals('gzip', $this->negotiate->encoding(['gzip', 'compress']));
$this->assertEquals('compress', $this->negotiate->encoding(['compress']));
$this->assertEquals('identity', $this->negotiate->encoding());
}
//--------------------------------------------------------------------
public function testAcceptLanguageBasics()
{
$this->request->setHeader('Accept-Language', 'da, en-gb;q=0.8, en;q=0.7');
$this->assertEquals('da', $this->negotiate->language(['da', 'en']));
$this->assertEquals('en-gb', $this->negotiate->language(['en-gb', 'en']));
$this->assertEquals('en', $this->negotiate->language(['en']));
}
//--------------------------------------------------------------------
}