Quellcode durchsuchen

- Fixing (some of) ZF-5577 - Adding Zend_Config support to Zend_Http_Client
- Still need to add support to adapters
- Improving coding standards


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17027 44c647ce-9c0f-0410-b52a-842ac1e357ba

shahar vor 16 Jahren
Ursprung
Commit
3b2ba6993c

+ 8 - 4
documentation/manual/en/module_specs/Zend_Http_Client.xml

@@ -15,8 +15,8 @@
         <title>Using Zend_Http_Client</title>
         <para>
             The class constructor optionally accepts a URL as its first parameter
-            (can be either a string or a Zend_Uri_Http object), and an optional
-            array of configuration parameters. Both can be left out,
+            (can be either a string or a Zend_Uri_Http object), and an array or 
+            Zend_Config object containing configuration options. Both can be left out,
             and set later using the setUri() and setConfig() methods.
             <example id="zend.http.client.introduction.example-1">
                 <title>Instantiating a Zend_Http_Client Object</title>
@@ -31,6 +31,10 @@ $client->setUri('http://example.org');
 $client->setConfig(array(
     'maxredirects' => 0,
     'timeout'      => 30));
+    
+// You can also use a Zend_Config object to set the client's configuration
+$config = new Zend_Config_Ini('httpclient.ini, 'secure');
+$client->setConfig($config);
 ]]></programlisting>
             </example>
 
@@ -49,8 +53,8 @@ $client->setConfig(array(
         <title>Configuration Parameters</title>
         <para>
             The constructor and setConfig() method accept an associative array
-            of configuration parameters. Setting these parameters is optional,
-            as they all have default values.
+            of configuration parameters, or a Zend_Config object. Setting these 
+            parameters is optional, as they all have default values.
             <table id="zend.http.client.configuration.table">
                 <title>Zend_Http_Client configuration parameters</title>
                 <tgroup cols="4">

+ 8 - 4
library/Zend/Http/Client.php

@@ -295,20 +295,24 @@ class Zend_Http_Client
     /**
      * Set configuration parameters for this HTTP client
      *
-     * @param array $config
+     * @param  Zend_Config | array $config
      * @return Zend_Http_Client
      * @throws Zend_Http_Client_Exception
      */
     public function setConfig($config = array())
     {
-        if (! is_array($config)) {
+        if ($config instanceof Zend_Config) {
+            $config = $config->toArray();
+
+        } elseif (! is_array($config)) {
             /** @see Zend_Http_Client_Exception */
             require_once 'Zend/Http/Client/Exception.php';
-            throw new Zend_Http_Client_Exception('Expected array parameter, given ' . gettype($config));
+            throw new Zend_Http_Client_Exception('Array or Zend_Config object expected, got ' . gettype($config));
         }
 
-        foreach ($config as $k => $v)
+        foreach ($config as $k => $v) {
             $this->config[strtolower($k)] = $v;
+        }
 
         // Pass configuration options to the adapter if it exists
         if ($this->adapter instanceof Zend_Http_Client_Adapter_Interface) {

+ 4 - 4
library/Zend/Http/Client/Adapter/Proxy.php

@@ -89,12 +89,12 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
         // If no proxy is set, fall back to Socket adapter
         if (! $this->config['proxy_host']) {
             return parent::connect($host, $port, $secure);
-        } 
+        }
 
         // Connect (a non-secure connection) to the proxy server
         return parent::connect(
-            $this->config['proxy_host'], 
-            $this->config['proxy_port'], 
+            $this->config['proxy_host'],
+            $this->config['proxy_port'],
             false
         );
     }
@@ -147,7 +147,7 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
         // Build request headers
         if ($this->negotiated) {
             $path = $uri->getPath();
-            if ($uri->getQuery()) { 
+            if ($uri->getQuery()) {
                 $path .= '?' . $uri->getQuery();
             }
             $request = "$method $path HTTP/$http_ver\r\n";

+ 5 - 5
library/Zend/Http/Client/Adapter/Socket.php

@@ -299,7 +299,7 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
 
         // Check headers to see what kind of connection / transfer encoding we have
         $headers = Zend_Http_Response::extractHeaders($response);
-        
+
         /**
          * Responses to HEAD requests and 204 or 304 responses are not expected
          * to have a body - stop reading here
@@ -310,10 +310,10 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
             // Close the connection if requested to do so by the server
             if (isset($headers['connection']) && $headers['connection'] == 'close') {
                 $this->close();
-            }    
+            }
             return $response;
         }
-        
+
         // If we got a 'transfer-encoding: chunked' header
         if (isset($headers['transfer-encoding'])) {
 
@@ -406,7 +406,7 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
         if (isset($headers['connection']) && $headers['connection'] == 'close') {
             $this->close();
         }
-        
+
         return $response;
     }
 
@@ -422,7 +422,7 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
     }
 
     /**
-     * Check if the socket has timed out - if so close connection and throw 
+     * Check if the socket has timed out - if so close connection and throw
      * an exception
      *
      * @throws Zend_Http_Client_Adapter_Exception with READ_TIMEOUT code

+ 6 - 6
tests/Zend/Http/Client/SocketTest.php

@@ -111,30 +111,30 @@ class Zend_Http_Client_SocketTest extends Zend_Http_Client_CommonHttpTests
 
     /**
      * Test that we get the right exception after a socket timeout
-     * 
+     *
      * @link http://framework.zend.com/issues/browse/ZF-7309
      */
     public function testExceptionOnReadTimeout()
     {
         // Set 1 second timeout
         $this->client->setConfig(array('timeout' => 1));
-        
+
         $start = microtime(true);
-        
+
         try {
             $this->client->request();
             $this->fail("Expected a timeout Zend_Http_Client_Adapter_Exception");
         } catch (Zend_Http_Client_Adapter_Exception $e) {
             $this->assertEquals(Zend_Http_Client_Adapter_Exception::READ_TIMEOUT, $e->getCode());
         }
-        
+
         $time = (microtime(true) - $start);
-        
+
         // We should be very close to 1 second
         $this->assertLessThan(2, $time);
     }
 
-    
+
     /**
      * Data Providers
      */

+ 95 - 22
tests/Zend/Http/Client/StaticTest.php

@@ -272,6 +272,85 @@ class Zend_Http_Client_StaticTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * Configuration Handling
+     */
+
+    /**
+     * Test that we can set a valid configuration array with some options
+     *
+     */
+    public function testConfigSetAsArray()
+    {
+        $config = array(
+            'timeout'    => 500,
+            'someoption' => 'hasvalue'
+        );
+
+        $this->_client->setConfig($config);
+
+        $hasConfig = $this->getObjectAttribute($this->_client, 'config');
+        foreach($config as $k => $v) {
+            $this->assertEquals($v, $hasConfig[$k]);
+        }
+    }
+
+    /**
+     * Test that a Zend_Config object can be used to set configuration
+     *
+     * @link http://framework.zend.com/issues/browse/ZF-5577
+     */
+    public function testConfigSetAsZendConfig()
+    {
+        require_once 'Zend/Config.php';
+
+        $config = new Zend_Config(array(
+            'timeout'  => 400,
+            'nested'   => array(
+                'item' => 'value',
+            )
+        ));
+
+        $this->_client->setConfig($config);
+
+        $hasConfig = $this->getObjectAttribute($this->_client, 'config');
+        $this->assertEquals($config->timeout, $hasConfig['timeout']);
+        $this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
+    }
+
+    /**
+     * Test that passing invalid variables to setConfig() causes an exception
+     *
+     * @dataProvider      invalidConfigProvider
+     * @expectedException Zend_Http_Client_Exception
+     */
+    public function testConfigSetInvalid($config)
+    {
+        $this->_client->setConfig($config);
+    }
+
+    /**
+     * Test that configuration options are passed to the adapter after the
+     * adapter is instantiated
+     *
+     * @link http://framework.zend.com/issues/browse/ZF-4557
+     */
+    public function testConfigPassToAdapterZF4557()
+    {
+        $adapter = new Zend_Http_Client_Adapter_Test();
+
+        // test that config passes when we set the adapter
+        $this->_client->setConfig(array('param' => 'value1'));
+        $this->_client->setAdapter($adapter);
+        $adapterCfg = $this->getObjectAttribute($adapter, 'config');
+        $this->assertEquals('value1', $adapterCfg['param']);
+
+        // test that adapter config value changes when we set client config
+        $this->_client->setConfig(array('param' => 'value2'));
+        $adapterCfg = $this->getObjectAttribute($adapter, 'config');
+        $this->assertEquals('value2', $adapterCfg['param']);
+    }
+
+    /**
      * Other Tests
      */
 
@@ -370,28 +449,6 @@ class Zend_Http_Client_StaticTest extends PHPUnit_Framework_TestCase
     }
 
     /**
-     * Test that configuration options are passed to the adapter after the
-     * adapter is instantiated
-     *
-     * @link http://framework.zend.com/issues/browse/ZF-4557
-     */
-    public function testConfigPassToAdapterZF4557()
-    {
-        $adapter = new Zend_Http_Client_Adapter_Test();
-
-        // test that config passes when we set the adapter
-        $this->_client->setConfig(array('param' => 'value1'));
-        $this->_client->setAdapter($adapter);
-        $adapterCfg = $this->getObjectAttribute($adapter, 'config');
-        $this->assertEquals('value1', $adapterCfg['param']);
-
-        // test that adapter config value changes when we set client config
-        $this->_client->setConfig(array('param' => 'value2'));
-        $adapterCfg = $this->getObjectAttribute($adapter, 'config');
-        $this->assertEquals('value2', $adapterCfg['param']);
-    }
-
-    /**
      * Test that POST data with mutli-dimentional array is properly encoded as
      * multipart/form-data
      *
@@ -463,4 +520,20 @@ class Zend_Http_Client_StaticTest extends PHPUnit_Framework_TestCase
             array("Injected\nnewline")
         );
     }
+
+    /**
+     * Data provider for invalid configuration containers
+     *
+     * @return array
+     */
+    static public function invalidConfigProvider()
+    {
+        return array(
+            array(false),
+            array('foo => bar'),
+            array(null),
+            array(new stdClass),
+            array(55)
+        );
+    }
 }

+ 1 - 1
tests/Zend/Http/Client/_files/testExceptionOnReadTimeout.php

@@ -1,7 +1,7 @@
 <?php
 
 /**
- * This script does nothing but sleep, and is used to test how 
+ * This script does nothing but sleep, and is used to test how
  * Zend_Http_Client handles an exceeded timeout
  */
 

+ 2 - 2
tests/Zend/Http/ResponseTest.php

@@ -70,7 +70,7 @@ class Zend_Http_ResponseTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
         $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getRawBody()));
     }
-    
+
     public function testChunkedResponseCaseInsensitiveZF5438()
     {
         $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_chunked_case');
@@ -81,7 +81,7 @@ class Zend_Http_ResponseTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
         $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getRawBody()));
     }
-    
+
 
     public function testLineBreaksCompatibility()
     {