Skip to content

Commit

Permalink
add base url option
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol Wnuk committed Jun 7, 2016
1 parent e316e34 commit 1321429
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class Client {
*/
protected $streamFilters = array();

/**
* @var string
*/
protected $baseUrl;

public function __construct() {
$this->mh = curl_multi_init();
}
Expand Down Expand Up @@ -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] ) {
Expand Down Expand Up @@ -474,4 +483,12 @@ public function __destruct() {
}
curl_multi_close( $this->mh );
}

/**
* @param string $baseUrl
*/
public function setBaseUrl($baseUrl)
{
$this->baseUrl = $baseUrl;
}
}
2 changes: 1 addition & 1 deletion tests/CaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ protected function createReq() {
protected function url($path) {
return 'http://' . $this->domain . $path;
}
}
}
19 changes: 17 additions & 2 deletions tests/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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'));
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -131,4 +146,4 @@ public function testJson() {
$this->assertNotEmpty($data);
$this->assertArrayHasKey('json_test', $data);
}
}
}

0 comments on commit 1321429

Please sign in to comment.