|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JDesrosiers\Silex\Provider\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use JDesrosiers\Silex\Provider\CorsServiceProvider; |
|
6
|
|
|
use Silex\Application; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Client; |
|
8
|
|
|
|
|
9
|
|
|
class CorsServiceProviderTest extends \PHPUnit_Framework_TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
protected $app; |
|
12
|
|
|
|
|
13
|
|
|
public function setUp() |
|
14
|
|
|
{ |
|
15
|
|
|
$this->app = new Application(); |
|
16
|
|
|
$this->app["debug"] = true; |
|
17
|
|
|
$this->app->register(new CorsServiceProvider(), [ |
|
18
|
|
|
"cors.maxAge" => 15, |
|
19
|
|
|
]); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testCorsPreFlight() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->app["cors-enabled"]($this->app); |
|
25
|
|
|
|
|
26
|
|
|
$this->app->get("/foo", function () { |
|
27
|
|
|
return "foo"; |
|
28
|
|
|
}); |
|
29
|
|
|
|
|
30
|
|
|
$headers = [ |
|
31
|
|
|
"HTTP_ORIGIN" => "http://www.foo.com", |
|
32
|
|
|
"HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "GET", |
|
33
|
|
|
"HTTP_ACCESS_CONTROL_REQUEST_HEADERS" => "content-type", |
|
34
|
|
|
]; |
|
35
|
|
|
$client = new Client($this->app, $headers); |
|
36
|
|
|
$client->request("OPTIONS", "/foo"); |
|
37
|
|
|
|
|
38
|
|
|
$response = $client->getResponse(); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertEquals("204", $response->getStatusCode()); |
|
41
|
|
|
$this->assertEquals("GET", $response->headers->get("Allow")); |
|
42
|
|
|
$this->assertEquals("GET", $response->headers->get("Access-Control-Allow-Methods")); |
|
43
|
|
|
$this->assertEquals("http://www.foo.com", $response->headers->get("Access-Control-Allow-Origin")); |
|
44
|
|
|
$this->assertEquals("content-type", $response->headers->get("Access-Control-Allow-Headers")); |
|
45
|
|
|
$this->assertEquals("15", $response->headers->get("Access-Control-Max-Age")); |
|
46
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
|
47
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
|
48
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
49
|
|
|
$this->assertEquals("", $response->getContent()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testCorsPreFlightFail() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->app["cors-enabled"]($this->app); |
|
55
|
|
|
|
|
56
|
|
|
$this->app->get("/foo", function () { |
|
57
|
|
|
return "foo"; |
|
58
|
|
|
}); |
|
59
|
|
|
|
|
60
|
|
|
$headers = [ |
|
61
|
|
|
"HTTP_ORIGIN" => "http://www.foo.com", |
|
62
|
|
|
"HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "POST", |
|
63
|
|
|
]; |
|
64
|
|
|
$client = new Client($this->app, $headers); |
|
65
|
|
|
$client->request("OPTIONS", "/foo"); |
|
66
|
|
|
|
|
67
|
|
|
$response = $client->getResponse(); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertEquals("204", $response->getStatusCode()); |
|
70
|
|
|
$this->assertEquals("GET", $response->headers->get("Allow")); |
|
71
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Methods")); |
|
72
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Origin")); |
|
73
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
|
74
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Max-Age")); |
|
75
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
|
76
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
|
77
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
78
|
|
|
$this->assertEquals("", $response->getContent()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function dataProviderAllowOrigin() |
|
82
|
|
|
{ |
|
83
|
|
|
return [ |
|
84
|
|
|
["*"], |
|
85
|
|
|
["http://www.foo.com"], |
|
86
|
|
|
["*.foo.com"], |
|
87
|
|
|
["http://*.foo.com"], |
|
88
|
|
|
["http://www.foo.com http://www.bar.com"], |
|
89
|
|
|
["*.foo.com http://www.bar.com"], |
|
90
|
|
|
["http://www.bar.com http://www.foo.com"], |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @dataProvider dataProviderAllowOrigin |
|
96
|
|
|
*/ |
|
97
|
|
|
public function testAllowOrigin($domain) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->app["cors-enabled"]($this->app, ["allowOrigin" => $domain]); |
|
100
|
|
|
|
|
101
|
|
|
$this->app->get("/foo", function () { |
|
102
|
|
|
return "foo"; |
|
103
|
|
|
}); |
|
104
|
|
|
|
|
105
|
|
|
$headers = [ |
|
106
|
|
|
"HTTP_ORIGIN" => "http://www.foo.com", |
|
107
|
|
|
]; |
|
108
|
|
|
$client = new Client($this->app, $headers); |
|
109
|
|
|
$client->request("GET", "/foo"); |
|
110
|
|
|
|
|
111
|
|
|
$response = $client->getResponse(); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertEquals("200", $response->getStatusCode()); |
|
114
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Methods")); |
|
115
|
|
|
$this->assertEquals("http://www.foo.com", $response->headers->get("Access-Control-Allow-Origin")); |
|
116
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
|
117
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Max-Age")); |
|
118
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
|
119
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
|
120
|
|
|
$this->assertEquals("foo", $response->getContent()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function dataProviderAllowOriginFail() |
|
124
|
|
|
{ |
|
125
|
|
|
return [ |
|
126
|
|
|
["http://foo.example.com"], |
|
127
|
|
|
["http://bar.foo.example.com"], |
|
128
|
|
|
["http://bar.www.foo.example.com"], |
|
129
|
|
|
["*w.foo.example.com"], |
|
130
|
|
|
["w*.foo.example.com"], |
|
131
|
|
|
["www.*.example.com"], |
|
132
|
|
|
["http://*w.foo.example.com"], |
|
133
|
|
|
["http://w*.foo.example.com"], |
|
134
|
|
|
["http://www.*.example.com"] |
|
135
|
|
|
]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @dataProvider dataProviderAllowOriginFail |
|
140
|
|
|
*/ |
|
141
|
|
|
public function testAllowOriginFail($domain) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->app["cors-enabled"]($this->app, ["allowOrigin" => $domain]); |
|
144
|
|
|
|
|
145
|
|
|
$this->app->get("/foo", function () { |
|
146
|
|
|
return "foo"; |
|
147
|
|
|
}); |
|
148
|
|
|
|
|
149
|
|
|
$headers = [ |
|
150
|
|
|
"HTTP_ORIGIN" => "http://www.foo.example.com", |
|
151
|
|
|
"HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "GET", |
|
152
|
|
|
]; |
|
153
|
|
|
$client = new Client($this->app, $headers); |
|
154
|
|
|
$client->request("OPTIONS", "/foo"); |
|
155
|
|
|
|
|
156
|
|
|
$response = $client->getResponse(); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertEquals("204", $response->getStatusCode()); |
|
159
|
|
|
$this->assertEquals("GET", $response->headers->get("Allow")); |
|
160
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Methods")); |
|
161
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Origin")); |
|
162
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
|
163
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Max-Age")); |
|
164
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
|
165
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
|
166
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
167
|
|
|
$this->assertEquals("", $response->getContent()); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testDefaultAllowMethodsWithMultipleAllow() |
|
171
|
|
|
{ |
|
172
|
|
|
$this->app["cors-enabled"]($this->app); |
|
173
|
|
|
|
|
174
|
|
|
$this->app->match("/foo", function () { |
|
175
|
|
|
return "foo"; |
|
176
|
|
|
})->method("GET|POST"); |
|
177
|
|
|
|
|
178
|
|
|
$headers = array( |
|
179
|
|
|
"HTTP_ORIGIN" => "http://www.foo.com", |
|
180
|
|
|
"HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "GET", |
|
181
|
|
|
); |
|
182
|
|
|
$client = new Client($this->app, $headers); |
|
183
|
|
|
$client->request("OPTIONS", "/foo"); |
|
184
|
|
|
|
|
185
|
|
|
$response = $client->getResponse(); |
|
186
|
|
|
|
|
187
|
|
|
$this->assertEquals("204", $response->getStatusCode()); |
|
188
|
|
|
$this->assertEquals("GET,POST", $response->headers->get("Allow")); |
|
189
|
|
|
$this->assertEquals("GET", $response->headers->get("Access-Control-Allow-Methods")); |
|
190
|
|
|
$this->assertEquals("http://www.foo.com", $response->headers->get("Access-Control-Allow-Origin")); |
|
191
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
|
192
|
|
|
$this->assertEquals("15", $response->headers->get("Access-Control-Max-Age")); |
|
193
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
|
194
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
|
195
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
196
|
|
|
$this->assertEquals("", $response->getContent()); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function testAllowMethods() |
|
200
|
|
|
{ |
|
201
|
|
|
$this->app["cors-enabled"]($this->app, ["allowMethods" => "GET"]); |
|
202
|
|
|
|
|
203
|
|
|
$this->app->match("/foo", function () { |
|
204
|
|
|
return "foo"; |
|
205
|
|
|
})->method("GET|POST"); |
|
206
|
|
|
|
|
207
|
|
|
$headers = [ |
|
208
|
|
|
"HTTP_ORIGIN" => "http://www.foo.com", |
|
209
|
|
|
"HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "GET", |
|
210
|
|
|
]; |
|
211
|
|
|
$client = new Client($this->app, $headers); |
|
212
|
|
|
$client->request("OPTIONS", "/foo"); |
|
213
|
|
|
|
|
214
|
|
|
$response = $client->getResponse(); |
|
215
|
|
|
|
|
216
|
|
|
$this->assertEquals("204", $response->getStatusCode()); |
|
217
|
|
|
$this->assertEquals("GET,POST", $response->headers->get("Allow")); |
|
218
|
|
|
$this->assertEquals("GET", $response->headers->get("Access-Control-Allow-Methods")); |
|
219
|
|
|
$this->assertEquals("http://www.foo.com", $response->headers->get("Access-Control-Allow-Origin")); |
|
220
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
|
221
|
|
|
$this->assertEquals("15", $response->headers->get("Access-Control-Max-Age")); |
|
222
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
|
223
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
|
224
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
225
|
|
|
$this->assertEquals("", $response->getContent()); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function testAllowHeadersFail() |
|
229
|
|
|
{ |
|
230
|
|
|
$this->app["cors-enabled"]($this->app, ["allowHeaders" => ""]); |
|
231
|
|
|
|
|
232
|
|
|
$this->app->get("/foo", function () { |
|
233
|
|
|
return "foo"; |
|
234
|
|
|
}); |
|
235
|
|
|
|
|
236
|
|
|
$headers = [ |
|
237
|
|
|
"HTTP_ORIGIN" => "http://www.foo.com", |
|
238
|
|
|
"HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "GET", |
|
239
|
|
|
"HTTP_ACCESS_CONTROL_REQUEST_HEADERS" => "if-modified-since", |
|
240
|
|
|
]; |
|
241
|
|
|
$client = new Client($this->app, $headers); |
|
242
|
|
|
$client->request("OPTIONS", "/foo"); |
|
243
|
|
|
|
|
244
|
|
|
$response = $client->getResponse(); |
|
245
|
|
|
|
|
246
|
|
|
$this->assertEquals("204", $response->getStatusCode()); |
|
247
|
|
|
$this->assertEquals("GET", $response->headers->get("Allow")); |
|
248
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Methods")); |
|
249
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Origin")); |
|
250
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
|
251
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Max-Age")); |
|
252
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
|
253
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
|
254
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
255
|
|
|
$this->assertEquals("", $response->getContent()); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
public function testAllowMethodsFail() |
|
259
|
|
|
{ |
|
260
|
|
|
$this->app["cors-enabled"]($this->app, ["allowMethods" => "GET"]); |
|
261
|
|
|
|
|
262
|
|
|
$this->app->match("/foo", function () { |
|
263
|
|
|
return "foo"; |
|
264
|
|
|
})->method("GET|POST"); |
|
265
|
|
|
|
|
266
|
|
|
$headers = [ |
|
267
|
|
|
"HTTP_ORIGIN" => "http://www.foo.com", |
|
268
|
|
|
"HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "POST", |
|
269
|
|
|
]; |
|
270
|
|
|
$client = new Client($this->app, $headers); |
|
271
|
|
|
$client->request("OPTIONS", "/foo"); |
|
272
|
|
|
|
|
273
|
|
|
$response = $client->getResponse(); |
|
274
|
|
|
|
|
275
|
|
|
$this->assertEquals("204", $response->getStatusCode()); |
|
276
|
|
|
$this->assertEquals("GET,POST", $response->headers->get("Allow")); |
|
277
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Methods")); |
|
278
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Origin")); |
|
279
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
|
280
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Max-Age")); |
|
281
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
|
282
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
|
283
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
284
|
|
|
$this->assertEquals("", $response->getContent()); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
public function testMultipleAllowMethods() |
|
288
|
|
|
{ |
|
289
|
|
|
$this->app["cors-enabled"]($this->app, ["allowMethods" => "GET,POST"]); |
|
290
|
|
|
|
|
291
|
|
|
$this->app->match("/foo", function () { |
|
292
|
|
|
return "foo"; |
|
293
|
|
|
})->method("GET|POST|DELETE"); |
|
294
|
|
|
|
|
295
|
|
|
$headers = array( |
|
296
|
|
|
"HTTP_ORIGIN" => "http://www.foo.com", |
|
297
|
|
|
"HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "GET", |
|
298
|
|
|
); |
|
299
|
|
|
$client = new Client($this->app, $headers); |
|
300
|
|
|
$client->request("OPTIONS", "/foo"); |
|
301
|
|
|
|
|
302
|
|
|
$response = $client->getResponse(); |
|
303
|
|
|
|
|
304
|
|
|
$this->assertEquals("204", $response->getStatusCode()); |
|
305
|
|
|
$this->assertEquals("GET,POST,DELETE", $response->headers->get("Allow")); |
|
306
|
|
|
$this->assertEquals("GET", $response->headers->get("Access-Control-Allow-Methods")); |
|
307
|
|
|
$this->assertEquals("http://www.foo.com", $response->headers->get("Access-Control-Allow-Origin")); |
|
308
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
|
309
|
|
|
$this->assertEquals("15", $response->headers->get("Access-Control-Max-Age")); |
|
310
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
|
311
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
|
312
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
313
|
|
|
$this->assertEquals("", $response->getContent()); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
public function testAllowCredentialsAndExposeHeaders() |
|
317
|
|
|
{ |
|
318
|
|
|
$this->app["cors-enabled"]($this->app, ["allowCredentials" => true, "exposeHeaders" => "Foo-Bar,Baz"]); |
|
319
|
|
|
|
|
320
|
|
|
$this->app->get("/foo", function () { |
|
321
|
|
|
return "foo"; |
|
322
|
|
|
}); |
|
323
|
|
|
|
|
324
|
|
|
$headers = [ |
|
325
|
|
|
"HTTP_ORIGIN" => "http://www.foo.com", |
|
326
|
|
|
]; |
|
327
|
|
|
$client = new Client($this->app, $headers); |
|
328
|
|
|
$client->request("GET", "/foo"); |
|
329
|
|
|
|
|
330
|
|
|
$response = $client->getResponse(); |
|
331
|
|
|
|
|
332
|
|
|
$this->assertEquals("200", $response->getStatusCode()); |
|
333
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Methods")); |
|
334
|
|
|
$this->assertEquals("http://www.foo.com", $response->headers->get("Access-Control-Allow-Origin")); |
|
335
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
|
336
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Max-Age")); |
|
337
|
|
|
$this->assertEquals("true", $response->headers->get("Access-Control-Allow-Credentials")); |
|
338
|
|
|
$this->assertEquals("Foo-Bar,Baz", $response->headers->get("Access-Control-Expose-Headers")); |
|
339
|
|
|
$this->assertEquals("foo", $response->getContent()); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
public function testNotEnabledMethod() |
|
343
|
|
|
{ |
|
344
|
|
|
$this->app["cors-enabled"]($this->app); |
|
345
|
|
|
|
|
346
|
|
|
$this->app->post("/foo", function () { |
|
347
|
|
|
return "foo"; |
|
348
|
|
|
}); |
|
349
|
|
|
|
|
350
|
|
|
$client = new Client($this->app); |
|
351
|
|
|
$client->request("GET", "/foo"); |
|
352
|
|
|
|
|
353
|
|
|
$response = $client->getResponse(); |
|
354
|
|
|
|
|
355
|
|
|
$this->assertEquals("405", $response->getStatusCode()); |
|
356
|
|
|
$this->assertEquals("OPTIONS, POST", $response->headers->get("Allow")); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
public function testRouteWithOnlyOptionsRespondsWith404() |
|
360
|
|
|
{ |
|
361
|
|
|
$this->app["cors-enabled"]($this->app); |
|
362
|
|
|
|
|
363
|
|
|
$client = new Client($this->app); |
|
364
|
|
|
$client->request("GET", "/foo"); |
|
365
|
|
|
|
|
366
|
|
|
$response = $client->getResponse(); |
|
367
|
|
|
|
|
368
|
|
|
$this->assertEquals("404", $response->getStatusCode()); |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|