From 5253cc2caa30416cca52eb188b1edda6266208d5 Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Fri, 24 Jan 2025 01:04:49 +1100 Subject: [PATCH] [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 --- src/Illuminate/Http/Client/Response.php | 62 +++++++++++++++++++++++++ tests/Http/HttpClientTest.php | 57 +++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/src/Illuminate/Http/Client/Response.php b/src/Illuminate/Http/Client/Response.php index 491eecf081fd..ac51d9c7ade8 100644 --- a/src/Illuminate/Http/Client/Response.php +++ b/src/Illuminate/Http/Client/Response.php @@ -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. * diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 64745bf49987..21c9718c2da6 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -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 () {