From 2db8587726a06e978863c6ac68ad88411cd366c4 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Wed, 15 May 2019 16:30:08 +0200 Subject: [PATCH 1/2] Correctly pass request type to kernel in preflight request --- composer.json | 2 +- src/SymfonyCache/HeaderReplaySubscriber.php | 2 +- .../HeaderReplaySubscriberTest.php | 43 +++++++++++++++++-- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 54a4915..438a0e7 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "require": { "php": "^5.6 | ^7.0", "friendsofsymfony/http-cache-bundle": "^2.1", - "friendsofsymfony/http-cache": "^2.1", + "friendsofsymfony/http-cache": "^2.7", "symfony/options-resolver": "^3.3 | ^4.0" }, "require-dev": { diff --git a/src/SymfonyCache/HeaderReplaySubscriber.php b/src/SymfonyCache/HeaderReplaySubscriber.php index 2f2f004..4d763a8 100644 --- a/src/SymfonyCache/HeaderReplaySubscriber.php +++ b/src/SymfonyCache/HeaderReplaySubscriber.php @@ -98,7 +98,7 @@ public function preHandle(CacheEvent $event) // Pass the duplicated request to the original kernel (so cache is bypassed) // which should result in the preflight request response handled by the // HeaderReplayListener - $preflightResponse = $httpCache->getKernel()->handle($duplicate); + $preflightResponse = $httpCache->getKernel()->handle($duplicate, $event->getRequestType()); // Early abort if the preflight response wanted to redirect for whatever // reason diff --git a/tests/SymfonyCache/HeaderReplaySubscriberTest.php b/tests/SymfonyCache/HeaderReplaySubscriberTest.php index 200a69d..8a35f70 100644 --- a/tests/SymfonyCache/HeaderReplaySubscriberTest.php +++ b/tests/SymfonyCache/HeaderReplaySubscriberTest.php @@ -146,10 +146,13 @@ public function testKernelIsCorrectlyCalledWithPreflightAcceptHeader() $kernel ->expects($this->once()) ->method('handle') - ->with($this->callback(function (Request $request) { - return 'HEAD' === $request->getMethod() && - HeaderReplayListener::CONTENT_TYPE === $request->headers->get('Accept'); - })) + ->with( + $this->callback(function (Request $request) { + return 'HEAD' === $request->getMethod() && + HeaderReplayListener::CONTENT_TYPE === $request->headers->get('Accept'); + }), + $this->equalTo(HttpKernelInterface::MASTER_REQUEST) + ) ->willReturn($response); $httpCache = $this->getHttpCacheKernelWithGivenKernel($kernel); @@ -166,6 +169,38 @@ public function testKernelIsCorrectlyCalledWithPreflightAcceptHeader() $subscriber->preHandle($cacheEvent); } + public function testKernelIsCorrectlyCalledWithRequestType() + { + $response = new Response(); + $kernel = $this->createMock(HttpCache::class); + $kernel + ->expects($this->once()) + ->method('handle') + ->with( + $this->callback(function (Request $request) { + return 'HEAD' === $request->getMethod() && + HeaderReplayListener::CONTENT_TYPE === $request->headers->get('Accept'); + }), + $this->equalTo(HttpKernelInterface::SUB_REQUEST) + ) + ->willReturn($response); + + $httpCache = $this->getHttpCacheKernelWithGivenKernel($kernel); + + $request = Request::create('/foobar', 'GET'); + $request->cookies->set('Foo', 'Bar'); + + $cacheEvent = new CacheEvent( + $httpCache, + $request, + null, + HttpKernelInterface::SUB_REQUEST + ); + + $subscriber = new HeaderReplaySubscriber(); + $subscriber->preHandle($cacheEvent); + } + public function testEarlyAbortIfRedirectionOnPreflightResponse() { $response = new RedirectResponse('https://foobar.com'); From cf18d2dff360be54d7d939eabf2e0c8d69c5a555 Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Wed, 15 May 2019 16:30:14 +0200 Subject: [PATCH 2/2] CS --- src/DependencyInjection/HeaderReplayExtension.php | 2 +- src/Event/HeaderReplayEvent.php | 2 +- src/EventListener/HeaderReplayListener.php | 2 +- src/EventListener/HeaderReplayStopPropagationListener.php | 2 +- src/HeaderReplayBundle.php | 2 +- src/SymfonyCache/HeaderReplaySubscriber.php | 2 +- tests/DependencyInjection/HeaderReplayExtensionTest.php | 2 +- tests/Event/HeaderReplayEventTest.php | 2 +- tests/EventListener/HeaderReplayListenerTest.php | 2 +- tests/EventListener/HeaderReplayStopPropagationListenerTest.php | 2 +- tests/SymfonyCache/DummyHttpCacheKernel.php | 2 +- tests/SymfonyCache/DummyNonHttpCacheKernel.php | 2 +- tests/SymfonyCache/HeaderReplaySubscriberTest.php | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/DependencyInjection/HeaderReplayExtension.php b/src/DependencyInjection/HeaderReplayExtension.php index 1c7409a..430af7b 100644 --- a/src/DependencyInjection/HeaderReplayExtension.php +++ b/src/DependencyInjection/HeaderReplayExtension.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/src/Event/HeaderReplayEvent.php b/src/Event/HeaderReplayEvent.php index 59864f7..fb2bc45 100644 --- a/src/Event/HeaderReplayEvent.php +++ b/src/Event/HeaderReplayEvent.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/src/EventListener/HeaderReplayListener.php b/src/EventListener/HeaderReplayListener.php index 43ac395..e939024 100644 --- a/src/EventListener/HeaderReplayListener.php +++ b/src/EventListener/HeaderReplayListener.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/src/EventListener/HeaderReplayStopPropagationListener.php b/src/EventListener/HeaderReplayStopPropagationListener.php index c8d2efc..d600da8 100644 --- a/src/EventListener/HeaderReplayStopPropagationListener.php +++ b/src/EventListener/HeaderReplayStopPropagationListener.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/src/HeaderReplayBundle.php b/src/HeaderReplayBundle.php index 14503ef..e816e2f 100644 --- a/src/HeaderReplayBundle.php +++ b/src/HeaderReplayBundle.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/src/SymfonyCache/HeaderReplaySubscriber.php b/src/SymfonyCache/HeaderReplaySubscriber.php index 4d763a8..0481529 100644 --- a/src/SymfonyCache/HeaderReplaySubscriber.php +++ b/src/SymfonyCache/HeaderReplaySubscriber.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/tests/DependencyInjection/HeaderReplayExtensionTest.php b/tests/DependencyInjection/HeaderReplayExtensionTest.php index fb4768b..79176d2 100644 --- a/tests/DependencyInjection/HeaderReplayExtensionTest.php +++ b/tests/DependencyInjection/HeaderReplayExtensionTest.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/tests/Event/HeaderReplayEventTest.php b/tests/Event/HeaderReplayEventTest.php index 855f69e..f17b3c4 100644 --- a/tests/Event/HeaderReplayEventTest.php +++ b/tests/Event/HeaderReplayEventTest.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/tests/EventListener/HeaderReplayListenerTest.php b/tests/EventListener/HeaderReplayListenerTest.php index c4b4f93..f5f3059 100644 --- a/tests/EventListener/HeaderReplayListenerTest.php +++ b/tests/EventListener/HeaderReplayListenerTest.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/tests/EventListener/HeaderReplayStopPropagationListenerTest.php b/tests/EventListener/HeaderReplayStopPropagationListenerTest.php index f26a937..0e5db46 100644 --- a/tests/EventListener/HeaderReplayStopPropagationListenerTest.php +++ b/tests/EventListener/HeaderReplayStopPropagationListenerTest.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/tests/SymfonyCache/DummyHttpCacheKernel.php b/tests/SymfonyCache/DummyHttpCacheKernel.php index 7076fd3..c028c47 100644 --- a/tests/SymfonyCache/DummyHttpCacheKernel.php +++ b/tests/SymfonyCache/DummyHttpCacheKernel.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/tests/SymfonyCache/DummyNonHttpCacheKernel.php b/tests/SymfonyCache/DummyNonHttpCacheKernel.php index bb675ae..ff925d6 100644 --- a/tests/SymfonyCache/DummyNonHttpCacheKernel.php +++ b/tests/SymfonyCache/DummyNonHttpCacheKernel.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle diff --git a/tests/SymfonyCache/HeaderReplaySubscriberTest.php b/tests/SymfonyCache/HeaderReplaySubscriberTest.php index 8a35f70..8cd844d 100644 --- a/tests/SymfonyCache/HeaderReplaySubscriberTest.php +++ b/tests/SymfonyCache/HeaderReplaySubscriberTest.php @@ -3,7 +3,7 @@ /* * terminal42/header-replay-bundle for Symfony * - * @copyright Copyright (c) 2008-2018, terminal42 gmbh + * @copyright Copyright (c) 2008-2019, terminal42 gmbh * @author terminal42 gmbh * @license MIT * @link http://github.com/terminal42/header-replay-bundle