diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php b/core/modules/serialization/tests/src/Unit/EventSubscriber/DefaultExceptionSubscriberTest.php similarity index 51% copy from core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php copy to core/modules/serialization/tests/src/Unit/EventSubscriber/DefaultExceptionSubscriberTest.php index 5dca44469c..e20f6f7fe2 100644 --- a/core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php +++ b/core/modules/serialization/tests/src/Unit/EventSubscriber/DefaultExceptionSubscriberTest.php @@ -1,43 +1,42 @@ getConfigFactoryStub(); - - // Format 'bananas' requested, yet only 'json' allowed. + public function testOn4xx() { $kernel = $this->prophesize(HttpKernelInterface::class); - $request = Request::create('/test?_format=bananas'); + $request = Request::create('/test'); + $request->setRequestFormat('json'); + $e = new MethodNotAllowedHttpException(['POST', 'PUT'], 'test message'); $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e); - $subscriber = new DefaultExceptionSubscriber($config_factory); - $subscriber->onException($event); + $subscriber = new DefaultExceptionSubscriber(new Serializer([], [new JsonEncoder()]), []); + $subscriber->on4xx($event); $response = $event->getResponse(); $this->assertInstanceOf(Response::class, $response); - $this->assertEquals('test message', $response->getContent()); + $this->assertEquals('{"message":"test message"}', $response->getContent()); $this->assertEquals(405, $response->getStatusCode()); $this->assertEquals('POST, PUT', $response->headers->get('Allow')); - // Also check that that text/plain content type was added. - $this->assertEquals('text/plain', $response->headers->get('Content-Type')); + $this->assertEquals('application/json', $response->headers->get('Content-Type')); } } diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php index 5dca44469c..c44be6136e 100644 --- a/core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php +++ b/core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php @@ -23,9 +23,8 @@ class DefaultExceptionSubscriberTest extends UnitTestCase { public function testOnExceptionWithUnknownFormat() { $config_factory = $this->getConfigFactoryStub(); - // Format 'bananas' requested, yet only 'json' allowed. $kernel = $this->prophesize(HttpKernelInterface::class); - $request = Request::create('/test?_format=bananas'); + $request = Request::create('/test'); $e = new MethodNotAllowedHttpException(['POST', 'PUT'], 'test message'); $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e); $subscriber = new DefaultExceptionSubscriber($config_factory); diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/ExceptionJsonSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/ExceptionJsonSubscriberTest.php new file mode 100644 index 0000000000..86c6d86bfd --- /dev/null +++ b/core/tests/Drupal/Tests/Core/EventSubscriber/ExceptionJsonSubscriberTest.php @@ -0,0 +1,38 @@ +prophesize(HttpKernelInterface::class); + $request = Request::create('/test'); + $e = new MethodNotAllowedHttpException(['POST', 'PUT'], 'test message'); + $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e); + $subscriber = new ExceptionJsonSubscriber(); + $subscriber->on4xx($event); + $response = $event->getResponse(); + + $this->assertInstanceOf(JsonResponse::class, $response); + $this->assertEquals('{"message":"test message"}', $response->getContent()); + $this->assertEquals(405, $response->getStatusCode()); + $this->assertEquals('POST, PUT', $response->headers->get('Allow')); + $this->assertEquals('application/json', $response->headers->get('Content-Type')); + } + +}