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

[8.x] Helper methods to dump requests of the Laravel HTTP client #36466

Merged
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
2 changes: 2 additions & 0 deletions src/Illuminate/Http/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
* @method \Illuminate\Http\Client\PendingRequest withToken(string $token, string $type = 'Bearer')
* @method \Illuminate\Http\Client\PendingRequest withoutRedirecting()
* @method \Illuminate\Http\Client\PendingRequest withoutVerifying()
* @method \Illuminate\Http\Client\PendingRequest dump()
* @method \Illuminate\Http\Client\PendingRequest dd()
* @method \Illuminate\Http\Client\Response delete(string $url, array $data = [])
* @method \Illuminate\Http\Client\Response get(string $url, array $query = [])
* @method \Illuminate\Http\Client\Response head(string $url, array $query = [])
Expand Down
35 changes: 35 additions & 0 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Symfony\Component\VarDumper\VarDumper;

class PendingRequest
{
Expand Down Expand Up @@ -452,6 +453,40 @@ public function beforeSending($callback)
});
}

/**
* Dump the request.
*
* @return $this
*/
public function dump()
{
$values = func_get_args();

return $this->beforeSending(function (Request $request, array $options) use ($values) {
foreach (array_merge($values, [$request, $options]) as $value) {
VarDumper::dump($value);
}
});
}

/**
* Dump the request and end the script.
*
* @return $this
*/
public function dd()
{
$values = func_get_args();

return $this->beforeSending(function (Request $request, array $options) use ($values) {
foreach (array_merge($values, [$request, $options]) as $value) {
VarDumper::dump($value);
}

exit(1);
});
}

/**
* Issue a GET request to the given URL.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Support/Facades/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* @method static \Illuminate\Http\Client\PendingRequest withToken(string $token, string $type = 'Bearer')
* @method static \Illuminate\Http\Client\PendingRequest withoutRedirecting()
* @method static \Illuminate\Http\Client\PendingRequest withoutVerifying()
* @method static \Illuminate\Http\Client\PendingRequest dump()
* @method static \Illuminate\Http\Client\PendingRequest dd()
* @method static \Illuminate\Http\Client\Response delete(string $url, array $data = [])
* @method static \Illuminate\Http\Client\Response get(string $url, array $query = [])
* @method static \Illuminate\Http\Client\Response head(string $url, array $query = [])
Expand Down
20 changes: 20 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Str;
use OutOfBoundsException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\VarDumper;

class HttpClientTest extends TestCase
{
Expand Down Expand Up @@ -781,4 +782,23 @@ function (Request $request) {

$this->factory->assertSentInOrder($executionOrder);
}

public function testCanDump()
{
$dumped = [];

VarDumper::setHandler(function ($value) use (&$dumped) {
$dumped[] = $value;
});

$this->factory->fake()->dump(1, 2, 3)->withOptions(['delay' => 1000])->get('http://foo.com');

$this->assertSame(1, $dumped[0]);
$this->assertSame(2, $dumped[1]);
$this->assertSame(3, $dumped[2]);
$this->assertInstanceOf(Request::class, $dumped[3]);
$this->assertSame(1000, $dumped[4]['delay']);

VarDumper::setHandler(null);
}
}