예제 #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());
     }
 }
예제 #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'));
 }
예제 #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());
     }
 }
예제 #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']]]);
 }
예제 #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');
 }
예제 #6
0
 public function testCanCastToString()
 {
     $client = new Client(['base_url' => 'http://localhost/']);
     $h = new History();
     $client->getEmitter()->attach($h);
     $mock = new Mock(array(new Response(301, array('Location' => '/redirect1', 'Content-Length' => 0)), new Response(307, array('Location' => '/redirect2', 'Content-Length' => 0)), new Response(200, array('Content-Length' => '2'), Stream::factory('HI'))));
     $client->getEmitter()->attach($mock);
     $request = $client->createRequest('GET', '/');
     $client->send($request);
     $this->assertEquals(3, count($h));
     $h = str_replace("\r", '', $h);
     $this->assertContains("> GET / HTTP/1.1\nHost: localhost\nUser-Agent:", $h);
     $this->assertContains("< HTTP/1.1 301 Moved Permanently\nLocation: /redirect1", $h);
     $this->assertContains("< HTTP/1.1 307 Temporary Redirect\nLocation: /redirect2", $h);
     $this->assertContains("< HTTP/1.1 200 OK\nContent-Length: 2\n\nHI", $h);
 }
예제 #7
0
 public function testDoesNotInfinitelyRecurse()
 {
     $client = new Client(['handler' => function () {
         throw new \RuntimeException('No network access');
     }]);
     $last = null;
     $client->getEmitter()->on('before', function (BeforeEvent $e) use(&$last) {
         $e->intercept(new Response(200));
         if (function_exists('xdebug_get_stack_depth')) {
             if ($last) {
                 $this->assertEquals($last, xdebug_get_stack_depth());
             } else {
                 $last = xdebug_get_stack_depth();
             }
         }
     });
     $requests = [];
     for ($i = 0; $i < 100; $i++) {
         $requests[] = $client->createRequest('GET', 'http://foo.com');
     }
     $pool = new Pool($client, $requests);
     $pool->wait();
 }
예제 #8
0
 /**
  * @expectedException \GuzzleHttp5Legacy\Exception\RequestException
  * @expectedExceptionMessage Too many state transitions
  */
 public function testDetectsInfiniteLoops()
 {
     $client = new Client(['fsm' => $fsm = new RequestFsm(function () {
         return new CompletedFutureArray(['status' => 200]);
     }, new MessageFactory(), 3)]);
     $request = $client->createRequest('GET', 'http://foo.com:123');
     $request->getEmitter()->on('before', function () {
         throw new \Exception('foo');
     });
     $request->getEmitter()->on('error', function ($e) {
         $e->retry();
     });
     $client->send($request);
 }
예제 #9
0
 public function testCanUseUrlWithCustomQuery()
 {
     $client = new Client();
     $url = Url::fromString('http://foo.com/bar');
     $query = new Query(['baz' => '123%20']);
     $query->setEncodingType(false);
     $url->setQuery($query);
     $r = $client->createRequest('GET', $url);
     $this->assertEquals('http://foo.com/bar?baz=123%20', $r->getUrl());
 }
 public function testCanForceMultipartUploadWithContentType()
 {
     $client = new Client();
     $client->getEmitter()->attach(new Mock([new Response(200)]));
     $history = new History();
     $client->getEmitter()->attach($history);
     $client->post('http://foo.com', ['headers' => ['Content-Type' => 'multipart/form-data'], 'body' => ['foo' => 'bar']]);
     $this->assertContains('multipart/form-data; boundary=', $history->getLastRequest()->getHeader('Content-Type'));
     $this->assertContains("Content-Disposition: form-data; name=\"foo\"\r\n\r\nbar", (string) $history->getLastRequest()->getBody());
 }
예제 #11
0
 public function testCanInterceptException()
 {
     $client = new Client(['base_url' => 'http://127.0.0.1:123']);
     $request = $client->createRequest('GET');
     $called = false;
     $request->getEmitter()->on('error', function (ErrorEvent $e) use(&$called) {
         $called = true;
         $e->intercept(new Response(200));
     });
     $request->getConfig()['timeout'] = 0.001;
     $request->getConfig()['connect_timeout'] = 0.001;
     $this->assertEquals(200, $client->send($request)->getStatusCode());
     $this->assertTrue($called);
 }
예제 #12
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);
예제 #13
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());
 }