Selaa lähdekoodia

Added support for auth with username and password in URI. Closes ZF-3491

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17648 44c647ce-9c0f-0410-b52a-842ac1e357ba
cogo 16 vuotta sitten
vanhempi
commit
b367271317

+ 6 - 1
documentation/manual/en/module_specs/Zend_Http_Client-Advanced.xml

@@ -234,7 +234,9 @@ $client->setRawData($xml)->setEncType('text/xml')->request('POST');
         <title>HTTP Authentication</title>
         <para>
             Currently, Zend_Http_Client only supports basic HTTP authentication.
-            This feature is utilized using the setAuth() method. The method
+            This feature is utilized using the <methodname>setAuth()</methodname> 
+            method, or by specifying a username and a password in the URI. The 
+            <methodname>setAuth()</methodname> method
             takes 3 parameters: The user name, the password and an optional
             authentication type parameter. As mentioned, currently only basic
             authentication is supported (digest authentication support is
@@ -247,6 +249,9 @@ $client->setAuth('shahar', 'myPassword!', Zend_Http_Client::AUTH_BASIC);
 
 // Since basic auth is default, you can just do this:
 $client->setAuth('shahar', 'myPassword!');
+
+// You can also specify username and password in the URI
+$client->setUri('http://christer:secret@example.com');
 ]]></programlisting>
             </example>
         </para>

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

@@ -267,6 +267,11 @@ class Zend_Http_Client
             throw new Zend_Http_Client_Exception('Passed parameter is not a valid HTTP URI.');
         }
 
+        // Set auth if username and password has been specified in the uri
+        if ($uri->getUsername() && $uri->getPassword()) {
+            $this->setAuth($uri->getUsername(), $uri->getPassword());
+        }
+
         // We have no ports, set the defaults
         if (! $uri->getPort()) {
             $uri->setPort(($uri->getScheme() == 'https' ? 443 : 80));
@@ -536,6 +541,9 @@ class Zend_Http_Client
         if ($user === false || $user === null) {
             $this->auth = null;
 
+            // Clear the auth information in the uri instance as well
+            $this->getUri()->setUsername('');
+            $this->getUri()->setPassword('');
         // Else, set up authentication
         } else {
             // Check we got a proper authentication type

+ 44 - 0
tests/Zend/Http/Client/CommonHttpTests.php

@@ -538,6 +538,32 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
     }
 
     /**
+     * Test that we can properly use Basic HTTP authentication by specifying username and password
+     * in the URI
+     *
+     */
+    public function testHttpAuthBasicWithCredentialsInUri()
+    {
+    	$uri = str_replace('http://', 'http://%s:%s@', $this->baseuri) . 'testHttpAuth.php';
+
+        $this->client->setParameterGet(array(
+            'user'   => 'alice',
+            'pass'   => 'secret',
+            'method' => 'Basic'
+        ));
+
+        // First - fail password
+        $this->client->setUri(sprintf($uri, 'alice', 'wrong'));
+        $res = $this->client->request();
+        $this->assertEquals(401, $res->getStatus(), 'Expected HTTP 401 response was not recieved');
+
+        // Now use good password
+        $this->client->setUri(sprintf($uri, 'alice', 'secret'));
+        $res = $this->client->request();
+        $this->assertEquals(200, $res->getStatus(), 'Expected HTTP 200 response was not recieved');
+    }
+
+    /**
      * Test we can unset HTTP authentication
      *
      */
@@ -556,6 +582,24 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
     }
 
     /**
+     * Test that we can unset HTTP authentication when credentials is specified in the URI
+     *
+     */
+    public function testCancelAuthWithCredentialsInUri()
+    {
+    	$uri = str_replace('http://', 'http://%s:%s@', $this->baseuri) . 'testHttpAuth.php';
+
+        // Set auth and cancel it
+        $this->client->setUri(sprintf($uri, 'alice', 'secret'));
+        $this->client->setAuth(false);
+        $res = $this->client->request();
+
+        $this->assertEquals(401, $res->getStatus(), 'Expected HTTP 401 response was not recieved');
+        $this->assertNotContains('alice', $res->getBody(), "Body contains the user name, but it shouldn't");
+        $this->assertNotContains('secret', $res->getBody(), "Body contains the password, but it shouldn't");
+    }
+
+    /**
      * Cookie and CookieJar Tests
      *
      */