/**
  * Transfers an HTTP request and populates a response
  *
  * @param TransactionInterface $transaction Transaction abject to populate
  *
  * @return ResponseInterface
  */
 public function send(TransactionInterface $transaction)
 {
     // HTTP/1.1 streams using the PHP stream wrapper require a
     // Connection: close header. Setting here so that it is added before
     // emitting the request.before_send event.
     $request = $transaction->getRequest();
     if ($request->getProtocolVersion() == '1.1' && !$request->hasHeader('Connection')) {
         $transaction->getRequest()->setHeader('Connection', 'close');
     }
     try {
         RequestEvents::emitBefore($transaction);
         if (!$transaction->getResponse()) {
             $this->createResponse($transaction);
             RequestEvents::emitComplete($transaction);
         }
         return $transaction->getResponse();
     } catch (RequestException $e) {
         if ($e->hasResponse() && $e->getResponse()->getBody()) {
             $message = trim($e->getResponse()->getBody()->__toString());
             if (empty($message)) {
                 $message = $e->getMessage();
             }
             throw new APIException($message, $e->getRequest(), $e->getResponse(), $e);
         }
         throw $e;
     }
 }
 public function send(TransactionInterface $transaction)
 {
     // HTTP/1.1 streams using the PHP stream wrapper require a
     // Connection: close header. Setting here so that it is added before
     // emitting the request.before_send event.
     $request = $transaction->getRequest();
     if ($request->getProtocolVersion() == '1.1' && !$request->hasHeader('Connection')) {
         $transaction->getRequest()->setHeader('Connection', 'close');
     }
     RequestEvents::emitBefore($transaction);
     if (!$transaction->getResponse()) {
         $this->createResponse($transaction);
         RequestEvents::emitComplete($transaction);
     }
     return $transaction->getResponse();
 }
 public function send(TransactionInterface $transaction)
 {
     RequestEvents::emitBefore($transaction);
     if (!$transaction->getResponse()) {
         $response = is_callable($this->response) ? call_user_func($this->response, $transaction) : $this->response;
         if (!$response instanceof ResponseInterface) {
             throw new \RuntimeException('Invalid mocked response');
         }
         // Read the request body if it is present
         if ($transaction->getRequest()->getBody()) {
             $transaction->getRequest()->getBody()->__toString();
         }
         $transaction->setResponse($response);
         RequestEvents::emitHeaders($transaction);
         RequestEvents::emitComplete($transaction);
     }
     return $transaction->getResponse();
 }
示例#4
0
 public function send(TransactionInterface $transaction)
 {
     RequestEvents::emitBefore($transaction);
     if ($response = $transaction->getResponse()) {
         return $response;
     }
     $factory = $this->curlFactory;
     $handle = $factory($transaction, $this->messageFactory, $this->checkoutEasyHandle());
     curl_exec($handle);
     $info = curl_getinfo($handle);
     $info['curl_result'] = curl_errno($handle);
     if ($info['curl_result']) {
         $this->handleError($transaction, $info, $handle);
     } else {
         $this->releaseEasyHandle($handle);
         RequestEvents::emitComplete($transaction, $info);
     }
     return $transaction->getResponse();
 }
 private function addHandle(TransactionInterface $transaction, BatchContext $context)
 {
     try {
         RequestEvents::emitBefore($transaction);
         // Only transfer if the request was not intercepted
         if (!$transaction->getResponse()) {
             $factory = $this->curlFactory;
             $context->addTransaction($transaction, $factory($transaction, $this->messageFactory));
         }
     } catch (RequestException $e) {
         $this->throwException($e, $context);
     }
 }
 public function testThrowsUnInterceptedErrors()
 {
     $ex = new \Exception('Foo');
     $client = new Client();
     $request = new Request('GET', '/');
     $t = new Transaction($client, $request);
     $errCalled = 0;
     $request->getEmitter()->on('before', function (BeforeEvent $e) use($ex) {
         throw $ex;
     });
     $request->getEmitter()->on('error', function (ErrorEvent $e) use(&$errCalled) {
         $errCalled++;
     });
     try {
         RequestEvents::emitBefore($t);
         $this->fail('Did not throw');
     } catch (RequestException $e) {
         $this->assertEquals(1, $errCalled);
     }
 }