/** * 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\Framework\Webapi\Exception(__('Operation allowed only in HTTPS')); } /** @var array $inputData */ $inputData = $this->_request->getRequestData(); $serviceMethodName = $route->getServiceMethod(); $serviceClassName = $route->getServiceClass(); $inputData = $this->paramsOverrider->override($inputData, $route->getParameters()); $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData); $service = $this->_objectManager->get($serviceClassName); /** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */ $outputData = call_user_func_array([$service, $serviceMethodName], $inputParams); $outputData = $this->serviceOutputProcessor->process($outputData, $serviceClassName, $serviceMethodName); if ($this->_request->getParam(FieldsFilter::FILTER_PARAMETER) && is_array($outputData)) { $outputData = $this->fieldsFilter->filter($outputData); } $this->_response->prepareResponse($outputData); } catch (\Exception $e) { $maskedException = $this->_errorProcessor->maskException($e); $this->_response->setException($maskedException); } return $this->_response; }
public function testGetMethodAllStoresInvalid() { $this->_routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['1'])); $this->_authorizationMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true)); $this->storeMock->expects($this->once())->method('getCode')->willReturn('admin'); $this->_requestMock->expects($this->once())->method('getMethod')->willReturn('get'); $this->_restController->dispatch($this->_requestMock); $this->assertTrue($this->_responseMock->isException()); $this->assertSame("Cannot perform GET operation with store code 'all'", $this->_responseMock->getException()[0]->getMessage()); }
public function testAuthorizationFailed() { $this->_authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(false)); $this->_oauthServiceMock->expects($this->any())->method('validateAccessTokenRequest')->will($this->returnValue('fred')); $this->_routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['5', '6'])); $this->_restController->dispatch($this->_requestMock); /** Ensure that response contains proper error message. */ $expectedMsg = 'Consumer is not authorized to access 5, 6'; AuthorizationException::NOT_AUTHORIZED; $this->assertTrue($this->_responseMock->isException()); $exceptionArray = $this->_responseMock->getException(); $this->assertEquals($expectedMsg, $exceptionArray[0]->getMessage()); }
public function testDispatchAllSchemaRequest() { $params = [\Magento\Framework\Webapi\Request::REQUEST_PARAM_SERVICES => 'all']; $this->_requestMock->expects($this->any())->method('getPathInfo')->willReturn(\Magento\Webapi\Controller\Rest::SCHEMA_PATH); $this->_requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap([[\Magento\Framework\Webapi\Request::REQUEST_PARAM_SERVICES, null, 'all']])); $this->_requestMock->expects($this->any())->method('getParams')->will($this->returnValue($params)); $this->_requestMock->expects($this->any())->method('getRequestedServices')->will($this->returnValue('all')); $schema = 'Some REST schema content'; $this->swaggerGeneratorMock->expects($this->any())->method('generate')->willReturn($schema); $this->swaggerGeneratorMock->expects($this->once())->method('getListOfServices')->willReturn(['listOfServices']); $this->_restController->dispatch($this->_requestMock); $this->assertEquals($schema, $this->_responseMock->getBody()); }
/** * Execute API request * * @return void * @throws AuthorizationException * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Webapi\Exception */ protected function processApiRequest() { $this->validateRequest(); /** @var array $inputData */ $inputData = $this->_request->getRequestData(); $route = $this->getCurrentRoute(); $serviceMethodName = $route->getServiceMethod(); $serviceClassName = $route->getServiceClass(); $inputData = $this->paramsOverrider->override($inputData, $route->getParameters()); $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData); $service = $this->_objectManager->get($serviceClassName); /** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */ $outputData = call_user_func_array([$service, $serviceMethodName], $inputParams); $outputData = $this->serviceOutputProcessor->process($outputData, $serviceClassName, $serviceMethodName); if ($this->_request->getParam(FieldsFilter::FILTER_PARAMETER) && is_array($outputData)) { $outputData = $this->fieldsFilter->filter($outputData); } $this->_response->prepareResponse($outputData); }
/** * Execute API request * * @return void * @throws AuthorizationException * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Webapi\Exception */ protected function processApiRequest() { $inputParams = $this->getInputParamsResolver()->resolve(); $route = $this->getInputParamsResolver()->getRoute(); $serviceMethodName = $route->getServiceMethod(); $serviceClassName = $route->getServiceClass(); $service = $this->_objectManager->get($serviceClassName); /** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */ $outputData = call_user_func_array([$service, $serviceMethodName], $inputParams); $outputData = $this->serviceOutputProcessor->process($outputData, $serviceClassName, $serviceMethodName); if ($this->_request->getParam(FieldsFilter::FILTER_PARAMETER) && is_array($outputData)) { $outputData = $this->fieldsFilter->filter($outputData); } $header = $this->getDeploymentConfig()->get(ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT); if ($header) { $this->_response->setHeader('X-Frame-Options', $header); } $this->_response->prepareResponse($outputData); }
public function testHasExceptionOfTypeIfExceptionsIsEmpty() { $this->responseRest->setException(new \Exception()); $hasException = $this->responseRest->hasExceptionOfType('Test\\Exception'); $this->assertFalse($hasException); }