/**
  * Find renderer which can render response in requested format.
  *
  * @return string
  * @throws \Magento\Framework\Webapi\Exception
  */
 protected function _getRendererClass()
 {
     $acceptTypes = $this->_request->getAcceptTypes();
     if (!is_array($acceptTypes)) {
         $acceptTypes = [$acceptTypes];
     }
     foreach ($acceptTypes as $acceptType) {
         foreach ($this->_renders as $rendererConfig) {
             $rendererType = $rendererConfig['type'];
             if ($acceptType == $rendererType || $acceptType == current(explode('/', $rendererType)) . '/*' || $acceptType == '*/*') {
                 return $rendererConfig['model'];
             }
         }
     }
     /** If server does not have renderer for any of the accepted types it SHOULD send 406 (not acceptable). */
     throw new \Magento\Framework\Webapi\Exception(new Phrase('Server cannot match any of the given Accept HTTP header media type(s) from the request: "%1" ' . 'with media types from the config of response renderer.', $acceptTypes), 0, \Magento\Framework\Webapi\Exception::HTTP_NOT_ACCEPTABLE);
 }
Example #2
0
 /**
  * Test for getAcceptTypes() method.
  *
  * @dataProvider providerAcceptType
  * @param string $acceptHeader Value of Accept HTTP header
  * @param array $expectedResult Method call result
  */
 public function testGetAcceptTypes($acceptHeader, $expectedResult)
 {
     $this->_request->expects($this->once())->method('getHeader')->with('Accept')->will($this->returnValue($acceptHeader));
     $this->assertSame($expectedResult, $this->_request->getAcceptTypes());
 }