diff --git a/lib/internal/Magento/Framework/Webapi/Rest/Response/RendererFactory.php b/lib/internal/Magento/Framework/Webapi/Rest/Response/RendererFactory.php index c86b97b0fa8e7..547b8fcb03640 100644 --- a/lib/internal/Magento/Framework/Webapi/Rest/Response/RendererFactory.php +++ b/lib/internal/Magento/Framework/Webapi/Rest/Response/RendererFactory.php @@ -72,14 +72,9 @@ protected function _getRendererClass() $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']; - } + $renderer = $this->getRendererConfig($acceptType); + if ($renderer !== null) { + return $renderer['model']; } } /** If server does not have renderer for any of the accepted types it SHOULD send 406 (not acceptable). */ @@ -93,4 +88,30 @@ protected function _getRendererClass() \Magento\Framework\Webapi\Exception::HTTP_NOT_ACCEPTABLE ); } + + /** + * Get renderer config by accept type. + * + * @param string $acceptType + * @return array|null + */ + private function getRendererConfig($acceptType) + { + // If Accept type = '*/*' then return default renderer. + if ($acceptType == '*/*' && isset($this->_renders['default'])) { + return $this->_renders['default']; + } + + foreach ($this->_renders as $rendererConfig) { + $rendererType = $rendererConfig['type']; + if ($acceptType == $rendererType + || $acceptType == current(explode('/', $rendererType)) . '/*' + || $acceptType == '*/*' + ) { + return $rendererConfig; + } + } + + return null; + } } diff --git a/lib/internal/Magento/Framework/Webapi/Test/Unit/Rest/Response/RendererFactoryTest.php b/lib/internal/Magento/Framework/Webapi/Test/Unit/Rest/Response/RendererFactoryTest.php index ba460c5a5f6e6..dd7aa845f3091 100644 --- a/lib/internal/Magento/Framework/Webapi/Test/Unit/Rest/Response/RendererFactoryTest.php +++ b/lib/internal/Magento/Framework/Webapi/Test/Unit/Rest/Response/RendererFactoryTest.php @@ -26,11 +26,18 @@ protected function setUp() )->disableOriginalConstructor()->getMock(); $renders = [ - 'default' => ['type' => '*/*', 'model' => \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class], + 'application_xml' => [ + 'type' => 'application/xml', + 'model' => \Magento\Framework\Webapi\Rest\Response\Renderer\Xml::class, + ], 'application_json' => [ 'type' => 'application/json', 'model' => \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class, ], + 'default' => [ + 'type' => '*/*', + 'model' => \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class + ], ]; $this->_factory = new \Magento\Framework\Webapi\Rest\Response\RendererFactory( @@ -42,29 +49,43 @@ protected function setUp() /** * Test GET method. + * + * @param array $acceptTypes + * @param string $model + * @dataProvider getTestDataProvider */ - public function testGet() + public function testGet($acceptTypes, $model) { - $acceptTypes = ['application/json']; - /** Mock request getAcceptTypes method to return specified value. */ $this->_requestMock->expects($this->once())->method('getAcceptTypes')->will($this->returnValue($acceptTypes)); /** Mock renderer. */ - $rendererMock = $this->getMockBuilder( - \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class - )->disableOriginalConstructor()->getMock(); + $rendererMock = $this->getMockBuilder($model)->disableOriginalConstructor()->getMock(); /** Mock object to return mocked renderer. */ $this->_objectManagerMock->expects( $this->once() )->method( 'get' )->with( - \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class + $model )->will( $this->returnValue($rendererMock) ); $this->_factory->get(); } + + /** + * Data provider for method testGet + * + * @return array + */ + public function getTestDataProvider() + { + return [ + [['*/*'], \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class], + [['application/json'], \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class], + [['application/xml'], \Magento\Framework\Webapi\Rest\Response\Renderer\Xml::class], + ]; + } /** * Test GET method with wrong Accept HTTP Header.