diff --git a/src/Client.php b/src/Client.php index f58b799..54c06f9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -127,6 +127,11 @@ class Client { */ protected $streamFilters = array(); + /** + * @var string + */ + protected $baseUrl; + public function __construct() { $this->mh = curl_multi_init(); } @@ -177,6 +182,10 @@ public function add($opts = array(), $params = array()) { $params = array(); } + if (isset($this->baseUrl, $opts[CURLOPT_URL])) { + $opts[CURLOPT_URL] = $this->baseUrl . $opts[CURLOPT_URL]; + } + if (isset($this->streamResult) && !isset($opts[CURLOPT_FILE])) { $opts[CURLOPT_FILE] = fopen($this->streamResult, 'r+'); if ( !$opts[CURLOPT_FILE] ) { @@ -474,4 +483,12 @@ public function __destruct() { } curl_multi_close( $this->mh ); } + + /** + * @param string $baseUrl + */ + public function setBaseUrl($baseUrl) + { + $this->baseUrl = $baseUrl; + } } diff --git a/tests/CaseTest.php b/tests/CaseTest.php index ebfc10f..235218c 100644 --- a/tests/CaseTest.php +++ b/tests/CaseTest.php @@ -31,4 +31,4 @@ protected function createReq() { protected function url($path) { return 'http://' . $this->domain . $path; } -} \ No newline at end of file +} diff --git a/tests/SimpleTest.php b/tests/SimpleTest.php index c01377d..1317037 100644 --- a/tests/SimpleTest.php +++ b/tests/SimpleTest.php @@ -27,7 +27,7 @@ public function testSleep() { $script_time_sleep = $count_sleep * $sleep_time; $script_time_running = $time_end - $time_begin; - + $this->assertEquals($count, count($results)); $this->assertTrue($script_time_running >= $script_time_sleep); } @@ -39,6 +39,14 @@ public function testGet() { $this->assertEquals(200, $result->getHttpCode()); } + public function testGetWithBaseUrl() { + $this->req->setBaseUrl('http://' . $this->domain); + + $result = $this->req->get('/simple.txt'); + $this->assertEquals('simple', $result->getBody()); + $this->assertEquals(200, $result->getHttpCode()); + } + public function testHeaders() { $this->req->enableHeaders(); $result = $this->req->get($this->url('/simple.txt')); @@ -96,6 +104,13 @@ public function testPost() { $this->assertEquals('POST', (string) $result); } + public function testPostWithBaseUrl() { + $this->req->setBaseUrl('http://' . $this->domain); + + $result = $this->req->post('/post.php', array('data' => 'post data')); + $this->assertEquals('POST', (string) $result); + } + public function testHttp404() { $result = $this->req->get($this->url('/simple-error/404.txt')); @@ -131,4 +146,4 @@ public function testJson() { $this->assertNotEmpty($data); $this->assertArrayHasKey('json_test', $data); } -} \ No newline at end of file +}