-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from BigZ/feat/enable-error-body-only-in-debug…
…-mode feature: Obfuscate exceptions that are not http exceptions
- Loading branch information
Showing
6 changed files
with
135 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ language: php | |
php: | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
|
||
install: | ||
- composer install | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace WizardsTest\ObjectManager; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; | ||
use Wizards\RestBundle\Exception\MultiPartHttpException; | ||
use Wizards\RestBundle\Services\FormatOptions; | ||
use Wizards\RestBundle\Subscriber\ExceptionSubscriber; | ||
use Psr\Log\LoggerInterface; | ||
use WizardsRest\Exception\HttpException; | ||
|
||
class ExceptionSubscriberTest extends TestCase | ||
{ | ||
public function testFormatJsonApi404() | ||
{ | ||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger->expects($this->once())->method('log'); | ||
$formatOptions = new FormatOptions('jsonapi'); | ||
$event = $this->createMock(GetResponseForExceptionEvent::class); | ||
$exception = $this->createMock(HttpException::class); | ||
|
||
$exception->method('getStatusCode')->willReturn(404); | ||
$event->method('getException')->willReturn($exception); | ||
$response = new Response( | ||
'{"errors":[{"detail":"Not Found"}]}', | ||
404, | ||
$formatOptions->getFormatSpecificHeaders() | ||
); | ||
$event->expects($this->once())->method('setResponse')->with($response); | ||
|
||
$subscriber = new ExceptionSubscriber($logger, $formatOptions); | ||
$subscriber->onKernelException($event); | ||
} | ||
|
||
public function testFormatJsonApi400() | ||
{ | ||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger->expects($this->once())->method('log'); | ||
$formatOptions = new FormatOptions('jsonapi'); | ||
$event = $this->createMock(GetResponseForExceptionEvent::class); | ||
$exception = new MultiPartHttpException(400, ['one', 'two']); | ||
$event->method('getException')->willReturn($exception); | ||
$response = new Response( | ||
'{"errors":[{"detail":"one"},{"detail":"two"}]}', | ||
400, | ||
$formatOptions->getFormatSpecificHeaders() | ||
); | ||
$event->expects($this->once())->method('setResponse')->with($response); | ||
|
||
$subscriber = new ExceptionSubscriber($logger, $formatOptions); | ||
$subscriber->onKernelException($event); | ||
} | ||
|
||
public function testObfuscateError() | ||
{ | ||
$logger = $this->createMock(LoggerInterface::class); | ||
$formatOptions = new FormatOptions('jsonapi'); | ||
$event = $this->createMock(GetResponseForExceptionEvent::class); | ||
|
||
$exception = new \RuntimeException('internal problems'); | ||
|
||
$event->method('getException')->willReturn($exception); | ||
$response = new Response( | ||
'Internal Server Error', | ||
500, | ||
$formatOptions->getFormatSpecificHeaders() | ||
); | ||
$event->expects($this->once())->method('setResponse')->with($response); | ||
|
||
$subscriber = new ExceptionSubscriber($logger, $formatOptions); | ||
$subscriber->onKernelException($event); | ||
} | ||
} |