Issue #2835626 by 20th, vijaycs85, himanshu-dixit, naveenvalecha, Jo Fitzgerald, Wim Leers, xjm, dawehner, catch, alexpott: Rename & document Drupal\Core\EventSubscriber\AcceptNegotiation406 KernelEvents::VIEW event subscriber
parent
45e8efc948
commit
823aae4cb0
|
@ -1044,8 +1044,8 @@ services:
|
||||||
arguments: ['@class_resolver', '@current_route_match', '%main_content_renderers%']
|
arguments: ['@class_resolver', '@current_route_match', '%main_content_renderers%']
|
||||||
tags:
|
tags:
|
||||||
- { name: event_subscriber }
|
- { name: event_subscriber }
|
||||||
accept_negotiation_406:
|
renderer_non_html:
|
||||||
class: Drupal\Core\EventSubscriber\AcceptNegotiation406
|
class: Drupal\Core\EventSubscriber\RenderArrayNonHtmlSubscriber
|
||||||
tags:
|
tags:
|
||||||
- { name: event_subscriber }
|
- { name: event_subscriber }
|
||||||
main_content_renderer.html:
|
main_content_renderer.html:
|
||||||
|
|
|
@ -8,25 +8,23 @@ use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
|
||||||
use Symfony\Component\HttpKernel\KernelEvents;
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* View subscriber rendering a 406 if we could not route or render a request.
|
* Throws 406 if requesting non-HTML format and controller returns render array.
|
||||||
*
|
|
||||||
* @todo fix or replace this in https://www.drupal.org/node/2364011
|
|
||||||
*/
|
*/
|
||||||
class AcceptNegotiation406 implements EventSubscriberInterface {
|
class RenderArrayNonHtmlSubscriber implements EventSubscriberInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throws an HTTP 406 error if we get this far, which we normally shouldn't.
|
* Throws an HTTP 406 error if client requested a non-HTML format.
|
||||||
*
|
*
|
||||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
|
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
|
||||||
* The event to process.
|
* The event to process.
|
||||||
*/
|
*/
|
||||||
public function onViewDetect406(GetResponseForControllerResultEvent $event) {
|
public function onRespond(GetResponseForControllerResultEvent $event) {
|
||||||
$request = $event->getRequest();
|
$request = $event->getRequest();
|
||||||
$result = $event->getControllerResult();
|
$result = $event->getControllerResult();
|
||||||
|
|
||||||
// If this is a render array then we assume that the router went with the
|
// If this is a render array then we assume that the router went with the
|
||||||
// generic controller and not one with a format. If the format requested is
|
// generic controller and not one with a format. If the format requested is
|
||||||
// not HTML though we can also assume that the requested format is invalid
|
// not HTML though, we can also assume that the requested format is invalid
|
||||||
// so we provide a 406 response.
|
// so we provide a 406 response.
|
||||||
if (is_array($result) && $request->getRequestFormat() !== 'html') {
|
if (is_array($result) && $request->getRequestFormat() !== 'html') {
|
||||||
throw new NotAcceptableHttpException('Not acceptable format: ' . $request->getRequestFormat());
|
throw new NotAcceptableHttpException('Not acceptable format: ' . $request->getRequestFormat());
|
||||||
|
@ -37,8 +35,7 @@ class AcceptNegotiation406 implements EventSubscriberInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public static function getSubscribedEvents() {
|
public static function getSubscribedEvents() {
|
||||||
$events[KernelEvents::VIEW][] = ['onViewDetect406', -10];
|
$events[KernelEvents::VIEW][] = ['onRespond', -10];
|
||||||
|
|
||||||
return $events;
|
return $events;
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,7 @@ class DynamicPageCacheSubscriber implements EventSubscriberInterface {
|
||||||
// may be returning a domain object that a KernelEvents::VIEW subscriber
|
// may be returning a domain object that a KernelEvents::VIEW subscriber
|
||||||
// must turn into an actual response, but perhaps a format is being
|
// must turn into an actual response, but perhaps a format is being
|
||||||
// requested that the subscriber does not support.
|
// requested that the subscriber does not support.
|
||||||
// @see \Drupal\Core\EventSubscriber\AcceptNegotiation406::onViewDetect406()
|
// @see \Drupal\Core\EventSubscriber\RenderArrayNonHtmlSubscriber::onResponse()
|
||||||
'request_format',
|
'request_format',
|
||||||
],
|
],
|
||||||
'bin' => 'dynamic_page_cache',
|
'bin' => 'dynamic_page_cache',
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
name: 'Array rendering for non-HTML requests subscriber test'
|
||||||
|
type: module
|
||||||
|
description: 'Support module for RenderArrayNonHtmlSubscriberTest.'
|
||||||
|
package: Testing
|
||||||
|
version: VERSION
|
||||||
|
core: 8.x
|
|
@ -0,0 +1,13 @@
|
||||||
|
render_array_non_html_subscriber_test.raw_string:
|
||||||
|
path: '/render_array_non_html_subscriber_test/raw_string'
|
||||||
|
defaults:
|
||||||
|
_controller: '\Drupal\render_array_non_html_subscriber_test\RenderArrayNonHtmlSubscriberTestController::rawString'
|
||||||
|
requirements:
|
||||||
|
_access: 'TRUE'
|
||||||
|
|
||||||
|
render_array_non_html_subscriber_test.render_array:
|
||||||
|
path: '/render_array_non_html_subscriber_test/render_array'
|
||||||
|
defaults:
|
||||||
|
_controller: '\Drupal\render_array_non_html_subscriber_test\RenderArrayNonHtmlSubscriberTestController::renderArray'
|
||||||
|
requirements:
|
||||||
|
_access: 'TRUE'
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\render_array_non_html_subscriber_test;
|
||||||
|
|
||||||
|
use Drupal\Core\Controller\ControllerBase;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
class RenderArrayNonHtmlSubscriberTestController extends ControllerBase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function rawString() {
|
||||||
|
return new Response($this->t('Raw controller response.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function renderArray() {
|
||||||
|
return [
|
||||||
|
'#type' => 'html_tag',
|
||||||
|
'#tag' => 'p',
|
||||||
|
'#value' => $this->t('Controller response successfully rendered.'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\Tests\system\Functional\Render;
|
||||||
|
|
||||||
|
use Drupal\Core\Url;
|
||||||
|
use Drupal\Tests\BrowserTestBase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Functional test verifying that render array throws 406 for non-HTML requests.
|
||||||
|
*
|
||||||
|
* @group Render
|
||||||
|
*/
|
||||||
|
class RenderArrayNonHtmlSubscriberTest extends BrowserTestBase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modules to enable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public static $modules = ['render_array_non_html_subscriber_test'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests handling of responses by events subscriber.
|
||||||
|
*/
|
||||||
|
public function testResponses() {
|
||||||
|
// Test that event subscriber does not interfere with normal requests.
|
||||||
|
$url = Url::fromRoute('render_array_non_html_subscriber_test.render_array');
|
||||||
|
|
||||||
|
$this->drupalGet($url);
|
||||||
|
$this->assertSession()->statusCodeEquals(200);
|
||||||
|
$this->assertRaw(t('Controller response successfully rendered.'));
|
||||||
|
|
||||||
|
// Test that correct response code is returned for any non-HTML format.
|
||||||
|
foreach (['json', 'hal+json', 'xml', 'foo'] as $format) {
|
||||||
|
$url = Url::fromRoute('render_array_non_html_subscriber_test.render_array', [
|
||||||
|
'_format' => $format,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->drupalGet($url);
|
||||||
|
$this->assertSession()->statusCodeEquals(406);
|
||||||
|
$this->assertNoRaw(t('Controller response successfully rendered.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that event subscriber does not interfere with raw string responses.
|
||||||
|
$url = Url::fromRoute('render_array_non_html_subscriber_test.raw_string', [
|
||||||
|
'_format' => 'foo',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->drupalGet($url);
|
||||||
|
$this->assertSession()->statusCodeEquals(200);
|
||||||
|
$this->assertRaw(t('Raw controller response.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue