Skip to content

Commit de00c98

Browse files
committed
Add basic querystring support.
1 parent 30a9d69 commit de00c98

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

lib/Cake/Network/Http/Client.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ public function config($config = null) {
102102
*/
103103
public function get($url, $data = [], $options = []) {
104104
$options = $this->_mergeOptions($options);
105-
$request = $this->_createRequest(Request::METHOD_GET, $url, $data, $options);
105+
$url = $this->buildUrl($url, $data, $options);
106+
$request = $this->_createRequest(
107+
Request::METHOD_GET,
108+
$url,
109+
[],
110+
$options
111+
);
106112
return $this->send($request, $options);
107113
}
108114

@@ -173,13 +179,17 @@ public function send(Request $request, $options = []) {
173179
* Generate a URL based on the scoped client options.
174180
*
175181
* @param string $url Either a full URL or just the path.
182+
* @param array $query The query data for the URL.
176183
* @param array $options The config options stored with Client::config()
177184
* @return string A complete url with scheme, port, host, path.
178185
*/
179-
public function buildUrl($url, $options = []) {
180-
if (empty($options)) {
186+
public function buildUrl($url, $query = [], $options = []) {
187+
if (empty($options) && empty($query)) {
181188
return $url;
182189
}
190+
if ($query) {
191+
$url .= '?' . http_build_query($query);
192+
}
183193
if (preg_match('#^https?://#', $url)) {
184194
return $url;
185195
}
@@ -204,11 +214,13 @@ public function buildUrl($url, $options = []) {
204214
/**
205215
* Creates a new request object based on the parameters.
206216
*
207-
*
217+
* @param string $method HTTP method name.
218+
* @param string $url The url including query string.
219+
* @param mixed $data The request body content.
220+
* @param array $options The options to use. Contains auth, proxy etc.
208221
* @return Cake\Network\Http\Request
209222
*/
210223
protected function _createRequest($method, $url, $data, $options) {
211-
$url = $this->buildUrl($url, $options);
212224
$request = new Request();
213225
$request->method($method)
214226
->url($url)

lib/Cake/Test/TestCase/Network/Http/ClientTest.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,61 +60,76 @@ public static function urlProvider() {
6060
[
6161
'http://example.com/test.html',
6262
'http://example.com/test.html',
63+
[],
6364
null,
6465
'Null options'
6566
],
6667
[
6768
'http://example.com/test.html',
6869
'http://example.com/test.html',
6970
[],
71+
[],
7072
'Simple string'
7173
],
7274
[
7375
'http://example.com/test.html',
7476
'/test.html',
77+
[],
7578
['host' => 'example.com'],
7679
'host name option',
7780
],
7881
[
7982
'https://example.com/test.html',
8083
'/test.html',
84+
[],
8185
['host' => 'example.com', 'scheme' => 'https'],
8286
'HTTPS',
8387
],
8488
[
8589
'http://example.com:8080/test.html',
8690
'/test.html',
91+
[],
8792
['host' => 'example.com', 'port' => '8080'],
8893
'Non standard port',
8994
],
9095
[
9196
'http://example.com/test.html',
9297
'/test.html',
98+
[],
9399
['host' => 'example.com', 'port' => '80'],
94100
'standard port, does not display'
95101
],
96102
[
97103
'https://example.com/test.html',
98104
'/test.html',
105+
[],
99106
['host' => 'example.com', 'scheme' => 'https', 'port' => '443'],
100107
'standard port, does not display'
101108
],
102109
[
103110
'http://example.com/test.html',
104111
'http://example.com/test.html',
112+
[],
105113
['host' => 'example.com', 'scheme' => 'https'],
106114
'options do not duplicate'
107115
],
116+
[
117+
'http://example.com/search?q=hi+there&cat%5Bid%5D%5B0%5D=2&cat%5Bid%5D%5B1%5D=3',
118+
'http://example.com/search',
119+
['q' => 'hi there', 'cat' => ['id' => [2, 3]]],
120+
[],
121+
'query string data.'
122+
],
108123
];
109124
}
110125

111126
/**
112127
* @dataProvider urlProvider
113128
*/
114-
public function testBuildUrl($expected, $url, $opts) {
129+
public function testBuildUrl($expected, $url, $query, $opts) {
115130
$http = new Client();
116131

117-
$result = $http->buildUrl($url, $opts);
132+
$result = $http->buildUrl($url, $query, $opts);
118133
$this->assertEquals($expected, $result);
119134
}
120135

@@ -179,4 +194,31 @@ public function testGetSimpleWithHeadersAndCookies() {
179194
$this->assertSame($result, $response);
180195
}
181196

197+
/**
198+
* test get request with querystring data
199+
*
200+
* @return void
201+
*/
202+
public function testGetQuerystring() {
203+
$response = new Response();
204+
205+
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
206+
$mock->expects($this->once())
207+
->method('send')
208+
->with($this->logicalAnd(
209+
$this->isInstanceOf('Cake\Network\Http\Request'),
210+
$this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3')
211+
))
212+
->will($this->returnValue($response));
213+
214+
$http = new Client([
215+
'host' => 'cakephp.org',
216+
'adapter' => $mock
217+
]);
218+
$result = $http->get('/search', [
219+
'q' => 'hi there',
220+
'Category' => ['id' => [2, 3]]
221+
]);
222+
$this->assertSame($result, $response);
223+
}
182224
}

0 commit comments

Comments
 (0)