Browse Source

fixed issue #ZF-9685: added a close method to prevent fatal error

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21919 44c647ce-9c0f-0410-b52a-842ac1e357ba
dragonbe 15 years ago
parent
commit
b4f6460799
2 changed files with 56 additions and 3 deletions
  1. 13 3
      library/Zend/Http/Client.php
  2. 43 0
      tests/Zend/Http/Client/StaticTest.php

+ 13 - 3
library/Zend/Http/Client.php

@@ -910,15 +910,25 @@ class Zend_Http_Client
                  'Zend_Http_Client');
         }
 
-        $fp = fopen($this->_stream_name, "w+b");
-        if(!$fp) {
+        if (false === ($fp = @fopen($this->_stream_name, "w+b"))) {
                 $this->close();
                 require_once 'Zend/Http/Client/Exception.php';
-                throw new Zend_Http_Client_Exception("Could not open temp file $name");
+                throw new Zend_Http_Client_Exception("Could not open temp file {$this->_stream_name}");
 
         }
         return $fp;
     }
+    
+    /**
+     * Closes the connection with the server
+     * 
+     * @return void
+     * @group ZF-9685
+     */
+    public function close()
+    {
+    	$this->getAdapter()->close();
+    }
 
     /**
      * Send the HTTP request and return an HTTP response object

+ 43 - 0
tests/Zend/Http/Client/StaticTest.php

@@ -542,6 +542,49 @@ class Zend_Http_Client_StaticTest extends PHPUnit_Framework_TestCase
         // if the bug exists this call should creates a fatal error
         $client->setAuth(false);
     }
+    
+	/**
+     * Testing if the connection isn't closed
+     * 
+     * @group ZF-9685
+     */
+    public function testOpenTempStreamWithValidFileDoesntThrowsException()
+    {
+    	$url = 'http://www.example.com';
+    	$config = array (
+			'output_stream' => realpath(dirname(__FILE__) . '/_files/zend_http_client_stream.file'),
+		);
+		$client = new Zend_Http_Client($url, $config);
+		try {
+			$result = $client->request();
+		} catch (Zend_Http_Client_Exception $e) {
+			$this->fail('Unexpected exception was thrown');
+		}
+		// we can safely return until we can verify link is still active
+		// @todo verify link is still active
+		return;
+    }
+    
+    /**
+     * Testing if the connection can be closed
+     * 
+     * @group ZF-9685
+     */
+    public function testOpenTempStreamWithBogusFileClosesTheConnection()
+    {
+    	$url = 'http://www.example.com';
+    	$config = array (
+			'output_stream' => '/path/to/bogus/file.ext',
+		);
+		$client = new Zend_Http_Client($url, $config);
+		try {
+			$result = $client->request();
+			$this->fail('Expected exception was not thrown');
+		} catch (Zend_Http_Client_Exception $e) {
+			// we return since we expect the exception
+			return;
+		}
+    }
 
     /**
      * Data providers