Browse Source

Fix tests

Jordi Boggiano 6 years ago
parent
commit
64384f8b15

+ 7 - 7
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

+ 6 - 2
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;
     }
 
 }

+ 51 - 0
tests/Composer/Test/Util/HttpDownloaderTest.php

@@ -0,0 +1,51 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * 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());
+        }
+    }
+}

+ 0 - 19
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());