/**
  * Test that given structured data, the request handler will serialize it.
  *
  * @dataProvider providerTestSerialization
  * @covers ::handle
  */
 public function testSerialization($data)
 {
     $request = new Request();
     $route_match = new RouteMatch('test', new Route('/rest/test', ['_plugin' => 'restplugin', '_format' => 'json']));
     $resource = $this->prophesize(StubRequestHandlerResourcePlugin::class);
     // Setup stub plugin manager that will return our plugin.
     $stub = $this->prophesize(ResourcePluginManager::class);
     $stub->getInstance(['id' => 'restplugin'])->willReturn($resource->reveal());
     $this->container->set('plugin.manager.rest', $stub->reveal());
     $response = new ResourceResponse($data);
     $resource->get(NULL, $request)->willReturn($response);
     $handler_response = $this->requestHandler->handle($route_match, $request);
     // Content is a serialized version of the data we provided.
     $this->assertEquals(json_encode($data), $handler_response->getContent());
 }
예제 #2
0
 /**
  * @covers ::getResponseFormat
  *
  * Note this does *not* need to test formats being requested that are not
  * accepted by the server, because the routing system would have already
  * prevented those from reaching RequestHandler.
  *
  * @param string[] $methods
  *   The HTTP methods to test.
  * @param string[] $supported_formats
  *   The supported formats for the REST route to be tested.
  * @param string|false $request_format
  *   The value for the ?_format URL query argument, if any.
  * @param string[] $request_headers
  *   The request headers to send, if any.
  * @param string|null $request_body
  *   The request body to send, if any.
  * @param string|null $expected_response_content_type
  *   The expected MIME type of the response, if any.
  * @param string $expected_response_content
  *   The expected content of the response.
  *
  * @dataProvider providerTestResponseFormat
  */
 public function testResponseFormat($methods, array $supported_formats, $request_format, array $request_headers, $request_body, $expected_response_content_type, $expected_response_content)
 {
     $rest_config_name = $this->randomMachineName();
     $parameters = [];
     if ($request_format !== FALSE) {
         $parameters['_format'] = $request_format;
     }
     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, $parameters, [], [], $request_headers, $request_body);
         $route_requirement_key_format = $request->isMethodSafe() ? '_format' : '_content_type_format';
         $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => $rest_config_name], [$route_requirement_key_format => implode('|', $supported_formats)]));
         $resource = $this->prophesize(StubRequestHandlerResourcePlugin::class);
         // Mock the configuration.
         $config = $this->prophesize(RestResourceConfigInterface::class);
         $config->getFormats($method)->willReturn($supported_formats);
         $config->getResourcePlugin()->willReturn($resource->reveal());
         $config->getCacheContexts()->willReturn([]);
         $config->getCacheTags()->willReturn([]);
         $config->getCacheMaxAge()->willReturn(12);
         $this->entityStorage->load($rest_config_name)->willReturn($config->reveal());
         // Mock the resource plugin.
         $response = new ResourceResponse($method !== 'DELETE' ? ['REST' => 'Drupal'] : NULL);
         $resource->getPluginDefinition()->willReturn([]);
         $method_prophecy = new MethodProphecy($resource, strtolower($method), [Argument::any(), $request]);
         $method_prophecy->willReturn($response);
         $resource->addMethodProphecy($method_prophecy);
         // Test the request handler.
         $handler_response = $this->requestHandler->handle($route_match, $request);
         $this->assertSame($expected_response_content_type, $handler_response->headers->get('Content-Type'));
         $this->assertEquals($expected_response_content, $handler_response->getContent());
     }
 }