Skip to content
Snippets Groups Projects
Commit c54d71a5 authored by Klimov Paul's avatar Klimov Paul
Browse files

Fixed inability to use URL with query parameters as `Client::$baseUrl`

parent 14f9850a
Branches
Tags
No related merge requests found
......@@ -6,6 +6,7 @@ Yii Framework 2 HTTP client extension Change Log
- Bug #74: Fixed unable to reuse `Request` instance for sending several requests with different data (klimov-paul)
- Bug #76: Fixed `HttpClientPanel` triggers `E_WARNING` on attempt to view history debug entry, generated without panel being attached (klimov-paul)
- Bug #79: Fixed inability to use URL with query parameters as `Client::$baseUrl` (klimov-paul)
2.0.2 October 31, 2016
......@@ -26,8 +27,8 @@ Yii Framework 2 HTTP client extension Change Log
- Enh #43: Events `EVENT_BEFORE_SEND` and `EVENT_AFTER_SEND` added to `Request` and `Client` (klimov-paul)
- Enh #46: Added `Request::getFullUrl()` allowing getting the full actual request URL (klimov-paul)
- Enh #47: Added `Message::addData()` allowing addition of the content data to already existing one (klimov-paul)
- Enh #50: Option 'protocolVersion' added to `Request::options` allowing specification of the HTTP protocol version (klimov-paul)
- Enh #58: Added `UrlEncodedFormatter::charset` allowing specification of content charset (klimov-paul)
- Enh #50: Option 'protocolVersion' added to `Request::$options` allowing specification of the HTTP protocol version (klimov-paul)
- Enh #58: Added `UrlEncodedFormatter::$charset` allowing specification of content charset (klimov-paul)
- Enh: Added `XmlFormatter::useTraversableAsArray` allowing processing `\Traversable` as array (klimov-paul)
......
......@@ -275,7 +275,19 @@ class Request extends Message
if (isset($params[0])) {
$url = $params[0];
unset($params[0]);
} else {
$url = '';
}
}
if (!empty($this->client->baseUrl)) {
if (empty($url)) {
$url = $this->client->baseUrl;
} elseif (!preg_match('/^https?:\\/\\//i', $url)) {
$url = $this->client->baseUrl . '/' . $url;
}
}
if (!empty($params)) {
if (strpos($url, '?') === false) {
$url .= '?';
......@@ -284,13 +296,6 @@ class Request extends Message
}
$url .= http_build_query($params);
}
}
if (!empty($this->client->baseUrl)) {
if (!preg_match('/^https?:\\/\\//i', $url)) {
$url = $this->client->baseUrl . '/' . $url;
}
}
return $url;
}
......
......@@ -129,26 +129,57 @@ EOL;
$this->assertContains('some content', $result);
}
/**
* Data provider for [[testGetFullUrl()]]
* @return array test data
*/
public function dataProviderGetFullUrl()
{
return [
[
'http://some-domain.com',
'test/url',
'http://some-domain.com/test/url'
],
[
'http://some-domain.com',
'http://another-domain.com/test',
'http://another-domain.com/test',
],
[
'http://some-domain.com',
['test/url', 'param1' => 'name1'],
'http://some-domain.com/test/url?param1=name1'
],
[
'http://some-domain.com?base-param=base',
null,
'http://some-domain.com?base-param=base',
],
[
'http://some-domain.com?base-param=base',
['param1' => 'name1'],
'http://some-domain.com?base-param=base&param1=name1',
],
];
}
/**
* @depends testSetupUrl
* @dataProvider dataProviderGetFullUrl
*
* @param string $baseUrl
* @param mixed $url
* @param string $expectedFullUrl
*/
public function testGetFullUrl()
public function testGetFullUrl($baseUrl, $url, $expectedFullUrl)
{
$client = new Client();
$client->baseUrl = 'http://some-domain.com';
$client->baseUrl = $baseUrl;
$request = new Request(['client' => $client]);
$url = 'test/url';
$request->setUrl($url);
$this->assertEquals('http://some-domain.com/test/url', $request->getFullUrl());
$url = 'http://another-domain.com/test';
$request->setUrl($url);
$this->assertEquals($url, $request->getFullUrl());
$url = ['test/url', 'param1' => 'name1'];
$request->setUrl($url);
$this->assertEquals('http://some-domain.com/test/url?param1=name1', $request->getFullUrl());
$this->assertEquals($expectedFullUrl, $request->getFullUrl());
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment