Browse Source

fix up http classes

main
Aaron Parecki 8 years ago
parent
commit
d993c7afc1
4 changed files with 21 additions and 41 deletions
  1. +3
    -6
      lib/Telegraph/HTTP.php
  2. +16
    -33
      lib/Telegraph/HTTPTest.php
  3. +1
    -1
      tests/APITest.php
  4. +1
    -1
      tests/ProcessTest.php

+ 3
- 6
lib/Telegraph/HTTP.php View File

@ -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),

+ 16
- 33
lib/Telegraph/HTTPTest.php View File

@ -1,28 +1,34 @@
<?php
namespace Telegraph;
class HTTPTest {
class HTTPTest extends HTTP {
public static function get($url) {
return self::_read_file($url);
private $_testDataPath;
public function __construct($testDataPath) {
$this->_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;
}
}

+ 1
- 1
tests/APITest.php View File

@ -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();

+ 1
- 1
tests/ProcessTest.php View File

@ -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();

Loading…
Cancel
Save