/** * Handle REST request * * @param \Magento\Framework\App\RequestInterface $request * @return \Magento\Framework\App\ResponseInterface */ public function dispatch(\Magento\Framework\App\RequestInterface $request) { $path = $this->_pathProcessor->process($request->getPathInfo()); $this->_request->setPathInfo($path); $this->areaList->getArea($this->_appState->getAreaCode())->load(\Magento\Framework\App\Area::PART_TRANSLATE); try { $this->checkPermissions(); $route = $this->getCurrentRoute(); if ($route->isSecure() && !$this->_request->isSecure()) { throw new \Magento\Webapi\Exception(__('Operation allowed only in HTTPS')); } /** @var array $inputData */ $inputData = $this->_request->getRequestData(); $serviceMethodName = $route->getServiceMethod(); $serviceClassName = $route->getServiceClass(); $inputData = $this->overrideParams($inputData, $route->getParameters()); $inputParams = $this->_serializer->getInputData($serviceClassName, $serviceMethodName, $inputData); $service = $this->_objectManager->get($serviceClassName); /** @var \Magento\Framework\Service\Data\AbstractExtensibleObject $outputData */ $outputData = call_user_func_array([$service, $serviceMethodName], $inputParams); $outputData = $this->dataObjectConverter->processServiceOutput($outputData); if ($this->_request->getParam(PartialResponseProcessor::FILTER_PARAMETER) && is_array($outputData)) { $outputData = $this->partialResponseProcessor->filter($outputData); } $this->_response->prepareResponse($outputData); } catch (\Exception $e) { $maskedException = $this->_errorProcessor->maskException($e); $this->_response->setException($maskedException); } return $this->_response; }
/** * Test sendResponse method with exception rendering. */ public function testSendResponseWithException() { /** Mock all required objects. */ $this->_rendererMock->expects($this->any())->method('getMimeType')->will($this->returnValue('application/json')); $this->_rendererMock->expects($this->any())->method('render')->will($this->returnCallback([$this, 'callbackForSendResponseTest'], $this->returnArgument(0))); $exceptionMessage = 'Message'; $exceptionHttpCode = \Magento\Webapi\Exception::HTTP_BAD_REQUEST; $exception = new \Magento\Webapi\Exception($exceptionMessage, 0, $exceptionHttpCode); $this->_errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnValue($exception)); $this->_responseRest->setException($exception); /** Start output buffering. */ ob_start(); $this->_responseRest->sendResponse(); /** Clear output buffering. */ ob_end_clean(); $actualResponse = $this->_responseRest->getBody(); $expectedResult = '{"message":"' . $exceptionMessage . '"}'; $this->assertStringStartsWith($expectedResult, $actualResponse, 'Response body is invalid'); }