3 namespace Drupal\Tests\Core\EventSubscriber;
5 use Drupal\Core\EventSubscriber\ExceptionJsonSubscriber;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\JsonResponse;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
10 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
11 use Symfony\Component\HttpKernel\HttpKernelInterface;
14 * @coversDefaultClass \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber
15 * @group EventSubscriber
17 class ExceptionJsonSubscriberTest extends UnitTestCase {
22 public function testOn4xx() {
23 $kernel = $this->prophesize(HttpKernelInterface::class);
24 $request = Request::create('/test');
25 $e = new MethodNotAllowedHttpException(['POST', 'PUT'], 'test message');
26 $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e);
27 $subscriber = new ExceptionJsonSubscriber();
28 $subscriber->on4xx($event);
29 $response = $event->getResponse();
31 $this->assertInstanceOf(JsonResponse::class, $response);
32 $this->assertEquals('{"message":"test message"}', $response->getContent());
33 $this->assertEquals(405, $response->getStatusCode());
34 $this->assertEquals('POST, PUT', $response->headers->get('Allow'));
35 $this->assertEquals('application/json', $response->headers->get('Content-Type'));