Skip to content

Commit 894eb86

Browse files
Merge pull request #3 from Sainsburys/master
Add "base url" setter
2 parents e316e34 + 1321429 commit 894eb86

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/Client.php

+17
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ class Client {
127127
*/
128128
protected $streamFilters = array();
129129

130+
/**
131+
* @var string
132+
*/
133+
protected $baseUrl;
134+
130135
public function __construct() {
131136
$this->mh = curl_multi_init();
132137
}
@@ -177,6 +182,10 @@ public function add($opts = array(), $params = array()) {
177182
$params = array();
178183
}
179184

185+
if (isset($this->baseUrl, $opts[CURLOPT_URL])) {
186+
$opts[CURLOPT_URL] = $this->baseUrl . $opts[CURLOPT_URL];
187+
}
188+
180189
if (isset($this->streamResult) && !isset($opts[CURLOPT_FILE])) {
181190
$opts[CURLOPT_FILE] = fopen($this->streamResult, 'r+');
182191
if ( !$opts[CURLOPT_FILE] ) {
@@ -474,4 +483,12 @@ public function __destruct() {
474483
}
475484
curl_multi_close( $this->mh );
476485
}
486+
487+
/**
488+
* @param string $baseUrl
489+
*/
490+
public function setBaseUrl($baseUrl)
491+
{
492+
$this->baseUrl = $baseUrl;
493+
}
477494
}

tests/CaseTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ protected function createReq() {
3131
protected function url($path) {
3232
return 'http://' . $this->domain . $path;
3333
}
34-
}
34+
}

tests/SimpleTest.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testSleep() {
2727
$script_time_sleep = $count_sleep * $sleep_time;
2828

2929
$script_time_running = $time_end - $time_begin;
30-
30+
3131
$this->assertEquals($count, count($results));
3232
$this->assertTrue($script_time_running >= $script_time_sleep);
3333
}
@@ -39,6 +39,14 @@ public function testGet() {
3939
$this->assertEquals(200, $result->getHttpCode());
4040
}
4141

42+
public function testGetWithBaseUrl() {
43+
$this->req->setBaseUrl('http://' . $this->domain);
44+
45+
$result = $this->req->get('/simple.txt');
46+
$this->assertEquals('simple', $result->getBody());
47+
$this->assertEquals(200, $result->getHttpCode());
48+
}
49+
4250
public function testHeaders() {
4351
$this->req->enableHeaders();
4452
$result = $this->req->get($this->url('/simple.txt'));
@@ -96,6 +104,13 @@ public function testPost() {
96104
$this->assertEquals('POST', (string) $result);
97105
}
98106

107+
public function testPostWithBaseUrl() {
108+
$this->req->setBaseUrl('http://' . $this->domain);
109+
110+
$result = $this->req->post('/post.php', array('data' => 'post data'));
111+
$this->assertEquals('POST', (string) $result);
112+
}
113+
99114

100115
public function testHttp404() {
101116
$result = $this->req->get($this->url('/simple-error/404.txt'));
@@ -131,4 +146,4 @@ public function testJson() {
131146
$this->assertNotEmpty($data);
132147
$this->assertArrayHasKey('json_test', $data);
133148
}
134-
}
149+
}

0 commit comments

Comments
 (0)