Skip to content

Commit

Permalink
[11.x] Add resource() method to Illuminate\Http\Client\Response (#5…
Browse files Browse the repository at this point in the history
…2412)

* Add resource method to Illuminate\Http\Client\Response

* Update Response.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
einar-hansen and taylorotwell authored Aug 7, 2024
1 parent 5a9d85b commit 56200cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Http\Client;

use ArrayAccess;
use GuzzleHttp\Psr7\StreamWrapper;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use LogicException;
Expand Down Expand Up @@ -104,6 +105,18 @@ public function collect($key = null)
return Collection::make($this->json($key));
}

/**
* Get the body of the response as a PHP resource.
*
* @return resource
*
* @throws \InvalidArgumentException
*/
public function resource()
{
return StreamWrapper::getResource($this->response->getBody());
}

/**
* Get a header from the response.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ public function testResponseObjectAsObject()
$this->assertSame('bar', $response->object()->result->foo);
}

public function testResponseCanBeReturnedAsResource()
{
$this->factory->fake([
'*' => ['result' => ['foo' => 'bar']],
]);

$response = $this->factory->get('http://foo.com/api');

$this->assertIsResource($response->resource());
$this->assertSame('{"result":{"foo":"bar"}}', stream_get_contents($response->resource()));
}

public function testResponseCanBeReturnedAsCollection()
{
$this->factory->fake([
Expand Down

0 comments on commit 56200cb

Please sign in to comment.