/** * Find renderer which can render response in requested format. * * @return string * @throws \Magento\Webapi\Exception */ protected function _getRendererClass() { $acceptTypes = $this->_request->getAcceptTypes(); if (!is_array($acceptTypes)) { $acceptTypes = array($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\Webapi\Exception(__('Server cannot understand Accept HTTP header media type.'), 0, \Magento\Webapi\Exception::HTTP_NOT_ACCEPTABLE); }
/** * Find renderer which can render response in requested format. * * @return string * @throws \Magento\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\Webapi\Exception(__('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.', implode(',', $acceptTypes)), 0, \Magento\Webapi\Exception::HTTP_NOT_ACCEPTABLE); }
/** * 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()); }