Example #1
0
 /**
  * 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;
 }
Example #2
0
 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());
 }
Example #3
0
 /**
  * Test sendResponse method without any exception
  */
 public function testSendResponseSuccessHandling()
 {
     $this->_responseRest->sendResponse();
     $this->assertTrue($this->_responseRest->getHttpResponseCode() == \Magento\Webapi\Controller\Response::HTTP_OK);
 }