Kaynağa Gözat

ZF-11030 and ZF-10162 (duplicates)
fixes and test for Zend_Http_Client not sending PUT data if not content-type is specified, not sending DELETE data at all is now fixed and tested.

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

mcleod@spaceweb.nl 14 yıl önce
ebeveyn
işleme
b47d9a4725

+ 3 - 0
library/Zend/Http/Client/Adapter/Curl.php

@@ -393,6 +393,9 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
         } elseif ($method == Zend_Http_Client::PUT) {
         } elseif ($method == Zend_Http_Client::PUT) {
             // This is a PUT by a setRawData string, not by file-handle
             // This is a PUT by a setRawData string, not by file-handle
             curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
             curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
+        } elseif ($method == Zend_Http_Client::DELETE) {
+            // This is a DELETE by a setRawData string
+            curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
         }
         }
 
 
         // set additional curl options
         // set additional curl options

+ 171 - 2
tests/Zend/Http/Client/CommonHttpTests.php

@@ -19,7 +19,6 @@
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @version    $Id$
  * @version    $Id$
  */
  */
-
 // Read local configuration
 // Read local configuration
 if (! defined('TESTS_ZEND_HTTP_CLIENT_BASEURI') &&
 if (! defined('TESTS_ZEND_HTTP_CLIENT_BASEURI') &&
     is_readable('TestConfiguration.php')) {
     is_readable('TestConfiguration.php')) {
@@ -195,6 +194,51 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $res = $this->client->request('POST');
         $res = $this->client->request('POST');
         $this->assertEquals(serialize($params), $res->getBody(), "POST data integrity test failed");
         $this->assertEquals(serialize($params), $res->getBody(), "POST data integrity test failed");
     }
     }
+    
+    /**
+     * Test we can properly send PUT parameters with
+     * application/x-www-form-urlencoded content type
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testPutDataUrlEncoded($params)
+    {
+        $this->client->setUri($this->baseuri . 'testPutData.php');
+        $this->client->setEncType(Zend_Http_Client::ENC_URLENCODED);
+        $this->client->setParameterPost($params);
+        $res = $this->client->request('PUT');
+        $this->assertEquals(serialize($params), $res->getBody(), "PUT data integrity test failed");
+    }
+    
+    /**
+     * Test we can properly send PUT parameters without
+     * content type, relying on default content type (urlencoded)
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testPutDataDefault($params)
+    {
+        $this->client->setUri($this->baseuri . 'testPutData.php');
+        // note that no content type is set
+        $this->client->setParameterPost($params);
+        $res = $this->client->request('PUT');
+        $this->assertEquals(serialize($params), $res->getBody(), "PUT data integrity test failed for default content-type");
+    }
+    
+    /**
+     * Test we can properly send parameters with
+     * application/x-www-form-urlencoded content type
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testDeleteDataUrlEncoded($params)
+    {
+        $this->client->setUri($this->baseuri . 'testDeleteData.php');
+        $this->client->setEncType(Zend_Http_Client::ENC_URLENCODED);
+        $this->client->setParameterPost($params);
+        $res = $this->client->request('DELETE');
+        $this->assertEquals(serialize($params), $res->getBody(), "DELETE data integrity test failed");
+    }
 
 
     /**
     /**
      * Test we can properly send POST parameters with
      * Test we can properly send POST parameters with
@@ -210,6 +254,100 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $res = $this->client->request('POST');
         $res = $this->client->request('POST');
         $this->assertEquals(serialize($params), $res->getBody(), "POST data integrity test failed");
         $this->assertEquals(serialize($params), $res->getBody(), "POST data integrity test failed");
     }
     }
+    
+    /**
+     * Test we can properly send PUT parameters with
+     * multipart/form-data content type
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testPutDataMultipart($params)
+    {
+        $this->client->setUri($this->baseuri . 'testRawPutData.php');
+        $this->client->setParameterPost($params);
+        $this->client->setMethod(Zend_Http_Client::PUT);
+        $this->client->setEncType(Zend_Http_Client::ENC_FORMDATA);
+        $response = $this->client->request();
+        $responseText = $response->getBody();
+        $this->_checkPresence($responseText, $params);
+    }
+    
+    /**
+     * Checks the presence of keys (non-numeric only) and values
+     * in the raw form data reponse for a PUT or DELETE request recursively
+     * 
+     * An example response (I do not know of a better way to check it):
+     * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
+     * Content-Disposition: form-data; name="quest"
+     * 
+     * To seek the holy grail
+     * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
+     * Content-Disposition: form-data; name="YourMother"
+     * 
+     * Was a hamster
+     * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
+     * Content-Disposition: form-data; name="specialChars"
+     * 
+     * <>$+ &?=[]^%
+     * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
+     * Content-Disposition: form-data; name="array[]"
+     * 
+     * firstItem
+     * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
+     * Content-Disposition: form-data; name="array[]"
+     * 
+     * secondItem
+     * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828
+     * Content-Disposition: form-data; name="array[]"
+     * 
+     * 3rdItem
+     * -----ZENDHTTPCLIENT-c63973519a6bb3ec45495876f5e15828--
+     * @param string $responseText
+     * @param string|integer|array $needle
+     */
+    protected function _checkPresence($responseText, $needle)
+    {
+        if (is_array($needle)) {
+            foreach ($needle as $key => $value) {
+                // treat array values recursively, let them re-enter this function
+                if (is_array($value)) {
+                    $this->_checkPresence($responseText, $value);
+                    continue;
+                }
+                // continue with non array keys and values
+                // numeric keys will not be present in the response
+                if (!is_int($key)) {
+                    $this->assertGreaterThan(
+                        0,
+                        strpos($responseText, $key), 
+                        "key '$key' is missing from the reponse for raw multipart PUT or DELETE request"
+                    );
+                }
+                $this->assertGreaterThan(
+                    0,
+                    strpos($responseText, $value), 
+                    "value '$value' is missing from the reponse for raw multipart PUT or DELETE request"
+                );
+            }
+        }
+    }
+    
+    /**
+     * Test we can properly send DELETE parameters with
+     * multipart/form-data content type
+     *
+     * @dataProvider parameterArrayProvider
+     */
+    public function testDeleteDataMultipart($params)
+    {
+        $this->client->setUri($this->baseuri . 'testRawDeleteData.php');
+        $this->client->setParameterPost($params);
+        $this->client->setMethod(Zend_Http_Client::DELETE);
+        $this->client->setEncType(Zend_Http_Client::ENC_FORMDATA);
+        $response = $this->client->request();
+        $responseText = $response->getBody();
+        $this->_checkPresence($responseText, $params);
+    }
 
 
     /**
     /**
      * Test using raw HTTP POST data
      * Test using raw HTTP POST data
@@ -222,6 +360,38 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         $res = $this->client->setRawData($data, 'text/html')->request('POST');
         $res = $this->client->setRawData($data, 'text/html')->request('POST');
         $this->assertEquals($data, $res->getBody(), 'Response body does not contain the expected data');
         $this->assertEquals($data, $res->getBody(), 'Response body does not contain the expected data');
     }
     }
+    
+    /**
+     * Test using raw HTTP PUT data
+     *
+     * @group ZF-11030
+     * 
+     */
+    public function testRawPutData()
+    {
+        $data = "Chuck Norris never wet his bed as a child. The bed wet itself out of fear.";
+        $this->client->setUri($this->baseuri . 'testRawPutData.php');
+        $this->client->setRawData($data, 'text/plain');
+        $this->client->setMethod(Zend_Http_Client::PUT);
+        $response = $this->client->request();
+        $this->assertEquals($data, $response->getBody(), 'Response body does not contain the expected data');
+    }
+    
+    /**
+     * Test using raw HTTP DELETE data
+     *
+     * @group ZF-11030
+     * 
+     */
+    public function testRawDeleteData()
+    {
+        $data = "Chuck Norris never wet his bed as a child. The bed wet itself out of fear.";
+        $this->client->setUri($this->baseuri . 'testRawDeleteData.php');
+        $this->client->setRawData($data, 'text/plain');
+        $this->client->setMethod(Zend_Http_Client::DELETE);
+        $response = $this->client->request();
+        $this->assertEquals($data, $response->getBody(), 'Response body does not contain the expected data');
+    }
 
 
     /**
     /**
      * Make sure we can reset the parameters between consecutive requests
      * Make sure we can reset the parameters between consecutive requests
@@ -854,7 +1024,6 @@ abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCa
         }
         }
 
 
         $res = $this->client->request('POST');
         $res = $this->client->request('POST');
-
         $this->assertEquals($expectedBody, $res->getBody(), 'Response body does not include expected upload parameters');
         $this->assertEquals($expectedBody, $res->getBody(), 'Response body does not include expected upload parameters');
     }
     }
 
 

+ 26 - 0
tests/Zend/Http/Client/_files/testDeleteData.php

@@ -0,0 +1,26 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: testDeleteData.php 23772 2011-07-17 21:35:29Z mcleod@spaceweb.nl $
+ */
+
+$variables = array();
+$data = file_get_contents('php://input');
+parse_str($data, $variables);
+echo serialize($variables);

+ 25 - 0
tests/Zend/Http/Client/_files/testPutData.php

@@ -0,0 +1,25 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id
+ */
+$variables = array();
+$data = file_get_contents('php://input');
+parse_str($data, $variables);
+echo serialize($variables);

+ 22 - 0
tests/Zend/Http/Client/_files/testRawDeleteData.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id:
+ */
+echo file_get_contents('php://input');

+ 22 - 0
tests/Zend/Http/Client/_files/testRawPutData.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id
+ */
+echo file_get_contents('php://input');