setFormat() public method

Associates a format with mime types.
public setFormat ( string $format, string | array $mimeTypes )
$format string The format
$mimeTypes string | array The associated mime types (the preferred one must be the first as it will be used as the content type)
 /**
  * Tests the getContentType() method when a priority format is found.
  *
  * @dataProvider priorityFormatProvider
  * @covers ::getContentType
  */
 public function testAPriorityFormatIsFound($priority, $format)
 {
     $request = new Request();
     $request->setFormat($format['format'], $format['mime_type']);
     $request->headers->set('Accept', sprintf('%s,application/json', $format['mime_type']));
     $this->assertSame($priority, $this->contentNegotiation->getContentType($request));
 }
 /**
  * {@inheritdoc}
  */
 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
 {
     foreach ($this->formats as $format => $mime_type) {
         $request->setFormat($format, $mime_type);
     }
     $request->setRequestFormat($this->negotiator->getContentType($request));
     return $this->app->handle($request, $type, $catch);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE)
 {
     // Register available mime types.
     foreach ($this->formats as $format => $mime_type) {
         $request->setFormat($format, $mime_type);
     }
     // Determine the request format using the negotiator.
     $request->setRequestFormat($this->getContentType($request));
     return $this->app->handle($request, $type, $catch);
 }
 /**
  * @return void
  */
 public function testOnKernelRequest()
 {
     $server = array('HTTP_ACCEPT' => 'application/vnd.com.swisscom.translation+json');
     $request = new Request(array(), array(), array(), array(), array(), $server);
     $request->setFormat('json', 'application/json');
     $eventDouble = $this->getMockBuilder('\\Graviton\\RestBundle\\Event\\RestEvent')->disableOriginalConstructor()->setMethods(array('getRequest'))->getMock();
     $eventDouble->expects($this->once())->method('getRequest')->willReturn($request);
     $containerDouble = $this->getMockBuilder('\\Symfony\\Component\\DependencyInjection\\Container')->disableOriginalConstructor()->setMethods(array('getParameter'))->getMock();
     $containerDouble->expects($this->once())->method('getParameter')->with($this->equalTo('graviton.rest.special_mimetypes'))->willReturn(array('json' => array('application/vnd.com.swisscom.translation+json')));
     $listener = new SpecialMimetypeRequestListener($containerDouble);
     $listener->onKernelRequest($eventDouble);
     $this->assertEquals('json', $request->getFormat('application/vnd.com.swisscom.translation+json'));
 }
 public function testActionWithUncatchableException()
 {
     $serializerException = $this->prophesize(ExceptionInterface::class);
     $serializerException->willExtend(\Exception::class);
     $flattenException = FlattenException::create($serializerException->reveal());
     $serializer = $this->prophesize(SerializerInterface::class);
     $serializer->serialize($flattenException, 'jsonproblem')->willReturn();
     $exceptionAction = new ExceptionAction($serializer->reveal(), ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']]);
     $request = new Request();
     $request->setFormat('jsonproblem', 'application/problem+json');
     $expected = new Response('', Response::HTTP_INTERNAL_SERVER_ERROR, ['Content-Type' => 'application/problem+json; charset=utf-8', 'X-Content-Type-Options' => 'nosniff', 'X-Frame-Options' => 'deny']);
     $this->assertEquals($expected, $exceptionAction($flattenException, $request));
 }
 public function testContentNegotiation()
 {
     $eventProphecy = $this->prophesize(GetResponseEvent::class);
     $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post'], [], [], [], '{}');
     $request->setMethod(Request::METHOD_POST);
     $request->headers->set('Content-Type', 'text/xml');
     $request->setFormat('xml', 'text/xml');
     // Workaround to avoid weird behaviors
     $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
     $serializerProphecy = $this->prophesize(SerializerInterface::class);
     $serializerProphecy->deserialize('{}', 'Foo', 'xml', [])->willReturn(new \stdClass())->shouldBeCalled();
     $serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
     $serializerContextBuilderProphecy->createFromRequest(Argument::type(Request::class), false, Argument::type('array'))->willReturn([])->shouldBeCalled();
     $listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), ['jsonld' => ['application/ld+json'], 'xml' => ['text/xml']]);
     $listener->onKernelRequest($eventProphecy->reveal());
 }
Example #7
0
 /**
  * Tests the live preview output for json output.
  */
 public function testLivePreview()
 {
     // We set up a request so it looks like an request in the live preview.
     $request = new Request();
     $request->setFormat('drupal_ajax', 'application/vnd.drupal-ajax');
     $request->headers->set('Accept', 'application/vnd.drupal-ajax');
     /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
     $request_stack = \Drupal::service('request_stack');
     $request_stack->push($request);
     $view = Views::getView('test_serializer_display_entity');
     $view->setDisplay('rest_export_1');
     $this->executeView($view);
     // Get the serializer service.
     $serializer = $this->container->get('serializer');
     $entities = array();
     foreach ($view->result as $row) {
         $entities[] = $row->_entity;
     }
     $expected = $serializer->serialize($entities, 'json');
     $view->live_preview = TRUE;
     $build = $view->preview();
     $rendered_json = $build['#plain_text'];
     $this->assertTrue(!isset($build['#markup']) && $rendered_json == $expected, 'Ensure the previewed json is escaped.');
     $view->destroy();
     $expected = $serializer->serialize($entities, 'xml');
     // Change the request format to xml.
     $view->setDisplay('rest_export_1');
     $view->getDisplay()->setOption('style', array('type' => 'serializer', 'options' => array('uses_fields' => FALSE, 'formats' => array('xml' => 'xml'))));
     $this->executeView($view);
     $build = $view->preview();
     $rendered_xml = $build['#plain_text'];
     $this->assertEqual($rendered_xml, $expected, 'Ensure we preview xml when we change the request format.');
 }
Example #8
0
 /**
  * @covers Symfony\Component\HttpFoundation\Request::getFormat
  * @covers Symfony\Component\HttpFoundation\Request::setFormat
  * @dataProvider getFormatToMimeTypeMapProvider
  */
 public function testGetFormatFromMimeType($format, $mimeTypes)
 {
     $request = new Request();
     foreach ($mimeTypes as $mime) {
         $this->assertEquals($format, $request->getFormat($mime));
     }
     $request->setFormat($format, $mimeTypes);
     foreach ($mimeTypes as $mime) {
         $this->assertEquals($format, $request->getFormat($mime));
     }
 }
Example #9
0
 public function testGetFormatWithCustomMimeType()
 {
     $request = new Request();
     $request->setFormat('custom', 'application/vnd.foo.api;myversion=2.3');
     $this->assertEquals('custom', $request->getFormat('application/vnd.foo.api;myversion=2.3'));
 }
 /**
  * Tests the live preview output for json output.
  */
 public function testLivePreview()
 {
     // We set up a request so it looks like an request in the live preview.
     $request = new Request();
     $request->setFormat('drupal_ajax', 'application/vnd.drupal-ajax');
     $request->headers->set('Accept', 'application/vnd.drupal-ajax');
     /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
     $request_stack = \Drupal::service('request_stack');
     $request_stack->push($request);
     $view = Views::getView('test_serializer_display_entity');
     $view->setDisplay('rest_export_1');
     $this->executeView($view);
     // Get the serializer service.
     $serializer = $this->container->get('serializer');
     $entities = array();
     foreach ($view->result as $row) {
         $entities[] = $row->_entity;
     }
     $expected = String::checkPlain($serializer->serialize($entities, 'json'));
     $view->live_preview = TRUE;
     $build = $view->preview();
     $rendered_json = $build['#markup'];
     $this->assertEqual($rendered_json, $expected, 'Ensure the previewed json is escaped.');
 }
 private function parseJsonRequest(Request $request)
 {
     $request->setFormat('json', 'application/json');
     if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
         $data = json_decode($request->getContent(), true);
         $request->request->replace(is_array($data) ? $data : array());
     }
 }
Example #12
0
 /**
  * Adds API formats to the HttpFoundation Request.
  *
  * @param Request $request
  * @param array   $formats
  */
 private function addRequestFormats(Request $request, array $formats)
 {
     foreach ($formats as $format => $mimeTypes) {
         $request->setFormat($format, $mimeTypes);
     }
 }
 /**
  * This before middleware validates that the request body is in a format
  * that the application understands.
  *
  * @param Request $request
  * @throws UnsupportedMediaTypeHttpException
  */
 public function validateRequestContentType(Request $request, Application $app)
 {
     // Define the "form" format so we can use it for validation
     $request->setFormat("form", array("application/x-www-form-urlencoded", "multipart/form-data"));
     $format = $request->getContentType();
     if (strlen($request->getContent()) > 0 && !in_array($format, $app["conneg.requestFormats"])) {
         // The request has a body but it is not a supported media type
         throw new UnsupportedMediaTypeHttpException();
     }
 }