public function ResourceResponseSubscriberTest::testOnResponseWithUncacheableResponse in Drupal 9
Same name and namespace in other branches
- 8 core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php \Drupal\Tests\rest\Unit\EventSubscriber\ResourceResponseSubscriberTest::testOnResponseWithUncacheableResponse()
@covers ::onResponse @covers ::getResponseFormat @covers ::renderResponseBody @covers ::flattenResponse
@dataProvider providerTestResponseFormat
File
- core/
modules/ rest/ tests/ src/ Unit/ EventSubscriber/ ResourceResponseSubscriberTest.php, line 169
Class
- ResourceResponseSubscriberTest
- @coversDefaultClass \Drupal\rest\EventSubscriber\ResourceResponseSubscriber @group rest
Namespace
Drupal\Tests\rest\Unit\EventSubscriberCode
public function testOnResponseWithUncacheableResponse($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 ModifiedResourceResponse($method !== 'DELETE' ? [
'REST' => 'Drupal',
] : NULL);
$this
->assertInstanceOf(ResourceResponseInterface::class, $handler_response);
$this
->assertNotInstanceOf(CacheableResponseInterface::class, $handler_response);
// The ResourceResponseSubscriber must then generate a response body and
// transform it to a plain Response object.
$resource_response_subscriber = $this
->getFunctioningResourceResponseSubscriber($route_match);
$event = new ResponseEvent($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
->assertNotInstanceOf(CacheableResponseInterface::class, $final_response);
$this
->assertSame($expected_response_content_type, $final_response->headers
->get('Content-Type'));
$this
->assertEquals($expected_response_content, $final_response
->getContent());
}
}