The onKernelResponse method must be connected to the kernel.response event. The WDT is only injected on well-formed HTML (with a proper tag). This means that the WDT is never included in sub-requests or ESI requests.
Author: Fabien Potencier (fabien@symfony.com)
Inheritance: implements Symfony\Component\EventDispatcher\EventSubscriberInterface
 /**
  * @depends testToolbarIsInjected
  */
 public function testToolbarIsNotInjectedOnNonHtmlRequests()
 {
     $response = new Response('<html><head></head><body></body></html>');
     $response->headers->set('X-Debug-Token', 'xxxxxxxx');
     $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'json'), HttpKernelInterface::MASTER_REQUEST, $response);
     $listener = new WebDebugToolbarListener($this->getTemplatingMock());
     $listener->onKernelResponse($event);
     $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
 }
 public static function getSubscribedEvents()
 {
     return array_merge(parent::getSubscribedEvents(), [KernelEvents::CONTROLLER => array('onKernelController')]);
 }
 public function testThrowingUrlGenerator()
 {
     $response = new Response();
     $response->headers->set('X-Debug-Token', 'xxxxxxxx');
     $urlGenerator = $this->getUrlGeneratorMock();
     $urlGenerator->expects($this->once())->method('generate')->with('_profiler', array('token' => 'xxxxxxxx'))->will($this->throwException(new \Exception('foo')));
     $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
     $listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, 'bottom', $urlGenerator);
     $listener->onKernelResponse($event);
     $this->assertEquals('Exception: foo', $response->headers->get('X-Debug-Error'));
 }