/** * Test handling exception during dispatch. */ public function testDispatchWithException() { $exceptionMessage = 'some error message'; $exception = new \Magento\Framework\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()); }
/** * 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\Framework\Webapi\Response::HTTP_OK, ['key' => 'value'], \Magento\Framework\Webapi\Response::MESSAGE_TYPE_SUCCESS); $this->assertTrue($this->_response->hasMessages(), 'New message is not added correctly.'); /** Test message getting functionality. */ $expectedMessage = [\Magento\Framework\Webapi\Response::MESSAGE_TYPE_SUCCESS => [['key' => 'value', 'message' => 'Message text', 'code' => \Magento\Framework\Webapi\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.'); }
/** * 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\Framework\Webapi\Exception) { // If the server does not support all MIME types accepted by the client it SHOULD send 406. $httpCode = $e->getHttpCode() == \Magento\Framework\Webapi\Exception::HTTP_NOT_ACCEPTABLE ? \Magento\Framework\Webapi\Exception::HTTP_NOT_ACCEPTABLE : \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR; } else { $httpCode = \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR; } /** If error was encountered during "error rendering" process then use error renderer. */ $this->_errorProcessor->renderException($e, $httpCode); } }
/** * 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; }