diff --git a/lib/HTTP.php b/lib/HTTP.php index 1a635f5..a2b7a7f 100644 --- a/lib/HTTP.php +++ b/lib/HTTP.php @@ -1,6 +1,41 @@ _class($url); + $http = new $class($url); + $http->timeout = $this->timeout; + $http->max_redirects = $this->max_redirects; + return $http->get($url); + } + + public function post($url, $body, $headers=array()) { + $class = $this->_class($url); + $http = new $class($url); + $http->timeout = $this->timeout; + $http->max_redirects = $this->max_redirects; + return $http->post($url, $body, $headers); + } + public function head($url) { + $class = $this->_class($url); + $http = new $class($url); + $http->timeout = $this->timeout; + $http->max_redirects = $this->max_redirects; + return $http->head($url); + } + + private function _class($url) { + if(preg_match('/brid\.gy|appspot\.com/', $url)) { + return 'p3k\HTTPStream'; + } else { + return 'p3k\HTTPCurl'; + } + } + +} diff --git a/tests/FetchTest.php b/tests/FetchTest.php index b5727e9..e3c2e4a 100644 --- a/tests/FetchTest.php +++ b/tests/FetchTest.php @@ -10,19 +10,19 @@ class FetchTest extends PHPUnit_Framework_TestCase { $this->http = new p3k\HTTP(); } - // public function testTimeout() { - // $url = 'https://nghttp2.org/httpbin/delay/2'; - // $this->http->timeout = 1; - // $response = $this->http->get($url); - // $this->assertEquals('timeout', $response['error']); - // } + public function testTimeout() { + $url = 'https://nghttp2.org/httpbin/delay/2'; + $this->http->timeout = 1; + $response = $this->http->get($url); + $this->assertEquals('timeout', $response['error']); + } - // public function testRedirectLimit() { - // $url = 'https://nghttp2.org/httpbin/redirect/3'; - // $this->http->max_redirects = 1; - // $response = $this->http->get($url); - // $this->assertEquals('too_many_redirects', $response['error']); - // } + public function testRedirectLimit() { + $url = 'https://nghttp2.org/httpbin/redirect/3'; + $this->http->max_redirects = 1; + $response = $this->http->get($url); + $this->assertEquals('too_many_redirects', $response['error']); + } public function testNoError() { $url = 'https://nghttp2.org/httpbin/ip';