From 5cd25d77a53c5d56d19bc852900f0de789c6e002 Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 5 Apr 2022 12:44:38 +0300 Subject: [PATCH 1/6] Skip processing for jsonapi --- src/HttpMiddleware/AssetHttpMiddleware.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/HttpMiddleware/AssetHttpMiddleware.php b/src/HttpMiddleware/AssetHttpMiddleware.php index c49a2fc..5175070 100644 --- a/src/HttpMiddleware/AssetHttpMiddleware.php +++ b/src/HttpMiddleware/AssetHttpMiddleware.php @@ -77,7 +77,11 @@ private function isJsonResponse(Response $response) : bool { return TRUE; } - return $response->headers->get('content-type') === 'application/json'; + $jsonTypes = [ + 'application/json', + 'application/vnd.api+json', + ]; + return in_array($response->headers->get('content-type'), $jsonTypes); } /** From cee9826ac19c8976c0eef9f39fee813f3b13e0bb Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 7 Apr 2022 10:01:04 +0300 Subject: [PATCH 2/6] Only redirect GET requests --- src/EventSubscriber/RedirectResponseSubscriber.php | 4 +++- src/HttpMiddleware/AssetHttpMiddleware.php | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/EventSubscriber/RedirectResponseSubscriber.php b/src/EventSubscriber/RedirectResponseSubscriber.php index 6a64b13..1611b06 100644 --- a/src/EventSubscriber/RedirectResponseSubscriber.php +++ b/src/EventSubscriber/RedirectResponseSubscriber.php @@ -75,9 +75,11 @@ private function buildRedirectUrl(string $url) : string { public function onResponse(ResponseEvent $event) : void { if ( !$this->validProxyDomains || - !$this->proxyManager->isConfigured(ProxyManagerInterface::DEFAULT_PROXY_DOMAIN) + !$this->proxyManager->isConfigured(ProxyManagerInterface::DEFAULT_PROXY_DOMAIN) || + $event->getRequest()->isMethod('GET') ) { // Nothing to do if default proxy domain is not defined. + // Only redirect on GET requests as well. return; } $response = $event->getResponse(); diff --git a/src/HttpMiddleware/AssetHttpMiddleware.php b/src/HttpMiddleware/AssetHttpMiddleware.php index 5175070..a021b53 100644 --- a/src/HttpMiddleware/AssetHttpMiddleware.php +++ b/src/HttpMiddleware/AssetHttpMiddleware.php @@ -113,13 +113,14 @@ public function handle( ) : Response { $response = $this->httpKernel->handle($request, $type, $catch); - if ($this->isXmlResponse($response)) { - return $response; - } // Nothing to do if asset path is not configured. if (!$this->proxyManager->isConfigured(ProxyManagerInterface::ASSET_PATH)) { return $response; } + + if ($this->isXmlResponse($response)) { + return $response; + } $content = $response->getContent(); if (!is_string($content)) { From ed1fdd1ef02c4baa952b9a002f273198cde31403 Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 7 Apr 2022 10:06:16 +0300 Subject: [PATCH 3/6] Revert xml response change --- src/HttpMiddleware/AssetHttpMiddleware.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/HttpMiddleware/AssetHttpMiddleware.php b/src/HttpMiddleware/AssetHttpMiddleware.php index a021b53..9d2bc92 100644 --- a/src/HttpMiddleware/AssetHttpMiddleware.php +++ b/src/HttpMiddleware/AssetHttpMiddleware.php @@ -113,14 +113,15 @@ public function handle( ) : Response { $response = $this->httpKernel->handle($request, $type, $catch); - // Nothing to do if asset path is not configured. - if (!$this->proxyManager->isConfigured(ProxyManagerInterface::ASSET_PATH)) { + if ($this->isXmlResponse($response)) { return $response; } - if ($this->isXmlResponse($response)) { + // Nothing to do if asset path is not configured. + if (!$this->proxyManager->isConfigured(ProxyManagerInterface::ASSET_PATH)) { return $response; } + $content = $response->getContent(); if (!is_string($content)) { From 95968273b32fb6b1b2d1f971558c5443d3928b2e Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 7 Apr 2022 10:06:47 +0300 Subject: [PATCH 4/6] Negate request method check --- src/EventSubscriber/RedirectResponseSubscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventSubscriber/RedirectResponseSubscriber.php b/src/EventSubscriber/RedirectResponseSubscriber.php index 1611b06..9ed89e5 100644 --- a/src/EventSubscriber/RedirectResponseSubscriber.php +++ b/src/EventSubscriber/RedirectResponseSubscriber.php @@ -76,7 +76,7 @@ public function onResponse(ResponseEvent $event) : void { if ( !$this->validProxyDomains || !$this->proxyManager->isConfigured(ProxyManagerInterface::DEFAULT_PROXY_DOMAIN) || - $event->getRequest()->isMethod('GET') + !$event->getRequest()->isMethod('GET') ) { // Nothing to do if default proxy domain is not defined. // Only redirect on GET requests as well. From e22e27d79b1cf3e040572428bcd4740f7eac9f15 Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 7 Apr 2022 10:21:03 +0300 Subject: [PATCH 5/6] Added test for post requests --- .../Unit/RedirectResponseSubscriberTest.php | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/src/Unit/RedirectResponseSubscriberTest.php b/tests/src/Unit/RedirectResponseSubscriberTest.php index 1085879..66cf36f 100644 --- a/tests/src/Unit/RedirectResponseSubscriberTest.php +++ b/tests/src/Unit/RedirectResponseSubscriberTest.php @@ -59,6 +59,33 @@ public function testNoProxyDomain() : void { $sut->onResponse($event->reveal()); } + /** + * Tests that response stays intact for non-get requests. + * + * @covers ::onResponse + * @covers ::__construct + */ + public function testPostRequestResponse() : void { + $proxyManagerMock = $this->prophesize(ProxyManagerInterface::class); + $proxyManagerMock->isConfigured(ProxyManagerInterface::DEFAULT_PROXY_DOMAIN) + ->shouldBeCalled() + ->willReturn(TRUE); + $request = $this->prophesize(Request::class); + $request->isMethod('GET') + ->willReturn(FALSE); + + $response = $this->prophesize(RedirectResponse::class); + $event = new ResponseEvent( + $this->createMock(HttpKernelInterface::class), + $request->reveal(), + HttpKernelInterface::MASTER_REQUEST, + $response->reveal() + ); + // Make sure response stays intact when the request method is not GET. + $sut = new RedirectResponseSubscriber($proxyManagerMock->reveal(), ['www.hel.fi']); + $sut->onResponse($event); + } + /** * Tests onResponse() with RedirectResponse. * @@ -83,9 +110,13 @@ public function testRedirectResponse() : void { $response->getTargetUrl() ->willReturn('http://localhost:8888/test?x=1'); + $request = $this->prophesize(Request::class); + $request->isMethod('GET') + ->willReturn(TRUE); + $event = new ResponseEvent( $this->createMock(HttpKernelInterface::class), - $this->createMock(Request::class), + $request->reveal(), HttpKernelInterface::MASTER_REQUEST, $response->reveal() ); @@ -118,6 +149,9 @@ public function testResponseRedirect() : void { $request->getSchemeAndHttpHost() ->shouldBeCalled() ->willReturn('http://localhost:8888'); + $request->isMethod('GET') + ->shouldBeCalled() + ->willReturn(TRUE); $request->getRequestUri() ->shouldBeCalled() ->willReturn('/test?x=1'); From e511f136862445eea1c213fa9f58dd559291bdc5 Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 7 Apr 2022 10:22:33 +0300 Subject: [PATCH 6/6] Fixed test --- tests/src/Unit/RedirectResponseSubscriberTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/src/Unit/RedirectResponseSubscriberTest.php b/tests/src/Unit/RedirectResponseSubscriberTest.php index 66cf36f..c504128 100644 --- a/tests/src/Unit/RedirectResponseSubscriberTest.php +++ b/tests/src/Unit/RedirectResponseSubscriberTest.php @@ -75,6 +75,8 @@ public function testPostRequestResponse() : void { ->willReturn(FALSE); $response = $this->prophesize(RedirectResponse::class); + $response->getTargetUrl() + ->shouldNotBeCalled(); $event = new ResponseEvent( $this->createMock(HttpKernelInterface::class), $request->reveal(),