Improve testing around http2 usage
authorgggeek <giunta.gaetano@gmail.com>
Thu, 26 May 2022 09:00:30 +0000 (09:00 +0000)
committergggeek <giunta.gaetano@gmail.com>
Thu, 26 May 2022 09:00:30 +0000 (09:00 +0000)
src/Client.php
src/Helper/Http.php
tests/5ServerTest.php
tests/6HTTPTest.php
tests/ci/config/apache_vhost

index 08a9a19..adecb1f 100644 (file)
@@ -4,6 +4,7 @@ namespace PhpXmlRpc;
 
 use PhpXmlRpc\Helper\Logger;
 use PhpXmlRpc\Helper\XMLParser;
+
 /**
  * Used to represent a client of an XML-RPC server.
  */
@@ -137,11 +138,13 @@ class Client
      *                     e.g. /xmlrpc/server.php
      *                     e.g. http://phpxmlrpc.sourceforge.net/server.php
      *                     e.g. https://james:bond@secret.service.com:443/xmlrpcserver?agent=007
+     *                     e.g. http2tls://fast-and-secure-services/endpoint
      * @param string $server the server name / ip address
      * @param integer $port the port the server is listening on, when omitted defaults to 80 or 443 depending on
      *                      protocol used
      * @param string $method the http protocol variant: defaults to 'http'; 'https', 'http11', 'http2' and 'http2tls' can
      *                       be used if CURL is installed. The value set here can be overridden in any call to $this->send().
+     *                       Use 'http2' to make the lib attempt to use http/2 without tls and 'http2tls' for secure http/2
      */
     public function __construct($path, $server = '', $port = '', $method = '')
     {
@@ -477,6 +480,7 @@ class Client
      *                         for $timeout seconds, the connection will be closed).
      * @param string $method valid values are 'http', 'http11', 'https', 'http2' and 'http2tls'. If left unspecified,
      *                       the http protocol chosen during creation of the object will be used.
+     *                       Use 'http2' to make the lib attempt to use http/2 without tls and 'http2tls' for secure http/2
      *
      * @return Response|Response[] Note that the client will always return a Response object, even if the call fails
      * @todo allow throwing exceptions instead of returning responses in case of failed calls and/or Fault responses
index 77d4a9d..7d6cb48 100644 (file)
@@ -121,8 +121,9 @@ class Http
             }
         }
 
-        if (preg_match('/^HTTP\/[0-9](?:\.[0-9])? ([0-9]{3}) /', $data, $matches)) {
-            $httpResponse['status_code'] = $matches[1];
+        if (preg_match('/^HTTP\/([0-9](?:\.[0-9])?) ([0-9]{3}) /', $data, $matches)) {
+            $httpResponse['protocol_version'] = $matches[1];
+            $httpResponse['status_code'] = $matches[2];
         }
 
         if ($httpResponse['status_code'] !== '200') {
index 0345b79..3fa4fe8 100644 (file)
@@ -141,6 +141,7 @@ class ServerTest extends PhpXmlRpc_PolyfillTestCase
         if (is_array($r)) {
             return $r;
         }
+        $this->validateResponse($r);
         if (is_array($errorCode)) {
             $this->assertContains($r->faultCode(), $errorCode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
         } else {
@@ -157,6 +158,11 @@ class ServerTest extends PhpXmlRpc_PolyfillTestCase
         }
     }
 
+    protected function validateResponse($r)
+    {
+        // to be implemented in subclasses
+    }
+
     /**
      * Adds (and replaces) query params to the url currently used by the client
      * @param array $data
index 83c361c..948117d 100644 (file)
@@ -13,6 +13,8 @@ include_once __DIR__ . '/5ServerTest.php';
  */
 class HTTPTest extends ServerTest
 {
+    protected $expectHttp2 = false;
+
     /**
      * Returns all test methods from the base class, except the ones which failed already
      *
@@ -313,12 +315,19 @@ class HTTPTest extends ServerTest
         {
             $this->markTestSkipped('CURL missing: cannot test http/2');
             return;
+        } else if (!defined('CURL_HTTP_VERSION_2'))
+        {
+            $this->markTestSkipped('CURL http/2 support missing: cannot test http/2');
+            return;
         }
 
         $this->method = 'http2'; // not an error the double assignment!
         $this->client->method = 'http2';
         //$this->client->keepalive = false; // q: is this a good idea?
+
+        $this->expectHttp2 = true;
         $this->$method();
+        $this->expectHttp2 = false;
     }
 
     /**
@@ -335,6 +344,10 @@ class HTTPTest extends ServerTest
         {
             $this->markTestSkipped('HTTPS SERVER definition missing: cannot test http/2 tls');
             return;
+        } else if (!defined('CURL_HTTP_VERSION_2'))
+        {
+            $this->markTestSkipped('CURL http/2 support missing: cannot test http/2 tls');
+            return;
         }
 
         $this->client->server = $this->args['HTTPSSERVER'];
@@ -345,7 +358,9 @@ class HTTPTest extends ServerTest
         $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
         $this->client->setSSLVersion($this->args['SSLVERSION']);
 
+        $this->expectHttp2 = true;
         $this->$method();
+        $this->expectHttp2 = false;
     }
 
     /**
@@ -423,4 +438,18 @@ class HTTPTest extends ServerTest
 
         $this->$method();
     }
+
+    /**
+     * @param \PhpXmlRpc\Response $r
+     * @return void
+     */
+    protected function validateResponse($r)
+    {
+        /// @todo check $r->httpResponse()['protocol_version']
+        if ($this->expectHttp2) {
+
+        } else {
+
+        }
+    }
 }
index 6c7b675..c040f8f 100644 (file)
@@ -2,11 +2,11 @@
 # HTTPSERVER
 # TESTS_ROOT_DIR
 
-# Enable http2
-Protocols h2 h2c http/1.1
-
 <VirtualHost *:80>
 
+  # Enable http2
+  Protocols h2c http/1.1
+
   DocumentRoot ${TESTS_ROOT_DIR}
 
   #ErrorLog "${TESTS_ROOT_DIR}/apache_error.log"
@@ -33,6 +33,9 @@ Protocols h2 h2c http/1.1
 
 <VirtualHost _default_:443>
 
+  # Enable http2
+  Protocols h2 http/1.1
+
   DocumentRoot ${TESTS_ROOT_DIR}
 
   #ErrorLog "${TESTS_ROOT_DIR}/apache_error.log"