Skip to content

Commit

Permalink
Add ArrayAccess support for get requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmason30 committed Apr 16, 2020
1 parent 2be2839 commit e737582
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
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';
});
}
}

0 comments on commit e737582

Please sign in to comment.