Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"ext-zlib": "*",
"psr/http-message": "^1.0",
"psr/log": "^1.0",
"psr/simple-cache": "^1.0"
Expand Down
32 changes: 32 additions & 0 deletions src/Config/AbstractConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

abstract class AbstractConfig
{
const COMPRESSION_TYPE_NONE = 'none';
const COMPRESSION_TYPE_GZIP = 'gzip';

protected $config;

protected $defaultReadTimeout = 5;
Expand All @@ -29,6 +32,7 @@ public function getDefaultConfig()
'writeTimeout' => $this->defaultWriteTimeout,
'connectTimeout' => $this->defaultConnectTimeout,
'defaultHeaders' => array(),
'gzipEnabled' => self::COMPRESSION_TYPE_NONE,
);
}

Expand Down Expand Up @@ -115,4 +119,32 @@ public function setDefaultHeaders(array $defaultHeaders)

return $this;
}

/**
* @return bool
*/
public function getGzipEnabled()
{
return $this->config['gzipEnabled'];
}

/**
* @param string $gzipEnabled
*
* @return $this
*/
public function setGzipEnabled($gzipEnabled)
{
if (!in_array(
$gzipEnabled,
array(self::COMPRESSION_TYPE_GZIP, self::COMPRESSION_TYPE_NONE),
true
)) {
throw new \InvalidArgumentException('gzipEnabled must be equal to '.self::COMPRESSION_TYPE_GZIP.' or '.self::COMPRESSION_TYPE_NONE);
}

$this->config['gzipEnabled'] = $gzipEnabled;

return $this;
}
}
1 change: 1 addition & 0 deletions src/Config/SearchConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function getDefaultConfig()
'defaultHeaders' => array(),
'defaultForwardToReplicas' => null,
'batchSize' => 1000,
'gzipEnabled' => self::COMPRESSION_TYPE_GZIP,
);
}

Expand Down
20 changes: 20 additions & 0 deletions src/RetryStrategy/ApiWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public function send($method, $path, $requestOptions = array(), $hosts = null)

private function request($method, $path, RequestOptions $requestOptions, $hosts, $timeout, $data = array())
{
$canCompress = $this->canEnableGzipCompress($method);

if ($canCompress) {
$requestOptions->addHeader('Content-Encoding', 'gzip');
Copy link
Member

@Ant-hem Ant-hem Jul 8, 2019

Choose a reason for hiding this comment

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

Is the underlying HTTP library computing the content-length header as well?

Copy link
Member

Choose a reason for hiding this comment

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

@chloelbn Seems like tests are still failing, is the underlying http library setting this header?
"Content-Type: application/json; charset=utf-8"

Copy link
Member

@Ant-hem Ant-hem Jul 10, 2019

Choose a reason for hiding this comment

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

Or, the gzip feature might not be enable on the test servers we are targeting with the PHP library. Are they the same at the CTS one @nunomaduro? I couldn't check on travis as they are ciphered.

$requestOptions->addHeader('Content-Length', null);
}

$uri = $this->createUri($path)
->withQuery($requestOptions->getBuiltQueryParameters())
->withScheme('https');
Expand All @@ -135,6 +142,7 @@ private function request($method, $path, RequestOptions $requestOptions, $hosts,
$request = $this->createRequest(
$method,
$uri,
$canCompress,
$requestOptions->getHeaders(),
$body
);
Expand Down Expand Up @@ -218,6 +226,7 @@ private function createUri($uri)
private function createRequest(
$method,
$uri,
$canCompress,
array $headers = array(),
$body = null,
$protocolVersion = '1.1'
Expand All @@ -236,9 +245,20 @@ private function createRequest(
}
}

if ($canCompress) {
$body = gzencode($body, 9);
$headers['Content-Length'] = strlen($body);
}

return new Request($method, $uri, $headers, $body, $protocolVersion);
}

private function canEnableGzipCompress($method)
{
return (AbstractConfig::COMPRESSION_TYPE_GZIP === $this->config->getGzipEnabled())
&& ('POST' === strtoupper($method) || 'PUT' === strtoupper($method));
}

/**
* @param string $level
* @param string $message
Expand Down
1 change: 0 additions & 1 deletion tests/Integration/IndexingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function testIndexing()
$multiResponse->wait();

/* Check 6 first records with getObject */

$objectID1 = $responses[0][0]['objectIDs'][0];
$objectID2 = $responses[1][0]['objectIDs'][0];
$objectID3 = $responses[2][0]['objectIDs'][0];
Expand Down
15 changes: 12 additions & 3 deletions tests/Unit/CopyResourcesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public function testCopySettings()
static::$client->copySettings('src', 'dest');
} catch (RequestException $e) {
$this->assertEndpointEquals($e->getRequest(), '/1/indexes/src/operation');
$this->assertBodySubset(array(
$this->assertHeaderIsSet('Content-Encoding', $e->getRequest());
$this->assertHeaderIsSet('Content-Length', $e->getRequest());
$this->assertBodyEncoded($e->getRequest());
$this->assertEncodedBodySubset(array(
'operation' => 'copy',
'destination' => 'dest',
'scope' => array('settings'),
Expand All @@ -38,7 +41,10 @@ public function testCopySynonyms()
static::$client->copySynonyms('src', 'dest');
} catch (RequestException $e) {
$this->assertEndpointEquals($e->getRequest(), '/1/indexes/src/operation');
$this->assertBodySubset(array(
$this->assertHeaderIsSet('Content-Encoding', $e->getRequest());
$this->assertHeaderIsSet('Content-Length', $e->getRequest());
$this->assertBodyEncoded($e->getRequest());
$this->assertEncodedBodySubset(array(
'operation' => 'copy',
'destination' => 'dest',
'scope' => array('synonyms'),
Expand All @@ -54,7 +60,10 @@ public function testCopyRules()
static::$client->copyRules('src', 'dest');
} catch (RequestException $e) {
$this->assertEndpointEquals($e->getRequest(), '/1/indexes/src/operation');
$this->assertBodySubset(array(
$this->assertHeaderIsSet('Content-Encoding', $e->getRequest());
$this->assertHeaderIsSet('Content-Length', $e->getRequest());
$this->assertBodyEncoded($e->getRequest());
$this->assertEncodedBodySubset(array(
'operation' => 'copy',
'destination' => 'dest',
'scope' => array('rules'),
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/RequestTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ protected function assertBodySubset($subset, RequestInterface $request)
$this->assertArraySubset($subset, $body, true);
}

protected function assertEncodedBodySubset(
$subset,
RequestInterface $request
) {
$body = json_decode(gzdecode($request->getBody()), true);
$this->assertArraySubset($subset, $body, true);
}

protected function assertQueryParametersSubset(array $subset, RequestInterface $request)
{
$params = $this->requestQueryParametersToArray($request);
Expand All @@ -44,6 +52,23 @@ protected function assertQueryParametersNotHasKey($key, RequestInterface $reques
$this->assertArrayNotHasKey($key, $params);
}

protected function assertBodyEncoded(RequestInterface $request)
{
return gzdecode($request->getBody());
}

protected function assertHeaderIsSet($headerName, RequestInterface $request)
{
$this->assertArrayHasKey($headerName, $request->getHeaders());
}

protected function assertHeaderIsNotSet(
$headerName,
RequestInterface $request
) {
$this->assertArrayNotHasKey($headerName, $request->getHeaders());
}

private function requestQueryParametersToArray(RequestInterface $request)
{
$array = array();
Expand Down
27 changes: 22 additions & 5 deletions tests/Unit/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,57 @@ public function testQueryAsNullValue()
try {
$client->searchUserIds(null);
} catch (RequestException $e) {
$this->assertBodySubset(array('query' => ''), $e->getRequest());
$this->assertHeaderIsSet('Content-Encoding', $e->getRequest());
$this->assertHeaderIsSet('Content-Length', $e->getRequest());
$this->assertEncodedBodySubset(array('query' => ''),
$e->getRequest());
}

$index = $client->initIndex('foo');

try {
$index->search(null);
} catch (RequestException $e) {
$this->assertBodySubset(array('query' => ''), $e->getRequest());
$this->assertHeaderIsSet('Content-Encoding', $e->getRequest());
$this->assertHeaderIsSet('Content-Length', $e->getRequest());
$this->assertEncodedBodySubset(array('query' => ''),
$e->getRequest());
}

try {
$index->searchSynonyms(null);
} catch (RequestException $e) {
$this->assertBodySubset(array('query' => ''), $e->getRequest());
$this->assertHeaderIsSet('Content-Encoding', $e->getRequest());
$this->assertHeaderIsSet('Content-Length', $e->getRequest());
$this->assertEncodedBodySubset(array('query' => ''),
$e->getRequest());
}

try {
$index->searchRules(null);
} catch (RequestException $e) {
$this->assertBodySubset(array('query' => ''), $e->getRequest());
$this->assertHeaderIsSet('Content-Encoding', $e->getRequest());
$this->assertHeaderIsSet('Content-Length', $e->getRequest());
$this->assertEncodedBodySubset(array('query' => ''),
$e->getRequest());
}

try {
$index->searchRules(null);
} catch (RequestException $e) {
$this->assertBodySubset(array('query' => ''), $e->getRequest());
$this->assertHeaderIsSet('Content-Encoding', $e->getRequest());
$this->assertHeaderIsSet('Content-Length', $e->getRequest());
$this->assertEncodedBodySubset(array('query' => ''),
$e->getRequest());
}

$client = PlacesClient::create('id', 'key');

try {
$client->search(null);
} catch (RequestException $e) {
$this->assertHeaderIsNotSet('Content-Encoding', $e->getRequest());
$this->assertHeaderIsNotSet('Content-Length', $e->getRequest());
$this->assertBodySubset(array('query' => ''), $e->getRequest());
}
}
Expand Down