Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Fix: decompressed response saved into file when streaming #199

Merged
merged 1 commit into from
Dec 2, 2019
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
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ protected function prepareHeaders($body, $uri)
// Set the Accept-encoding header if not set - depending on whether
// zlib is available or not.
if (! $this->getRequest()->getHeaders()->has('Accept-Encoding')) {
if (function_exists('gzinflate')) {
if (empty($this->config['outputstream']) && function_exists('gzinflate')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious: can you explain why this is needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the test - we'd like to have uncompressed content stored in the file if we are using streams and we set the file.

$headers['Accept-Encoding'] = 'gzip, deflate';
} else {
$headers['Accept-Encoding'] = 'identity';
Expand Down
30 changes: 30 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use ReflectionMethod;
use Zend\Http\Client;
use Zend\Http\Client\Adapter\AdapterInterface;
use Zend\Http\Client\Adapter\Curl;
use Zend\Http\Client\Adapter\Proxy;
use Zend\Http\Client\Adapter\Socket;
use Zend\Http\Client\Adapter\Test;
use Zend\Http\Client\Exception as ClientException;
use Zend\Http\Cookies;
Expand Down Expand Up @@ -607,4 +610,31 @@ public function testSetCookieAcceptOnlyArray()
$this->expectExceptionMessage('Invalid cookies passed as parameter, it must be an array');
$client->setCookies(new SetCookie('name', 'value'));
}

/**
* @return AdapterInterface[]
*/
public function adapterWithStreamSupport()
{
yield 'curl' => [new Curl()];
yield 'proxy' => [new Proxy()];
yield 'socket' => [new Socket()];
}

/**
* @dataProvider adapterWithStreamSupport
*/
public function testStreamCompression(AdapterInterface $adapter)
{
$tmpFile = tempnam(sys_get_temp_dir(), 'stream');

$client = new Client('https://www.gnu.org/licenses/gpl-3.0.txt');
$client->setAdapter($adapter);
$client->setStream($tmpFile);
$client->send();

$response = $client->getResponse();

self::assertSame($response->getBody(), file_get_contents($tmpFile));
}
}