Skip to content

Commit c1e9314

Browse files
committed
Updated url construcition logic, so it does not produce double slashes. Removed autoprefixing of url with slash, as that should be done by users explicitely.
1 parent 753536c commit c1e9314

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

src/Codeception/Module/REST.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,10 @@ public function sendUNLINK($url, array $linkEntries)
575575
protected function execute($method, $url, $parameters = [], $files = [])
576576
{
577577
// allow full url to be requested
578-
if (strpos($url, '://') === false) {
579-
$url = $this->config['url'] . $url;
580-
if ($this->config['url'] && strpos($url, '://') === false && strpos($url, '/') !== 0 && $this->config['url'][0] !== '/') {
581-
$url = '/' . $url;
582-
}
578+
if (!$url) {
579+
$url = $this->config['url'];
580+
} elseif (strpos($url, '://') === false && $this->config['url']) {
581+
$url = rtrim($this->config['url'], '/') . '/' . ltrim($url, '/');
583582
}
584583

585584
$this->params = $parameters;

tests/unit/Codeception/Module/RestTest.php

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public function testSeeResponseJsonMatchesXPathWorksWithAmpersand()
311311
$this->module->seeResponseJsonMatchesXpath('//comment');
312312
}
313313

314-
314+
315315
public function testSeeResponseJsonMatchesJsonPathFails()
316316
{
317317
$this->shouldFail();
@@ -322,8 +322,8 @@ public function testSeeResponseJsonMatchesJsonPathFails()
322322
$this->module->seeResponseIsJson();
323323
$this->module->seeResponseJsonMatchesJsonPath('$[*].profile');
324324
}
325-
326-
325+
326+
327327
public function testStructuredJsonPathAndXPath()
328328
{
329329
$this->setStubResponse(
@@ -442,6 +442,63 @@ public function testAmDigestAuthenticatedThrowsExceptionWithFunctionalModules()
442442
$this->module->amDigestAuthenticated('username', 'password');
443443
}
444444

445+
/**
446+
* @param $configUrl
447+
* @param $requestUrl
448+
* @param $expectedFullUrl
449+
*
450+
* @dataProvider configAndRequestUrls
451+
*/
452+
public function testRestExecute($configUrl, $requestUrl, $expectedFullUrl)
453+
{
454+
$connectionModule = $this->createMock(
455+
\Codeception\Module\UniversalFramework::class
456+
);
457+
$connectionModule
458+
->expects($this->once())
459+
->method('_request')
460+
->will(
461+
$this->returnCallback(function($method,
462+
$uri,
463+
$parameters,
464+
$files,
465+
$server,
466+
$content
467+
) use ($expectedFullUrl) {
468+
\PHPUnit\Framework\Assert::assertEquals($expectedFullUrl, $uri);
469+
})
470+
);
471+
472+
$config = ['url' => $configUrl];
473+
474+
/** @var \Codeception\Module\REST */
475+
$module = Stub::make('\Codeception\Module\REST');
476+
$module->_setConfig($config);
477+
$module->_inject($connectionModule);
478+
$module->_initialize();
479+
$module->_before(Stub::makeEmpty('\Codeception\Test\Test'));
480+
481+
$module->sendGET($requestUrl);
482+
}
483+
484+
public static function configAndRequestUrls()
485+
{
486+
return [
487+
//$configUrl, $requestUrl, $expectedFullUrl
488+
['v1/', 'healthCheck', 'v1/healthCheck'],
489+
['/v1', '/healthCheck', '/v1/healthCheck'],
490+
['v1', 'healthCheck', 'v1/healthCheck'],
491+
['http://v1/', '/healthCheck', 'http://v1/healthCheck'],
492+
['http://v1', 'healthCheck', 'http://v1/healthCheck'],
493+
['http://v1', 'http://v2/healthCheck', 'http://v2/healthCheck'],
494+
['http://v1', '', 'http://v1'],
495+
['http://v1', '/', 'http://v1/'],
496+
['', 'http://v1', 'http://v1'],
497+
['', 'healthCheck', 'healthCheck'],
498+
['/', 'healthCheck', '/healthCheck'],
499+
];
500+
}
501+
445502
protected function shouldFail()
446503
{
447504
$this->expectException('PHPUnit\Framework\AssertionFailedError');

0 commit comments

Comments
 (0)