flush() public static method

Flush the received requests from the server
public static flush ( )
Example #1
0
 public function testStripsFragmentFromHost()
 {
     Server::flush();
     Server::enqueue("HTTP/1.1 200 OK\r\n\r\nContent-Length: 0\r\n\r\n");
     // This will fail if the removal of the #fragment is not performed
     $url = Url::fromString(Server::$url)->setPath(null)->setFragment('foo');
     $client = new Client();
     $client->get($url);
 }
 public function testDelaysConcurrently()
 {
     Server::flush();
     Server::enqueue([new Response()]);
     $a = new CurlMultiHandler();
     $expected = microtime(true) + 100 / 1000;
     $response = $a(new Request('GET', Server::$url), ['delay' => 100]);
     $response->wait();
     $this->assertGreaterThanOrEqual($expected, microtime(true));
 }
Example #3
0
 /**
  * @dataProvider hasBodyProvider
  */
 public function testSendsWithBody($method)
 {
     Server::flush();
     Server::enqueue([new Response(200)]);
     call_user_func("GuzzleHttp\\{$method}", Server::$url, ['headers' => ['foo' => 'bar'], 'body' => 'test', 'query' => ['a' => '1']]);
     $sent = Server::received(true)[0];
     $this->assertEquals(strtoupper($method), $sent->getMethod());
     $this->assertEquals('/?a=1', $sent->getResource());
     $this->assertEquals('bar', $sent->getHeader('foo'));
     $this->assertEquals('test', $sent->getBody());
 }
 public function testCanSendMagicAsyncRequests()
 {
     $client = new Client();
     Server::flush();
     Server::enqueue([new Response(200, ['Content-Length' => 2], 'hi')]);
     $p = $client->getAsync(Server::$url, ['query' => ['test' => 'foo']]);
     $this->assertInstanceOf(PromiseInterface::class, $p);
     $this->assertEquals(200, $p->wait()->getStatusCode());
     $received = Server::received(true);
     $this->assertCount(1, $received);
     $this->assertEquals('test=foo', $received[0]->getUri()->getQuery());
 }
 public function testUsesContentLengthWhenOverInMemorySize()
 {
     Server::flush();
     Server::enqueue([new Response()]);
     $stream = Psr7\stream_for(str_repeat('.', 1000000));
     $handler = new CurlHandler();
     $request = new Request('PUT', Server::$url, ['Content-Length' => 1000000], $stream);
     $handler($request, [])->wait();
     $received = Server::received()[0];
     $this->assertEquals(1000000, $received->getHeaderLine('Content-Length'));
     $this->assertFalse($received->hasHeader('Transfer-Encoding'));
 }
Example #6
0
 public function testReleasesAdditionalEasyHandles()
 {
     Server::flush();
     Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"]);
     $a = new CurlAdapter(new MessageFactory(), ['max_handles' => 2]);
     $client = new Client(['base_url' => Server::$url, 'adapter' => $a]);
     $request = $client->createRequest('GET', '/', ['events' => ['headers' => function (HeadersEvent $e) use($client) {
         $client->get('/', ['events' => ['headers' => function (HeadersEvent $e) {
             $e->getClient()->get('/');
         }]]);
     }]]);
     $transaction = new Transaction($client, $request);
     $a->send($transaction);
     $this->assertCount(2, $this->readAttribute($a, 'handles'));
 }
 public function testInvokesOnStatsOnSuccess()
 {
     Server::flush();
     Server::enqueue([new Psr7\Response(200)]);
     $req = new Psr7\Request('GET', Server::$url);
     $gotStats = null;
     $handler = new Handler\CurlHandler();
     $promise = $handler($req, ['on_stats' => function (TransferStats $stats) use(&$gotStats) {
         $gotStats = $stats;
     }]);
     $response = $promise->wait();
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals(200, $gotStats->getResponse()->getStatusCode());
     $this->assertEquals(Server::$url, (string) $gotStats->getEffectiveUri());
     $this->assertEquals(Server::$url, (string) $gotStats->getRequest()->getUri());
     $this->assertGreaterThan(0, $gotStats->getTransferTime());
 }
 public function testDebugAttributeWritesStreamInfoToBuffer()
 {
     if (defined('HHVM_VERSION')) {
         $this->markTestSkipped('HHVM has not implemented this?');
         return;
     }
     $buffer = fopen('php://temp', 'r+');
     Server::flush();
     Server::enqueue("HTTP/1.1 200 OK\r\nContent-Length: 8\r\nContent-Type: text/plain\r\n\r\nhi there");
     $client = new Client(['base_url' => Server::$url, 'adapter' => new StreamAdapter(new MessageFactory())]);
     $client->get('/', ['debug' => $buffer]);
     fseek($buffer, 0);
     $contents = stream_get_contents($buffer);
     $this->assertContains('<http://127.0.0.1:8125/> [CONNECT]', $contents);
     $this->assertContains('<http://127.0.0.1:8125/> [FILE_SIZE_IS] message: "Content-Length: 8"', $contents);
     $this->assertContains('<http://127.0.0.1:8125/> [PROGRESS] bytes_max: "8"', $contents);
     $this->assertContains('<http://127.0.0.1:8125/> [MIME_TYPE_IS] message: "text/plain"', $contents);
 }
 public function testDoesNotSaveToWhenFailed()
 {
     Server::flush();
     Server::enqueue(["HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n"]);
     $tmp = tempnam('/tmp', 'test_save_to');
     unlink($tmp);
     $a = new CurlAdapter(new MessageFactory());
     $client = new Client(['base_url' => Server::$url, 'adapter' => $a]);
     try {
         $client->get('/', ['save_to' => $tmp]);
     } catch (ServerException $e) {
         $this->assertFileNotExists($tmp);
     }
 }
 public function testThrowsImmediatelyWhenInstructed()
 {
     Server::flush();
     Server::enqueue(["HTTP/1.1 501\r\nContent-Length: 0\r\n\r\n"]);
     $c = new Client(['base_url' => Server::$url]);
     $request = $c->createRequest('GET', '/');
     $request->getEmitter()->on('error', function (ErrorEvent $e) {
         $e->throwImmediately(true);
     });
     $transactions = [new Transaction($c, $request)];
     $a = new MultiAdapter(new MessageFactory());
     try {
         $a->sendAll(new \ArrayIterator($transactions), 1);
         $this->fail('Did not throw');
     } catch (RequestException $e) {
         $this->assertSame($request, $e->getRequest());
     }
 }
Example #11
0
 public function testClientUsesSslByDefault()
 {
     Server::flush();
     Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nfoo"]);
     $f = new CurlFactory();
     $client = new Client(['base_url' => Server::$url, 'adapter' => new MultiAdapter(new MessageFactory(), ['handle_factory' => $f])]);
     $client->get();
     $this->assertEquals(2, $_SERVER['last_curl'][CURLOPT_SSL_VERIFYHOST]);
     $this->assertEquals(true, $_SERVER['last_curl'][CURLOPT_SSL_VERIFYPEER]);
     $this->assertFileExists($_SERVER['last_curl'][CURLOPT_CAINFO]);
 }
Example #12
0
 public function testContentLengthIntegrationTest()
 {
     Server::flush();
     Server::enqueue([new Response(200)]);
     $client = new Client(['base_url' => Server::$url]);
     $this->assertEquals(200, $client->put('/', ['body' => 'test'])->getStatusCode());
     $request = Server::received(true)[0];
     $this->assertEquals('PUT', $request->getMethod());
     $this->assertEquals('4', $request->getHeader('Content-Length'));
     $this->assertEquals('test', (string) $request->getBody());
 }
Example #13
0
 public function testThrowsAndReleasesWhenErrorDuringCompleteEvent()
 {
     Server::flush();
     Server::enqueue("HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n");
     $request = new Request('GET', Server::$url);
     $request->getEmitter()->on('complete', function (CompleteEvent $e) {
         throw new RequestException('foo', $e->getRequest());
     });
     $t = new Transaction(new Client(), $request);
     $a = new MultiAdapter(new MessageFactory());
     try {
         $a->send($t);
         $this->fail('Did not throw');
     } catch (RequestException $e) {
         $this->assertSame($request, $e->getRequest());
     }
 }
Example #14
0
 public function testRewindsStreamOnComplete()
 {
     Server::flush();
     Server::enqueue("HTTP/1.1 200 OK\r\nFoo: bar\r\nContent-Length: 4\r\n\r\ntest");
     $t = new Transaction(new Client(), new Request('GET', Server::$url));
     $a = new MultiAdapter(new MessageFactory());
     $response = $a->send($t);
     $this->assertEquals('test', $response->getBody()->read(4));
 }
Example #15
0
 public function testCanSendPayloadWithGet()
 {
     Server::flush();
     Server::enqueue(["HTTP/1.1 200 OK\r\n\r\n"]);
     $request = new Request('GET', Server::$url, [], Stream::factory('foo'));
     $this->emit($request);
     $t = new Transaction(new Client(), $request);
     $f = new CurlFactory();
     $h = $f($t, new MessageFactory());
     curl_exec($h);
     curl_close($h);
     $sent = Server::received(true)[0];
     $this->assertEquals('foo', (string) $sent->getBody());
     $this->assertEquals(3, (string) $sent->getHeader('Content-Length'));
 }
 public function testDoesNotAddContentTypeByDefault()
 {
     Server::flush();
     Server::enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
     $client = new Client(['base_url' => Server::$url, 'adapter' => new StreamAdapter(new MessageFactory())]);
     $client->put('/', ['body' => 'foo']);
     $requests = Server::received(true);
     $this->assertEquals('', $requests[0]->getHeader('Content-Type'));
     $this->assertEquals(3, $requests[0]->getHeader('Content-Length'));
 }
 private function runConnectionTest($queue, $stream, $msg, $statusCode = null)
 {
     $obj = new \stdClass();
     $er = null;
     $client = new Client();
     $request = $client->createRequest('PUT', Server::$url, ['body' => $stream]);
     $request->getEmitter()->on('error', function (ErrorEvent $e) use(&$er) {
         $er = $e;
     });
     $transaction = $this->getMockBuilder('GuzzleHttp\\Adapter\\Transaction')->setMethods(['getResponse', 'setResponse'])->setConstructorArgs([$client, $request])->getMock();
     $transaction->expects($this->any())->method('setResponse')->will($this->returnCallback(function ($r) use(&$obj) {
         $obj->res = $r;
     }));
     $transaction->expects($this->any())->method('getResponse')->will($this->returnCallback(function () use($obj, &$called) {
         $caller = debug_backtrace()[6]['function'];
         if ($caller == 'addHandle') {
             return null;
         } elseif ($caller == 'validateResponseWasSet') {
             return ++$called == 2 ? $obj->res : null;
         } else {
             return $obj->res;
         }
     }));
     $a = new MultiAdapter(new MessageFactory());
     Server::flush();
     Server::enqueue($queue);
     $a->sendAll(new \ArrayIterator([$transaction]), 10);
     if ($msg) {
         $this->assertNotNull($er);
         $this->assertContains($msg, $er->getException()->getMessage());
     } else {
         $this->assertEquals($statusCode, $transaction->getResponse()->getStatusCode());
     }
 }
Example #18
0
 public function testDoesNotAlwaysAddContentType()
 {
     Server::flush();
     Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"]);
     $client = new Client();
     $client->put(Server::$url . '/foo', ['body' => 'foo']);
     $request = Server::received(true)[0];
     $this->assertEquals('', $request->getHeader('Content-Type'));
 }
 public function testStreamIgnoresZeroTimeout()
 {
     Server::flush();
     Server::enqueue([new Psr7\Response(200)]);
     $req = new Psr7\Request('GET', Server::$url);
     $gotStats = null;
     $handler = new StreamHandler();
     $promise = $handler($req, ['connect_timeout' => 10, 'timeout' => 0]);
     $response = $promise->wait();
     $this->assertEquals(200, $response->getStatusCode());
 }