1st pass at cleanup of unit tests; a couple of nitpicks from SLInsights
authorgggeek <giunta.gaetano@gmail.com>
Sun, 5 Nov 2017 20:42:23 +0000 (20:42 +0000)
committergggeek <giunta.gaetano@gmail.com>
Sun, 5 Nov 2017 20:42:23 +0000 (20:42 +0000)
NEWS
demo/client/proxy.php
src/Client.php
tests/3LocalhostTest.php
tests/4LocalhostMultiTest.php

diff --git a/NEWS b/NEWS
index cd3a977..f8c9afd 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,10 @@
+XML-RPC for PHP version 4.2.3 - 2017/11/5
+
+* fixed: compatibility with Basic/Digest/NTLM auth when using client in cURL mode (issue #55)
+
+* improved: added unit tests for Basic and Digest http auth
+
+
 XML-RPC for PHP version 4.2.2 - 2017/10/15
 
 * fixed: compatibility with Lighttpd target servers when using client in cURL mode and request body size > 1024 bytes (issue #56)
index 7173db0..a8c696c 100644 (file)
@@ -31,7 +31,7 @@ class PhpXmlRpcProxy
      *
      * @throws Exception
      */
-    function __call($name, $arguments)
+    public function __call($name, $arguments)
     {
         $encoder = new PhpXmlRpc\Encoder();
         $valueArray = array();
@@ -57,4 +57,4 @@ $stateNo = rand(1, 51);
 $proxy = new PhpXmlRpcProxy(new \PhpXmlRpc\Client('http://phpxmlrpc.sourceforge.net/server.php'));
 $stateName = $proxy->getStateName($stateNo);
 
-echo "State $stateNo is ".htmlspecialchars($stateName);
\ No newline at end of file
+echo "State $stateNo is ".htmlspecialchars($stateName);
index ede6a68..4698478 100644 (file)
@@ -125,7 +125,7 @@ class Client
     public function __construct($path, $server = '', $port = '', $method = '')
     {
         // allow user to specify all params in $path
-        if ($server == '' and $port == '' and $method == '') {
+        if ($server == '' && $port == '' && $method == '') {
             $parts = parse_url($path);
             $server = $parts['host'];
             $path = isset($parts['path']) ? $parts['path'] : '';
index 118f10b..9be1f3a 100644 (file)
@@ -116,7 +116,7 @@ class LocalhostTest extends PHPUnit_Framework_TestCase
      * @param PhpXmlRpc\Request|array $msg
      * @param int|array $errorCode
      * @param bool $returnResponse
-     * @return mixed|\PhpXmlRpc\Response|\PhpXmlRpc\Response[]|\PhpXmlRpc\Value|string|void
+     * @return mixed|\PhpXmlRpc\Response|\PhpXmlRpc\Response[]|\PhpXmlRpc\Value|string|null
      */
     protected function send($msg, $errorCode = 0, $returnResponse = false)
     {
@@ -141,10 +141,22 @@ class LocalhostTest extends PHPUnit_Framework_TestCase
                 return $r->value();
             }
         } else {
-            return;
+            return null;
         }
     }
 
+    /**
+     * Adds (and replaces) query params to the url currently used by the client
+     * @param array $data
+     */
+    protected function addQueryParams($data)
+    {
+        $query = parse_url($this->client->path, PHP_URL_QUERY);
+        parse_str($query, $vars);
+        $query = http_build_query(array_merge($vars, $data));
+        $this->client->path = parse_url($this->client->path, PHP_URL_PATH) . '?' . $query;
+    }
+
     public function testString()
     {
         $sendString = "here are 3 \"entities\": < > & " .
@@ -235,7 +247,7 @@ class LocalhostTest extends PHPUnit_Framework_TestCase
         PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
         // no encoding declaration either in the http header or xml prolog, let mb_detect_encoding
         // (used on the server side) sort it out
-        $this->client->path = $this->args['URI'].'?DETECT_ENCODINGS[]=EUC-JP&DETECT_ENCODINGS[]=UTF-8';
+        $this->addQueryParams(array('DETECT_ENCODINGS' => array('EUC-JP', 'UTF-8')));
         $v = $this->send(mb_convert_encoding($str, 'EUC-JP', 'UTF-8'));
         $this->assertEquals($sendString, $v->scalarval());
         PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
@@ -261,7 +273,7 @@ class LocalhostTest extends PHPUnit_Framework_TestCase
 
         // no encoding declaration either in the http header or xml prolog, let mb_detect_encoding
         // (used on the server side) sort it out
-        $this->client->path = $this->args['URI'].'?DETECT_ENCODINGS[]=ISO-8859-1&DETECT_ENCODINGS[]=UTF-8';
+        $this->addQueryParams(array('DETECT_ENCODINGS' => array('ISO-8859-1', 'UTF-8')));
         $v = $this->send($str);
         $this->assertEquals($sendString, $v->scalarval());
     }
@@ -598,7 +610,7 @@ And turned it into nylon';
             $val = $r[3]->value();
             $this->assertTrue(is_array($val), "good2 did not return array");
         }
-        $this->client->return_type = 'xmlrpcvals';
+        //$this->client->return_type = 'xmlrpcvals';
     }
 
     public function testCatchWarnings()
@@ -618,9 +630,9 @@ And turned it into nylon';
             new xmlrpcval('whatever', 'string'),
         ));
         $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
-        $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=1';
+        $this->addQueryParams(array('EXCEPTION_HANDLING' => 1));
         $v = $this->send($m, 1); // the error code of the expected exception
-        $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=2';
+        $this->addQueryParams(array('EXCEPTION_HANDLING' => 2));
         // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
         // as php will generate an http status code of either 200 or 500...
         $v = $this->send($m, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
index 8788ddc..4462349 100644 (file)
@@ -14,92 +14,127 @@ include_once __DIR__ . '/3LocalhostTest.php';
 class LocalhostMultiTest extends LocalhostTest
 {
     /**
+     * Returns all test methods from the base class, except the ones which failed already
+     *
      * @todo reintroduce skipping of tests which failed when executed individually if test runs happen as separate processes
      * @todo reintroduce skipping of tests within the loop
      */
-    function _runtests()
+    public function getSingleHttpTestMethods()
     {
         $unsafeMethods = array('testHttps', 'testCatchExceptions', 'testUtf8Method', 'testServerComments', 'testExoticCharsetsRequests',
             'testExoticCharsetsRequests2', 'testExoticCharsetsRequests3',
             // @todo the following are currently not compatible w Digest Auth (most likely because of client copy) and should be fixed
             'testcatchWarnings', 'testWrappedMethodAsSource', 'testTransferOfObjectViaWrapping');
+
+        $methods = array();
         foreach(get_class_methods('LocalhostTest') as $method)
         {
             if(strpos($method, 'test') === 0 && !in_array($method, $unsafeMethods))
             {
-                if (!isset(self::$failed_tests[$method]))
-                    $this->$method();
+                if (!isset(self::$failed_tests[$method])) {
+                    $methods[$method] = array($method);
+                }
             }
-            /*if ($this->_failed)
-            {
-                break;
-            }*/
         }
+
+        return $methods;
     }
 
-    function testDeflate()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testDeflate($method)
     {
         if(!function_exists('gzdeflate'))
         {
             $this->markTestSkipped('Zlib missing: cannot test deflate functionality');
             return;
         }
+
         $this->client->accepted_compression = array('deflate');
         $this->client->request_compression = 'deflate';
-        $this->_runtests();
+
+        $this->$method();
     }
 
-    function testGzip()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testGzip($method)
     {
         if(!function_exists('gzdeflate'))
         {
             $this->markTestSkipped('Zlib missing: cannot test gzip functionality');
             return;
         }
+
         $this->client->accepted_compression = array('gzip');
         $this->client->request_compression = 'gzip';
-        $this->_runtests();
+
+        $this->$method();
     }
 
-    function testKeepAlives()
+    public function testKeepAlives()
     {
         if(!function_exists('curl_init'))
         {
             $this->markTestSkipped('CURL missing: cannot test http 1.1');
             return;
         }
+
         $this->method = 'http11';
+        $this->client->method = 'http11';
         $this->client->keepalive = true;
-        $this->_runtests();
+
+        // to successfully test keepalive, we have to reuse the same client for all tests, we can not recreate one on setup/teardown...
+        foreach ($this->getSingleHttpTestMethods() as $method) {
+            $this->$method;
+        }
     }
 
-    function testProxy()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testProxy($method)
     {
-        if ($this->args['PROXYSERVER'])
+        if (!$this->args['PROXYSERVER'])
         {
-            $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
-            $this->_runtests();
-        }
-        else
             $this->markTestSkipped('PROXY definition missing: cannot test proxy');
+            return;
+        }
+
+        $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
+
+        $this->$method();
     }
 
-    function testHttp11()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testHttp11($method)
     {
         if(!function_exists('curl_init'))
         {
             $this->markTestSkipped('CURL missing: cannot test http 1.1');
             return;
         }
+
         $this->method = 'http11'; // not an error the double assignment!
         $this->client->method = 'http11';
-        //$this->client->verifyhost = 0;
-        //$this->client->verifypeer = 0;
         $this->client->keepalive = false;
-        $this->_runtests();
+
+        $this->$method();
     }
 
-    function testHttp11Gzip()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testHttp11Gzip($method)
     {
         if(!function_exists('curl_init'))
         {
@@ -111,10 +146,15 @@ class LocalhostMultiTest extends LocalhostTest
         $this->client->keepalive = false;
         $this->client->accepted_compression = array('gzip');
         $this->client->request_compression = 'gzip';
-        $this->_runtests();
+
+        $this->$method();
     }
 
-    function testHttp11Deflate()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testHttp11Deflate($method)
     {
         if(!function_exists('curl_init'))
         {
@@ -126,10 +166,15 @@ class LocalhostMultiTest extends LocalhostTest
         $this->client->keepalive = false;
         $this->client->accepted_compression = array('deflate');
         $this->client->request_compression = 'deflate';
-        $this->_runtests();
+
+        $this->$method();
     }
 
-    function testHttp11Proxy()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testHttp11Proxy($method)
     {
         if(!function_exists('curl_init'))
         {
@@ -141,22 +186,27 @@ class LocalhostMultiTest extends LocalhostTest
             $this->markTestSkipped('PROXY definition missing: cannot test proxy w. http 1.1');
             return;
         }
+
         $this->method = 'http11'; // not an error the double assignment!
         $this->client->method = 'http11';
         $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
-        //$this->client->verifyhost = 0;
-        //$this->client->verifypeer = 0;
         $this->client->keepalive = false;
-        $this->_runtests();
+
+        $this->$method();
     }
 
-    function testHttps()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testHttps($method)
     {
         if(!function_exists('curl_init'))
         {
             $this->markTestSkipped('CURL missing: cannot test https functionality');
             return;
         }
+
         $this->client->server = $this->args['HTTPSSERVER'];
         $this->method = 'https';
         $this->client->method = 'https';
@@ -164,10 +214,15 @@ class LocalhostMultiTest extends LocalhostTest
         $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
         $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
         $this->client->setSSLVersion($this->args['SSLVERSION']);
-        $this->_runtests();
+
+        $this->$method();
     }
 
-    function testHttpsProxy()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testHttpsProxy($method)
     {
         if(!function_exists('curl_init'))
         {
@@ -187,53 +242,83 @@ class LocalhostMultiTest extends LocalhostTest
         $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
         $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
         $this->client->setSSLVersion($this->args['SSLVERSION']);
-        $this->_runtests();
+
+        $this->$method();
     }
 
-    function testUTF8Responses()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testUTF8Responses($method)
     {
-        //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
-        $this->client->path = $this->args['URI'].'?RESPONSE_ENCODING=UTF-8';
-        $this->_runtests();
+        $this->addQueryParams(array('RESPONSE_ENCODING' => 'UTF-8'));
+
+        $this->$method();
     }
 
-    function testUTF8Requests()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testUTF8Requests($method)
     {
         $this->client->request_charset_encoding = 'UTF-8';
-        $this->_runtests();
+
+        $this->$method();
     }
 
-    function testISOResponses()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testISOResponses($method)
     {
-        //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
-        $this->client->path = $this->args['URI'].'?RESPONSE_ENCODING=ISO-8859-1';
-        $this->_runtests();
+        $this->addQueryParams(array('RESPONSE_ENCODING' => 'ISO-8859-1'));
+
+        $this->$method();
     }
 
-    function testISORequests()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testISORequests($method)
     {
         $this->client->request_charset_encoding = 'ISO-8859-1';
-        $this->_runtests();
+
+        $this->$method();
     }
 
-    function testBasicAuth()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testBasicAuth($method)
     {
         $this->client->setCredentials('test', 'test');
-        $this->client->path = $this->args['URI'].'?FORCE_AUTH=Basic';
-        $this->_runtests();
+        $this->addQueryParams(array('FORCE_AUTH' => 'Basic'));
+
+        $this->$method();
     }
 
-    function testDigestAuth()
+    /**
+     * @dataProvider getSingleHttpTestMethods
+     * @param string $method
+     */
+    public function testDigestAuth($method)
     {
         if (!function_exists('curl_init'))
         {
             $this->markTestSkipped('CURL missing: cannot test digest auth functionality');
             return;
         }
+
         $this->client->setCredentials('test', 'test', CURLAUTH_DIGEST);
-        $this->client->path = $this->args['URI'].'?FORCE_AUTH=Digest';
+        $this->addQueryParams(array('FORCE_AUTH' => 'Digest'));
         $this->method = 'http11';
         $this->client->method = 'http11';
-        $this->_runtests();
+
+        $this->$method();
     }
 }