Skip to content

Commit

Permalink
RequestException - check if readable before access (#2081)
Browse files Browse the repository at this point in the history
* check if readable before access

* Add unit tests for RequestException::getResponseBodySummary

* use real object in test

* update test for older PHP version support

* remove no longer used imports

* Update RequestExceptionTest.php
  • Loading branch information
SpacePossum authored and Nyholm committed Aug 8, 2018
1 parent 572593a commit 5c7a5c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Exception/RequestException.php
Expand Up @@ -126,7 +126,7 @@ public static function getResponseBodySummary(ResponseInterface $response)
{
$body = $response->getBody();

if (!$body->isSeekable()) {
if (!$body->isSeekable() || !$body->isReadable()) {
return null;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Exception/RequestExceptionTest.php
Expand Up @@ -4,6 +4,7 @@
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Stream;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -174,4 +175,27 @@ public function testObfuscateUrlWithUsernameAndPassword()
$e = RequestException::create($r, new Response(500));
$this->assertContains('http://user:***@www.oo.com', $e->getMessage());
}

public function testGetResponseBodySummaryOfNonReadableStream()
{
$this->assertNull(RequestException::getResponseBodySummary(new Response(500, [], new ReadSeekOnlyStream())));
}
}

final class ReadSeekOnlyStream extends Stream
{
public function __construct()
{
parent::__construct(fopen('php://memory', 'wb'));
}

public function isSeekable()
{
return true;
}

public function isReadable()
{
return false;
}
}

0 comments on commit 5c7a5c5

Please sign in to comment.