From d993c7afc1d6ec3026907ab8035a9a389eec86cc Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Fri, 19 Feb 2016 11:34:27 -0800 Subject: [PATCH] fix up http classes --- lib/Telegraph/HTTP.php | 9 +++---- lib/Telegraph/HTTPTest.php | 49 +++++++++++++------------------------- tests/APITest.php | 2 +- tests/ProcessTest.php | 2 +- 4 files changed, 21 insertions(+), 41 deletions(-) diff --git a/lib/Telegraph/HTTP.php b/lib/Telegraph/HTTP.php index 7dc687d..8044575 100644 --- a/lib/Telegraph/HTTP.php +++ b/lib/Telegraph/HTTP.php @@ -3,7 +3,7 @@ namespace Telegraph; class HTTP { - public static function get($url) { + public function get($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); @@ -17,7 +17,7 @@ class HTTP { ); } - public static function post($url, $body, $headers=array()) { + public function post($url, $body, $headers=array()) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); @@ -25,9 +25,7 @@ class HTTP { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_HEADER, true); - if (self::$_proxy) curl_setopt($ch, CURLOPT_PROXY, self::$_proxy); $response = curl_exec($ch); - self::_debug($response); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); return array( 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), @@ -36,13 +34,12 @@ class HTTP { ); } - public static function head($url) { + public function head($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - if (self::$_proxy) curl_setopt($ch, CURLOPT_PROXY, self::$_proxy); $response = curl_exec($ch); return array( 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), diff --git a/lib/Telegraph/HTTPTest.php b/lib/Telegraph/HTTPTest.php index 06af003..0d87227 100644 --- a/lib/Telegraph/HTTPTest.php +++ b/lib/Telegraph/HTTPTest.php @@ -1,28 +1,34 @@ _testDataPath = $testDataPath; + } + + public function get($url) { + return $this->_read_file($url); } - public static function post($url, $body, $headers=array()) { - return self::_read_file($url); + public function post($url, $body, $headers=array()) { + return $this->_read_file($url); } - public static function head($url) { - $response = self::_read_file($url); + public function head($url) { + $response = $this->_read_file($url); return array( 'code' => $response['code'], 'headers' => $response['headers'] ); } - private static function _read_file($url) { - $filename = dirname(__FILE__).'/../../tests/data/'.preg_replace('/https?:\/\//', '', $url); + private function _read_file($url) { + $filename = $this->_testDataPath.preg_replace('/https?:\/\//', '', $url); if(!file_exists($filename)) { - $filename = dirname(__FILE__).'/../../tests/data/404.response.txt'; + $filename = $this->_testDataPath.'404.response.txt'; } $response = file_get_contents($filename); @@ -45,27 +51,4 @@ class HTTPTest { ); } - public static function parse_headers($headers) { - $retVal = array(); - $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers)); - foreach($fields as $field) { - if(preg_match('/([^:]+): (.+)/m', $field, $match)) { - $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) { - return strtoupper($m[0]); - }, strtolower(trim($match[1]))); - // If there's already a value set for the header name being returned, turn it into an array and add the new value - $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) { - return strtoupper($m[0]); - }, strtolower(trim($match[1]))); - if(isset($retVal[$match[1]])) { - if(!is_array($retVal[$match[1]])) - $retVal[$match[1]] = array($retVal[$match[1]]); - $retVal[$match[1]][] = $match[2]; - } else { - $retVal[$match[1]] = trim($match[2]); - } - } - } - return $retVal; - } } diff --git a/tests/APITest.php b/tests/APITest.php index e12532f..66db003 100644 --- a/tests/APITest.php +++ b/tests/APITest.php @@ -8,7 +8,7 @@ class APITest extends PHPUnit_Framework_TestCase { public function setUp() { $this->client = new API(); - $this->client->http = new Telegraph\HTTPTest(); + $this->client->http = new Telegraph\HTTPTest(dirname(__FILE__).'/data/'); ORM::for_table('users')->raw_query('TRUNCATE users')->delete_many(); ORM::for_table('roles')->raw_query('TRUNCATE roles')->delete_many(); ORM::for_table('sites')->raw_query('TRUNCATE sites')->delete_many(); diff --git a/tests/ProcessTest.php b/tests/ProcessTest.php index 3aecd2b..17f8788 100644 --- a/tests/ProcessTest.php +++ b/tests/ProcessTest.php @@ -8,7 +8,7 @@ class ProcessTest extends PHPUnit_Framework_TestCase { private $api; public function setUp() { - $this->http = new Telegraph\HTTPTest(); + $this->http = new Telegraph\HTTPTest(dirname(__FILE__).'/data/'); $this->api = new API(); $this->api->http = $this->http; ORM::for_table('users')->raw_query('TRUNCATE users')->delete_many();