/** * @covers Guzzle\Http\Plugin\CachePlugin::onRequestSent * @covers Guzzle\Http\Plugin\CachePlugin::onRequestBeforeSend * @covers Guzzle\Http\Plugin\CachePlugin::saveCache * @covers Guzzle\Http\Plugin\CachePlugin::getCacheKey * @covers Guzzle\Http\Plugin\CachePlugin::canResponseSatisfyRequest */ public function testSavesResponsesInCache() { // Send a 200 OK script to the testing server $this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndata", "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest")); // Create a new Cache plugin $plugin = new CachePlugin($this->adapter); $client = new Client($this->getServer()->getUrl()); $client->setCurlMulti(new \Guzzle\Http\Curl\CurlMulti()); $client->getEventDispatcher()->addSubscriber($plugin); $request = $client->get(); $request->send(); // Calculate the cache key like the cache plugin does $key = $plugin->getCacheKey($request); // Make sure that the cache plugin set the request in cache $this->assertNotNull($this->adapter->fetch($key)); // Clear out the requests stored on the server to make sure we didn't send a new request $this->getServer()->flush(); // Test that the request is set manually // The test server has no more script data, so if it actually sends a // request it will fail the test. $this->assertEquals($key, $plugin->getCacheKey($request)); $request->setState('new'); $request->send(); $this->assertEquals('data', $request->getResponse()->getBody(true)); // Make sure a request wasn't sent $this->assertEquals(0, count($this->getServer()->getReceivedRequests(false))); }
public function testTransfersBatches() { $client = new Client('http://localhost:123'); $request = $client->get(); $multi = $this->getMock('Guzzle\\Http\\Curl\\CurlMultiInterface'); $client->setCurlMulti($multi); $multi->expects($this->once())->method('add')->with($request); $multi->expects($this->once())->method('send'); $batch = new BatchRequestTransfer(2); $batch->transfer(array($request)); }
public function testTrimsDownMaxHandleCount() { $this->getServer()->flush(); $this->getServer()->enqueue(array("HTTP/1.1 307 OK\r\nLocation: /foo\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 307 OK\r\nLocation: /foo\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 307 OK\r\nLocation: /foo\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 307 OK\r\nLocation: /foo\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")); $client = new Client($this->getServer()->getUrl()); $client->setCurlMulti(new CurlMultiProxy(2)); $request = $client->get(); $request->send(); $this->assertEquals(200, $request->getResponse()->getStatusCode()); $handles = $this->readAttribute($client->getCurlMulti(), 'handles'); $this->assertEquals(2, count($handles)); }
public function testTransfersBatches() { $client = new Client('http://127.0.0.1:123'); $request = $client->get(); // For some reason... PHP unit clones the request, which emits a request.clone event. This causes the // 'sorted' property of the event dispatcher to contain an array in the cloned request that is not present in // the original. $request->dispatch('request.clone'); $multi = $this->getMock('Guzzle\\Http\\Curl\\CurlMultiInterface'); $client->setCurlMulti($multi); $multi->expects($this->once())->method('add')->with($request); $multi->expects($this->once())->method('send'); $batch = new BatchRequestTransfer(2); $batch->transfer(array($request)); }
/** * @covers Guzzle\Http\Curl\CurlMulti * @expectedException RuntimeException * @expectedExceptionMessage Testing! */ public function testCatchesExceptionsBeforeSendingCurlMulti() { $client = new Client($this->getServer()->getUrl()); $multi = new CurlMulti(); $client->setCurlMulti($multi); $multi->getEventDispatcher()->addListener(CurlMulti::BEFORE_SEND, function () { throw new \RuntimeException('Testing!'); }); $client->get()->send(); }
public function testCatchesExceptionsBeforeSendingSingleRequest() { $client = new Client($this->getServer()->getUrl()); $multi = new CurlMulti(); $client->setCurlMulti($multi); $request = $client->get(); $request->getEventDispatcher()->addListener('request.before_send', function () { throw new \RuntimeException('Testing!'); }); try { $request->send(); $this->fail('Did not throw'); } catch (\RuntimeException $e) { // Ensure it was removed $this->assertEquals(0, count($multi)); } }
public function testAllowsCustomCurlMultiObjects() { $mock = $this->getMock('Guzzle\\Http\\Curl\\CurlMulti', array('add', 'send')); $mock->expects($this->once())->method('add')->will($this->returnSelf()); $mock->expects($this->once())->method('send')->will($this->returnSelf()); $client = new Client(); $client->setCurlMulti($mock); $request = $client->get(); $request->setResponse(new Response(200), true); $client->send($request); }