public function testStripsFragmentFromHost() { puzzle_test_Server::flush(); puzzle_test_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 = puzzle_Url::fromString(puzzle_test_Server::$url)->setPath(null)->setFragment('foo'); $client = new puzzle_Client(); $client->get($url); }
public function testDoesNotSaveToWhenFailed() { puzzle_test_Server::flush(); puzzle_test_Server::enqueue(array("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 puzzle_adapter_curl_CurlAdapter(new puzzle_message_MessageFactory()); $client = new puzzle_Client(array('base_url' => puzzle_test_Server::$url, 'adapter' => $a)); try { $client->get('/', array('save_to' => $tmp)); } catch (puzzle_exception_ServerException $e) { $this->assertFileNotExists($tmp); } }
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 testRelativeLinkBasedLatestRequest() { $client = new puzzle_Client(array('base_url' => 'http://www.foo.com')); $client->getEmitter()->attach(new puzzle_subscriber_Mock(array("HTTP/1.1 301 Moved Permanently\r\nLocation: http://www.bar.com\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"))); $response = $client->get('/'); $this->assertEquals('http://www.bar.com/redirect', $response->getEffectiveUrl()); }
/** * @expectedException puzzle_exception_RequestException * @expectedExceptionMessage foo */ public function testClientWrapsExceptions() { $client = new puzzle_Client(); $client->getEmitter()->on('before', array($this, '__callback_throwFooException')); $client->get('http://httpbin.org'); }
/** * @expectedException puzzle_exception_ClientException */ public function testFullTransaction() { $client = new puzzle_Client(); $client->getEmitter()->attach(new puzzle_subscriber_Mock(array(new puzzle_message_Response(403)))); $client->get('http://httpbin.org'); }
public function testCanDisableExceptions() { $client = new puzzle_Client(); $this->assertEquals(500, $client->get('http://test.com', array('subscribers' => array(new puzzle_subscriber_Mock(array(new puzzle_message_Response(500)))), 'exceptions' => false))->getStatusCode()); }
public function testClientUsesSslByDefault() { puzzle_test_Server::flush(); puzzle_test_Server::enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nfoo")); $f = new puzzle_adapter_curl_CurlFactory(); $client = new puzzle_Client(array('base_url' => puzzle_test_Server::$url, 'adapter' => new puzzle_adapter_curl_MultiAdapter(new puzzle_message_MessageFactory(), array('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]); }
public function testDebugAttributeWritesStreamInfoToBuffer() { if (defined('HHVM_VERSION')) { $this->markTestSkipped('HHVM has not implemented this?'); return; } $buffer = fopen('php://temp', 'r+'); puzzle_test_Server::flush(); puzzle_test_Server::enqueue("HTTP/1.1 200 OK\r\nContent-Length: 8\r\nContent-Type: text/plain\r\n\r\nhi there"); $client = new puzzle_Client(array('base_url' => puzzle_test_Server::$url, 'adapter' => new puzzle_adapter_StreamAdapter(new puzzle_message_MessageFactory()))); $client->get('/', array('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); }
* * # 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);
/** * Note: Longest test name ever. */ public function testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent() { $request = new puzzle_message_Request('GET', '/'); $this->_closure_testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent_ex = new puzzle_exception_RequestException('foo', $request); $client = new puzzle_Client(); $client->getEmitter()->on('before', array($this, '__callback_testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent_1')); $this->_closure_testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent_called = false; $client->getEmitter()->on('error', array($this, '__callback_testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent_2')); try { $client->get('http://foo.com'); $this->fail('Did not throw'); } catch (puzzle_exception_RequestException $e) { $this->assertTrue($this->_closure_testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent_called); } }