Exemplo n.º 1
0
 public function testNestedFutureErrorsAreResolvedWhenSending()
 {
     $c = new Client();
     $total = 3;
     Server::enqueue([new Response(500), new Response(501), new Response(502)]);
     $c->getEmitter()->on('error', function (ErrorEvent $e) use(&$total) {
         if (--$total) {
             $e->retry();
         }
     });
     try {
         $c->get(Server::$url);
         $this->fail('Did not throw!');
     } catch (RequestException $e) {
         $this->assertEquals(502, $e->getResponse()->getStatusCode());
     }
 }
Exemplo n.º 2
0
 public function testCookiesAreExtractedFromRedirectResponses()
 {
     $jar = new CookieJar();
     $cookie = new Cookie($jar);
     $history = new History();
     $mock = new Mock(["HTTP/1.1 302 Moved Temporarily\r\n" . "Set-Cookie: test=583551; Domain=www.foo.com; Expires=Wednesday, 23-Mar-2050 19:49:45 GMT; Path=/\r\n" . "Location: /redirect\r\n\r\n", "HTTP/1.1 200 OK\r\n" . "Content-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\n" . "Content-Length: 0\r\n\r\n"]);
     $client = new Client(['base_url' => 'http://www.foo.com']);
     $client->getEmitter()->attach($cookie);
     $client->getEmitter()->attach($mock);
     $client->getEmitter()->attach($history);
     $client->get();
     $request = $client->createRequest('GET', '/');
     $client->send($request);
     $this->assertEquals('test=583551', $request->getHeader('Cookie'));
     $requests = $history->getRequests();
     // Confirm subsequent requests have the cookie.
     $this->assertEquals('test=583551', $requests[2]->getHeader('Cookie'));
     // Confirm the redirected request has the cookie.
     $this->assertEquals('test=583551', $requests[1]->getHeader('Cookie'));
 }
Exemplo n.º 3
0
 public function testCanMockFailedFutureResponses()
 {
     $client = new Client(['base_url' => 'http://test.com']);
     $request = $client->createRequest('GET', '/', ['future' => true]);
     // The first mock will be a mocked future response.
     $future = self::createFuture(function () use($client) {
         // When dereferenced, we will set a mocked response and send
         // another request.
         $client->get('http://httpbin.org', ['events' => ['before' => function (BeforeEvent $e) {
             $e->intercept(new Response(404));
         }]]);
     });
     $mock = new Mock([$future]);
     $request->getEmitter()->attach($mock);
     $response = $client->send($request);
     $this->assertSame($future, $response);
     $this->assertFalse($this->readAttribute($response, 'isRealized'));
     try {
         $response->wait();
         $this->fail('Did not throw');
     } catch (RequestException $e) {
         $this->assertEquals(404, $e->getResponse()->getStatusCode());
     }
 }
Exemplo n.º 4
0
 /**
  * @expectedException \GuzzleHttp5Legacy\Exception\BadResponseException
  * @expectedExceptionMessage Redirect URL, https://foo.com/redirect2, does not use one of the allowed redirect protocols: http
  */
 public function testThrowsWhenRedirectingToInvalidUrlProtocol()
 {
     $mock = new Mock(["HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect1\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 301 Moved Permanently\r\nLocation: https://foo.com/redirect2\r\nContent-Length: 0\r\n\r\n"]);
     $client = new Client();
     $client->getEmitter()->attach($mock);
     $client->get('http://www.example.com/foo', ['allow_redirects' => ['protocols' => ['http']]]);
 }
Exemplo n.º 5
0
 /**
  * @expectedException \GuzzleHttp5Legacy\Exception\ClientException
  */
 public function testFullTransaction()
 {
     $client = new Client();
     $client->getEmitter()->attach(new Mock([new Response(403)]));
     $client->get('http://httpbin.org');
 }
Exemplo n.º 6
0
 /**
  * @expectedExceptionMessage Noo!
  * @expectedException \GuzzleHttp5Legacy\Exception\RequestException
  */
 public function testThrowsExceptionsSynchronously()
 {
     $client = new Client(['handler' => new MockHandler(['error' => new \Exception('Noo!')])]);
     $client->get('http://localhost:123/foo');
 }
 public function testCanDisableExceptions()
 {
     $client = new Client();
     $this->assertEquals(500, $client->get('http://test.com', ['subscribers' => [new Mock([new Response(500)])], 'exceptions' => false])->getStatusCode());
 }
Exemplo n.º 8
0
$total = isset($_SERVER['REQUESTS']) ? $_SERVER['REQUESTS'] : 1000;
$parallel = isset($_SERVER['PARALLEL']) ? $_SERVER['PARALLEL'] : 100;
$client = new Client(['base_url' => Server::$url]);
$t = microtime(true);
for ($i = 0; $i < $total; $i++) {
    $client->get('/guzzle-server/perf');
}
$totalTime = microtime(true) - $t;
$perRequest = $totalTime / $total * 1000;
printf("Serial: %f (%f ms / request) %d total\n", $totalTime, $perRequest, $total);
// Create a generator used to yield batches of requests
$reqs = function () use($client, $total) {
    for ($i = 0; $i < $total; $i++) {
        (yield $client->createRequest('GET', '/guzzle-server/perf'));
    }
};
$t = microtime(true);
Pool::send($client, $reqs(), ['parallel' => $parallel]);
$totalTime = microtime(true) - $t;
$perRequest = $totalTime / $total * 1000;
printf("Batch:  %f (%f ms / request) %d total with %d in parallel\n", $totalTime, $perRequest, $total, $parallel);
$handler = new CurlMultiHandler(['max_handles' => $parallel]);
$client = new Client(['handler' => $handler, 'base_url' => Server::$url]);
$t = microtime(true);
for ($i = 0; $i < $total; $i++) {
    $client->get('/guzzle-server/perf');
}
unset($client);
$totalTime = microtime(true) - $t;
$perRequest = $totalTime / $total * 1000;
printf("Future: %f (%f ms / request) %d total\n", $totalTime, $perRequest, $total);