Beispiel #1
0
 /**
  * @param string $user
  * @param string $apiKey
  *
  * @return Request2
  */
 public function getRequest($user, $apiKey)
 {
     if ($this->req === null) {
         $this->req = new Request2();
         $this->req->setAdapter('curl')->setHeader('user-agent', 'Services_Librato');
     }
     $this->req->setAuth($user, $apiKey);
     return $this->req;
 }
Beispiel #2
0
 public function testSetRequest()
 {
     $newswire = new Newswire('apikey');
     $req = new \HTTP_Request2();
     $req->setAdapter('mock');
     $this->assertInstanceOf('PEAR2\\Services\\NYTimes\\Newswire', $newswire->accept($req));
 }
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 protected function setUp()
 {
     $this->mock = new HTTP_Request2_Adapter_Mock();
     $request = new HTTP_Request2();
     $request->setAdapter($this->mock);
     $this->validator = new Services_W3C_CSSValidator($request);
 }
Beispiel #4
0
 private function request($method, $path, $params = array())
 {
     $url = $this->api . rtrim($path, '/') . '/';
     if (!strcmp($method, "POST")) {
         $req = new HTTP_Request2($url, HTTP_Request2::METHOD_POST);
         $req->setHeader('Content-type: application/json');
         if ($params) {
             $req->setBody(json_encode($params));
         }
     } else {
         if (!strcmp($method, "GET")) {
             $req = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
             $url = $req->getUrl();
             $url->setQueryVariables($params);
         } else {
             if (!strcmp($method, "DELETE")) {
                 $req = new HTTP_Request2($url, HTTP_Request2::METHOD_DELETE);
                 $url = $req->getUrl();
                 $url->setQueryVariables($params);
             }
         }
     }
     $req->setAdapter('curl');
     $req->setConfig(array('timeout' => 30));
     $req->setAuth($this->auth_id, $this->auth_token, HTTP_Request2::AUTH_BASIC);
     $req->setHeader(array('Connection' => 'close', 'User-Agent' => 'PHPPlivo'));
     $r = $req->send();
     $status = $r->getStatus();
     $body = $r->getbody();
     $response = json_decode($body, true);
     return array("status" => $status, "response" => $response);
 }
 protected function addHttpMock(Net_WebFinger $wf)
 {
     $this->adapter = new HTTP_Request2_Adapter_LogMock();
     $req = new HTTP_Request2();
     $req->setAdapter($this->adapter);
     $wf->setHttpClient($req);
     return $this;
 }
Beispiel #6
0
 /**
  * @expectedException Services_Yadis_Exception
  * @expectedExceptionMessage Invalid response to Yadis protocol received: A test error
  */
 public function testGetException()
 {
     $httpMock = new HTTP_Request2_Adapter_Mock();
     $httpMock->addResponse(new HTTP_Request2_Exception('A test error', 500));
     $http = new HTTP_Request2();
     $http->setAdapter($httpMock);
     $sy = new Services_Yadis('http://example.org/openid');
     $sy->setHttpRequest($http);
     $xrds = $sy->discover();
 }
Beispiel #7
0
function getContents($url, $start = 0, $userAgent = null)
{
    if ($start > 0) {
        $url .= "&start={$start}";
    }
    $http = new HTTP_Request2($url);
    $http->setAdapter('curl');
    if ($userAgent !== null) {
        $http->setHeader('User-Agent', $userAgent);
    }
    return $http->send();
}
 public function setUp()
 {
     parent::setUp();
     Yii::configure(Yii::$app, ['components' => ['user' => ['class' => 'yii\\web\\User', 'identityClass' => 'common\\models\\User'], 'p24' => ['class' => \merigold\przelewy24\src\Przelewy24Component::className(), 'merchant_id' => $this->merchant_id, 'pos_id' => $this->merchant_id, 'CRC' => $this->CRC]]]);
     Yii::$container->set('HTTP_Request2', function () {
         $response = new HTTP_Request2_Response("HTTP/1.1 200 OK", true);
         $response->appendBody($this->succesresponse);
         $mockAdapter = new HTTP_Request2_Adapter_Mock();
         $mockAdapter->addResponse($response);
         $stub = new HTTP_Request2(\merigold\przelewy24\src\Przelewy24Component::TEST_URL, HTTP_Request2::METHOD_POST);
         $stub->setAdapter($mockAdapter);
         return $stub;
     });
 }
 public function testShouldCompleteTransaction()
 {
     $mock = new HTTP_Request2_Adapter_Mock();
     $mock->addResponse('HTTP/1.1 200 OK');
     $request = new HTTP_Request2();
     $request->setAdapter($mock);
     $object = Payment_Process2::factory('Dummy');
     $object->login = '******';
     $object->password = '******';
     $object->action = Payment_Process2::ACTION_NORMAL;
     $object->amount = 1;
     $object->setRequest($request);
     $object->setPayment($this->aValidPayment());
     $result = $object->process();
     $this->assertTrue($result instanceof Payment_Process2_Result_Dummy);
 }
Beispiel #10
0
 private function http2_request($method, $path, $params)
 {
     $url = $this->api . $path;
     $http_method = \HTTP_Request2::METHOD_POST;
     if (!strcmp($method, "GET")) {
         $http_method = \HTTP_Request2::METHOD_GET;
     } else {
         if (!strcmp($method, "DELETE")) {
             $http_method = \HTTP_Request2::METHOD_DELETE;
         }
     }
     $req = new \HTTP_Request2($url, $http_method);
     if ($http_method === \HTTP_Request2::METHOD_POST && $params) {
         $req->setBody(json_encode($params));
     }
     $req->setAdapter('curl');
     $req->setConfig(array('timeout' => 30, 'ssl_verify_peer' => FALSE));
     $req->setHeader(array('Authorization' => $this->auth_token, 'Connection' => 'close', 'User-Agent' => 'CheckMobi/http2_request', 'Content-type' => 'application/json'));
     $r = $req->send();
     $status = $r->getStatus();
     $body = $r->getbody();
     $response = json_decode($body, true);
     return array("status" => $status, "response" => $response);
 }
 public static function mockDataProviderHelper($file)
 {
     $process = self::aProcessor();
     $mock = new HTTP_Request2_Adapter_Mock();
     $mock->addResponse(self::aResponse($file));
     $request = new HTTP_Request2();
     $request->setAdapter($mock);
     $process->setRequest($request);
     return array($process);
 }
 public function testResponseException()
 {
     $mock = new HTTP_Request2_Adapter_Mock();
     $mock->addResponse(new HTTP_Request2_Exception('Shit happens'));
     $req = new HTTP_Request2('http://www.example.com/');
     $req->setAdapter($mock);
     try {
         $req->send();
     } catch (Exception $e) {
         $this->assertEquals('Shit happens', $e->getMessage());
         return;
     }
     $this->fail('Expected HTTP_Request2_Exception was not thrown');
 }
Beispiel #13
0
 /**
  * Instantiates HTTP_Request2.  Abstracted for testing.
  * 
  * @see directRequest()
  * @return HTTP_Request2_Response
  */
 protected function getHTTPRequest2Instance()
 {
     // @codeCoverageIgnoreStart
     $request = new HTTP_Request2();
     $request->setAdapter('curl');
     return $request;
     // @codeCoverageIgnoreEnd
 }
Beispiel #14
0
 /**
  * The actual method for sending requests
  *
  * @param	string	$url
  * @param	array	$headers
  * @param	array	$params
  * @return	HTTP_Request2_Response
  */
 protected function send($method, $url, array $params = array(), array $headers = array())
 {
     $headers['GData-Version'] = '2';
     $headers['User-Agent'] = 'Vivvo/' . VIVVO_VERSION . ' (' . self::$request_adapter . ') PHP/' . PHP_VERSION;
     $request = new HTTP_Request2($url);
     $request->setAdapter(self::$request_adapter);
     $request->setConfig('ssl_verify_peer', false);
     $request->setConfig('follow_redirects', true);
     $request->setHeader($headers);
     $request->setMethod($method);
     if ($method == HTTP_Request2::METHOD_POST) {
         $request->addPostParameter($params);
     }
     return $request->send();
 }
 public function testShouldFailGracefullyOnFailedTransaction()
 {
     $response = new HTTP_Request2_Response('HTTP/1.1 200 OK');
     $response->appendBody(file_get_contents(dirname(__FILE__) . '/data/TrustCommerce/error.html'));
     $mock = new HTTP_Request2_Adapter_Mock();
     $mock->addResponse($response);
     $request = new HTTP_Request2();
     $request->setAdapter($mock);
     $object = Payment_Process2::factory('TrustCommerce');
     $object->setPayment($this->aValidPayment());
     $object->login = '******';
     $object->password = '******';
     $object->amount = 1;
     $object->action = Payment_Process2::ACTION_NORMAL;
     $object->setRequest($request);
     $result = $object->process();
     $this->assertTrue($result instanceof Payment_Process2_Result_TrustCommerce);
     $this->assertSame(Payment_Process2::RESULT_OTHER, $result->getCode(), $result->getMessage());
 }
 /**
  * Send the request to Airbrake using PEAR
  * @return integer
  * @author Rich Cavanaugh
  **/
 public function pearRequest($url, $headers, $body)
 {
     if (!class_exists('HTTP_Request2')) {
         require_once 'HTTP/Request2.php';
     }
     if (!class_exists('HTTP_Request2_Adapter_Socket')) {
         require_once 'HTTP/Request2/Adapter/Socket.php';
     }
     $adapter = new HTTP_Request2_Adapter_Socket();
     $req = new HTTP_Request2($url, HTTP_Request2::METHOD_POST);
     $req->setAdapter($adapter);
     $req->setHeader($headers);
     $req->setBody($body);
     return $req->send()->getStatus();
 }
 public function testShouldFailGracefullyOnFailedTransaction()
 {
     $mock = new HTTP_Request2_Adapter_Mock();
     $mock->addResponse('HTTP/1.1 404 Not Found');
     $request = new HTTP_Request2();
     $request->setAdapter($mock);
     $options = array('keyfile' => __FILE__, 'authorizeUri' => "http://google.com/");
     $object = Payment_Process2::factory('LinkPoint', $options);
     $object->login = '******';
     $object->password = '******';
     $object->amount = 1;
     $object->action = Payment_Process2::ACTION_NORMAL;
     $object->setRequest($request);
     $object->setPayment($this->aValidPayment());
     $result = $object->process();
     $this->assertTrue($result instanceof PEAR_Error);
 }
Beispiel #18
0
 /**
  * Notify a new error
  * @param String $message
  * @param Date $raised_at UTC date
  * @param array $backtrace
  * @param array $request
  * @param array $environnement
  * @param array $data
  * @return boolean
  */
 public function notify($message, $raised_at = null, $backtrace = array(), $request = null, $environnement = null, $data = null)
 {
     $http_request = new HTTP_Request2($this->formatUrl(), HTTP_Request2::METHOD_POST);
     if (!is_null($this->adapter)) {
         $http_request->setAdapter($this->adapter);
     }
     if (is_null($raised_at)) {
         $raised_at = date('c');
     }
     if (count($backtrace) > 1 && !isset($backtrace[0]['file'])) {
         array_shift($backtrace);
     }
     $http_request->addPostParameter('api_key', $this->api_key);
     $http_request->addPostParameter('version', $this->version);
     $http_request->addPostParameter('error', array('message' => $message, 'raised_at' => $raised_at, 'backtrace' => $backtrace, 'request' => $request, 'environment' => $environnement, 'data' => $data));
     try {
         $response = $http_request->send();
         if ($response->getStatus() == 200) {
             return true;
         }
         return false;
     } catch (HTTP_Request2_Exception $e) {
         return false;
     }
 }
Beispiel #19
0
 /**
  * testCheckAuthentication 
  * 
  * @return void
  */
 public function testCheckAuthentication()
 {
     $opEndpoint = new OpenID_ServiceEndpoint();
     $opEndpoint->setURIs(array($this->opEndpointURL));
     $opEndpoints = new OpenID_ServiceEndpoints($this->claimedID, $opEndpoint);
     $this->discover = $this->getMock('OpenID_Discover', array('__get'), array($this->claimedID));
     $this->discover->expects($this->once())->method('__get')->will($this->returnValue($opEndpoints));
     $this->store->expects($this->once())->method('getDiscover')->will($this->returnValue($this->discover));
     $this->store->expects($this->once())->method('getNonce')->will($this->returnValue(false));
     $this->createObjects();
     $adapter = new HTTP_Request2_Adapter_Mock();
     $content = "HTTP/1.1 200\n";
     $content .= "Content-Type: text/html; charset=iso-8859-1\n\n\n";
     $content .= "foo:bar\n";
     $adapter->addResponse($content);
     $httpRequest = new HTTP_Request2();
     $httpRequest->setAdapter($adapter);
     $this->assertion->expects($this->once())->method('getHTTPRequest2Instance')->will($this->returnValue($httpRequest));
     $result = $this->assertion->checkAuthentication();
 }
Beispiel #20
0
 public function setUp()
 {
     $this->mock = new HTTP_Request2_Adapter_Mock();
     $request = new HTTP_Request2();
     $request->setAdapter($this->mock);
     $this->akismet = new Services_Akismet2('http://blog.example.com/', 'AABBCCDDEEFF', array(), $request);
 }
 protected function createRequest(HTTP_Request2_Adapter $adapter)
 {
     $request = new HTTP_Request2();
     $request->setAdapter($adapter);
     return $request;
 }
 protected function getHTTPRequester()
 {
     $r = new HTTP_Request2();
     $r->setAdapter($this->adapter);
     return $r;
 }
Beispiel #23
0
 /**
  * Sends the request to the Scribd API
  *
  * @param string $uri    The API URI to request
  * @param string $method The HTTP method to use
  *
  * @throws Services_Scribd_Exception
  * @return void
  */
 protected function sendRequest($uri, $method)
 {
     $config = array('timeout' => $this->timeout);
     $request = new HTTP_Request2($uri, $method, $config);
     $request->setHeader('User-Agent', '@package-name@-@package-version@');
     if ($this->requestAdapter !== null) {
         $request->setAdapter($this->requestAdapter);
     }
     if ($method === HTTP_Request2::METHOD_POST) {
         if (array_key_exists('file', $this->arguments)) {
             $request->addUpload('file', $this->arguments['file']);
             unset($this->arguments['file']);
         }
         $request = $request->addPostParameter($this->arguments);
     }
     try {
         $response = $request->send();
     } catch (HTTP_Request2_Exception $e) {
         throw new Services_Scribd_Exception($e->getMessage(), $e->getCode());
     }
     if ($response->getStatus() !== 200) {
         throw new Services_Scribd_Exception('Invalid response returned from server', $response->getStatus());
     }
     return $response->getBody();
 }
 public function testShouldFailGracefullyOnFailedTransaction()
 {
     $mock = new HTTP_Request2_Adapter_Mock();
     $mock->addResponse('HTTP/1.1 404 Not Found');
     $request = new HTTP_Request2();
     $request->setAdapter($mock);
     $object = Payment_Process2::factory('PayPal');
     $object->login = '******';
     $object->password = '******';
     $object->amount = 1;
     $object->action = Payment_Process2::ACTION_NORMAL;
     $object->setRequest($request);
     $object->setPayment($this->aValidPayment());
     $result = $object->process();
     $this->assertTrue($result instanceof PEAR_Error);
 }
Beispiel #25
0
 public function testNoOAuthParametersInGET()
 {
     $mockReq = new HTTP_Request2('http://example.com');
     $mockReq->setAdapter(new HTTP_Request2_Adapter_Mock());
     $req = new HTTP_OAuth_Consumer_Request();
     $req->accept($mockReq);
     $req->foo = 'bar';
     $req->oauth_consumer_key = 'key';
     $req->send();
     $this->assertEquals('http://example.com/?foo=bar', $req->getUrl()->getUrl());
 }
 /**
  * Create a HTTP client and mock adapter.
  *
  * @return \HTTP_Request2
  */
 protected function setupClient()
 {
     $client = new \HTTP_Request2();
     $client->setAdapter("HTTP_Request2_Adapter_Mock");
     return $client;
 }
Beispiel #27
0
 private function _scrape_page($url)
 {
     $this->proxies = getProxyIPS(TRUE, 500);
     require_once 'HTTP/Request2.php';
     if (count($this->proxies) > 0) {
         foreach ($this->proxies as $index => $proxy) {
             $now = time();
             $totalAttempts = $proxy->fails + $proxy->connects;
             $failureRate = $totalAttempts == 0 ? 0 : $proxy->fails / $totalAttempts * 100;
             $gracePeriod = $now - strtotime($proxy->last_warn_time);
             $scheme = 'http';
             switch ($proxy->scheme) {
                 case 'https':
                 case 'HTTPS':
                     //Request2 only takes http, but proxy should still work
                     $scheme = 'http';
                     break;
                 case 'socks4':
                 case 'socks5':
                     $scheme = 'socks5';
                     break;
             }
             $proxyString = $scheme . '://' . $proxy->proxy_host . ":" . $proxy->proxy_port;
             //we don't want to mess with the ones that have too many warnings
             //let's try 25% failure/connect ratio should exclude this proxy temporarily
             if ($failureRate > 25 && $this->warn_period > $gracePeriod) {
                 log_message('error', "Skipping {$proxyString} due to {$failureRate}% failure rate.\nTime calc: {$gracePeriod} period: {$this->warn_period}");
                 continue;
             }
             try {
                 $options = array("timeout" => "5", "follow_redirects" => true, "max_redirects" => 3);
                 $scrape = new HTTP_Request2($url, HTTP_Request2::METHOD_GET, $options);
                 $scrape->setAdapter('curl');
                 $scrape->setConfig(array('proxy_host' => trim($proxy->proxy_host), 'proxy_port' => trim($proxy->proxy_port)));
                 $aKey = array_rand($this->agents);
                 $scrape->setHeader(array('Accept-Charset' => 'utf-8', 'User-Agent' => $this->agents[$aKey]));
                 // send http request
                 $response = $scrape->send();
                 $body = $response->getBody();
                 $status = $response->getStatus();
                 $source = parse_url($url);
                 $upData = array();
                 //echo "From: $proxyString\nResponse status: ".$response->getStatus()."\n";
                 $this->CI->db->where("id", $proxy->id);
                 $this->CI->db->set('connects', 'connects+1', FALSE);
                 $this->CI->db->update($this->_table_proxy_ips);
                 $updateFlag = $html = false;
                 $title = '';
                 if ($status == '200' || $status == '304') {
                     $body = preg_replace('/[^[:print:]]/', '', $body);
                     $html = str_get_html($body);
                     if (!$html) {
                         $headerLog = '';
                         foreach ($response->getHeader() as $k => $v) {
                             $headerLog .= "\t{$k}: {$v}\n";
                         }
                         log_message('error', "!method_exists\n" . $response->getStatus() . "\ntitle: {$title}\nheaders: {$headerLog}\n{$proxyString}\n{$url}");
                     } else {
                         $title = $html->find('title', 0);
                         $title = $title ? strtolower($title->plaintext) : '';
                         $html->clear();
                         unset($html);
                         //echo "got: $url\ntitle: $title\n";
                     }
                 }
                 //find any known phantom sites
                 if (strpos($title, 'onlinecollegesuniversity.com') || strpos($title, 'articlesdigest.info') || strpos($title, 'ihowandwhy.com')) {
                     $updateFlag = true;
                     if ((int) $proxy->warns >= $this->warn_max * 2) {
                         log_message('error', "Ban status Phantom {$title}:\n{$proxyString}\n{$url}");
                         $upData = array('use_flag' => 0, 'ban_source' => $source['host'] . ' - ' . $title, 'ban_type' => $status, 'ban_agent' => $this->agents[$aKey]);
                         unset($this->proxies[$index]);
                     } else {
                         log_message('error', "Phantom site {$title}:\n{$proxyString}\n{$url}");
                     }
                 } elseif ($status >= 500) {
                     // Server Error -- assume this is ban
                     $updateFlag = true;
                     log_message('error', "Ban status {$status}:\n{$proxyString}\n{$url}");
                     $upData = array('use_flag' => 0, 'ban_source' => $source['host'], 'ban_type' => $status, 'ban_agent' => $this->agents[$aKey]);
                     unset($this->proxies[$index]);
                 } elseif ($status == 404) {
                     $updateFlag = true;
                     if ((int) $proxy->warns >= $this->warn_max * 2) {
                         log_message('error', "Ban status {$status}:\n{$proxyString}\n{$url}");
                         $upData = array('use_flag' => 0, 'ban_source' => $source['host'], 'ban_type' => $status, 'ban_agent' => $this->agents[$aKey]);
                     } else {
                         log_message('error', "Warning {$status}:\n{$proxyString}\n{$url}");
                     }
                 } elseif ($status >= 400) {
                     $updateFlag = true;
                     if ((int) $proxy->warns >= $this->warn_max) {
                         log_message('error', "Ban status {$status}:\n{$proxyString}\n{$url}");
                         $upData = array('use_flag' => 0, 'ban_source' => $source['host'], 'ban_type' => $status, 'ban_agent' => $this->agents[$aKey]);
                     } else {
                         log_message('error', "Warning {$status}:\n{$proxyString}\n{$url}");
                     }
                 }
                 if ($updateFlag) {
                     $this->CI->db->set('warns', 'warns+1', FALSE);
                     $this->CI->db->set('last_warn_time', 'now()', FALSE);
                     $this->CI->db->where("id", $proxy->id);
                     $this->CI->db->update($this->_table_proxy_ips, $upData);
                 }
                 foreach ($response->getCookies() as $c) {
                     /* echo "\tname: {$c['name']}, value: {$c['value']}".(empty($c['expires'])? '': ", expires: {$c['expires']}").(empty($c['domain'])? '': ", domain: {$c['domain']}").(empty($c['path'])? '': ", path: {$c['path']}").", secure: ".($c['secure']? 'yes': 'no')."\n";*/
                     $scrape->addCookie($c['name'], $c['value']);
                 }
                 unset($scrape);
                 return $status == '200' || $status == '304' ? $body : false;
             } catch (HTTP_Request2_Exception $e) {
                 //do proxy deactivation here...
                 //once we have a good sample & connection failure is > 75% - kill proxy
                 if ((int) $proxy->fails > 10 && ($failureRate > 75 || (int) $proxy->connects == 0)) {
                     log_message('error', "Connection Ban status {$e->getNativeCode()}:\n{$proxyString}\n{$url}\n" . $e->getMessage() . "\nFails: {$proxy->fails} - {$failureRate}%");
                     $this->CI->db->where('id', $proxy->id);
                     $this->CI->db->set('fails', 'fails+1', FALSE);
                     $this->CI->db->set('last_warn_time', 'now()', FALSE);
                     $this->CI->db->update($this->_table_proxy_ips, array('use_flag' => 0, 'ban_source' => $proxy->proxy_host . ':' . $proxy->proxy_port, 'ban_type' => "Connection: " . $e->getNativeCode(), 'ban_agent' => $this->agents[$aKey]));
                     unset($this->proxies[$index]);
                 } else {
                     log_message('error', "Connection Warning {$e->getNativeCode()}:\n{$proxyString}\nFails: {$proxy->fails} rate: {$failureRate}\n{$url}");
                     $this->CI->db->where('id', $proxy->id);
                     $this->CI->db->set('fails', 'fails+1', FALSE);
                     $this->CI->db->set('last_warn_time', 'now()', FALSE);
                     $this->CI->db->update($this->_table_proxy_ips);
                 }
                 unset($scrape);
                 //return false;
             }
         }
     } else {
         log_message('error', 'We are out of proxies');
         email_alertToTeam('amzecs _scrape_page() error', 'We are out of proxies');
     }
     log_message('error', "amzecs _scrape_page() error - Neither success or failure\n{$proxyString}");
     email_alertToTeam('amzecs _scrape_page() error', "Neither success or failure\n{$proxyString}");
     return false;
 }