コード例 #1
0
ファイル: functions.php プロジェクト: puzzlehttp/puzzle
 /**
  * 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));
 }
コード例 #2
0
 public function testEmitspuzzle_event_HeadersEventForHeadRequest()
 {
     puzzle_test_Server::enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK"));
     $this->_closure_testEmitspuzzle_event_HeadersEventForHeadRequest_ee = null;
     $client = new puzzle_Client(array('adapter' => new puzzle_adapter_curl_MultiAdapter(new puzzle_message_MessageFactory())));
     $client->head(puzzle_test_Server::$url, array('events' => array('headers' => array($this, '__callback_testEmitspuzzle_event_HeadersEventForHeadRequest'))));
     $this->assertInstanceOf('puzzle_event_HeadersEvent', $this->_closure_testEmitspuzzle_event_HeadersEventForHeadRequest_ee);
 }
コード例 #3
0
ファイル: AbstractCurl.php プロジェクト: puzzlehttp/puzzle
 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);
 }
コード例 #4
0
 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);
 }
コード例 #5
0
 public function __construct(tubepress_api_event_EventDispatcherInterface $eventDispatcher, puzzle_Client $delegate, tubepress_api_log_LoggerInterface $logger)
 {
     parent::__construct($eventDispatcher, $logger);
     if (!function_exists('puzzle_request')) {
         require TUBEPRESS_ROOT . '/vendor/puzzlehttp/puzzle/src/main/php/puzzle/functions.php';
     }
     if (!function_exists('puzzle_stream_create')) {
         require TUBEPRESS_ROOT . '/vendor/puzzlehttp/streams/src/main/php/puzzle/stream/functions.php';
     }
     $this->_delegate = $delegate;
     $this->_delegate->setDefaultOption('verify', TUBEPRESS_ROOT . '/vendor/puzzlehttp/puzzle/src/main/php/puzzle/cacert.pem');
     $delegate->getEmitter()->attach($this);
 }
コード例 #6
0
ファイル: CurlAdapterTest.php プロジェクト: puzzlehttp/puzzle
 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);
     }
 }
コード例 #7
0
 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());
     }
 }
コード例 #8
0
ファイル: HistoryTest.php プロジェクト: puzzlehttp/puzzle
 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);
 }
コード例 #9
0
ファイル: MockTest.php プロジェクト: puzzlehttp/puzzle
 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());
     }
 }
コード例 #10
0
ファイル: CookieTest.php プロジェクト: puzzlehttp/puzzle
 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'));
 }
コード例 #11
0
ファイル: HttpErrorTest.php プロジェクト: puzzlehttp/puzzle
 /**
  * @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');
 }
コード例 #12
0
ファイル: RedirectTest.php プロジェクト: puzzlehttp/puzzle
 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());
 }
コード例 #13
0
ファイル: perf.php プロジェクト: puzzlehttp/puzzle
/*
 * Runs a performance test against the node.js server for both serial and
 * parallel requests. Requires PHP 5.5 or greater.
 *
 *     # 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));
コード例 #14
0
 public function testDoesNotAddContentTypeByDefault()
 {
     puzzle_test_Server::flush();
     puzzle_test_Server::enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
     $client = new puzzle_Client(array('base_url' => puzzle_test_Server::$url, 'adapter' => new puzzle_adapter_StreamAdapter(new puzzle_message_MessageFactory())));
     $client->put('/', array('body' => 'foo'));
     $requests = puzzle_test_Server::received(true);
     $this->assertEquals('', $requests[0]->getHeader('Content-Type'));
     $this->assertEquals(3, $requests[0]->getHeader('Content-Length'));
 }
コード例 #15
0
ファイル: Server.php プロジェクト: puzzlehttp/puzzle
 /**
  * Flush the received requests from the server
  * @throws RuntimeException
  */
 public static function flush()
 {
     self::start();
     return self::$client->delete('guzzle-server/requests');
 }
コード例 #16
0
ファイル: ClientTest.php プロジェクト: puzzlehttp/puzzle
 public function testUsesProxyEnvironmentVariables()
 {
     $http = isset($_SERVER['HTTP_PROXY']) ? $_SERVER['HTTP_PROXY'] : null;
     $https = isset($_SERVER['HTTPS_PROXY']) ? $_SERVER['HTTPS_PROXY'] : null;
     unset($_SERVER['HTTP_PROXY']);
     unset($_SERVER['HTTPS_PROXY']);
     $client = new puzzle_Client();
     $this->assertNull($client->getDefaultOption('proxy'));
     $_SERVER['HTTP_PROXY'] = '127.0.0.1';
     $client = new puzzle_Client();
     $this->assertEquals(array('http' => '127.0.0.1'), $client->getDefaultOption('proxy'));
     $_SERVER['HTTPS_PROXY'] = '127.0.0.2';
     $client = new puzzle_Client();
     $this->assertEquals(array('http' => '127.0.0.1', 'https' => '127.0.0.2'), $client->getDefaultOption('proxy'));
     $_SERVER['HTTP_PROXY'] = $http;
     $_SERVER['HTTPS_PROXY'] = $https;
 }
コード例 #17
0
 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());
     }
 }
コード例 #18
0
 /**
  * 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);
     }
 }
コード例 #19
0
 public function testCanForceMultipartUploadWithContentType()
 {
     $client = new puzzle_Client();
     $client->getEmitter()->attach(new puzzle_subscriber_Mock(array(new puzzle_message_Response(200))));
     $history = new puzzle_subscriber_History();
     $client->getEmitter()->attach($history);
     $client->post('http://foo.com', array('headers' => array('Content-Type' => 'multipart/form-data'), 'body' => array('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());
 }
コード例 #20
0
ファイル: CurlFactoryTest.php プロジェクト: puzzlehttp/puzzle
 public function testDoesNotAlwaysAddContentType()
 {
     puzzle_test_Server::flush();
     puzzle_test_Server::enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"));
     $client = new puzzle_Client();
     $client->put(puzzle_test_Server::$url . '/foo', array('body' => 'foo'));
     $rx = puzzle_test_Server::received(true);
     $request = $rx[0];
     $this->assertEquals('', $request->getHeader('Content-Type'));
 }
コード例 #21
0
ファイル: FunctionsTest.php プロジェクト: puzzlehttp/puzzle
 /**
  * @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'));
 }