Skip to content

Commit

Permalink
[11.x] Helper methods to dump responses of the Laravel HTTP client (#…
Browse files Browse the repository at this point in the history
…54317)

* Add dump and dd methods to Response class for debugging

* Remove unused VarDumper import from Response class

* Update Response.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
morrislaptop and taylorotwell authored Jan 23, 2025
1 parent e365c08 commit 5253cc2
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
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

0 comments on commit 5253cc2

Please sign in to comment.