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 CorsEnableControllerTest 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
|
|
|
$controller = $app->get("/foo", function () { |
20
|
|
|
return "foo"; |
21
|
|
|
}); |
22
|
|
|
$app["cors-enabled"]($controller); |
23
|
|
|
|
24
|
|
|
$app->get("/bar", function () { |
25
|
|
|
return "bar"; |
26
|
|
|
}); |
27
|
|
|
|
28
|
|
|
$this->client = new Client($app, ["HTTP_ORIGIN" => "http://www.foo.com"]); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testEnabledPreflight() |
32
|
|
|
{ |
33
|
|
|
$this->client->request("OPTIONS", "/foo"); |
34
|
|
|
$response = $this->client->getResponse(); |
35
|
|
|
|
36
|
|
|
$this->assertTrue($response->isEmpty()); |
37
|
|
|
$this->assertTrue($response->headers->has("Access-Control-Allow-Origin")); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testNotEnabledPreflight() |
41
|
|
|
{ |
42
|
|
|
$this->client->request("OPTIONS", "/bar"); |
43
|
|
|
$response = $this->client->getResponse(); |
44
|
|
|
|
45
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Origin")); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testEnabledController() |
49
|
|
|
{ |
50
|
|
|
$this->client->request("GET", "/foo"); |
51
|
|
|
$response = $this->client->getResponse(); |
52
|
|
|
|
53
|
|
|
$this->assertTrue($response->isOk()); |
54
|
|
|
$this->assertTrue($response->headers->has("Access-Control-Allow-Origin")); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testNotEnabledController() |
58
|
|
|
{ |
59
|
|
|
$this->client->request("GET", "/bar"); |
60
|
|
|
$response = $this->client->getResponse(); |
61
|
|
|
|
62
|
|
|
$this->assertTrue($response->isOk()); |
63
|
|
|
$this->assertFalse($response->headers->has("Access-Control-Allow-Origin")); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|