protected function handle200Response(RequestInterface $request, Response $validateResponse) { $request->setResponse($validateResponse); if ($this->canCache->canCacheResponse($validateResponse)) { $this->storage->cache($request, $validateResponse); } return false; }
/** * 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); $request->setState(RequestInterface::STATE_ERROR); $request->dispatch('request.exception', array('request' => $request, 'exception' => $item)); // Only throw if the exception wasn't handled if ($request->getState() == RequestInterface::STATE_ERROR) { throw $item; } } return $this; }
/** * 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 proceed(RequestInterface $request) { $this->dispatch('mock.request', ['plugin' => $this, 'request' => $request]); $response = $this->dequeue($request->getUrl()); if (!$response) { throw new \OutOfBoundsException('Mock queue for given url is empty'); } if ($response instanceof Response) { if ($this->readBodies && $request instanceof EntityEnclosingRequestInterface) { $request->getEventDispatcher()->addListener('request.sent', $f = function (Event $event) use(&$f) { // @codingStandardsIgnoreStart while ($data = $event['request']->getBody()->read(8096)) { } // @codingStandardsIgnoreEnd // Remove the listener after one-time use $event['request']->getEventDispatcher()->removeListener('request.sent', $f); }); } $request->setResponse($response); } elseif ($response instanceof CurlException) { // Emulates exceptions encountered while transferring requests $response->setRequest($request); $state = $request->setState(RequestInterface::STATE_ERROR, ['exception' => $response]); // Only throw if the exception wasn't handled if ($state == RequestInterface::STATE_ERROR) { throw $response; } } return $this; }
/** * Revalidate a cached response * * @param RequestInterface $request Request to revalidate * @param Response $response Response to revalidate * * @return bool */ public function revalidate(RequestInterface $request, Response $response) { $revalidate = clone $request; $revalidate->getEventDispatcher()->removeSubscriber($this); $revalidate->removeHeader('Pragma')->removeHeader('Cache-Control')->setHeader('If-Modified-Since', $response->getDate()); if ($response->getEtag()) { $revalidate->setHeader('If-None-Match', '"' . $response->getEtag() . '"'); } try { $validateResponse = $revalidate->send(); if ($validateResponse->getStatusCode() == 200) { // The server does not support validation, so use this response $request->setResponse($validateResponse); // Store this response in cache if possible if ($validateResponse->canCache()) { $this->saveCache($this->getCacheKey($request), $validateResponse, $validateResponse->getMaxAge()); } return false; } if ($validateResponse->getStatusCode() == 304) { // Make sure that this response has the same ETage if ($validateResponse->getEtag() != $response->getEtag()) { return false; } // Replace cached headers with any of these headers from the // origin server that might be more up to date $modified = false; foreach (array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified') as $name) { if ($validateResponse->hasHeader($name)) { $modified = true; $response->setHeader($name, $validateResponse->getHeader($name)); } } // Store the updated response in cache if ($modified && $response->canCache()) { $this->saveCache($this->getCacheKey($request), $response, $response->getMaxAge()); } return true; } } catch (BadResponseException $e) { // 404 errors mean the resource no longer exists, so remove from // cache, and prevent an additional request by throwing the exception if ($e->getResponse()->getStatusCode() == 404) { $this->getCacheAdapter()->delete($this->getCacheKey($request)); throw $e; } } // Other exceptions encountered in the revalidation request are ignored // in hopes that sending a request to the origin server will fix it return false; }
/** * Get a response from the front of the list and add it to a request * * @param RequestInterface $request Request to mock * * @return MockPlugin */ public function dequeue(RequestInterface $request) { $this->dispatch('mock.request', array('plugin' => $this, 'request' => $request)); $request->setResponse(array_shift($this->queue), true); if ($this->readBodies && $request instanceof EntityEnclosingRequestInterface) { $request->getEventDispatcher()->addListener('request.sent', function (Event $event) { while ($data = $event['request']->getBody()->read(8096)) { } }); } return $this; }
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)) { } $event['request']->getEventDispatcher()->removeListener('request.sent', $f); }); } $request->setResponse($item); } elseif ($item instanceof CurlException) { $item->setRequest($request); $state = $request->setState(RequestInterface::STATE_ERROR, array('exception' => $item)); if ($state == RequestInterface::STATE_ERROR) { throw $item; } } return $this; }
/** * 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); // Store this response in cache if possible if ($validateResponse->canCache()) { $this->storage->cache($this->cacheKey->getCacheKey($request), $validateResponse, $validateResponse->getMaxAge()); } return false; }
/** * Get a response from the front of the list and add it to a request * * @param RequestInterface $request Request to mock * * @return MockPlugin */ public function dequeue(RequestInterface $request) { $this->dispatch('mock.request', array('plugin' => $this, 'request' => $request)); $request->setResponse(array_shift($this->queue), true); return $this; }