Skip to content

Commit

Permalink
Merge pull request #30 from php-http/Nyholm-rewind
Browse files Browse the repository at this point in the history
Make sure we rewind streams
  • Loading branch information
mekras committed Feb 9, 2017
2 parents 3f9ff1f + 59ad156 commit aaf0a18
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ private function addRequestBodyOptions(RequestInterface $request, array $options
$body = $request->getBody();
$bodySize = $body->getSize();
if ($bodySize !== 0) {
if ($body->isSeekable()) {
$body->rewind();
}

// Message has non empty body.
if (null === $bodySize || $bodySize > 1024 * 1024) {
// Avoid full loading large or unknown size body into memory
Expand Down
41 changes: 41 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Http\Client\Curl\Tests;

use GuzzleHttp\Psr7\Stream;
use Http\Client\Curl\Client;
use Zend\Diactoros\Request;

Expand Down Expand Up @@ -31,6 +32,46 @@ public function testExpectHeader()
static::assertContains('Expect:', $headers);
}



public function testRewindStream()
{
$client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()
->setMethods(['__none__'])->getMock();

$bodyOptions = new \ReflectionMethod(Client::class, 'addRequestBodyOptions');
$bodyOptions->setAccessible(true);

$body = \GuzzleHttp\Psr7\stream_for('abcdef');
$body->seek(3);
$request = new Request('http://foo.com', 'POST', $body);
$options = $bodyOptions->invoke($client, $request, []);

static::assertEquals('abcdef', $options[CURLOPT_POSTFIELDS]);
}

public function testRewindLargeStream()
{
$client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()
->setMethods(['__none__'])->getMock();

$bodyOptions = new \ReflectionMethod(Client::class, 'addRequestBodyOptions');
$bodyOptions->setAccessible(true);

$content = 'abcdef';
while (strlen($content) < 1024*1024+100) {
$content .= '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890';
}

$length = strlen($content);
$body = \GuzzleHttp\Psr7\stream_for($content);
$body->seek(40);
$request = new Request('http://foo.com', 'POST', $body);
$options = $bodyOptions->invoke($client, $request, []);

static::assertTrue(false !== strstr($options[CURLOPT_READFUNCTION](null, null, $length), 'abcdef'), 'Steam was not rewinded');
}

/**
* Discovery should be used if no factory given.
*/
Expand Down

0 comments on commit aaf0a18

Please sign in to comment.