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

[11.x] Helper methods to dump responses of the Laravel HTTP client #54317

Merged
merged 3 commits into from
Jan 23, 2025
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
62 changes: 62 additions & 0 deletions src/Illuminate/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,68 @@ public function throwIfServerError()
return $this->serverError() ? $this->throw() : $this;
}

/**
* Dump the content from the response.
*
* @param string|null $key
* @return $this
*/
public function dump($key = null)
{
$content = $this->body();

$json = json_decode($content);

if (json_last_error() === JSON_ERROR_NONE) {
$content = $json;
}

if (! is_null($key)) {
dump(data_get($content, $key));
} else {
dump($content);
}

return $this;
}

/**
* Dump the content from the response and end the script.
*
* @param string|null $key
* @return never
*/
public function dd($key = null)
{
$this->dump($key);

exit(1);
}

/**
* Dump the headers from the response.
*
* @return $this
*/
public function dumpHeaders()
{
dump($this->headers());

return $this;
}

/**
* Dump the headers from the response and end the script.
*
* @return never
*/
public function ddHeaders()
{
$this->dumpHeaders();

exit(1);
}

/**
* Determine if the given offset exists.
*
Expand Down
57 changes: 57 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,63 @@ public function testCanDump()
VarDumper::setHandler(null);
}

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

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

$this->factory->fake([
'200.com' => $this->factory::response('hello', 200),
]);

$this->factory->get('http://200.com')->dump();

$this->assertSame('hello', $dumped[0]);

VarDumper::setHandler(null);
}

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

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

$this->factory->fake([
'200.com' => $this->factory::response(['hello' => 'world'], 200),
]);

$this->factory->get('http://200.com')->dump('hello');

$this->assertSame('world', $dumped[0]);

VarDumper::setHandler(null);
}

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

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

$this->factory->fake([
'200.com' => $this->factory::response('hello', 200, ['hello' => 'world']),
]);

$this->factory->get('http://200.com')->dumpHeaders();

$this->assertSame(['hello' => ['world']], $dumped[0]);

VarDumper::setHandler(null);
}

public function testResponseSequenceIsMacroable()
{
ResponseSequence::macro('customMethod', function () {
Expand Down