Skip to content

Commit 5253cc2

Browse files
[11.x] Helper methods to dump responses of the Laravel HTTP client (#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]>
1 parent e365c08 commit 5253cc2

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

src/Illuminate/Http/Client/Response.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,68 @@ public function throwIfServerError()
396396
return $this->serverError() ? $this->throw() : $this;
397397
}
398398

399+
/**
400+
* Dump the content from the response.
401+
*
402+
* @param string|null $key
403+
* @return $this
404+
*/
405+
public function dump($key = null)
406+
{
407+
$content = $this->body();
408+
409+
$json = json_decode($content);
410+
411+
if (json_last_error() === JSON_ERROR_NONE) {
412+
$content = $json;
413+
}
414+
415+
if (! is_null($key)) {
416+
dump(data_get($content, $key));
417+
} else {
418+
dump($content);
419+
}
420+
421+
return $this;
422+
}
423+
424+
/**
425+
* Dump the content from the response and end the script.
426+
*
427+
* @param string|null $key
428+
* @return never
429+
*/
430+
public function dd($key = null)
431+
{
432+
$this->dump($key);
433+
434+
exit(1);
435+
}
436+
437+
/**
438+
* Dump the headers from the response.
439+
*
440+
* @return $this
441+
*/
442+
public function dumpHeaders()
443+
{
444+
dump($this->headers());
445+
446+
return $this;
447+
}
448+
449+
/**
450+
* Dump the headers from the response and end the script.
451+
*
452+
* @return never
453+
*/
454+
public function ddHeaders()
455+
{
456+
$this->dumpHeaders();
457+
458+
exit(1);
459+
}
460+
399461
/**
400462
* Determine if the given offset exists.
401463
*

tests/Http/HttpClientTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,63 @@ public function testCanDump()
16061606
VarDumper::setHandler(null);
16071607
}
16081608

1609+
public function testResponseCanDump()
1610+
{
1611+
$dumped = [];
1612+
1613+
VarDumper::setHandler(function ($value) use (&$dumped) {
1614+
$dumped[] = $value;
1615+
});
1616+
1617+
$this->factory->fake([
1618+
'200.com' => $this->factory::response('hello', 200),
1619+
]);
1620+
1621+
$this->factory->get('http://200.com')->dump();
1622+
1623+
$this->assertSame('hello', $dumped[0]);
1624+
1625+
VarDumper::setHandler(null);
1626+
}
1627+
1628+
public function testResponseCanDumpWithKey()
1629+
{
1630+
$dumped = [];
1631+
1632+
VarDumper::setHandler(function ($value) use (&$dumped) {
1633+
$dumped[] = $value;
1634+
});
1635+
1636+
$this->factory->fake([
1637+
'200.com' => $this->factory::response(['hello' => 'world'], 200),
1638+
]);
1639+
1640+
$this->factory->get('http://200.com')->dump('hello');
1641+
1642+
$this->assertSame('world', $dumped[0]);
1643+
1644+
VarDumper::setHandler(null);
1645+
}
1646+
1647+
public function testResponseCanDumpHeaders()
1648+
{
1649+
$dumped = [];
1650+
1651+
VarDumper::setHandler(function ($value) use (&$dumped) {
1652+
$dumped[] = $value;
1653+
});
1654+
1655+
$this->factory->fake([
1656+
'200.com' => $this->factory::response('hello', 200, ['hello' => 'world']),
1657+
]);
1658+
1659+
$this->factory->get('http://200.com')->dumpHeaders();
1660+
1661+
$this->assertSame(['hello' => ['world']], $dumped[0]);
1662+
1663+
VarDumper::setHandler(null);
1664+
}
1665+
16091666
public function testResponseSequenceIsMacroable()
16101667
{
16111668
ResponseSequence::macro('customMethod', function () {

0 commit comments

Comments
 (0)