You are here

public function ResourceResponseSubscriberTest::testOnResponseWithCacheableResponse in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php \Drupal\Tests\rest\Unit\EventSubscriber\ResourceResponseSubscriberTest::testOnResponseWithCacheableResponse()
  2. 10 core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php \Drupal\Tests\rest\Unit\EventSubscriber\ResourceResponseSubscriberTest::testOnResponseWithCacheableResponse()

@covers ::onResponse @covers ::getResponseFormat @covers ::renderResponseBody @covers ::flattenResponse

@dataProvider providerTestResponseFormat

File

core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php, line 118

Class

ResourceResponseSubscriberTest
@coversDefaultClass \Drupal\rest\EventSubscriber\ResourceResponseSubscriber @group rest

Namespace

Drupal\Tests\rest\Unit\EventSubscriber

Code

public function testOnResponseWithCacheableResponse($methods, array $supported_response_formats, array $supported_request_formats, $request_format, array $request_headers, $request_body, $expected_response_format, $expected_response_content_type, $expected_response_content) {
  foreach ($request_headers as $key => $value) {
    unset($request_headers[$key]);
    $key = strtoupper(str_replace('-', '_', $key));
    $request_headers[$key] = $value;
  }
  foreach ($methods as $method) {
    $request = Request::create('/rest/test', $method, [], [], [], $request_headers, $request_body);

    // \Drupal\Core\StackMiddleware\NegotiationMiddleware normally takes care
    // of this so we'll hard code it here.
    if ($request_format) {
      $request
        ->setRequestFormat($request_format);
    }
    $route_requirements = $this
      ->generateRouteRequirements($supported_response_formats, $supported_request_formats);
    $route_match = new RouteMatch('test', new Route('/rest/test', [
      '_rest_resource_config' => $this
        ->randomMachineName(),
    ], $route_requirements));

    // The RequestHandler must return a ResourceResponseInterface object.
    $handler_response = new ResourceResponse($method !== 'DELETE' ? [
      'REST' => 'Drupal',
    ] : NULL);
    $this
      ->assertInstanceOf(ResourceResponseInterface::class, $handler_response);
    $this
      ->assertInstanceOf(CacheableResponseInterface::class, $handler_response);

    // The ResourceResponseSubscriber must then generate a response body and
    // transform it to a plain CacheableResponse object.
    $resource_response_subscriber = $this
      ->getFunctioningResourceResponseSubscriber($route_match);
    $event = new FilterResponseEvent($this
      ->prophesize(HttpKernelInterface::class)
      ->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $handler_response);
    $resource_response_subscriber
      ->onResponse($event);
    $final_response = $event
      ->getResponse();
    $this
      ->assertNotInstanceOf(ResourceResponseInterface::class, $final_response);
    $this
      ->assertInstanceOf(CacheableResponseInterface::class, $final_response);
    $this
      ->assertSame($expected_response_content_type, $final_response->headers
      ->get('Content-Type'));
    $this
      ->assertEquals($expected_response_content, $final_response
      ->getContent());
  }
}