@@ -26,12 +26,29 @@ class CorsServiceTest extends TestCase
2626 */
2727 public function itCanHaveOptions (): void
2828 {
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 );
3240
3341 $ 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 ' ]);
3552 }
3653
3754 /**
@@ -63,6 +80,24 @@ public function itNormalizesFalseExposedHeaders(): void
6380 $ this ->assertEquals ([], $ this ->getOptionsFromService ($ service )['exposedHeaders ' ]);
6481 }
6582
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+
66101 /**
67102 * @test
68103 */
@@ -90,14 +125,34 @@ public function itThrowsExceptionOnInvalidOriginsArray(): void
90125 */
91126 public function itNormalizesWildcardOrigins (): void
92127 {
93- $ origins = ['* ' ];
94-
95- $ service = new CorsService (['allowedOrigins ' => $ origins ]);
128+ $ service = new CorsService (['allowedOrigins ' => ['* ' ]]);
96129 $ this ->assertInstanceOf (CorsService::class, $ service );
97130
98131 $ this ->assertTrue ($ this ->getOptionsFromService ($ service )['allowAllOrigins ' ]);
99132 }
100133
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+
101156 /**
102157 * @test
103158 */
@@ -115,12 +170,32 @@ public function itConvertsWildcardOriginPatterns(): void
115170 */
116171 public function itNormalizesUnderscoreOptions (): void
117172 {
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 );
121184 $ this ->assertInstanceOf (CorsService::class, $ service );
122185
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+ );
124199 }
125200
126201 /**
0 commit comments