Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add resource() method to Illuminate\Http\Client\Response #52412

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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