Remove deprecated HTTPHelper

* Remove the HTTP Helper
* Remove from Server Containter
* Removed legacy share tests that use it

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2018-03-13 16:30:41 +01:00
parent ede723f1b1
commit 7785c3752f
No known key found for this signature in database
GPG Key ID: F941078878347C0C
10 changed files with 16 additions and 386 deletions

View File

@ -85,13 +85,6 @@ class RequestHandlerControllerTest extends TestCase {
self::loginHelper(self::TEST_FILES_SHARING_API_USER1); self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
\OC\Share\Share::registerBackend('test', 'Test\Share\Backend'); \OC\Share\Share::registerBackend('test', 'Test\Share\Backend');
$config = $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor()->getMock();
$clientService = $this->getMockBuilder(IClientService::class)->getMock();
$httpHelperMock = $this->getMockBuilder('\OC\HTTPHelper')
->setConstructorArgs([$config, $clientService])
->getMock();
$httpHelperMock->expects($this->any())->method('post')->with($this->anything())->will($this->returnValue(true));
$this->share = $this->getMockBuilder(IShare::class)->getMock(); $this->share = $this->getMockBuilder(IShare::class)->getMock();
$this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider') $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
@ -110,8 +103,6 @@ class RequestHandlerControllerTest extends TestCase {
$this->cloudIdManager = new CloudIdManager(); $this->cloudIdManager = new CloudIdManager();
$this->registerHttpHelper($httpHelperMock);
$this->s2s = new RequestHandlerController( $this->s2s = new RequestHandlerController(
'federatedfilesharing', 'federatedfilesharing',
\OC::$server->getRequest(), \OC::$server->getRequest(),
@ -134,32 +125,9 @@ class RequestHandlerControllerTest extends TestCase {
$query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share`'); $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share`');
$query->execute(); $query->execute();
$this->restoreHttpHelper();
parent::tearDown(); parent::tearDown();
} }
/**
* Register an http helper mock for testing purposes.
* @param \OC\HTTPHelper $httpHelper helper mock
*/
private function registerHttpHelper($httpHelper) {
$this->oldHttpHelper = \OC::$server->query('HTTPHelper');
\OC::$server->registerService('HTTPHelper', function ($c) use ($httpHelper) {
return $httpHelper;
});
}
/**
* Restore the original http helper
*/
private function restoreHttpHelper() {
$oldHttpHelper = $this->oldHttpHelper;
\OC::$server->registerService('HTTPHelper', function ($c) use ($oldHttpHelper) {
return $oldHttpHelper;
});
}
/** /**
* @medium * @medium
*/ */

View File

@ -696,7 +696,6 @@ return array(
'OC\\Group\\Group' => $baseDir . '/lib/private/Group/Group.php', 'OC\\Group\\Group' => $baseDir . '/lib/private/Group/Group.php',
'OC\\Group\\Manager' => $baseDir . '/lib/private/Group/Manager.php', 'OC\\Group\\Manager' => $baseDir . '/lib/private/Group/Manager.php',
'OC\\Group\\MetaData' => $baseDir . '/lib/private/Group/MetaData.php', 'OC\\Group\\MetaData' => $baseDir . '/lib/private/Group/MetaData.php',
'OC\\HTTPHelper' => $baseDir . '/lib/private/HTTPHelper.php',
'OC\\HintException' => $baseDir . '/lib/private/HintException.php', 'OC\\HintException' => $baseDir . '/lib/private/HintException.php',
'OC\\Hooks\\BasicEmitter' => $baseDir . '/lib/private/Hooks/BasicEmitter.php', 'OC\\Hooks\\BasicEmitter' => $baseDir . '/lib/private/Hooks/BasicEmitter.php',
'OC\\Hooks\\Emitter' => $baseDir . '/lib/private/Hooks/Emitter.php', 'OC\\Hooks\\Emitter' => $baseDir . '/lib/private/Hooks/Emitter.php',

View File

@ -726,7 +726,6 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Group\\Group' => __DIR__ . '/../../..' . '/lib/private/Group/Group.php', 'OC\\Group\\Group' => __DIR__ . '/../../..' . '/lib/private/Group/Group.php',
'OC\\Group\\Manager' => __DIR__ . '/../../..' . '/lib/private/Group/Manager.php', 'OC\\Group\\Manager' => __DIR__ . '/../../..' . '/lib/private/Group/Manager.php',
'OC\\Group\\MetaData' => __DIR__ . '/../../..' . '/lib/private/Group/MetaData.php', 'OC\\Group\\MetaData' => __DIR__ . '/../../..' . '/lib/private/Group/MetaData.php',
'OC\\HTTPHelper' => __DIR__ . '/../../..' . '/lib/private/HTTPHelper.php',
'OC\\HintException' => __DIR__ . '/../../..' . '/lib/private/HintException.php', 'OC\\HintException' => __DIR__ . '/../../..' . '/lib/private/HintException.php',
'OC\\Hooks\\BasicEmitter' => __DIR__ . '/../../..' . '/lib/private/Hooks/BasicEmitter.php', 'OC\\Hooks\\BasicEmitter' => __DIR__ . '/../../..' . '/lib/private/Hooks/BasicEmitter.php',
'OC\\Hooks\\Emitter' => __DIR__ . '/../../..' . '/lib/private/Hooks/Emitter.php', 'OC\\Hooks\\Emitter' => __DIR__ . '/../../..' . '/lib/private/Hooks/Emitter.php',

View File

@ -1,120 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Björn Schießle <bjoern@schiessle.org>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Morris Jobke <hey@morrisjobke.de>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
/**
* Class HTTPHelper
*
* @package OC
* @deprecated Use \OCP\Http\Client\IClientService
*/
class HTTPHelper {
const USER_AGENT = 'ownCloud Server Crawler';
/** @var \OCP\IConfig */
private $config;
/** @var IClientService */
private $clientService;
/**
* @param IConfig $config
* @param IClientService $clientService
*/
public function __construct(IConfig $config,
IClientService $clientService) {
$this->config = $config;
$this->clientService = $clientService;
}
/**
* Get URL content
* @param string $url Url to get content
* @throws \Exception If the URL does not start with http:// or https://
* @return string of the response or false on error
* This function get the content of a page via curl, if curl is enabled.
* If not, file_get_contents is used.
* @deprecated Use \OCP\Http\Client\IClientService
*/
public function getUrlContent($url) {
try {
$client = $this->clientService->newClient();
$response = $client->get($url);
return $response->getBody();
} catch (\Exception $e) {
return false;
}
}
/**
* Returns the response headers of a HTTP URL without following redirects
* @param string $location Needs to be a HTTPS or HTTP URL
* @return array
* @deprecated Use \OCP\Http\Client\IClientService
*/
public function getHeaders($location) {
$client = $this->clientService->newClient();
$response = $client->get($location);
return $response->getHeaders();
}
/**
* Checks whether the supplied URL begins with HTTPS:// or HTTP:// (case insensitive)
* @param string $url
* @return bool
*/
public function isHTTPURL($url) {
return stripos($url, 'https://') === 0 || stripos($url, 'http://') === 0;
}
/**
* send http post request
*
* @param string $url
* @param array $fields data send by the request
* @return array
* @deprecated Use \OCP\Http\Client\IClientService
*/
public function post($url, array $fields) {
$client = $this->clientService->newClient();
try {
$response = $client->post(
$url,
[
'body' => $fields,
'connect_timeout' => 10,
]
);
} catch (\Exception $e) {
return ['success' => false, 'result' => $e->getMessage()];
}
return ['success' => true, 'result' => $response->getBody()];
}
}

View File

@ -630,13 +630,6 @@ class Server extends ServerContainer implements IServerContainer {
}); });
$this->registerAlias('DatabaseConnection', IDBConnection::class); $this->registerAlias('DatabaseConnection', IDBConnection::class);
$this->registerService('HTTPHelper', function (Server $c) {
$config = $c->getConfig();
return new HTTPHelper(
$config,
$c->getHTTPClientService()
);
});
$this->registerService(\OCP\Http\Client\IClientService::class, function (Server $c) { $this->registerService(\OCP\Http\Client\IClientService::class, function (Server $c) {
$user = \OC_User::getUser(); $user = \OC_User::getUser();
@ -1582,16 +1575,6 @@ class Server extends ServerContainer implements IServerContainer {
return $this->query('CredentialsManager'); return $this->query('CredentialsManager');
} }
/**
* Returns an instance of the HTTP helper class
*
* @deprecated Use getHTTPClientService()
* @return \OC\HTTPHelper
*/
public function getHTTPHelper() {
return $this->query('HTTPHelper');
}
/** /**
* Get the certificate manager for the user * Get the certificate manager for the user
* *

View File

@ -1991,7 +1991,22 @@ class Share extends Constants {
while ($result['success'] === false && $try < 2) { while ($result['success'] === false && $try < 2) {
$federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING'); $federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING');
$endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares';
$result = \OC::$server->getHTTPHelper()->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, $fields); $client = \OC::$server->getHTTPClientService()->newClient();
try {
$response = $client->post(
$protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT,
[
'body' => $fields,
'connect_timeout' => 10,
]
);
$result = ['success' => true, 'result' => $response->getBody()];
} catch (\Exception $e) {
$result = ['success' => false, 'result' => $e->getMessage()];
}
$try++; $try++;
$protocol = 'http://'; $protocol = 'http://';
} }

View File

@ -347,14 +347,6 @@ interface IServerContainer extends IContainer {
*/ */
public function createEventSource(); public function createEventSource();
/**
* Returns an instance of the HTTP helper class
* @return \OC\HTTPHelper
* @deprecated 8.1.0 Use \OCP\Http\Client\IClientService
* @since 8.0.0
*/
public function getHTTPHelper();
/** /**
* Returns an instance of the HTTP client service * Returns an instance of the HTTP client service
* *

View File

@ -1,123 +0,0 @@
<?php
/**
* Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
class HTTPHelperTest extends \Test\TestCase {
/** @var \OCP\IConfig*/
private $config;
/** @var \OC\HTTPHelper */
private $httpHelperMock;
/** @var \OCP\Http\Client\IClientService */
private $clientService;
protected function setUp() {
parent::setUp();
$this->config = $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor()->getMock();
$this->clientService = $this->createMock(IClientService::class);
$this->httpHelperMock = $this->getMockBuilder('\OC\HTTPHelper')
->setConstructorArgs(array($this->config, $this->clientService))
->setMethods(array('getHeaders'))
->getMock();
}
public function isHttpTestData() {
return array(
array('http://wwww.owncloud.org/enterprise/', true),
array('https://wwww.owncloud.org/enterprise/', true),
array('HTTPS://WWW.OWNCLOUD.ORG', true),
array('HTTP://WWW.OWNCLOUD.ORG', true),
array('FILE://WWW.OWNCLOUD.ORG', false),
array('file://www.owncloud.org', false),
array('FTP://WWW.OWNCLOUD.ORG', false),
array('ftp://www.owncloud.org', false),
);
}
/**
* @dataProvider isHttpTestData
*/
public function testIsHTTP($url, $expected) {
$this->assertSame($expected, $this->httpHelperMock->isHTTPURL($url));
}
public function testPostSuccess() {
$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
->disableOriginalConstructor()->getMock();
$this->clientService
->expects($this->once())
->method('newClient')
->will($this->returnValue($client));
$response = $this->getMockBuilder('\OCP\Http\Client\IResponse')
->disableOriginalConstructor()->getMock();
$client
->expects($this->once())
->method('post')
->with(
'https://owncloud.org',
[
'body' => [
'Foo' => 'Bar',
],
'connect_timeout' => 10,
]
)
->will($this->returnValue($response));
$response
->expects($this->once())
->method('getBody')
->will($this->returnValue('Body of the requested page'));
$response = $this->httpHelperMock->post('https://owncloud.org', ['Foo' => 'Bar']);
$expected = [
'success' => true,
'result' => 'Body of the requested page'
];
$this->assertSame($expected, $response);
}
public function testPostException() {
$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
->disableOriginalConstructor()->getMock();
$this->clientService
->expects($this->once())
->method('newClient')
->will($this->returnValue($client));
$client
->expects($this->once())
->method('post')
->with(
'https://owncloud.org',
[
'body' => [
'Foo' => 'Bar',
],
'connect_timeout' => 10,
]
)
->will($this->throwException(new \Exception('Something failed')));
$response = $this->httpHelperMock->post('https://owncloud.org', ['Foo' => 'Bar']);
$expected = [
'success' => false,
'result' => 'Something failed'
];
$this->assertSame($expected, $response);
}
}

View File

@ -91,7 +91,6 @@ class ServerTest extends \Test\TestCase {
['Hasher', '\OC\Security\Hasher'], ['Hasher', '\OC\Security\Hasher'],
['Hasher', '\OCP\Security\IHasher'], ['Hasher', '\OCP\Security\IHasher'],
['HTTPHelper', '\OC\HTTPHelper'],
['HttpClientService', '\OC\Http\Client\ClientService'], ['HttpClientService', '\OC\Http\Client\ClientService'],
['HttpClientService', '\OCP\Http\Client\IClientService'], ['HttpClientService', '\OCP\Http\Client\IClientService'],

View File

@ -542,53 +542,6 @@ class ShareTest extends \Test\TestCase {
); );
} }
public function dataRemoteShareUrlCalls() {
return [
['admin@localhost', 'localhost'],
['admin@https://localhost', 'localhost'],
['admin@http://localhost', 'localhost'],
['admin@localhost/subFolder', 'localhost/subFolder'],
];
}
/**
* @dataProvider dataRemoteShareUrlCalls
*
* @param string $shareWith
* @param string $urlHost
*/
public function testRemoteShareUrlCalls($shareWith, $urlHost) {
$httpHelperMock = $this->getMockBuilder('OC\HTTPHelper')
->disableOriginalConstructor()
->getMock();
$this->overwriteService('HTTPHelper', $httpHelperMock);
$httpHelperMock->expects($this->at(0))
->method('post')
->with($this->stringStartsWith('https://' . $urlHost . '/ocs/v2.php/cloud/shares'), $this->anything())
->willReturn(['success' => false, 'result' => 'Exception']);
$httpHelperMock->expects($this->at(1))
->method('post')
->with($this->stringStartsWith('http://' . $urlHost . '/ocs/v2.php/cloud/shares'), $this->anything())
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
\OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $shareWith, \OCP\Constants::PERMISSION_READ);
$shares = \OCP\Share::getItemShared('test', 'test.txt');
$share = array_shift($shares);
$httpHelperMock->expects($this->at(0))
->method('post')
->with($this->stringStartsWith('https://' . $urlHost . '/ocs/v2.php/cloud/shares/' . $share['id'] . '/unshare'), $this->anything())
->willReturn(['success' => false, 'result' => 'Exception']);
$httpHelperMock->expects($this->at(1))
->method('post')
->with($this->stringStartsWith('http://' . $urlHost . '/ocs/v2.php/cloud/shares/' . $share['id'] . '/unshare'), $this->anything())
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
\OC\Share\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $shareWith);
$this->restoreService('HTTPHelper');
}
/** /**
* @dataProvider dataProviderTestGroupItems * @dataProvider dataProviderTestGroupItems
* @param array $ungrouped * @param array $ungrouped
@ -666,41 +619,6 @@ class ShareTest extends \Test\TestCase {
); );
} }
/**
* Make sure that a user cannot have multiple identical shares to remote users
*/
public function testOnlyOneRemoteShare() {
$httpHelperMock = $this->getMockBuilder('OC\HTTPHelper')
->disableOriginalConstructor()
->getMock();
$this->overwriteService('HTTPHelper', $httpHelperMock);
$httpHelperMock->expects($this->at(0))
->method('post')
->with($this->stringStartsWith('https://localhost/ocs/v2.php/cloud/shares'), $this->anything())
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
\OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost', \OCP\Constants::PERMISSION_READ);
$shares = \OCP\Share::getItemShared('test', 'test.txt');
$share = array_shift($shares);
//Try share again
try {
\OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost', \OCP\Constants::PERMISSION_READ);
$this->fail('Identical remote shares are not allowed');
} catch (\Exception $e) {
$this->assertEquals('Sharing test.txt failed, because this item is already shared with foo@localhost', $e->getMessage());
}
$httpHelperMock->expects($this->at(0))
->method('post')
->with($this->stringStartsWith('https://localhost/ocs/v2.php/cloud/shares/' . $share['id'] . '/unshare'), $this->anything())
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
\OC\Share\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost');
$this->restoreService('HTTPHelper');
}
/** /**
* Test case for #17560 * Test case for #17560
*/ */