public function test_ShouldReuseSameProxyOnRedirect()
 {
     $proxyMock = $this->getRotatingProxyMock("test", true);
     $proxyMock->expects($this->once())->method("setupRequest")->willReturnArgument(0);
     $proxies = [$proxyMock];
     $useOwnIp = false;
     $rotator = new ProxyRotator($proxies, $useOwnIp);
     $rotator->setReuseSameProxyOnRedirect(true);
     $client = new Client();
     $request = $client->createRequest("GET", "/");
     $request->getConfig()->set("redirect_count", 1);
     $rotator->setupRequest($request);
     // setupRequest will not be called
     $request->getConfig()->remove("redirect_count");
     $rotator->setupRequest($request);
     // setupRequest will be called
 }
 /**
  * Test if redirect requests use the same proxy
  */
 public function test_integration_RedirectingRequestsShouldUseTheSameProxy()
 {
     $client = new Client();
     $proxy0 = new RotatingProxy("0", null, null, null, null);
     $proxy1 = new RotatingProxy("1", null, null, null, null);
     $proxies = [0 => $proxy0, 1 => $proxy1];
     $success = true;
     $redirect = false;
     $responses2Proxy = [[$success, $proxy1], [$redirect, $proxy1], [$success, null], [$success, $proxy0]];
     $randKeys = [];
     $responses = [];
     foreach ($responses2Proxy as $key => $val) {
         if ($val[1] !== null) {
             $randKeys[$key] = array_search($val[1], $proxies);
         }
         $responses[$key] = $val[0] ? new Response(200) : new Response(301, ["Location" => "http://localhost/"]);
     }
     $h = $this->getHelper(null, null, $randKeys);
     $useOwnIp = false;
     $rotator = new ProxyRotator($proxies, $useOwnIp, $h->getRandomMock());
     $rotator->setReuseSameProxyOnRedirect(true);
     $sub = new RotatingProxySubscriber($rotator);
     $mock = new Mock($responses);
     // Add the mock subscriber to the client.
     $client->getEmitter()->attach($mock);
     $client->getEmitter()->attach($sub);
     // build requests - we need to do this _after_ the $mock hast been attached to the client,
     // otherwise a real request is sent.
     $req1 = $client->createRequest("GET");
     $req2 = $client->createRequest("GET");
     $req3 = $client->createRequest("GET");
     $requests = [$req1, $req2, $req3];
     // making only 3 requests but will receive all 4 responses (verified because $proxy0 will have 1 total request)
     //        $options = [
     //            "before" => function(BeforeEvent $ev){
     //                echo "Before: Proxy ".$ev->getRequest()->getConfig()->get("proxy")."\n";
     //            },
     //            "complete" => function(CompleteEvent $ev){
     //                echo "Complete: Proxy ".$ev->getRequest()->getConfig()->get("proxy")."\n";
     //            },
     //            "error" => function(ErrorEvent $ev){
     //                echo "Error: Proxy ".$ev->getRequest()->getConfig()->get("proxy")."\n";
     //            },
     //            "end" => function(EndEvent $ev){
     //                echo "End: Proxy ".$ev->getRequest()->getConfig()->get("proxy")."\n";
     //            },
     //        ];
     $pool = new Pool($client, $requests);
     $pool->wait();
     $this->assertEquals(2, $proxy1->getTotalRequests(), "Proxy {$proxy1->getProxyString()} should have 2 total request");
     $this->assertEquals(0, $proxy1->getCurrentTotalFails(), "Proxy {$proxy1->getProxyString()} should have 0 failed requests");
     $this->assertEquals(1, $proxy0->getTotalRequests(), "Proxy {$proxy0->getProxyString()} should have 1 total request");
 }