File tree Expand file tree Collapse file tree 3 files changed +38
-5
lines changed
Expand file tree Collapse file tree 3 files changed +38
-5
lines changed Original file line number Diff line number Diff line change @@ -276,8 +276,15 @@ public function varyHeader(Response $response, string $header): Response
276276 {
277277 if (!$ response ->headers ->has ('Vary ' )) {
278278 $ response ->headers ->set ('Vary ' , $ header );
279- } elseif (!in_array ($ header , explode (', ' , (string ) $ response ->headers ->get ('Vary ' )))) {
280- $ response ->headers ->set ('Vary ' , ((string ) $ response ->headers ->get ('Vary ' )) . ', ' . $ header );
279+ } else {
280+ $ varyHeaders = $ response ->getVary ();
281+ if (!in_array ($ header , $ varyHeaders , true )) {
282+ if (count ($ response ->headers ->all ('Vary ' )) === 1 ) {
283+ $ response ->setVary (((string )$ response ->headers ->get ('Vary ' )) . ', ' . $ header );
284+ } else {
285+ $ response ->setVary ($ header , false );
286+ }
287+ }
281288 }
282289
283290 return $ response ;
Original file line number Diff line number Diff line change @@ -274,6 +274,32 @@ public function itAppendsAnExistingVaryHeader(): void
274274 $ this ->assertEquals ('Content-Type, Origin ' , $ response ->headers ->get ('Vary ' ));
275275 }
276276
277+ /**
278+ * @test
279+ * @see http://www.w3.org/TR/cors/index.html#resource-implementation
280+ */
281+ public function itAppendsMultipleExistingVaryHeaders (): void
282+ {
283+ $ app = $ this ->createStackedApp (
284+ array (
285+ 'allowedOrigins ' => ['* ' ],
286+ 'supportsCredentials ' => true ,
287+ ),
288+ array (
289+ 'Vary ' => [
290+ 'Content-Type ' ,
291+ 'Referer ' ,
292+ ],
293+ )
294+ );
295+ $ request = $ this ->createValidActualRequest ();
296+
297+ $ response = $ app ->handle ($ request );
298+
299+ $ this ->assertCount (3 , $ response ->headers ->all ('Vary ' ));
300+ $ this ->assertEquals (['Content-Type ' , 'Referer ' , 'Origin ' ], $ response ->headers ->all ('Vary ' ));
301+ }
302+
277303 /**
278304 * @test
279305 */
@@ -555,7 +581,7 @@ private function createValidPreflightRequest(): Request
555581
556582 /**
557583 * @param CorsInputOptions $options
558- * @param string[] $responseHeaders
584+ * @param array<array< string>|string> $responseHeaders
559585 * @return MockApp
560586 */
561587 private function createStackedApp (array $ options = array (), array $ responseHeaders = array ()): MockApp
Original file line number Diff line number Diff line change 2121 */
2222class MockApp
2323{
24- /** @var string[] */
24+ /** @var array<array< string>|string> */
2525 private $ responseHeaders ;
2626
2727 /**
@@ -30,7 +30,7 @@ class MockApp
3030 private $ cors ;
3131
3232 /**
33- * @param string[] $responseHeaders
33+ * @param array<array< string>|string> $responseHeaders
3434 * @param CorsInputOptions $options
3535 */
3636 public function __construct (array $ responseHeaders , array $ options = [])
You can’t perform that action at this time.
0 commit comments