/** * Throw a too many redirects exception for a request * * @param RequestInterface $original Request * @param int $max Max allowed redirects * * @throws TooManyRedirectsException when too many redirects have been issued */ protected function throwTooManyRedirectsException(RequestInterface $original, $max) { $original->getEventDispatcher()->addListener('request.complete', $func = function ($e) use(&$func, $original, $max) { $original->getEventDispatcher()->removeListener('request.complete', $func); $str = "{$max} redirects were issued for this request:\n" . $e['request']->getRawHeaders(); throw new TooManyRedirectsException($str); }); }
/** * 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; }
protected function visit_exceptions(RequestInterface $request, $value, $flags) { if ($value === false || $value === 0) { $dispatcher = $request->getEventDispatcher(); foreach ($dispatcher->getListeners('request.error') as $listener) { if (is_array($listener) && $listener[0] == 'Akeeba\\ARS\\Amazon\\Guzzle\\Http\\Message\\Request' && ($listener[1] = 'onRequestError')) { $dispatcher->removeListener('request.error', $listener); break; } } } }