13
13
namespace Fruitcake \Cors \Tests ;
14
14
15
15
use Fruitcake \Cors \CorsService ;
16
+ use Fruitcake \Cors \Exceptions \InvalidOptionException ;
16
17
use PHPUnit \Framework \TestCase ;
17
18
18
19
class CorsServiceTest extends TestCase
@@ -36,7 +37,6 @@ public function it_can_have_no_options()
36
37
{
37
38
$ service = new CorsService ();
38
39
$ this ->assertInstanceOf (CorsService::class, $ service );
39
-
40
40
}
41
41
42
42
/**
@@ -46,6 +46,73 @@ public function it_can_have_empty_options()
46
46
{
47
47
$ service = new CorsService ([]);
48
48
$ this ->assertInstanceOf (CorsService::class, $ service );
49
+ }
50
+
51
+ /**
52
+ * @test
53
+ */
54
+ public function it_throws_exception_on_invalid_exposed_headers ()
55
+ {
56
+ $ this ->expectException (InvalidOptionException::class);
57
+
58
+ $ service = new CorsService (['exposedHeaders ' => true ]);
59
+ }
60
+
61
+ /**
62
+ * @test
63
+ */
64
+ public function it_throws_exception_on_invalid_origins_array ()
65
+ {
66
+ $ this ->expectException (InvalidOptionException::class);
67
+
68
+ $ service = new CorsService (['allowedOrigins ' => 'string ' ]);
69
+ }
70
+
71
+ /**
72
+ * @test
73
+ */
74
+ public function it_normalizes_wildcard_options ()
75
+ {
76
+ $ origins = ['* ' ];
77
+
78
+ $ service = new CorsService (['allowedOrigins ' => $ origins ]);
79
+ $ this ->assertInstanceOf (CorsService::class, $ service );
80
+
81
+ $ this ->assertEquals (true , $ this ->getOptionsFromService ($ service )['allowedOrigins ' ]);
82
+ }
83
+
84
+ /**
85
+ * @test
86
+ */
87
+ public function it_converts_origin_patterns ()
88
+ {
89
+ $ service = new CorsService (['allowedOrigins ' => ['*.mydomain.com ' ]]);
90
+ $ this ->assertInstanceOf (CorsService::class, $ service );
91
+
92
+ $ patterns = $ this ->getOptionsFromService ($ service )['allowedOriginsPatterns ' ];
93
+ $ this ->assertEquals (['#^.*\.mydomain\.com\z#u ' ], $ patterns );
94
+ }
95
+
96
+ /**
97
+ * @test
98
+ */
99
+ public function it_normalizes_underscore_options ()
100
+ {
101
+ $ origins = ['localhost ' ];
102
+
103
+ $ service = new CorsService (['allowed_origins ' => $ origins ]);
104
+ $ this ->assertInstanceOf (CorsService::class, $ service );
105
+
106
+ $ this ->assertEquals ($ origins , $ this ->getOptionsFromService ($ service )['allowedOrigins ' ]);
107
+ }
108
+
109
+ private function getOptionsFromService (CorsService $ service )
110
+ {
111
+ $ reflected = new \ReflectionClass ($ service );
112
+
113
+ $ property = $ reflected ->getProperty ('options ' );
114
+ $ property ->setAccessible (true );
49
115
116
+ return $ property ->getValue ($ service );
50
117
}
51
118
}
0 commit comments