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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
<?php namespace Aura\Accept;
class MediaTest extends AcceptTestCase { protected function newMedia($server = array()) { return new Media\MediaNegotiator(new ValueFactory,$server); }
/** * @dataProvider mediaProvider * @param $accept * @param $expect * @param $negotiator_class * @param $value_class */ public function testGetMedia($server, $expected, $negotiator_class, $value_class) { $media = $this->newMedia($server); $this->assertAcceptValues($media, $expected, $negotiator_class, $value_class); }
/** * @dataProvider mediaNegotiateProvider * @param $accept * @param $available * @param $expected_value * @param $expected_params */ public function testGetMedia_negotiate($server, $available, $expected_value, $expected_params) { $media = $this->newMedia($server); $actual = $media->negotiate($available);
if ($expected_value === false) { $this->assertFalse($actual); } else { $this->assertInstanceOf('Aura\Accept\Media\MediaValue', $actual->available); $this->assertSame($expected_value, $actual->available->getValue()); $this->assertSame($expected_params, $actual->available->getParameters()); } }
public function mediaNegotiateProvider() { return array( array( // nothing available 'server' => array('HTTP_ACCEPT' => 'application/json, application/xml, text/*, */*'), 'available' => array(), 'expected_value' => false, 'expected_params' => array(), ), array( // explicitly accepts */*, and no matching media are available 'server' => array('HTTP_ACCEPT' => 'application/json, application/xml, text/*, */*'), 'available' => array('foo/bar', 'baz/dib'), 'expected_value' => 'foo/bar', 'expected_params' => array(), ), array( // explictly accepts application/xml, which is explictly available. // note that it returns the *available* value, which is determined // by the developer, not the acceptable value, which is determined // by the user/client/headers. 'server' => array('HTTP_ACCEPT' => 'application/json, application/xml, text/*, */*'), 'available' => array('application/XML', 'text/csv'), 'expected_value' => 'application/XML', 'expected_params' => array(), ), array( // a subtype is available 'server' => array('HTTP_ACCEPT' => 'application/json, application/xml, text/*, */*'), 'available' => array('foo/bar', 'text/csv', 'baz/qux'), 'expected_value' => 'text/csv', 'expected_params' => array(), ), array( // no acceptable media specified, use first available 'server' => array(), 'available' => array('application/json', 'application/xml'), 'expected_value' => 'application/json', 'expected_params' => array(), ), array( // media is available but quality level is not acceptable 'server' => array('HTTP_ACCEPT' => 'application/json, application/xml, text/*, foo/bar;q=0'), 'available' => array('foo/bar'), 'expected_value' => false, 'expected_params' => array(), ), array( // override with file extension 'server' => array( 'HTTP_ACCEPT' => 'text/html, text/xhtml, text/plain', 'REQUEST_URI' => '/path/to/resource.json', ), 'available' => array('text/html', 'application/json'), 'expected_value' => 'application/json', 'expected_params' => array(), ), array( // check against parameters when they are available 'server' => array('HTTP_ACCEPT' => 'text/html;level=2, text/html;level=1;q=0.5'), 'available' => array('text/html;level=1'), 'expected_value' => 'text/html', 'expected_params' => array('level' => '1'), ), array( // check against parameters when they are not available 'server' => array('HTTP_ACCEPT' => 'text/html;level=2, text/html;level=1;q=0.5'), 'available' => array('text/html;level=3'), 'expected_value' => false, 'expected_params' => array(), ), array( // */* is available 'server' => array('HTTP_ACCEPT' => 'application/json, application/xml, text/csv'), 'available' => array('*/*'), 'expected_value' => '*/*', 'expected_params' => array(), ), array( // check that quality values are properly sorted 'server' => array('HTTP_ACCEPT' => 'application/yaml;q=.1,application/json;q=.3,application/xml;q=.2'), 'available' => array('application/yaml', 'application/json', 'application/xml'), 'expected_value' => 'application/json', 'expected_params' => array() ) ); }
public function mediaProvider() { return array( array( 'server' => array('HTTP_ACCEPT' => 'text/*;q=0.9, text/html, text/xhtml;q=0.8'), 'expect' => array( array( 'type' => 'text', 'subtype' => 'html', 'value' => 'text/html', 'quality' => 1.0, 'parameters' => array(), ), array( 'type' => 'text', 'subtype' => '*', 'value' => 'text/*', 'quality' => 0.9, 'parameters' => array(), ), array( 'type' => 'text', 'subtype' => 'xhtml', 'value' => 'text/xhtml', 'quality' => 0.8, 'parameters' => array(), ), ), 'negotiator_class' => 'Aura\Accept\Media\MediaNegotiator', 'value_class' => 'Aura\Accept\Media\MediaValue', ), array( 'server' => array('HTTP_ACCEPT' => 'text/json;version=1,text/html;q=1;version=2,application/xml+xhtml;q=0'), 'expect' => array( array( 'type' => 'text', 'subtype' => 'json', 'value' => 'text/json', 'quality' => 1.0, 'parameters' => array('version' => 1), ), array( 'type' => 'text', 'subtype' => 'html', 'value' => 'text/html', 'quality' => 1.0, 'parameters' => array('version' => 2), ), array( 'type' => 'application', 'subtype' => 'xml+xhtml', 'value' => 'application/xml+xhtml', 'quality' => 0, 'parameters' => array(), ), ), 'negotiator_class' => 'Aura\Accept\Media\MediaNegotiator', 'value_class' => 'Aura\Accept\Media\MediaValue', ), array( 'server' => array('HTTP_ACCEPT' => 'text/json;version=1;foo=bar,text/html;version=2,application/xml+xhtml'), 'expect' => array( array( 'type' => 'text', 'subtype' => 'json', 'value' => 'text/json', 'quality' => 1.0, 'parameters' => array('version' => 1, 'foo' => 'bar'), ), array( 'type' => 'text', 'subtype' => 'html', 'value' => 'text/html', 'quality' => 1.0, 'parameters' => array('version' => 2), ), array( 'type' => 'application', 'subtype' => 'xml+xhtml', 'value' => 'application/xml+xhtml', 'quality' => 1.0, 'parameters' => array(), ), ), 'negotiator_class' => 'Aura\Accept\Media\MediaNegotiator', 'value_class' => 'Aura\Accept\Media\MediaValue', ), array( 'server' => array('HTTP_ACCEPT' => 'text/json;q=0.9;version=1;foo="bar"'), 'expect' => array( array( 'type' => 'text', 'subtype' => 'json', 'value' => 'text/json', 'quality' => 0.9, 'parameters' => array('version' => 1, 'foo' => 'bar'), ), ), 'negotiator_class' => 'Aura\Accept\Media\MediaNegotiator', 'value_class' => 'Aura\Accept\Media\MediaValue', ), ); } }
|