Просмотр исходного кода

fix for ZF-10664 api-change do not merge backward. You can now post a file with Zend_Rest_Client and a prepared Zend_Http_Client, if you do no reset the parameters explicitly

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24512 44c647ce-9c0f-0410-b52a-842ac1e357ba
mcleod@spaceweb.nl 14 лет назад
Родитель
Сommit
c822b25239
2 измененных файлов с 51 добавлено и 1 удалено
  1. 30 1
      library/Zend/Rest/Client.php
  2. 21 0
      tests/Zend/Rest/ClientTest.php

+ 30 - 1
library/Zend/Rest/Client.php

@@ -50,6 +50,13 @@ class Zend_Rest_Client extends Zend_Service_Abstract
      * @var Zend_Uri_Http
      */
     protected $_uri = null;
+    
+    /**
+     * Flag indicating the Zend_Http_Client is fresh and needs no reset.
+     * Must be set explicitly if you want to keep preset parameters.
+     * @var bool true if you do not want a reset. Default false.
+     */
+    protected $_noReset = false;
 
     /**
      * Constructor
@@ -118,7 +125,29 @@ class Zend_Rest_Client extends Zend_Service_Abstract
          * Get the HTTP client and configure it for the endpoint URI.  Do this each time
          * because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses.
          */
-        self::getHttpClient()->resetParameters()->setUri($this->_uri);
+        if ($this->_noReset) {
+            // if $_noReset we do not want to reset on this request, 
+            // but we do on any subsequent request
+            $this->_noReset = false;
+        } else {
+            self::getHttpClient()->resetParameters();
+        }
+        
+        self::getHttpClient()->setUri($this->_uri);
+    }
+    
+    /**
+     * Tells Zend_Rest_Client not to reset all parameters on it's 
+     * Zend_Http_Client. If you want no reset, this must be called explicitly
+     * before every request for which you do not want to reset the parameters.
+     * Parameters will accumulate between requests, but as soon as you do not
+     * call this function prior to any request, all preset parameters will be reset
+     * as by default.
+     * @param boolean $bool
+     */
+    public function setNoReset($bool = true)
+    {
+        $this->_noReset = $bool;
     }
 
     /**

+ 21 - 0
tests/Zend/Rest/ClientTest.php

@@ -51,6 +51,27 @@ class Zend_Rest_ClientTest extends PHPUnit_Framework_TestCase
 
         $this->rest = new Zend_Rest_Client('http://framework.zend.com/');
     }
+    
+    /**
+     * @group ZF-10664
+     * 
+     * Test that you can post a file using a preset 
+     * Zend_Http_Client that has a file to post,
+     * by calling $restClient->setNoReset() prior to issuing the
+     * restPost() call.    
+     */
+    public function testCanPostFileInPresetHttpClient()
+    {
+        $client = new Zend_Rest_Client('http://framework.zend.com');
+        $httpClient = new Zend_Http_Client();
+        $text = 'this is some plain text';
+        $httpClient->setFileUpload('some_text.txt', 'upload', $text, 'text/plain');
+        $client->setHttpClient($httpClient);
+        $client->setNoReset();
+        $client->restPost('/file');
+        $request = $httpClient->getLastRequest();
+        $this->assertTrue(strpos($request, $text) !== false, 'The file is not in the request');
+    }
 
     public function testUri()
     {