1 | <?php |
||
9 | class CorsServiceProviderTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | protected $app; |
||
12 | |||
13 | public function setUp() |
||
22 | |||
23 | public function testCorsPreFlight() |
||
50 | |||
51 | public function testCorsPreFlightFail() |
||
77 | |||
78 | public function dataProviderAllowOrigin() |
||
89 | |||
90 | /** |
||
91 | * @dataProvider dataProviderAllowOrigin |
||
92 | */ |
||
93 | public function testAllowOrigin($domain) |
||
94 | { |
||
95 | $this->app["cors.allowOrigin"] = $domain; |
||
96 | |||
97 | $this->app->get("/foo", function () { |
||
98 | return "foo"; |
||
99 | }); |
||
100 | |||
101 | $headers = [ |
||
102 | "HTTP_ORIGIN" => "http://www.foo.com", |
||
103 | ]; |
||
104 | $client = new Client($this->app, $headers); |
||
105 | $client->request("GET", "/foo"); |
||
106 | |||
107 | $response = $client->getResponse(); |
||
108 | |||
109 | $this->assertEquals("200", $response->getStatusCode()); |
||
110 | $this->assertFalse($response->headers->has("Access-Control-Allow-Methods")); |
||
111 | $this->assertEquals("http://www.foo.com", $response->headers->get("Access-Control-Allow-Origin")); |
||
112 | $this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
||
113 | $this->assertFalse($response->headers->has("Access-Control-Max-Age")); |
||
114 | $this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
||
115 | $this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
||
116 | $this->assertEquals("foo", $response->getContent()); |
||
117 | } |
||
118 | |||
119 | public function testAllowOriginFail() |
||
147 | |||
148 | public function testDefaultAllowMethodsWithMultipleAllow() |
||
174 | |||
175 | public function testAllowMethods() |
||
203 | |||
204 | public function testAllowHeadersFail() |
||
205 | { |
||
206 | $this->app["cors.allowHeaders"] = ""; |
||
207 | |||
208 | $this->app->get("/foo", function () { |
||
209 | return "foo"; |
||
210 | }); |
||
211 | |||
212 | $headers = [ |
||
213 | "HTTP_ORIGIN" => "http://www.foo.com", |
||
214 | "HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "GET", |
||
215 | "HTTP_ACCESS_CONTROL_REQUEST_HEADERS" => "if-modified-since", |
||
216 | ]; |
||
217 | $client = new Client($this->app, $headers); |
||
218 | $client->request("OPTIONS", "/foo"); |
||
219 | |||
220 | $response = $client->getResponse(); |
||
221 | |||
222 | $this->assertEquals("204", $response->getStatusCode()); |
||
223 | $this->assertEquals("GET", $response->headers->get("Allow")); |
||
224 | $this->assertFalse($response->headers->has("Access-Control-Allow-Methods")); |
||
225 | $this->assertFalse($response->headers->has("Access-Control-Allow-Origin")); |
||
226 | $this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
||
227 | $this->assertFalse($response->headers->has("Access-Control-Max-Age")); |
||
228 | $this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
||
229 | $this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
||
230 | $this->assertFalse($response->headers->has("Content-Type")); |
||
231 | $this->assertEquals("", $response->getContent()); |
||
232 | } |
||
233 | |||
234 | public function testAllowMethodsFail() |
||
235 | { |
||
236 | $this->app["cors.allowMethods"] = "GET"; |
||
237 | |||
238 | $this->app->match("/foo", function () { |
||
239 | return "foo"; |
||
240 | })->method("GET|POST"); |
||
241 | |||
242 | $headers = [ |
||
243 | "HTTP_ORIGIN" => "http://www.foo.com", |
||
244 | "HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "POST", |
||
245 | ]; |
||
246 | $client = new Client($this->app, $headers); |
||
247 | $client->request("OPTIONS", "/foo"); |
||
248 | |||
249 | $response = $client->getResponse(); |
||
250 | |||
251 | $this->assertEquals("204", $response->getStatusCode()); |
||
252 | $this->assertEquals("GET,POST", $response->headers->get("Allow")); |
||
253 | $this->assertFalse($response->headers->has("Access-Control-Allow-Methods")); |
||
254 | $this->assertFalse($response->headers->has("Access-Control-Allow-Origin")); |
||
255 | $this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
||
256 | $this->assertFalse($response->headers->has("Access-Control-Max-Age")); |
||
257 | $this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
||
258 | $this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
||
259 | $this->assertFalse($response->headers->has("Content-Type")); |
||
260 | $this->assertEquals("", $response->getContent()); |
||
261 | } |
||
262 | |||
263 | public function testMultipleAllowMethods() |
||
264 | { |
||
265 | $this->app["cors.allowMethods"] = "GET,POST"; |
||
266 | |||
267 | $this->app->match("/foo", function () { |
||
268 | return "foo"; |
||
269 | })->method("GET|POST|DELETE"); |
||
270 | |||
271 | $headers = array( |
||
272 | "HTTP_ORIGIN" => "http://www.foo.com", |
||
273 | "HTTP_ACCESS_CONTROL_REQUEST_METHOD" => "GET", |
||
274 | ); |
||
275 | $client = new Client($this->app, $headers); |
||
276 | $client->request("OPTIONS", "/foo"); |
||
277 | |||
278 | $response = $client->getResponse(); |
||
279 | |||
280 | $this->assertEquals("204", $response->getStatusCode()); |
||
281 | $this->assertEquals("GET,POST,DELETE", $response->headers->get("Allow")); |
||
282 | $this->assertEquals("GET", $response->headers->get("Access-Control-Allow-Methods")); |
||
283 | $this->assertEquals("http://www.foo.com", $response->headers->get("Access-Control-Allow-Origin")); |
||
284 | $this->assertFalse($response->headers->has("Access-Control-Allow-Headers")); |
||
285 | $this->assertEquals("15", $response->headers->get("Access-Control-Max-Age")); |
||
286 | $this->assertFalse($response->headers->has("Access-Control-Allow-Credentials")); |
||
287 | $this->assertFalse($response->headers->has("Access-Control-Expose-Headers")); |
||
288 | $this->assertFalse($response->headers->has("Content-Type")); |
||
289 | $this->assertEquals("", $response->getContent()); |
||
290 | } |
||
291 | |||
292 | public function testAllowCredentialsAndExposeHeaders() |
||
318 | |||
319 | public function testNotEnabledMethod() |
||
333 | |||
334 | public function testRouteWithOptionsOnlyRespondsWith404() |
||
343 | } |
||
344 |