function test_hook_modify() { $this->hook_flags = array(); $rest = REST_Client::factory('async', array('verbose' => false))->addFireHook(array($this, 'fire_hook_modify'))->addFetchHook(array($this, 'fetch_hook_modify')); $r = REST_Request::newInstance()->setProtocol('http')->setHost($this->test_host)->setPort($this->test_port); $rest->fire($r->get('/')); $resp = $rest->fetch(); $this->assertEquals('/mymodifiedurl', $r->getUrl()); $this->assertEquals(404, $resp->code); }
function test_huge_async() { $requests = 50000; $clients = 150; $this->async->setOption('queue_size', $clients); $r = REST_Request::newInstance()->setProtocol('http')->setHost($this->test_host)->setPort($this->test_port)->setMethod('GET')->setUrl('/'); for ($i = 0; $i < $requests; $i++) { $this->async->fire($r); if ($this->async->overflow()) { while ($response = $this->async->fetch()) { $this->assertEquals(200, $response->code); } } } while ($response = $this->async->fetch()) { $this->assertEquals(200, $response->code); } $this->assertEquals($requests, $this->async->getInfo('requests')); }
/** * Launch a synchrone request * returns the request identifier or false if fire is aborted * @param array * @return integer or false */ public function fire(REST_Request $request) { if ($this->loads === 0) { $this->time = microtime(true); } ++$this->loads; self::$handles++; // create a fresh request identifier $request->setCurlOption(CURLOPT_USERAGENT, 'REST_Client/' . self::$version); // launch the fire hooks foreach ($this->fire_hook as $hook) { $ret = call_user_func($hook, $request, self::$handles, $this); // this hook want to stop the fire ? if ($ret === false) { ++$this->loads_null; self::$handles--; return false; } } // configure curl client curl_setopt_array($this->handle, $request->toCurl()); if (!is_resource($this->handle)) { ++$this->loads_null; return trigger_error(sprintf('%s::%s() cURL session was lost', __CLASS__, $method), E_USER_ERROR); } // send the request and create the response object ++$this->requests; $response = new REST_Response(curl_exec($this->handle), curl_errno($this->handle), curl_error($this->handle)); if (!$response->isError()) { foreach (REST_Response::$properties as $name => $const) { $response->{$name} = curl_getinfo($this->handle, $const); } } $response->id = self::$handles; $this->responses[] = $response; // append the response to the stack return self::$handles; // return a unique identifier for the request }
function test_medium_async() { $this->async->setOption('queue_size', 3); $r = REST_Request::newInstance()->setProtocol('http')->setHost('fr.php.net')->setHttpProxy($this->http_proxy); $dom = $this->async->fire($r->get('/dom')); $curl = $this->async->fire($r->get('/curl')); $strings = $this->async->fire($r->get('/strings')); $pcre = $this->async->fire($r->get('/pcre')); $xml = $this->async->fire($r->get('/xml')); $ftp = $this->async->fire($r->get('/ftp')); $sockets = $this->async->fire($r->get('/sockets')); $z = array(); while ($response = $this->async->fetch()) { $z[$response->id] = $response->content; } $this->assertContains('PHP: cURL - Manual', $z[$curl]); $this->assertContains('PHP: DOM - Manual', $z[$dom]); $this->assertContains('id="book.strings"', $z[$strings]); $this->assertContains('PHP: PCRE - Manual', $z[$pcre]); $this->assertContains('id="book.xml"', $z[$xml]); $this->assertContains('PHP: FTP - Manual', $z[$ftp]); $this->assertContains('PHP: Sockets - Manual', $z[$sockets]); $this->assertEquals(7, $this->async->getInfo('requests')); }
public function __construct($host = 'localhost', $port = 80, $options = array()) { $this->request = REST_Request::newInstance()->setProtocol('http')->setHost($host)->setPort($port)->setCurlOptions($options); $this->client = REST_Client::factory('sync', array('verbose' => false)); }