@@ -26,12 +26,29 @@ class CorsServiceTest extends TestCase
26
26
*/
27
27
public function itCanHaveOptions (): void
28
28
{
29
- $ service = new CorsService ([
30
- 'allowedOrigins ' => ['localhost ' ]
31
- ]);
29
+ $ options = [
30
+ 'allowedOrigins ' => ['localhost ' ],
31
+ 'allowedOriginsPatterns ' => ['/something/ ' ],
32
+ 'allowedHeaders ' => ['x-custom ' ],
33
+ 'allowedMethods ' => ['PUT ' ],
34
+ 'maxAge ' => 684 ,
35
+ 'supportsCredentials ' => true ,
36
+ 'exposedHeaders ' => ['x-custom-2 ' ],
37
+ ];
38
+
39
+ $ service = new CorsService ($ options );
32
40
33
41
$ this ->assertInstanceOf (CorsService::class, $ service );
34
- $ this ->assertEquals (['localhost ' ], $ this ->getOptionsFromService ($ service )['allowedOrigins ' ]);
42
+
43
+ $ normalized = $ this ->getOptionsFromService ($ service );
44
+
45
+ $ this ->assertEquals ($ options ['allowedOrigins ' ], $ normalized ['allowedOrigins ' ]);
46
+ $ this ->assertEquals ($ options ['allowedOriginsPatterns ' ], $ normalized ['allowedOriginsPatterns ' ]);
47
+ $ this ->assertEquals ($ options ['allowedHeaders ' ], $ normalized ['allowedHeaders ' ]);
48
+ $ this ->assertEquals ($ options ['allowedMethods ' ], $ normalized ['allowedMethods ' ]);
49
+ $ this ->assertEquals ($ options ['maxAge ' ], $ normalized ['maxAge ' ]);
50
+ $ this ->assertEquals ($ options ['supportsCredentials ' ], $ normalized ['supportsCredentials ' ]);
51
+ $ this ->assertEquals ($ options ['exposedHeaders ' ], $ normalized ['exposedHeaders ' ]);
35
52
}
36
53
37
54
/**
@@ -63,6 +80,24 @@ public function itNormalizesFalseExposedHeaders(): void
63
80
$ this ->assertEquals ([], $ this ->getOptionsFromService ($ service )['exposedHeaders ' ]);
64
81
}
65
82
83
+ /**
84
+ * @test
85
+ */
86
+ public function itAllowsNullMaxAge (): void
87
+ {
88
+ $ service = new CorsService (['maxAge ' => null ]);
89
+ $ this ->assertNull ($ this ->getOptionsFromService ($ service )['maxAge ' ]);
90
+ }
91
+
92
+ /**
93
+ * @test
94
+ */
95
+ public function itAllowsZeroMaxAge (): void
96
+ {
97
+ $ service = new CorsService (['maxAge ' => 0 ]);
98
+ $ this ->assertEquals (0 , $ this ->getOptionsFromService ($ service )['maxAge ' ]);
99
+ }
100
+
66
101
/**
67
102
* @test
68
103
*/
@@ -90,14 +125,34 @@ public function itThrowsExceptionOnInvalidOriginsArray(): void
90
125
*/
91
126
public function itNormalizesWildcardOrigins (): void
92
127
{
93
- $ origins = ['* ' ];
94
-
95
- $ service = new CorsService (['allowedOrigins ' => $ origins ]);
128
+ $ service = new CorsService (['allowedOrigins ' => ['* ' ]]);
96
129
$ this ->assertInstanceOf (CorsService::class, $ service );
97
130
98
131
$ this ->assertTrue ($ this ->getOptionsFromService ($ service )['allowAllOrigins ' ]);
99
132
}
100
133
134
+ /**
135
+ * @test
136
+ */
137
+ public function itNormalizesWildcardHeaders (): void
138
+ {
139
+ $ service = new CorsService (['allowedHeaders ' => ['* ' ]]);
140
+ $ this ->assertInstanceOf (CorsService::class, $ service );
141
+
142
+ $ this ->assertTrue ($ this ->getOptionsFromService ($ service )['allowAllHeaders ' ]);
143
+ }
144
+
145
+ /**
146
+ * @test
147
+ */
148
+ public function itNormalizesWildcardMethods (): void
149
+ {
150
+ $ service = new CorsService (['allowedMethods ' => ['* ' ]]);
151
+ $ this ->assertInstanceOf (CorsService::class, $ service );
152
+
153
+ $ this ->assertTrue ($ this ->getOptionsFromService ($ service )['allowAllMethods ' ]);
154
+ }
155
+
101
156
/**
102
157
* @test
103
158
*/
@@ -115,12 +170,32 @@ public function itConvertsWildcardOriginPatterns(): void
115
170
*/
116
171
public function itNormalizesUnderscoreOptions (): void
117
172
{
118
- $ origins = ['localhost ' ];
119
-
120
- $ service = new CorsService (['allowed_origins ' => $ origins ]);
173
+ $ options = [
174
+ 'allowed_origins ' => ['localhost ' ],
175
+ 'allowed_origins_patterns ' => ['/something/ ' ],
176
+ 'allowed_headers ' => ['x-custom ' ],
177
+ 'allowed_methods ' => ['PUT ' ],
178
+ 'max_age ' => 684 ,
179
+ 'supports_credentials ' => true ,
180
+ 'exposed_headers ' => ['x-custom-2 ' ],
181
+ ];
182
+
183
+ $ service = new CorsService ($ options );
121
184
$ this ->assertInstanceOf (CorsService::class, $ service );
122
185
123
- $ this ->assertEquals ($ origins , $ this ->getOptionsFromService ($ service )['allowedOrigins ' ]);
186
+ $ this ->assertEquals ($ options ['allowed_origins ' ], $ this ->getOptionsFromService ($ service )['allowedOrigins ' ]);
187
+ $ this ->assertEquals (
188
+ $ options ['allowed_origins_patterns ' ],
189
+ $ this ->getOptionsFromService ($ service )['allowedOriginsPatterns ' ]
190
+ );
191
+ $ this ->assertEquals ($ options ['allowed_headers ' ], $ this ->getOptionsFromService ($ service )['allowedHeaders ' ]);
192
+ $ this ->assertEquals ($ options ['allowed_methods ' ], $ this ->getOptionsFromService ($ service )['allowedMethods ' ]);
193
+ $ this ->assertEquals ($ options ['exposed_headers ' ], $ this ->getOptionsFromService ($ service )['exposedHeaders ' ]);
194
+ $ this ->assertEquals ($ options ['max_age ' ], $ this ->getOptionsFromService ($ service )['maxAge ' ]);
195
+ $ this ->assertEquals (
196
+ $ options ['supports_credentials ' ],
197
+ $ this ->getOptionsFromService ($ service )['supportsCredentials ' ]
198
+ );
124
199
}
125
200
126
201
/**
0 commit comments