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

[10.x] Fix Stringable objects not converted to string in HTTP facade Query parameters and Body #48849

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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: 15 additions & 2 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use JsonSerializable;
Expand Down Expand Up @@ -1035,10 +1036,22 @@ protected function sendRequest(string $method, string $url, array $options = [])
$this->transferStats = $transferStats;
};

return $this->buildClient()->$clientMethod($method, $url, $this->mergeOptions([
$mergedOptions = $this->mergeOptions([
'laravel_data' => $laravelData,
'on_stats' => $onStats,
], $options));
], $options);

$mergedOptions = array_map(function($mergedOption) {
if ( is_array( $mergedOption ) ) {
array_walk_recursive($mergedOption, function (&$value) {
$value = $value instanceof Stringable ? $value->toString() : $value;
});
}

return $mergedOption instanceof Stringable ? $mergedOption->toString() : $mergedOption;
}, $mergedOptions);

return $this->buildClient()->$clientMethod($method, $url, $mergedOptions);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a gigantic fan of this implementation.

Super happy for someone to come up with a more elegant approach!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a build in stringable interface. But then you could just cast anything coming true to string instead of using the method. One caveat would be the boolean bug though.

}

/**
Expand Down
77 changes: 77 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,57 @@ public function toArray(): array
});
}

public function testCanSendJsonDataWithStringable()
{
$this->factory->fake();

$this->factory->withHeaders([
'X-Test-Header' => 'foo',
'X-Test-ArrayHeader' => ['bar', 'baz'],
])->post('http://foo.com/json', [
'name' => Str::of('Taylor'),
]);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/json' &&
$request->hasHeader('Content-Type', 'application/json') &&
$request->hasHeader('X-Test-Header', 'foo') &&
$request->hasHeader('X-Test-ArrayHeader', ['bar', 'baz']) &&
$request['name'] === 'Taylor';
});
}

public function testCanSendFormDataWithStringable()
{
$this->factory->fake();

$this->factory->asForm()->post('http://foo.com/form', [
'name' => Str::of('Taylor'),
'title' => 'Laravel Developer',
]);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/form' &&
$request->hasHeader('Content-Type', 'application/x-www-form-urlencoded') &&
$request['name'] === 'Taylor';
});
}

public function testCanSendFormDataWithStringableInArrays()
{
$this->factory->fake();

$this->factory->asForm()->post('http://foo.com/form', [
'posts' => [['title' => Str::of('Taylor')]],
]);

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'http://foo.com/form' &&
$request->hasHeader('Content-Type', 'application/x-www-form-urlencoded') &&
$request['posts'][0]['title'] === 'Taylor';
});
}

public function testRecordedCallsAreEmptiedWhenFakeIsCalled()
{
$this->factory->fake([
Expand Down Expand Up @@ -800,6 +851,32 @@ public function testWithQueryParametersAllowsOverridingParameterOnRequest()
});
}

public function testWithStringableQueryParameters()
{
$this->factory->fake();

$this->factory->withQueryParameters(
['foo' => Str::of('bar'),]
)->get('https://laravel.com');

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'https://laravel.com?foo=bar';
});
}

public function testWithArrayStringableQueryParameters()
{
$this->factory->fake();

$this->factory->withQueryParameters(
['foo' => ['bar', Str::of('baz')]],
)->get('https://laravel.com');

$this->factory->assertSent(function (Request $request) {
return $request->url() === 'https://laravel.com?foo%5B0%5D=bar&foo%5B1%5D=baz';
});
}

public function testGetWithArrayQueryParam()
{
$this->factory->fake();
Expand Down