Esempio n. 1
0
    /**
     * Test handling exception during dispatch.
     */
    public function testDispatchWithException()
    {
        $this->_appStateMock->expects($this->any())->method('isInstalled')->will($this->returnValue(true));
        $exceptionMessage = 'some error message';
        $exception = new \Magento\Webapi\Exception($exceptionMessage);
        $this->_soapServerMock->expects($this->any())->method('handle')->will($this->throwException($exception));
        $this->_errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnValue($exception));
        $encoding = "utf-8";
        $this->_soapServerMock->expects($this->any())->method('getApiCharset')->will($this->returnValue($encoding));
        $this->_soapController->dispatch($this->_requestMock);
        $expectedMessage = <<<EXPECTED_MESSAGE
<?xml version="1.0" encoding="{$encoding}"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" >
   <env:Body>
      <env:Fault>
         <env:Code>
            <env:Value>env:Sender</env:Value>
         </env:Code>
         <env:Reason>
            <env:Text xml:lang="en">some error message</env:Text>
         </env:Reason>
      </env:Fault>
   </env:Body>
</env:Envelope>
EXPECTED_MESSAGE;
        $this->assertXmlStringEqualsXmlString($expectedMessage, $this->_responseMock->getBody());
    }
Esempio n. 2
0
 /**
  * Test addMessage, hasMessage, getMessage, and clearMessages methods.
  */
 public function testMessagesCrud()
 {
     /** Test that new object does not contain any messages. */
     $this->assertFalse($this->_response->hasMessages(), 'New object contains messages.');
     /** Test message adding functionality. */
     $this->_response->addMessage('Message text', \Magento\Webapi\Controller\Response::HTTP_OK, ['key' => 'value'], \Magento\Webapi\Controller\Response::MESSAGE_TYPE_SUCCESS);
     $this->assertTrue($this->_response->hasMessages(), 'New message is not added correctly.');
     /** Test message getting functionality. */
     $expectedMessage = [\Magento\Webapi\Controller\Response::MESSAGE_TYPE_SUCCESS => [['key' => 'value', 'message' => 'Message text', 'code' => \Magento\Webapi\Controller\Response::HTTP_OK]]];
     $this->assertEquals($expectedMessage, $this->_response->getMessages(), 'Message is got incorrectly.');
     /** Test message clearing functionality. */
     $this->_response->clearMessages();
     $this->assertFalse($this->_response->hasMessages(), 'Message is not cleared.');
 }
Esempio n. 3
0
 /**
  * Send response to the client, render exceptions if they are present.
  *
  * @return void
  */
 public function sendResponse()
 {
     try {
         if ($this->isException()) {
             $this->_renderMessages();
         }
         parent::sendResponse();
     } catch (\Exception $e) {
         if ($e instanceof \Magento\Webapi\Exception) {
             // If the server does not support all MIME types accepted by the client it SHOULD send 406.
             $httpCode = $e->getHttpCode() == \Magento\Webapi\Exception::HTTP_NOT_ACCEPTABLE ? \Magento\Webapi\Exception::HTTP_NOT_ACCEPTABLE : \Magento\Webapi\Exception::HTTP_INTERNAL_ERROR;
         } else {
             $httpCode = \Magento\Webapi\Exception::HTTP_INTERNAL_ERROR;
         }
         /** If error was encountered during "error rendering" process then use error renderer. */
         $this->_errorProcessor->renderException($e, $httpCode);
     }
 }
Esempio n. 4
0
 /**
  * Replace WSDL xml encoding from config, if present, else default to UTF-8 and set it to the response object.
  *
  * @param string $responseBody
  * @return $this
  */
 protected function _setResponseBody($responseBody)
 {
     $this->_response->setBody(preg_replace('/<\\?xml version="([^\\"]+)"([^\\>]+)>/i', '<?xml version="$1" encoding="' . $this->_soapServer->getApiCharset() . '"?>', $responseBody));
     return $this;
 }