Пример #1
0
 /**
  * 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
 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
 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
 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
 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
 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
 /**
  * @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
 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
/*
 * 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
 /**
  * Flush the received requests from the server
  * @throws RuntimeException
  */
 public static function flush()
 {
     self::start();
     return self::$client->delete('guzzle-server/requests');
 }
Пример #16
0
 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
 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
 /**
  * @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'));
 }