fix Client::getUrl
authorgggeek <giunta.gaetano@gmail.com>
Wed, 1 Feb 2023 17:21:03 +0000 (17:21 +0000)
committergggeek <giunta.gaetano@gmail.com>
Wed, 1 Feb 2023 17:21:03 +0000 (17:21 +0000)
src/Client.php
tests/07ClientTest.php

index 800e928..4982222 100644 (file)
@@ -918,25 +918,29 @@ class Client
      */
     public function getUrl($component = null)
     {
-        switch($component) {
-            case PHP_URL_SCHEME:
-                return $this->method;
-            case PHP_URL_HOST:
-                return $this->server;
-            case PHP_URL_PORT:
-                return $this->port;
-            case  PHP_URL_PATH:
-                return $this->path;
-            case '':
-                $url = $this->method . '://' . $this->server;
-                if (($this->port == 80 && in_array($this->method, array('http', 'http10', 'http11', 'h2c'))) ||
-                    ($this->port == 443 && in_array($this->method, array('https', 'h2')))) {
-                    return $url . $this->path;
-                } else {
-                    return $url . ':' . $this->port . $this->path;
-                }
-            default:
-                throw new ValueErrorException("Unsupported component '$component'");
+        if (is_int($component) || ctype_digit($component)) {
+            switch ($component) {
+                case PHP_URL_SCHEME:
+                    return $this->method;
+                case PHP_URL_HOST:
+                    return $this->server;
+                case PHP_URL_PORT:
+                    return $this->port;
+                case  PHP_URL_PATH:
+                    return $this->path;
+                case '':
+
+                default:
+                    throw new ValueErrorException("Unsupported component '$component'");
+            }
+        }
+
+        $url = $this->method . '://' . $this->server;
+        if ($this->port == 0 || ($this->port == 80 && in_array($this->method, array('http', 'http10', 'http11', 'h2c'))) ||
+            ($this->port == 443 && in_array($this->method, array('https', 'h2')))) {
+            return $url . $this->path;
+        } else {
+            return $url . ':' . $this->port . $this->path;
         }
     }
 
index 07a6abd..e84e920 100644 (file)
@@ -85,4 +85,16 @@ class ClientTest extends PhpXmlRpc_LoggerAwareTestCase
         $ro = $r->value();
         is_object($ro) && $this->assertEquals('hello', $ro->scalarVal());
     }
+
+    public function testgetUrl()
+    {
+        $m = $this->client->getUrl(PHP_URL_SCHEME);
+        $this->assertEquals($m, $this->client->method);
+        $h = $this->client->getUrl(PHP_URL_HOST);
+        $this->assertEquals($h, $this->client->server);
+        $p = $this->client->getUrl(PHP_URL_PORT);
+        $this->assertEquals($p, $this->client->port);
+        $p = $this->client->getUrl(PHP_URL_PATH);
+        $this->assertEquals($p, $this->client->path);
+    }
 }