Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Add ArrayAccess support for Http client get requests #32401

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\HandlerStack;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;

class PendingRequest
Expand Down Expand Up @@ -466,8 +467,22 @@ public function send(string $method, string $url, array $options = [])

return retry($this->tries ?? 1, function () use ($method, $url, $options) {
try {
$laravelData = $options[$this->bodyFormat] ?? $options['query'] ?? [];

// Attempt to parse query string on url if no request data found
$urlStr = Str::of($url);
if (! $laravelData && $method === 'GET' && $urlStr->contains('?')) {
$laravelData = (string) $urlStr->after('?');
}

// If found data is a string then treat it as query parameters and parse them to an array
if (is_string($laravelData)) {
parse_str($laravelData, $parsedData);
$laravelData = is_array($parsedData) ? $parsedData : [];
}

return tap(new Response($this->buildClient()->request($method, $url, $this->mergeOptions([
'laravel_data' => $options[$this->bodyFormat] ?? [],
'laravel_data' => $laravelData,
'on_stats' => function ($transferStats) {
$this->transferStats = $transferStats;
},
Expand Down
11 changes: 9 additions & 2 deletions src/Illuminate/Http/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Http\Client;

use ArrayAccess;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use LogicException;

Expand Down Expand Up @@ -66,9 +67,15 @@ public function hasHeader($key, $value = null)
return ! empty($this->request->getHeaders()[$key]);
}

$headers = $this->headers();

if (! Arr::has($headers, $key)) {
return false;
}

$value = is_array($value) ? $value : [$value];

return empty(array_diff($value, $this->headers()[$key]));
return empty(array_diff($value, $headers[$key]));
}

/**
Expand All @@ -79,7 +86,7 @@ public function hasHeader($key, $value = null)
*/
public function header($key)
{
return $this->headers()[$key];
return Arr::get($this->headers(), $key, [null]);
}

/**
Expand Down
21 changes: 15 additions & 6 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ public function testGetWithArrayQueryParam()
$this->factory->get('http://foo.com/get', ['foo' => 'bar']);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?foo=bar';
return $request->url() === 'http://foo.com/get?foo=bar'
&& $request['foo'] === 'bar';
});
}

Expand All @@ -280,7 +281,8 @@ public function testGetWithStringQueryParam()
$this->factory->get('http://foo.com/get', 'foo=bar');

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?foo=bar';
return $request->url() === 'http://foo.com/get?foo=bar'
&& $request['foo'] === 'bar';
});
}

Expand All @@ -291,7 +293,9 @@ public function testGetWithQuery()
$this->factory->get('http://foo.com/get?foo=bar&page=1');

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?foo=bar&page=1';
return $request->url() === 'http://foo.com/get?foo=bar&page=1'
&& $request['foo'] === 'bar'
&& $request['page'] === '1';
});
}

Expand All @@ -302,7 +306,10 @@ public function testGetWithQueryWontEncode()
$this->factory->get('http://foo.com/get?foo;bar;1;5;10&page=1');

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?foo;bar;1;5;10&page=1';
return $request->url() === 'http://foo.com/get?foo;bar;1;5;10&page=1'
&& ! isset($request['foo'])
&& ! isset($request['bar'])
&& $request['page'] === '1';
});
}

Expand All @@ -313,7 +320,8 @@ public function testGetWithArrayQueryParamOverwrites()
$this->factory->get('http://foo.com/get?foo=bar&page=1', ['hello' => 'world']);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?hello=world';
return $request->url() === 'http://foo.com/get?hello=world'
&& $request['hello'] === 'world';
});
}

Expand All @@ -324,7 +332,8 @@ public function testGetWithArrayQueryParamEncodes()
$this->factory->get('http://foo.com/get', ['foo;bar; space test' => 'laravel']);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/get?foo%3Bbar%3B%20space%20test=laravel';
return $request->url() === 'http://foo.com/get?foo%3Bbar%3B%20space%20test=laravel'
&& $request['foo;bar; space test'] === 'laravel';
});
}
}