From bbdba6d907e4ce8c86e735119944e839e14ebd2c Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 8 Feb 2015 23:13:25 +0700 Subject: [PATCH] Fix #86: Add default timeout of 30 seconds --- README.md | 2 ++ src/Curl/Curl.php | 12 ++++++++ tests/PHPCurlClass/PHPCurlClassTest.php | 40 +++++++++++++++++++++++++ tests/PHPCurlClass/server.php | 14 +++++++++ 4 files changed, 68 insertions(+) diff --git a/README.md b/README.md index 46f365e..43d0821 100644 --- a/README.md +++ b/README.md @@ -166,12 +166,14 @@ setCookie($key, $value) setCookieFile($cookie_file) setCookieJar($cookie_jar) setDefaultJsonDecoder() +setDefaultTimeout() setDefaultUserAgent() setHeader($key, $value) setJsonDecoder($func) setOpt($option, $value) setReferer($referer) setReferrer($referrer) +setTimeout($seconds) setUserAgent($user_agent) success($callback) unsetHeader($key) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 31d9f8e..582b7a8 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -5,6 +5,7 @@ namespace Curl; class Curl { const VERSION = '2.1.4'; + const DEFAULT_TIMEOUT = 30; private $cookies = array(); private $headers = array(); @@ -52,6 +53,7 @@ class Curl $this->curl = curl_init(); $this->setDefaultUserAgent(); $this->setDefaultJsonDecoder(); + $this->setDefaultTimeout(); $this->setOpt(CURLINFO_HEADER_OUT, true); $this->setOpt(CURLOPT_RETURNTRANSFER, true); $this->headers = new CaseInsensitiveArray(); @@ -268,6 +270,16 @@ class Curl $this->setOpt(CURLOPT_USERAGENT, $user_agent); } + public function setTimeout($seconds) + { + $this->setOpt(CURLOPT_TIMEOUT, $seconds); + } + + public function setDefaultTimeout() + { + $this->setTimeout(self::DEFAULT_TIMEOUT); + } + public function setReferer($referer) { $this->setReferrer($referer); diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 901e3ab..9cf70fe 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -483,6 +483,44 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals('cookie1=scrumptious,cookie2=mouthwatering', $test->curl->response_headers['Set-Cookie']); } + public function testDefaultTimeout() { + $test = new Test(); + $test->server('timeout', 'GET', array( + 'seconds' => '31', + )); + $this->assertTrue($test->curl->error); + $this->assertTrue($test->curl->curl_error); + $this->assertEquals(CURLE_OPERATION_TIMEOUTED, $test->curl->error_code); + $this->assertEquals(CURLE_OPERATION_TIMEOUTED, $test->curl->curl_error_code); + $this->assertFalse($test->curl->http_error); + } + + public function testTimeoutError() { + $test = new Test(); + $test->curl->setTimeout(5); + $test->server('timeout', 'GET', array( + 'seconds' => '10', + )); + $this->assertTrue($test->curl->error); + $this->assertTrue($test->curl->curl_error); + $this->assertEquals(CURLE_OPERATION_TIMEOUTED, $test->curl->error_code); + $this->assertEquals(CURLE_OPERATION_TIMEOUTED, $test->curl->curl_error_code); + $this->assertFalse($test->curl->http_error); + } + + public function testTimeout() { + $test = new Test(); + $test->curl->setTimeout(10); + $test->server('timeout', 'GET', array( + 'seconds' => '5', + )); + $this->assertFalse($test->curl->error); + $this->assertFalse($test->curl->curl_error); + $this->assertNotEquals(CURLE_OPERATION_TIMEOUTED, $test->curl->error_code); + $this->assertNotEquals(CURLE_OPERATION_TIMEOUTED, $test->curl->curl_error_code); + $this->assertFalse($test->curl->http_error); + } + public function testError() { $test = new Test(); @@ -490,6 +528,7 @@ class CurlTest extends PHPUnit_Framework_TestCase $test->curl->get(Test::ERROR_URL); $this->assertTrue($test->curl->error); $this->assertTrue($test->curl->curl_error); + $this->assertEquals(CURLE_OPERATION_TIMEOUTED, $test->curl->error_code); $this->assertEquals(CURLE_OPERATION_TIMEOUTED, $test->curl->curl_error_code); } @@ -875,6 +914,7 @@ class CurlTest extends PHPUnit_Framework_TestCase $curl->complete(function ($instance) use (&$success_called, &$error_called, &$complete_called) { PHPUnit_Framework_Assert::assertTrue($instance->error); PHPUnit_Framework_Assert::assertTrue($instance->curl_error); + PHPUnit_Framework_Assert::assertEquals(CURLE_OPERATION_TIMEOUTED, $instance->error_code); PHPUnit_Framework_Assert::assertEquals(CURLE_OPERATION_TIMEOUTED, $instance->curl_error_code); }); $curl->get(array( diff --git a/tests/PHPCurlClass/server.php b/tests/PHPCurlClass/server.php index 8e0393d..806af59 100644 --- a/tests/PHPCurlClass/server.php +++ b/tests/PHPCurlClass/server.php @@ -145,6 +145,20 @@ if ($test == 'http_basic_auth') { header('ETag: ' . md5_file($unsafe_file_path)); readfile($unsafe_file_path); exit; +} elseif ($test === 'timeout') { + $unsafe_seconds = $_GET['seconds']; + $start = time(); + while (true) { + echo '.'; + ob_flush(); + flush(); + sleep(1); + $elapsed = time() - $start; + if ($elapsed >= $unsafe_seconds) { + break; + } + } + exit; } elseif ($test === 'error_message') { if (function_exists('http_response_code')) { http_response_code(401);