Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
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
12 changes: 12 additions & 0 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,18 @@ public function sink($to)
return $this;
}

/**
* Indicate that the response body should be streamed instead of buffered in memory.
*
* @return $this
*/
public function stream()
{
$this->options['stream'] = true;

return $this;
}

/**
* Specify the timeout (in seconds) for the request.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Illuminate/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Illuminate\Http\Client;

use ArrayAccess;
use Generator;
use GuzzleHttp\Psr7\StreamWrapper;
use GuzzleHttp\Psr7\Utils;
use Illuminate\Support\Collection;
use Illuminate\Support\Fluent;
use Illuminate\Support\Traits\Macroable;
Expand Down Expand Up @@ -163,6 +165,26 @@ public function resource()
return StreamWrapper::getResource($this->response->getBody());
}

/**
* Get an iterator that yields each line of the response body.
*
* @return \Generator<int, string>
*/
public function lines(): Generator
{
$body = $this->response->getBody();

while (! $body->eof()) {
$line = Utils::readLine($body);

if ($line === '') {
continue;
}

yield rtrim($line, "\r\n");
}
}

/**
* Get a header from the response.
*
Expand Down
71 changes: 71 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,77 @@ public function testSinkWhenStubbedByPath()
$this->assertSame(json_encode(['page' => 'foo']), stream_get_contents($resource));
}

public function testStreamMethodSetsGuzzleStreamOption()
{
$request = $this->factory->stream();

$this->assertTrue($request->getOptions()['stream']);
}

public function testStreamMethodIsChainable()
{
$this->factory->fake([
'*' => $this->factory::response('ok'),
]);

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

$this->assertTrue($response->successful());
}

public function testResponseLinesYieldsEachLineOfTheBody()
{
$this->factory->fake([
'*' => $this->factory::response("first\nsecond\nthird"),
]);

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

$this->assertSame(
['first', 'second', 'third'],
iterator_to_array($response->lines(), false),
);
}

public function testResponseLinesHandlesMixedLineEndings()
{
$this->factory->fake([
'*' => $this->factory::response("alpha\r\nbeta\ngamma\r\ndelta"),
]);

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

$this->assertSame(
['alpha', 'beta', 'gamma', 'delta'],
iterator_to_array($response->lines(), false),
);
}

public function testResponseLinesPreservesBlankLinesBetweenContent()
{
$this->factory->fake([
'*' => $this->factory::response("one\n\ntwo\n\nthree\n"),
]);

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

$this->assertSame(
['one', '', 'two', '', 'three'],
iterator_to_array($response->lines(), false),
);
}

public function testResponseLinesReturnsEmptyGeneratorForEmptyBody()
{
$this->factory->fake([
'*' => $this->factory::response(''),
]);

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

$this->assertSame([], iterator_to_array($response->lines(), false));
}

public function testCanAssertAgainstOrderOfHttpRequestsWithUrlStrings()
{
$this->factory->fake();
Expand Down
Loading