Skip to content

Commit 8edb048

Browse files
committed
Allow empty strings in url and base_url. Resolves #51.
1 parent c4115cd commit 8edb048

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ The test package includes a simple server script which returns debug information
245245

246246
``` sh
247247
$ php -S localhost:8888 RestClientTest.php
248-
$ phpunit RestClientTest
248+
$ phpunit RestClientTest.php
249249
```
250250

251251
* Requires PHP > 5.5.7 in order for `getallheaders` data to populate.

RestClientTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,39 @@ public function test_build_non_indexed_queries() : void {
211211
$this->assertEquals("foo=+bar&baz=1&bat%5B%5D=foo&bat%5B%5D=bar&bat%5B%5D=baz%5B12%5D",
212212
$response_json->SERVER->QUERY_STRING);
213213
}
214+
215+
public function test_empty_base_url() : void {
216+
global $TEST_SERVER_URL;
217+
218+
$api = new RestClient(
219+
['base_url' => null]
220+
);
221+
$result = $api->get($TEST_SERVER_URL);
222+
$this->assertEquals(200, $result->info->http_code);
223+
}
224+
225+
public function test_empty_url() : void {
226+
global $TEST_SERVER_URL;
227+
228+
$api = new RestClient(
229+
['base_url' => $TEST_SERVER_URL]
230+
);
231+
$result = $api->get('');
232+
$this->assertEquals(200, $result->info->http_code);
233+
}
234+
235+
public function test_null_url() : void {
236+
global $TEST_SERVER_URL;
237+
238+
$api = new RestClient(
239+
['base_url' => $TEST_SERVER_URL]
240+
);
241+
try {
242+
$result = $api->get(null);
243+
} catch (TypeError $e) {
244+
$this->assertEquals(TypeError::class, get_class($e));
245+
}
246+
}
214247
}
215248

216249

restclient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ public function execute(string $url, string $method='GET', $parameters=[], array
188188
}
189189

190190
if($client->options['base_url']){
191-
if($client->url[0] !== '/' && substr($client->options['base_url'], -1) !== '/')
192-
$client->url = '/' . $client->url;
193-
$client->url = $client->options['base_url'] . $client->url;
191+
$client->url = sprintf("%s/%s",
192+
rtrim((string) $client->options['base_url'], '/'),
193+
ltrim((string) $client->url, '/'));
194194
}
195195
$curlopt[CURLOPT_URL] = $client->url;
196196

0 commit comments

Comments
 (0)