public function testRedirectsRequests() { $mock = new Mock(); $history = new History(); $mock->addMultiple(["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: /redirect2\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"]); $client = new Client(['base_url' => 'http://test.com']); $client->getEmitter()->attach($history); $client->getEmitter()->attach($mock); $request = $client->createRequest('GET', '/foo'); // Ensure "end" is called only once $called = 0; $request->getEmitter()->on('end', function () use(&$called) { $called++; }); $response = $client->send($request); $this->assertEquals(200, $response->getStatusCode()); $this->assertContains('/redirect2', $response->getEffectiveUrl()); // Ensure that two requests were sent $requests = $history->getRequests(true); $this->assertEquals('/foo', $requests[0]->getPath()); $this->assertEquals('GET', $requests[0]->getMethod()); $this->assertEquals('/redirect1', $requests[1]->getPath()); $this->assertEquals('GET', $requests[1]->getMethod()); $this->assertEquals('/redirect2', $requests[2]->getPath()); $this->assertEquals('GET', $requests[2]->getMethod()); $this->assertEquals(1, $called); }
/** * @issue https://github.com/guzzle/guzzle/issues/867 */ public function testDoesNotFailInEventSystemForNetworkError() { $c = new Client(); $r = $c->createRequest('GET', Server::$url, ['timeout' => 1, 'connect_timeout' => 1, 'proxy' => 'http://127.0.0.1:123/foo']); $events = []; $fn = function (AbstractTransferEvent $event) use(&$events) { $events[] = [get_class($event), $event->hasResponse(), $event->getResponse()]; }; $pool = new Pool($c, [$r], ['error' => $fn, 'end' => $fn]); $pool->wait(); $this->assertCount(2, $events); $this->assertEquals('GuzzleHttp5Legacy\\Event\\ErrorEvent', $events[0][0]); $this->assertFalse($events[0][1]); $this->assertNull($events[0][2]); $this->assertEquals('GuzzleHttp5Legacy\\Event\\EndEvent', $events[1][0]); $this->assertFalse($events[1][1]); $this->assertNull($events[1][2]); }
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')); }
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()); } }
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); }
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(); }
/** * @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); }
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()); }
/** * @expectedException \InvalidArgumentException */ public function testValidatesEventIsArray() { $client = new Client(['base_url' => 'http://test.com']); $client->createRequest('GET', '/', ['events' => ['before' => '123']]); }
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); }
Server::wait(); // Get custom make variables $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;