|
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 OptionsTest 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
|
|
|
|
|
19
|
|
|
$this->app["options"]($this->app); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testOptionsMethod() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->app->get("/foo", function () { |
|
25
|
|
|
return "foo"; |
|
26
|
|
|
}); |
|
27
|
|
|
$this->app->post("/foo", function () { |
|
28
|
|
|
return "foo"; |
|
29
|
|
|
}); |
|
30
|
|
|
|
|
31
|
|
|
$client = new Client($this->app); |
|
32
|
|
|
$client->request("OPTIONS", "/foo"); |
|
33
|
|
|
|
|
34
|
|
|
$response = $client->getResponse(); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertEquals("204", $response->getStatusCode()); |
|
37
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
38
|
|
|
$this->assertEquals("GET,POST", $response->headers->get("Allow")); |
|
39
|
|
|
$this->assertEquals("", $response->getContent()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testOptionsMethodWithRequirements() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->app->get("/foo/{foo}", function () { |
|
45
|
|
|
return "foo"; |
|
46
|
|
|
})->assert("foo", "\d+"); |
|
47
|
|
|
|
|
48
|
|
|
$client = new Client($this->app); |
|
49
|
|
|
$client->request("OPTIONS", "/foo/23"); |
|
50
|
|
|
|
|
51
|
|
|
$response = $client->getResponse(); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertEquals("204", $response->getStatusCode()); |
|
54
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
55
|
|
|
$this->assertEquals("GET", $response->headers->get("Allow")); |
|
56
|
|
|
$this->assertEquals("", $response->getContent()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testOptionsMethodWithRequirements404() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->app->get("/foo/{foo}", function () { |
|
62
|
|
|
return "foo"; |
|
63
|
|
|
})->assert("foo", "\d+"); |
|
64
|
|
|
|
|
65
|
|
|
$client = new Client($this->app); |
|
66
|
|
|
$client->request("OPTIONS", "/foo/asdf"); |
|
67
|
|
|
|
|
68
|
|
|
$response = $client->getResponse(); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertEquals("404", $response->getStatusCode()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|