public function onBefore(BeforeEvent $event)
 {
     $request = $event->getRequest();
     if (file_exists($this->getFullFilePath($request))) {
         $responsedata = file_get_contents($this->getFullFilePath($request));
         $mf = new MessageFactory();
         $event->intercept($mf->fromMessage($responsedata));
     }
 }
Exemple #2
0
 /**
  * @throws \OutOfBoundsException|\Exception
  */
 public function onBefore(BeforeEvent $event)
 {
     if (!($item = array_shift($this->queue))) {
         throw new \OutOfBoundsException('Mock queue is empty');
     } elseif ($item instanceof RequestException) {
         throw $item;
     }
     // Emulate reading a response body
     $request = $event->getRequest();
     if ($this->readBodies && $request->getBody()) {
         while (!$request->getBody()->eof()) {
             $request->getBody()->read(8096);
         }
     }
     $saveTo = $event->getRequest()->getConfig()->get('save_to');
     if (null !== $saveTo) {
         $body = $item->getBody();
         if (is_resource($saveTo)) {
             fwrite($saveTo, $body);
         } elseif (is_string($saveTo)) {
             file_put_contents($saveTo, $body);
         } elseif ($saveTo instanceof StreamInterface) {
             $saveTo->write($body);
         }
     }
     $event->intercept($item);
 }
 public function onBefore(BeforeEvent $event)
 {
     $request = $event->getRequest();
     if (!($response = $this->cache->fetch($request->__toString()))) {
         return;
     }
     $event->intercept($response);
 }
 public function onBefore(BeforeEvent $event)
 {
     $request = $event->getRequest();
     if (isset(self::$purgeMethods[$request->getMethod()])) {
         $this->storage->purge($request->getUrl());
         if ('PURGE' === $request->getMethod()) {
             $event->intercept(new Response(204));
         }
     }
 }
Exemple #5
0
 public function testInterceptsWithEvent()
 {
     $t = new Transaction(new Client(), new Request('GET', '/'));
     $t->exception = new \Exception('foo');
     $e = new BeforeEvent($t);
     $response = new Response(200);
     $e->intercept($response);
     $this->assertTrue($e->isPropagationStopped());
     $this->assertSame($t->response, $response);
     $this->assertNull($t->exception);
 }
 public function testInterceptsWithEvent()
 {
     $response = new Response(200);
     $res = null;
     $t = new Transaction(new Client(), new Request('GET', '/'));
     $t->getRequest()->getEmitter()->on('complete', function ($e) use(&$res) {
         $res = $e;
     });
     $e = new BeforeEvent($t);
     $e->intercept($response);
     $this->assertTrue($e->isPropagationStopped());
     $this->assertSame($res->getClient(), $e->getClient());
 }
Exemple #7
0
 /**
  * @throws \OutOfBoundsException|\Exception
  */
 public function onBefore(BeforeEvent $event)
 {
     if (!($item = array_shift($this->queue))) {
         throw new \OutOfBoundsException('Mock queue is empty');
     } elseif ($item instanceof RequestException) {
         throw $item;
     }
     // Emulate reading a response body
     $request = $event->getRequest();
     if ($this->readBodies && $request->getBody()) {
         while (!$request->getBody()->eof()) {
             $request->getBody()->read(8096);
         }
     }
     $event->intercept($item);
 }
 public function cacheCheck(BeforeEvent $event, $name)
 {
     $request = $event->getRequest();
     if ($request->getMethod() == 'GET') {
         $url = substr($request->getUrl(), 0, strpos($request->getUrl(), '?'));
         if (!preg_match('/token|oauth/', $url)) {
             $this->_cache_name = md5($request->getUrl() . (string) $request->getBody());
             if ($cache = $this->store->get($this->_cache_name, NULL)) {
                 \Log::info('getting response from cache');
                 $body = \GuzzleHttp\Stream\create(json_encode($cache));
                 $response = new \GuzzleHttp\Message\Response(200, array(), $body);
                 $event->intercept($response);
                 $event->stopPropagation();
             }
         }
     }
 }
 public function cacheCheck(BeforeEvent $event, $name)
 {
     if (!$this->cache) {
         return;
     }
     $request = $event->getRequest();
     if ($request->getMethod() == 'GET') {
         $this->_cache_name = md5($request->getUrl() . (string) $request->getBody());
         if ($cache = $this->store->get($this->_cache_name, NULL)) {
             \Log::info('getting response from cache');
             $body = \GuzzleHttp\Stream\create($cache);
             $response = new \GuzzleHttp\Message\Response(200, array(), $body);
             $event->intercept($response);
             $event->stopPropagation();
         }
     }
 }
 public function onBefore(BeforeEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->enabled) {
         $request->getConfig()->set('cache.disable', true);
     }
     if (!$this->canCacheRequest($request)) {
         $this->cacheMiss($request);
         return;
     }
     if (!($response = $this->storage->fetch($request))) {
         $this->cacheMiss($request);
         return;
     }
     $request->getConfig()->set('cache_lookup', 'HIT');
     $request->getConfig()->set('cache_hit', true);
     $event->intercept($response);
 }
Exemple #11
0
 /**
  * @throws \OutOfBoundsException|\Exception
  */
 public function onBefore(BeforeEvent $event)
 {
     if (!($item = array_shift($this->queue))) {
         throw new \OutOfBoundsException('Mock queue is empty');
     } elseif ($item instanceof RequestException) {
         throw $item;
     }
     // Emulate the receiving of the response headers
     $request = $event->getRequest();
     $transaction = new Transaction($event->getClient(), $request);
     $transaction->setResponse($item);
     $request->getEmitter()->emit('headers', new HeadersEvent($transaction));
     // Emulate reading a response body
     if ($this->readBodies && $request->getBody()) {
         while (!$request->getBody()->eof()) {
             $request->getBody()->read(8096);
         }
     }
     $event->intercept($item);
 }
Exemple #12
0
 protected function fail(\Exception $error, BeforeEvent $event)
 {
     $this->exceptions[] = $error;
     // Set a stub response.
     // The exception will actually be thrown in
     // `verify()`
     // If we threw the exception here,
     // it would be caught by Guzzle,
     // and wrapped into a RequestException
     $event->intercept(new Response(200));
 }
 /**
  * Checks if a request can be cached, and if so, intercepts with a cached
  * response is available.
  *
  * @param BeforeEvent $event
  */
 public function onBefore(BeforeEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->canCacheRequest($request)) {
         $this->cacheMiss($request);
         return;
     }
     if (!($response = $this->storage->fetch($request))) {
         $this->cacheMiss($request);
         return;
     }
     $response->setHeader('Age', Utils::getResponseAge($response));
     $valid = $this->validate($request, $response);
     // Validate that the response satisfies the request
     if ($valid) {
         $request->getConfig()->set('cache_lookup', 'HIT');
         $request->getConfig()->set('cache_hit', true);
         $event->intercept($response);
     } else {
         $this->cacheMiss($request);
     }
 }
 public function interceptResponse(BeforeEvent $event)
 {
     $response = MockResponse::getResponse($event->getRequest());
     $event->intercept($response);
     $event->stopPropagation();
 }
 /**
  * Check if the request is cached and intercept it.
  *
  * @param BeforeEvent $event Guzzle 4/5 event.
  *
  * @return void
  */
 public function onBefore(BeforeEvent $event)
 {
     $request = $event->getRequest();
     $this->request = $request;
     if (!Utils::canCacheRequest($request)) {
         return;
     }
     $key = $request->getMethod() . ' ' . $request->getUrl();
     if (!$this->cache->contains($key)) {
         return;
     }
     $response = $this->cache->fetch($key);
     $event->intercept($response);
 }