Browse Source

Allow string payloads for GET requests.

Allow pre-built query strings in GET requests.

Refs #6540
Mark Story 11 years ago
parent
commit
2b6648164c
2 changed files with 34 additions and 3 deletions
  1. 4 3
      src/Network/Http/Client.php
  2. 30 0
      tests/TestCase/Network/Http/ClientTest.php

+ 4 - 3
src/Network/Http/Client.php

@@ -192,7 +192,7 @@ class Client
      * @param array $options Additional options for the request.
      * @return \Cake\Network\Http\Response
      */
-    public function get($url, array $data = [], array $options = [])
+    public function get($url, $data = [], array $options = [])
     {
         $options = $this->_mergeOptions($options);
         $body = [];
@@ -369,7 +369,7 @@ class Client
      * Generate a URL based on the scoped client options.
      *
      * @param string $url Either a full URL or just the path.
-     * @param array $query The query data for the URL.
+     * @param string|array $query The query data for the URL.
      * @param array $options The config options stored with Client::config()
      * @return string A complete url with scheme, port, host, path.
      */
@@ -380,7 +380,8 @@ class Client
         }
         if ($query) {
             $q = (strpos($url, '?') === false) ? '?' : '&';
-            $url .= $q . http_build_query($query);
+            $url .= $q;
+            $url .= is_string($query) ? $query : http_build_query($query);
         }
         if (preg_match('#^https?://#', $url)) {
             return $url;

+ 30 - 0
tests/TestCase/Network/Http/ClientTest.php

@@ -218,6 +218,36 @@ class ClientTest extends TestCase
     }
 
     /**
+     * test get request with string of query data.
+     *
+     * @return void
+     */
+    public function testGetQuerystringString()
+    {
+        $response = new Response();
+
+        $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
+        $mock->expects($this->once())
+            ->method('send')
+            ->with($this->logicalAnd(
+                $this->isInstanceOf('Cake\Network\Http\Request'),
+                $this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3')
+            ))
+            ->will($this->returnValue([$response]));
+
+        $http = new Client([
+            'host' => 'cakephp.org',
+            'adapter' => $mock
+        ]);
+        $data = [
+            'q' => 'hi there',
+            'Category' => ['id' => [2, 3]]
+        ];
+        $result = $http->get('/search', http_build_query($data));
+        $this->assertSame($result, $response);
+    }
+
+    /**
      * Test a GET with a request body. Services like
      * elasticsearch use this feature.
      *