Skip to content

Commit

Permalink
Ensure that Request::getFullUrl() always returns a string (an excep…
Browse files Browse the repository at this point in the history
…tion will be thrown in case of an invalid call/config).
  • Loading branch information
rhertogh committed Jul 15, 2023
1 parent c932203 commit ce63a27
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/CurlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function format(Request $request)
$content = http_build_query((array)$data, '', '&', $this->encodingType);
$request->setFullUrl(null);
$url = $request->getFullUrl();
$url .= ($url === null || strpos($url, '?') === false) ? '?' : '&';
$url .= (strpos($url, '?') === false) ? '?' : '&';
$url .= $content;
$request->setFullUrl($url);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace yii\httpclient;

use yii\base\InvalidCallException;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;

Expand Down Expand Up @@ -86,7 +87,7 @@ public function setUrl($url)

/**
* Returns target URL.
* @return string|array target URL or URL parameters
* @return string|array|null target URL or URL parameters
*/
public function getUrl()
{
Expand Down Expand Up @@ -337,6 +338,10 @@ private function createFullUrl($url)
$url .= http_build_query($params);
}

if ($url === null) {
throw new InvalidCallException('Either the $url or the $client->baseUrl must be set.');
}

return $url;
}

Expand Down
2 changes: 1 addition & 1 deletion src/UrlEncodedFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function format(Request $request)
if (!empty($content)) {
$request->setFullUrl(null);
$url = $request->getFullUrl();
$url .= ($url === null || strpos($url, '?') === false) ? '?' : '&';
$url .= (strpos($url, '?') === false) ? '?' : '&';
$url .= $content;
$request->setFullUrl($url);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/CurlFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function testFormat()
public function testFormatMethodGet()
{
$request = new Request();
$request->setUrl('https://yiiframework.com/');
$request->setMethod('GET');
$data = [
'name1' => 'value1',
Expand Down Expand Up @@ -80,4 +81,4 @@ public function testFormatPostRequestWithEmptyBody()

$this->assertEquals('0', $headers['content-length'][0]);
}
}
}
9 changes: 9 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ public function testGetFullUrl($baseUrl, $url, $expectedFullUrl)
$this->assertEquals($expectedFullUrl, $request->getFullUrl());
}

public function testGetFullUrlWithoutUrlAndClientBaseUrl()
{
$client = new Client();
$request = new Request(['client' => $client]);

$this->expectException('yii\\base\\InvalidCallException');
var_dump($request->getFullUrl());
}

/**
* @depends testToString
*/
Expand Down
3 changes: 2 additions & 1 deletion tests/UrlEncodedFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testFormatMethodGet()
'name2' => 'value2',
];
$request->setData($data);
$request->setUrl('https://yiiframework.com/');

$formatter = new UrlEncodedFormatter();
$formatter->format($request);
Expand Down Expand Up @@ -103,4 +104,4 @@ public function testFormatPutRequestWithInfileOption()

$this->assertEquals($expectedHeaders, $headers);
}
}
}

0 comments on commit ce63a27

Please sign in to comment.