Skip to content

Commit

Permalink
Merge pull request #459 from piotrantosik/patch-4
Browse files Browse the repository at this point in the history
Use allowCustomResponseCode instead X-Status-Code
  • Loading branch information
makasim authored Feb 14, 2018
2 parents c260ba1 + 80a6a71 commit 17ee0b6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
10 changes: 8 additions & 2 deletions EventListener/ReplyToHttpResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ public function onKernelException(GetResponseForExceptionEvent $event)
}

$response = $this->replyToSymfonyResponseConverter->convert($event->getException());
if (false == $response->headers->has('X-Status-Code')) {
$response->headers->set('X-Status-Code', $response->getStatusCode());

// BC for SF < 3.3
if (!method_exists($event, 'allowCustomResponseCode')) {
if (false === $response->headers->has('X-Status-Code')) {
$response->headers->set('X-Status-Code', $response->getStatusCode());
}
} else {
$event->allowCustomResponseCode();
}

$event->setResponse($response);
Expand Down
54 changes: 54 additions & 0 deletions Tests/EventListener/ReplyToHttpResponseListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public function shouldSetXStatusFromResponseStatusCode()
$reply
);

// BC for SF < 3.3
if (method_exists($event, 'allowCustomResponseCode')) {
$this->markTestSkipped('BC for SF < 3.3');
}

$converterMock = $this->createReplyToSymfonyResponseConverterMock();
$converterMock
->expects($this->once())
Expand Down Expand Up @@ -132,6 +137,11 @@ public function shouldNotSetXStatusIfAlreadySet()
$reply
);

// BC for SF < 3.3
if (method_exists($event, 'allowCustomResponseCode')) {
$this->markTestSkipped('BC for SF < 3.3');
}

$converterMock = $this->createReplyToSymfonyResponseConverterMock();
$converterMock
->expects($this->once())
Expand All @@ -149,6 +159,50 @@ public function shouldNotSetXStatusIfAlreadySet()
$this->assertEquals(666, $event->getResponse()->headers->get('X-Status-Code'));
}

/**
* @test
*/
public function shouldCallAllowCustomResponseCode()
{
$reply = new HttpRedirect('/foo/bar');
$response = new Response('', 302);

$eventMock = $this
->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent')
->setMethods(null)
->setConstructorArgs([$this->createHttpKernelMock(),
new Request,
Kernel::MASTER_REQUEST,
$reply])
->getMock();

// BC for SF < 3.3
if (!method_exists($eventMock, 'allowCustomResponseCode')) {
$this->markTestSkipped('BC for SF < 3.3');
}

$converterMock = $this->createReplyToSymfonyResponseConverterMock();
$converterMock
->expects($this->once())
->method('convert')
->with($this->identicalTo($reply))
->will($this->returnValue($response))
;

$eventMock
->expects($this->once())
->method('allowCustomResponseCode')
;

$listener = new ReplyToHttpResponseListener($converterMock);

$listener->onKernelException($eventMock);

$this->assertInstanceOf(Response::class, $eventMock->getResponse());
$this->assertEquals(302, $eventMock->getResponse()->getStatusCode());
$this->assertEquals(true, $eventMock->isAllowingCustomResponseCode());
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|ReplyToSymfonyResponseConverter
*/
Expand Down

0 comments on commit 17ee0b6

Please sign in to comment.