public function testCanForeach() { $c = new puzzle_Client(); $requests = array($c->createRequest('GET', 'http://test.com'), $c->createRequest('POST', 'http://test.com'), $c->createRequest('PUT', 'http://test.com')); $t = new puzzle_adapter_TransactionIterator(new ArrayIterator($requests), $c, array()); $methods = array(); foreach ($t as $trans) { $this->assertInstanceOf('puzzle_adapter_TransactionInterface', $trans); $methods[] = $trans->getRequest()->getMethod(); } $this->assertEquals(array('GET', 'POST', 'PUT'), $methods); }
/** * Send a custom request * * @param string $method HTTP request method * @param string $url URL of the request * @param array $options Options to use with the request. * * @return puzzle_message_ResponseInterface */ function puzzle_request($method, $url, array $options = array()) { static $client; if (!$client) { $client = new puzzle_Client(); } return $client->send($client->createRequest($method, $url, $options)); }
public function testHandlesCurlErrors() { $client = new puzzle_Client(); $request = $client->createRequest('GET', 'http://localhost:123', array('connect_timeout' => 0.001, 'timeout' => 0.001)); $this->_closure_testHandlesCurlErrors_r = new puzzle_message_Response(200); $request->getEmitter()->on('error', array($this, '__callback_testHandlesCurlErrors')); $transaction = new puzzle_adapter_Transaction($client, $request); $a = new puzzle_adapter_curl_CurlAdapter(new puzzle_message_MessageFactory()); $a->send($transaction); $this->assertSame($this->_closure_testHandlesCurlErrors_r, $transaction->getResponse()); }
public function testEmitsAfterSendEvent() { $this->_closure_testEmitsAfterSendEvent_ee = null; puzzle_test_Server::flush(); puzzle_test_Server::enqueue("HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 8\r\n\r\nhi there"); $client = new puzzle_Client(array('adapter' => new puzzle_adapter_StreamAdapter(new puzzle_message_MessageFactory()))); $request = $client->createRequest('GET', puzzle_test_Server::$url); $request->getEmitter()->on('complete', array($this, '__callback_testEmitsAfterSendEvent')); $client->send($request); $this->assertInstanceOf('puzzle_event_CompleteEvent', $this->_closure_testEmitsAfterSendEvent_ee); $this->assertSame($request, $this->_closure_testEmitsAfterSendEvent_ee->getRequest()); $this->assertEquals(200, $this->_closure_testEmitsAfterSendEvent_ee->getResponse()->getStatusCode()); }
public function testThrowsImmediatelyIfInstructed() { $client = new puzzle_Client(); $request = $client->createRequest('GET', 'http://httbin.org'); $request->getEmitter()->on('error', array($this, '__callback_testThrowsImmediatelyIfInstructed_1')); $this->_closure_sent = array(); $f = new puzzle_adapter_FakeParallelAdapter(new puzzle_adapter_MockAdapter(array($this, '__callback_testThrowsImmediatelyIfInstructed_2'))); $tIter = new puzzle_adapter_TransactionIterator(array($request), $client, array()); try { $f->sendAll($tIter, 1); $this->fail('Did not throw'); } catch (puzzle_exception_RequestException $e) { $this->assertSame($request, $e->getRequest()); } }
public function testCanCastToString() { $client = new puzzle_Client(array('base_url' => 'http://localhost/')); $h = new puzzle_subscriber_History(); $client->getEmitter()->attach($h); $mock = new puzzle_subscriber_Mock(array(new puzzle_message_Response(301, array('Location' => '/redirect1', 'Content-Length' => 0)), new puzzle_message_Response(307, array('Location' => '/redirect2', 'Content-Length' => 0)), new puzzle_message_Response(200, array('Content-Length' => '2'), puzzle_stream_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 testCanMockBadRequestExceptions() { $client = new puzzle_Client(array('base_url' => 'http://test.com')); $request = $client->createRequest('GET', '/'); $ex = new puzzle_exception_RequestException('foo', $request); $mock = new puzzle_subscriber_Mock(array($ex)); $this->assertCount(1, $mock); $request->getEmitter()->attach($mock); try { $client->send($request); $this->fail('Did not dequeue an exception'); } catch (puzzle_exception_RequestException $e) { $this->assertSame($e, $ex); $this->assertSame($request, $ex->getRequest()); } }
public function testCookiesAreExtractedFromRedirectResponses() { $jar = new puzzle_cookie_CookieJar(); $cookie = new puzzle_subscriber_Cookie($jar); $history = new puzzle_subscriber_History(); $mock = new puzzle_subscriber_Mock(array("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 puzzle_Client(array('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 testCanDisableAuthPerRequest() { $client = new puzzle_Client(array('defaults' => array('auth' => 'foo'))); $request = $client->createRequest('GET', 'http://test.com'); $config = $request->getConfig(); $this->assertEquals('foo', $config['auth']); $request = $client->createRequest('GET', 'http://test.com', array('auth' => null)); $this->assertFalse($request->getConfig()->hasKey('auth')); }
public function testThrowsImmediatelyWhenInstructed() { puzzle_test_Server::flush(); puzzle_test_Server::enqueue(array("HTTP/1.1 501\r\nContent-Length: 0\r\n\r\n")); $c = new puzzle_Client(array('base_url' => puzzle_test_Server::$url)); $request = $c->createRequest('GET', '/'); $request->getEmitter()->on('error', array($this, '__callback_testThrowsImmediatelyWhenInstructed')); $transactions = array(new puzzle_adapter_Transaction($c, $request)); $a = new puzzle_adapter_curl_MultiAdapter(new puzzle_message_MessageFactory()); try { $a->sendAll(new ArrayIterator($transactions), 1); $this->fail('Did not throw'); } catch (puzzle_exception_RequestException $e) { $this->assertSame($request, $e->getRequest()); } }
/** * @expectedException InvalidArgumentException */ public function testValidatesEventIsArray() { $client = new puzzle_Client(array('base_url' => 'http://test.com')); $client->createRequest('GET', '/', array('events' => array('before' => '123'))); }
public function testDoesNotForceDecode() { puzzle_test_Server::flush(); $content = gzencode('test'); $message = "HTTP/1.1 200 OK\r\n" . "Content-Encoding: gzip\r\n" . "Content-Length: " . strlen($content) . "\r\n\r\n" . $content; puzzle_test_Server::enqueue($message); $client = new puzzle_Client(); $request = $client->createRequest('GET', puzzle_test_Server::$url, array('headers' => array('Accept-Encoding' => 'gzip'), 'decode_content' => false)); $this->emit($request); $t = new puzzle_adapter_Transaction($client, $request); $f = new puzzle_adapter_curl_CurlFactory(); $h = $f->__invoke($t, new puzzle_message_MessageFactory()); curl_exec($h); curl_close($h); $rx = puzzle_test_Server::received(true); $sent = $rx[0]; $this->assertSame('gzip', $sent->getHeader('Accept-Encoding')); $this->assertSame($content, (string) $t->getResponse()->getBody()); }
/** * @expectedException InvalidArgumentException * @expectedExceptionMessage Invalid event format */ public function testBatchValidatesTheEventFormat() { $client = new puzzle_Client(); $requests = array($client->createRequest('GET', 'http://foo.com/baz')); puzzle_batch($client, $requests, array('complete' => 'foo')); }
* * # Basic usage * make perf * # With custom options * REQUESTS=100 PARALLEL=5000 make perf */ require dirname(__FILE__) . '/bootstrap.php'; // Wait until the server is responding puzzle_test_Server::wait(); // Get custom make variables $total = isset($_SERVER['REQUESTS']) ? $_SERVER['REQUESTS'] : 1000; $parallel = isset($_SERVER['PARALLEL']) ? $_SERVER['PARALLEL'] : 25; $client = new puzzle_Client(array('base_url' => puzzle_test_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 to sendAll $reqs = function () use($client, $total) { for ($i = 0; $i < $total; $i++) { (yield $client->createRequest('GET', '/guzzle-server/perf')); } }; $t = microtime(true); $client->sendAll($reqs(), array('parallel' => $parallel)); $totalTime = microtime(true) - $t; $perRequest = $totalTime / $total * 1000; printf("Parallel: %f (%f ms / request) %d total with %d in parallel\n", $totalTime, $perRequest, $total, $parallel);