From 28a102a876c4dd4f3b858399c62dedf281bf7463 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 12 Aug 2013 22:09:34 +0300 Subject: [PATCH] [interactive request] set correct status from response, now it always set 500 http status --- EventListener/InteractiveRequestListener.php | 17 ++-- .../InteractiveRequestListenerTest.php | 86 ++++++++++++++++++- 2 files changed, 91 insertions(+), 12 deletions(-) diff --git a/EventListener/InteractiveRequestListener.php b/EventListener/InteractiveRequestListener.php index 6a51cce7..7046c799 100644 --- a/EventListener/InteractiveRequestListener.php +++ b/EventListener/InteractiveRequestListener.php @@ -23,20 +23,19 @@ public function onKernelException(GetResponseForExceptionEvent $event) if ($interactiveRequest instanceof ResponseInteractiveRequest) { $event->setResponse($interactiveRequest->getResponse()); - $event->stopPropagation(); - - return; - } - - if ($interactiveRequest instanceof RedirectUrlInteractiveRequest) { + } else if ($interactiveRequest instanceof RedirectUrlInteractiveRequest) { $event->setResponse(new RedirectResponse($interactiveRequest->getUrl())); - $event->stopPropagation(); - + } + + if ($event->getResponse()) { + if (false == $event->getResponse()->headers->has('X-Status-Code')) { + $event->getResponse()->headers->set('X-Status-Code', $event->getResponse()->getStatusCode()); + } + return; } $ro = new \ReflectionObject($interactiveRequest); - $event->setException(new LogicException( sprintf('Cannot convert interactive request %s to symfony response.', $ro->getShortName()), null, diff --git a/Tests/EventListener/InteractiveRequestListenerTest.php b/Tests/EventListener/InteractiveRequestListenerTest.php index 7cda51d0..2f14d57e 100644 --- a/Tests/EventListener/InteractiveRequestListenerTest.php +++ b/Tests/EventListener/InteractiveRequestListenerTest.php @@ -69,6 +69,29 @@ public function shouldSetRedirectResponseIfExceptionInstanceOfRedirectUrlInterac $this->assertSame($interactiveRequest, $event->getException()); } + /** + * @test + */ + public function shouldSetXStatusCodeWhenExceptionInstanceOfRedirectUrlInteractiveRequest() + { + $interactiveRequest = new RedirectUrlInteractiveRequest('/foo/bar'); + + $event = new GetResponseForExceptionEvent( + $this->createHttpKernelMock(), + new Request, + 'requestType', + $interactiveRequest + ); + + $listener = new InteractiveRequestListener; + + $listener->onKernelException($event); + + $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $event->getResponse()); + $this->assertTrue($event->getResponse()->headers->has('X-Status-Code')); + $this->assertEquals(302, $event->getResponse()->headers->get('X-Status-Code')); + } + /** * @test */ @@ -96,11 +119,43 @@ public function shouldSetResponseIfExceptionInstanceOfResponseInteractiveRequest /** * @test */ - public function shouldChangeInteractiveRequestToLogicExceptionIfNotSupported() + public function shouldSetXStatusCodeWhenExceptionInstanceOfResponseInteractiveRequest() { - $expectedResponse = new Response('foobar'); + $expectedStatus = 555; + + $response = new Response('foobar', $expectedStatus); + + $interactiveRequest = new ResponseInteractiveRequest($response); + + $event = new GetResponseForExceptionEvent( + $this->createHttpKernelMock(), + new Request, + 'requestType', + $interactiveRequest + ); + + $listener = new InteractiveRequestListener; + + $listener->onKernelException($event); + + //guard + $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $event->getResponse()); + + $this->assertTrue($event->getResponse()->headers->has('X-Status-Code')); + $this->assertEquals(555, $event->getResponse()->headers->get('X-Status-Code')); + } + + /** + * @test + */ + public function shouldNotSetXStatusCodeIfAlreadySetWhenExceptionInstanceOfResponseInteractiveRequest() + { + $expectedStatus = 555; + + $response = new Response('foobar', $expectedStatus); + $response->headers->set('X-Status-Code', 666); - $interactiveRequest = $this->getMock('Payum\Request\BaseInteractiveRequest'); + $interactiveRequest = new ResponseInteractiveRequest($response); $event = new GetResponseForExceptionEvent( $this->createHttpKernelMock(), @@ -113,6 +168,31 @@ public function shouldChangeInteractiveRequestToLogicExceptionIfNotSupported() $listener->onKernelException($event); + //guard + $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $event->getResponse()); + + $this->assertTrue($event->getResponse()->headers->has('X-Status-Code')); + $this->assertEquals(666, $event->getResponse()->headers->get('X-Status-Code')); + } + + /** + * @test + */ + public function shouldChangeInteractiveRequestToLogicExceptionIfNotSupported() + { + $notSupportedInteractiveRequest = $this->getMock('Payum\Request\BaseInteractiveRequest'); + + $event = new GetResponseForExceptionEvent( + $this->createHttpKernelMock(), + new Request, + 'requestType', + $notSupportedInteractiveRequest + ); + + $listener = new InteractiveRequestListener; + + $listener->onKernelException($event); + $this->assertNull($event->getResponse()); $this->assertInstanceOf('Payum\Exception\LogicException', $event->getException()); $this->assertStringStartsWith(