Browse Source

use file_get_contents only for appengine URLs

pull/39/head
Aaron Parecki 5 years ago
parent
commit
ed88b4881b
2 changed files with 49 additions and 14 deletions
  1. +37
    -2
      lib/HTTP.php
  2. +12
    -12
      tests/FetchTest.php

+ 37
- 2
lib/HTTP.php View File

@ -1,6 +1,41 @@
<?php
namespace p3k;
class HTTP extends HTTPStream {
}
class HTTP {
public $timeout = 4;
public $max_redirects = 8;
public function get($url) {
$class = $this->_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';
}
}
}

+ 12
- 12
tests/FetchTest.php View File

@ -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';

Loading…
Cancel
Save