Beispiel #1
0
 public function testDefaultHeaders()
 {
     $defaultHeaders = new Headers(array('custom-header' => 'custom header value', 'next-custom-header' => 'sum ting wong'));
     $this->object->setDefaultHeaders($defaultHeaders);
     $request = new Request(MessageTypes::PING);
     $this->useRequest($request);
     $response = new Response(MessageTypes::PONG);
     $response->getHeaders()->set('custom-header', 'custom header value')->set('next-custom-header', 'sum ting wong');
     $this->useResponse($request, $response);
     $this->object->handleCall();
 }
Beispiel #2
0
 public function testHeadersForwarding()
 {
     $this->object->setForwardedHeaders(array('custom-header'));
     $request = new Request(MessageTypes::ONE_WAY, 'owc', array('zia'));
     $request->getHeaders()->set('request-id', '1234567890')->set('custom-header', 'some value')->set('next-custom-header', 'next value');
     $this->useRequest($request);
     $this->rpcTarget->expects($this->once())->method('owc')->with('zia')->will($this->returnValue('OK'));
     $response = new Response(MessageTypes::ONE_WAY_ACK);
     $response->getHeaders()->set('request-id', '1234567890')->set('custom-header', 'some value');
     $this->useResponse($request, $response);
     $this->object->handleCall();
 }
Beispiel #3
0
 public function testResultShouldBeSerializedUsingResponseContentType()
 {
     $request = new Request(MessageTypes::REQUEST, 'basic', array('zia'));
     $this->useRequest($request);
     $this->rpcTarget->expects($this->once())->method('basic')->with('zia')->will($this->returnCallback(function ($arg1, $request, Headers $headers) {
         $headers->set('content-type', 'application/wookieb');
         return 'OK';
     }));
     $response = new Response(MessageTypes::RESPONSE, 'OK');
     $response->getHeaders()->set('content-type', 'application/wookieb');
     $this->useResponse($request, $response);
     $this->object->handleCall();
 }
 /**
  * {@inheritDoc}
  */
 public function sendResponse(Response $response)
 {
     $message = array($response->getType(), (string) $response->getHeaders());
     if (MessageTypes::isResponseTypeWithResult($response->getType())) {
         $message[2] = $response->getResultBody();
     }
     try {
         $this->socket->sendMulti($message);
         $this->waitingForResponse = false;
     } catch (\Exception $e) {
         $this->waitingForResponse = false;
         throw new TransportException('Unable to send request', null, $e);
     }
 }
Beispiel #5
0
 protected function useResponse(Request $request, Response $response, $filter = null)
 {
     if ($request->isExpectingResult()) {
         $this->serializer->expects($this->once())->method($response->getType() === MessageTypes::ERROR ? 'serializeError' : 'serializeResult')->with($request->getMethodName(), $this->equalTo($response->getResultBody(), 0, 0), $response->getHeaders()->get('content-type'))->will($this->returnValue($response->getResultBody()));
     } else {
         $this->serializer->expects($this->never())->method('serializeResult');
     }
     if (!$filter) {
         $filter = function () {
         };
     }
     $this->transport->expects($this->once())->method('sendResponse')->with($response)->will($this->returnCallback($filter));
 }
Beispiel #6
0
 private function handleError(Response $response, Request $request)
 {
     $responseData = $this->serializer->unserializeError($request->getMethodName(), $response->getResultBody(), $response->getHeaders()->get('Content-Type'));
     if ($responseData instanceof \Exception) {
         $this->replaceExceptionTrace($responseData);
         throw $responseData;
     }
     if ($request->getType() === MessageTypes::PING) {
         $msg = 'Error caught during ping';
     } else {
         $msg = 'Error caught during execution of method "' . $request->getMethodName() . '"';
     }
     throw new ErrorResponseException($msg, $responseData);
 }
Beispiel #7
0
 private function createResponse($type, $result, Response $response, Request $request)
 {
     $response->setType($type);
     $this->forwardHeaders($request, $response);
     if (!MessageTypes::isResponseTypeWithResult($type)) {
         return $response;
     }
     if ($type === MessageTypes::ERROR) {
         if ($result instanceof \Exception) {
             ExceptionChanger::clean($result);
         }
         $resultBody = $this->serializer->serializeError($request->getMethodName(), $result, $response->getHeaders()->get('content-type'));
     } else {
         $resultBody = $this->serializer->serializeResult($request->getMethodName(), $result, $response->getHeaders()->get('content-type'));
     }
     $response->setResultBody($resultBody);
     return $response;
 }
 /**
  * {@inheritDoc}
  */
 public function receiveResponse()
 {
     try {
         $result = $this->socket->recvMulti();
     } catch (\ZMQSocketException $e) {
         throw new TransportException('Cannot receive response', 0, $e);
     }
     if ($result === false) {
         throw new TimeoutException('Timeout (' . $this->getTimeout() . 's) reached');
     }
     if (!isset($result[0])) {
         throw new FormatException('Invalid response - no response type', $result);
     }
     if (!isset($result[1])) {
         throw new FormatException('Invalid response - no headers', $result);
     }
     $response = new Response($result[0]);
     $response->setHeaders(new Headers(Parser::parseHeaders($result[1])));
     if (!MessageTypes::isResponseTypeWithResult($response->getType())) {
         return $response;
     }
     if (!isset($result[2])) {
         throw new FormatException('Invalid response - no response body', $result);
     }
     return $response->setResultBody($result[2]);
 }