/** * @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 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 onBefore(BeforeEvent $event) { $request = $event->getRequest(); // Set the appropriate Content-Type for a request if one is not set and // there are form fields if (!($body = $request->getBody())) { return; } $this->addContentLength($request, $body); if ($body instanceof AppliesHeadersInterface) { // Synchronize the body with the request headers $body->applyRequestHeaders($request); } elseif (!$request->hasHeader('Content-Type')) { $this->addContentType($request, $body); } $this->addExpectHeader($request, $body); }
public function _trackRetries(BeforeEvent $e) { $e->getRequest()->getConfig()->set('_pool_retries', $e->getRetryCount()); }
public function onBefore(BeforeEvent $event) { $this->cookieJar->addCookieHeader($event->getRequest()); }