onResponse example

'drupalSettings' => ['foo' => 'bar'],
      ]);

      $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor');
      $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
      $event = new ResponseEvent(
        \Drupal::service('http_kernel'),
        new Request(),
        HttpKernelInterface::MAIN_REQUEST,
        $response
      );
      $subscriber->onResponse($event);
      $expected = [
        'command' => 'settings',
      ];
      $this->assertCommand($response->getCommands()$expected$message);
    };

    $config = $this->config('system.performance');

    $config->set('js.preprocess', FALSE)->save();
    $assert('Settings command exists when JS aggregation is disabled.');

    
$path_matcher->reveal(),
      $language_manager
    );

    // A link that might otherwise be set 'active'.     $content = '<a data-drupal-link-system-path="other-page">Other page</a>';

    // Assert response with non-html content type gets ignored.     $response = new Response();
    $response->setContent($content);
    $response->headers->get('Content-Type', 'application/json');
    $subscriber->onResponse(new ResponseEvent(
      $this->prophesize(KernelInterface::class)->reveal(),
      $request_stack->getCurrentRequest(),
      HttpKernelInterface::MAIN_REQUEST,
      $response
    ));
    $this->assertSame($response->getContent()$content);
  }

  /** * Tests certain response types ignored by the ActiveLinkResponseFilter. * * @covers ::onResponse */

    /** * @dataProvider provideResponses */
    public function testInvoke(?string $expected, array $responseArgs)
    {
        $response = new Response(...$responseArgs);
        $listener = new DisallowRobotsIndexingListener();

        $event = new ResponseEvent($this->createMock(HttpKernelInterface::class)new Request(), KernelInterface::MAIN_REQUEST, $response);

        $listener->onResponse($event);

        $this->assertSame($expected$response->headers->get('X-Robots-Tag'), 'Header doesn\'t match expectations');
    }

    public static function provideResponses(): iterable
    {
        yield 'No header' => ['noindex', []];

        yield 'Header already set' => [
            'something else',
            ['', 204, ['X-Robots-Tag' => 'something else']],
        ];
$response = new AjaxResponse([]);
    $response->headers->set('Content-Type', 'application/json; charset=utf-8');

    $ajax_response_attachments_processor = $this->createMock('\Drupal\Core\Render\AttachmentsResponseProcessorInterface');
    $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
    $event = new ResponseEvent(
      $this->createMock('\Symfony\Component\HttpKernel\HttpKernelInterface'),
      $request,
      HttpKernelInterface::MAIN_REQUEST,
      $response
    );
    $subscriber->onResponse($event);
    $this->assertEquals('text/html; charset=utf-8', $response->headers->get('Content-Type'));
    $this->assertEquals('<textarea>[]</textarea>', $response->getContent());
  }

}
$request = new Request();
    $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => 'restplugin']['_format' => 'json']));

    $handler_response = new ResourceResponse($data);
    $resource_response_subscriber = $this->getFunctioningResourceResponseSubscriber($route_match);
    $event = new ResponseEvent(
      $this->prophesize(HttpKernelInterface::class)->reveal(),
      $request,
      HttpKernelInterface::MAIN_REQUEST,
      $handler_response
    );
    $resource_response_subscriber->onResponse($event);

    // Content is a serialized version of the data we provided.     $this->assertEquals($expected_response !== FALSE ? $expected_response : Json::encode($data)$event->getResponse()->getContent());
  }

  public function providerTestSerialization() {
    return [
      // The default data for \Drupal\rest\ResourceResponse.       'default' => [NULL, ''],
      'empty string' => [''],
      'simple string' => ['string'],
      
public function testOnResponse($content$expected_content) {
    $event = new ResponseEvent(
      $this->prophesize(HttpKernelInterface::class)->reveal(),
      Request::create('/'),
      HttpKernelInterface::MAIN_REQUEST,
      new Response($content, 200, [
        'Content-Type' => 'application/rss+xml',
      ])
    );

    $url_filter = new RssResponseRelativeUrlFilter();
    $url_filter->onResponse($event);

    $this->assertEquals($expected_content$event->getResponse()->getContent());
  }

}
Home | Imprint | This part of the site doesn't use cookies.