Skip to content

Commit 56200cb

Browse files
[11.x] Add resource() method to Illuminate\Http\Client\Response (#52412)
* Add resource method to Illuminate\Http\Client\Response * Update Response.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 5a9d85b commit 56200cb

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Illuminate/Http/Client/Response.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Http\Client;
44

55
use ArrayAccess;
6+
use GuzzleHttp\Psr7\StreamWrapper;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Traits\Macroable;
89
use LogicException;
@@ -104,6 +105,18 @@ public function collect($key = null)
104105
return Collection::make($this->json($key));
105106
}
106107

108+
/**
109+
* Get the body of the response as a PHP resource.
110+
*
111+
* @return resource
112+
*
113+
* @throws \InvalidArgumentException
114+
*/
115+
public function resource()
116+
{
117+
return StreamWrapper::getResource($this->response->getBody());
118+
}
119+
107120
/**
108121
* Get a header from the response.
109122
*

tests/Http/HttpClientTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ public function testResponseObjectAsObject()
327327
$this->assertSame('bar', $response->object()->result->foo);
328328
}
329329

330+
public function testResponseCanBeReturnedAsResource()
331+
{
332+
$this->factory->fake([
333+
'*' => ['result' => ['foo' => 'bar']],
334+
]);
335+
336+
$response = $this->factory->get('http://foo.com/api');
337+
338+
$this->assertIsResource($response->resource());
339+
$this->assertSame('{"result":{"foo":"bar"}}', stream_get_contents($response->resource()));
340+
}
341+
330342
public function testResponseCanBeReturnedAsCollection()
331343
{
332344
$this->factory->fake([

0 commit comments

Comments
 (0)