1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JDesrosiers\Silex\Provider\Test; |
4
|
|
|
|
5
|
|
|
use JDesrosiers\Silex\Provider\CorsServiceProvider; |
6
|
|
|
use Silex\Application; |
7
|
|
|
use Symfony\Component\HttpKernel\Client; |
8
|
|
|
|
9
|
|
|
class CorsEnableControllerCollectionTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
protected $client; |
12
|
|
|
|
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
$app = new Application(); |
16
|
|
|
$app["debug"] = true; |
17
|
|
|
$app->register(new CorsServiceProvider()); |
18
|
|
|
|
19
|
|
|
$controllers = $app["controllers_factory"]; |
20
|
|
|
$controllers->get("/", function () { |
21
|
|
|
return "foo"; |
22
|
|
|
}); |
23
|
|
|
$app->mount("/foo", $app["cors-enabled"]($controllers)); |
24
|
|
|
|
25
|
|
|
$app->get("/bar", function () { |
26
|
|
|
return "bar"; |
27
|
|
|
}); |
28
|
|
|
|
29
|
|
|
$this->client = new Client($app, ["HTTP_ORIGIN" => "http://www.foo.com"]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testEnabledPreflight() |
33
|
|
|
{ |
34
|
|
|
$this->client->request("OPTIONS", "/foo/"); |
35
|
|
|
$response = $this->client->getResponse(); |
36
|
|
|
|
37
|
|
|
$this->assertTrue($response->isEmpty()); |
38
|
|
|
$this->assertTrue($response->headers->has("Access-Control-Allow-Origin")); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testNotEnabledPreflight() |
42
|
|
|
{ |
43
|
|
|
$this->client->request("OPTIONS", "/bar"); |
44
|
|
|
$response = $this->client->getResponse(); |
45
|
|
|
|
46
|
|
|
$this->assertTrue($response->isEmpty()); |
47
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Origin")); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testEnabledController() |
51
|
|
|
{ |
52
|
|
|
$this->client->request("GET", "/foo/"); |
53
|
|
|
$response = $this->client->getResponse(); |
54
|
|
|
|
55
|
|
|
$this->assertTrue($response->isOk()); |
56
|
|
|
$this->assertTrue($response->headers->has("Access-Control-Allow-Origin")); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testNotEnabledController() |
60
|
|
|
{ |
61
|
|
|
$this->client->request("GET", "/bar"); |
62
|
|
|
$response = $this->client->getResponse(); |
63
|
|
|
|
64
|
|
|
$this->assertTrue($response->isOk()); |
65
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Origin")); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|