Example #1
0
 /**
  * Get a response from the front of the list and add it to a request
  *
  * @param RequestInterface $request Request to mock
  *
  * @return self
  * @throws CurlException When request.send is called and an exception is queued
  */
 public function dequeue(RequestInterface $request)
 {
     $this->dispatch('mock.request', array('plugin' => $this, 'request' => $request));
     $item = array_shift($this->queue);
     if ($item instanceof Response) {
         if ($this->readBodies && $request instanceof EntityEnclosingRequestInterface) {
             $request->getEventDispatcher()->addListener('request.sent', $f = function (Event $event) use(&$f) {
                 while ($data = $event['request']->getBody()->read(8096)) {
                 }
                 // Remove the listener after one-time use
                 $event['request']->getEventDispatcher()->removeListener('request.sent', $f);
             });
         }
         $request->setResponse($item);
     } elseif ($item instanceof CurlException) {
         // Emulates exceptions encountered while transferring requests
         $item->setRequest($request);
         $state = $request->setState(RequestInterface::STATE_ERROR, array('exception' => $item));
         // Only throw if the exception wasn't handled
         if ($state == RequestInterface::STATE_ERROR) {
             throw $item;
         }
     }
     return $this;
 }
Example #2
0
 /**
  * Handles a 200 response response from revalidating. The server does not support validation, so use this response.
  *
  * @param RequestInterface $request          Request that was sent
  * @param Response         $validateResponse Response received
  *
  * @return bool Returns true if valid, false if invalid
  */
 protected function handle200Response(RequestInterface $request, Response $validateResponse)
 {
     $request->setResponse($validateResponse);
     if ($this->canCache->canCacheResponse($validateResponse)) {
         $this->storage->cache($request, $validateResponse);
     }
     return false;
 }