Skip to content

Commit

Permalink
Merge pull request #21054 from nextcloud/feature/noid/default-gzip-en…
Browse files Browse the repository at this point in the history
…coding-for-http-clients

Allow gzip encoded requests by default
  • Loading branch information
MorrisJobke authored May 20, 2020
2 parents 526905c + 8123737 commit 0a45f44
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 47 deletions.
5 changes: 3 additions & 2 deletions lib/private/App/AppStore/Fetcher/Fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ protected function fetch($ETag, $content) {

$options = [
'timeout' => 10,
'headers' => ['Accept-Encoding' => 'gzip'],
];

if ($ETag !== '') {
$options['headers']['If-None-Match'] = $ETag;
$options['headers'] = [
'If-None-Match' => $ETag,
];
}

$client = $this->clientService->newClient();
Expand Down
4 changes: 4 additions & 0 deletions lib/private/Http/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ private function buildRequestOptions(array $options): array {
$options[RequestOptions::HEADERS]['User-Agent'] = 'Nextcloud Server Crawler';
}

if (!isset($options[RequestOptions::HEADERS]['Accept-Encoding'])) {
$options[RequestOptions::HEADERS]['Accept-Encoding'] = 'gzip';
}

return $options;
}

Expand Down
47 changes: 5 additions & 42 deletions tests/lib/App/AppStore/Fetcher/FetcherBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,7 @@ public function testGetWithAlreadyExistingFileAndOutdatedTimestamp() {
$client
->expects($this->once())
->method('get')
->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->with($this->endpoint)
->willReturn($response);
$response
->expects($this->once())
Expand Down Expand Up @@ -350,15 +342,7 @@ public function testGetWithAlreadyExistingFileAndNoVersion() {
$client
->expects($this->once())
->method('get')
->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->with($this->endpoint)
->willReturn($response);
$response
->expects($this->once())
Expand Down Expand Up @@ -446,15 +430,7 @@ public function testGetWithAlreadyExistingFileAndOutdatedVersion() {
$client
->expects($this->once())
->method('get')
->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->with($this->endpoint)
->willReturn($response);
$response
->expects($this->once())
Expand Down Expand Up @@ -519,15 +495,7 @@ public function testGetWithExceptionInClient() {
$client
->expects($this->once())
->method('get')
->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->with($this->endpoint)
->willThrowException(new \Exception());

$this->assertSame([], $this->fetcher->get());
Expand Down Expand Up @@ -584,8 +552,7 @@ public function testGetMatchingETag() {
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
'If-None-Match' => '"myETag"',
'If-None-Match' => '"myETag"'
]
])
)->willReturn($response);
Expand Down Expand Up @@ -657,7 +624,6 @@ public function testGetNoMatchingETag() {
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
'If-None-Match' => '"myETag"',
]
])
Expand Down Expand Up @@ -744,9 +710,6 @@ public function testFetchAfterUpgradeNoETag() {
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
],
])
)
->willReturn($response);
Expand Down
10 changes: 7 additions & 3 deletions tests/lib/Http/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ private function setUpDefaultRequestOptions(): void {
],
'headers' => [
'User-Agent' => 'Nextcloud Server Crawler',
'Accept-Encoding' => 'gzip',
],
'timeout' => 30,
];
Expand Down Expand Up @@ -467,7 +468,8 @@ public function testSetDefaultOptionsWithNotInstalled(): void {
$this->assertEquals([
'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt',
'headers' => [
'User-Agent' => 'Nextcloud Server Crawler'
'User-Agent' => 'Nextcloud Server Crawler',
'Accept-Encoding' => 'gzip',
],
'timeout' => 30,
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
Expand Down Expand Up @@ -502,7 +504,8 @@ public function testSetDefaultOptionsWithProxy(): void {
'https' => 'foo'
],
'headers' => [
'User-Agent' => 'Nextcloud Server Crawler'
'User-Agent' => 'Nextcloud Server Crawler',
'Accept-Encoding' => 'gzip',
],
'timeout' => 30,
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
Expand Down Expand Up @@ -538,7 +541,8 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void {
'no' => ['bar']
],
'headers' => [
'User-Agent' => 'Nextcloud Server Crawler'
'User-Agent' => 'Nextcloud Server Crawler',
'Accept-Encoding' => 'gzip',
],
'timeout' => 30,
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
Expand Down

0 comments on commit 0a45f44

Please sign in to comment.