From 64384f8b15314e618f1a6821dd71325e8b8178ab Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 16 Nov 2018 14:43:25 +0100 Subject: [PATCH] Fix tests --- src/Composer/Util/HttpDownloader.php | 14 ++--- src/Composer/Util/Url.php | 8 ++- .../Composer/Test/Util/HttpDownloaderTest.php | 51 +++++++++++++++++++ .../Test/Util/RemoteFilesystemTest.php | 19 ------- 4 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 tests/Composer/Test/Util/HttpDownloaderTest.php diff --git a/src/Composer/Util/HttpDownloader.php b/src/Composer/Util/HttpDownloader.php index da3906728..b259ef3e5 100644 --- a/src/Composer/Util/HttpDownloader.php +++ b/src/Composer/Util/HttpDownloader.php @@ -126,11 +126,6 @@ class HttpDownloader private function addJob($request, $sync = false) { - // capture username/password from URL if there is one - if (preg_match('{^https?://([^:/]+):([^@/]+)@([^/]+)}i', $request['url'], $match)) { - $this->io->setAuthentication($originUrl, rawurldecode($match[1]), rawurldecode($match[2])); - } - $job = array( 'id' => $this->idGen++, 'status' => self::STATUS_QUEUED, @@ -138,12 +133,17 @@ class HttpDownloader 'sync' => $sync, ); + $origin = Url::getOrigin($this->config, $job['request']['url']); + + // capture username/password from URL if there is one + if (preg_match('{^https?://([^:/]+):([^@/]+)@([^/]+)}i', $request['url'], $match)) { + $this->io->setAuthentication($origin, rawurldecode($match[1]), rawurldecode($match[2])); + } + $curl = $this->curl; $rfs = $this->rfs; $io = $this->io; - $origin = Url::getOrigin($this->config, $job['request']['url']); - if ($curl && preg_match('{^https?://}i', $job['request']['url'])) { $resolver = function ($resolve, $reject) use (&$job, $curl, $origin) { // start job diff --git a/src/Composer/Util/Url.php b/src/Composer/Util/Url.php index 3266c4220..2874524ed 100644 --- a/src/Composer/Util/Url.php +++ b/src/Composer/Util/Url.php @@ -59,7 +59,7 @@ class Url return $url; } - $origin = parse_url($url, PHP_URL_HOST); + $origin = (string) parse_url($url, PHP_URL_HOST); if (strpos($origin, '.github.com') === (strlen($origin) - 11)) { return 'github.com'; @@ -69,6 +69,10 @@ class Url return 'packagist.org'; } + if ($origin === '') { + $origin = $url; + } + // Gitlab can be installed in a non-root context (i.e. gitlab.com/foo). When downloading archives the originUrl // is the host without the path, so we look for the registered gitlab-domains matching the host here if ( @@ -83,7 +87,7 @@ class Url } } - return $origin ?: $url; + return $origin; } } diff --git a/tests/Composer/Test/Util/HttpDownloaderTest.php b/tests/Composer/Test/Util/HttpDownloaderTest.php new file mode 100644 index 000000000..b65aa760a --- /dev/null +++ b/tests/Composer/Test/Util/HttpDownloaderTest.php @@ -0,0 +1,51 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Util; + +use Composer\Util\HttpDownloader; +use PHPUnit\Framework\TestCase; + +class HttpDownloaderTest extends TestCase +{ + private function getConfigMock() + { + $config = $this->getMockBuilder('Composer\Config')->getMock(); + $config->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function ($key) { + if ($key === 'github-domains' || $key === 'gitlab-domains') { + return array(); + } + })); + + return $config; + } + + /** + * @group slow + */ + public function testCaptureAuthenticationParamsFromUrl() + { + $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); + $io->expects($this->once()) + ->method('setAuthentication') + ->with($this->equalTo('github.com'), $this->equalTo('user'), $this->equalTo('pass')); + + $fs = new HttpDownloader($io, $this->getConfigMock()); + try { + $fs->get('https://user:pass@github.com/composer/composer/404'); + } catch (\Composer\Downloader\TransportException $e) { + $this->assertNotEquals(200, $e->getCode()); + } + } +} diff --git a/tests/Composer/Test/Util/RemoteFilesystemTest.php b/tests/Composer/Test/Util/RemoteFilesystemTest.php index 8d1bf3194..2c7f3112a 100644 --- a/tests/Composer/Test/Util/RemoteFilesystemTest.php +++ b/tests/Composer/Test/Util/RemoteFilesystemTest.php @@ -143,25 +143,6 @@ class RemoteFilesystemTest extends TestCase $this->assertNull($this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, 'HTTP/1.1 404 Not Found', 404, 0, 0)); } - /** - * @group slow - */ - public function testCaptureAuthenticationParamsFromUrl() - { - $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); - $io->expects($this->once()) - ->method('setAuthentication') - ->with($this->equalTo('github.com'), $this->equalTo('user'), $this->equalTo('pass')); - - $fs = new RemoteFilesystem($io, $this->getConfigMock()); - try { - $fs->getContents('github.com', 'https://user:pass@github.com/composer/composer/404'); - } catch (\Exception $e) { - $this->assertInstanceOf('Composer\Downloader\TransportException', $e); - $this->assertNotEquals(200, $e->getCode()); - } - } - public function testGetContents() { $fs = new RemoteFilesystem($this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getConfigMock());