Skip to content

Commit

Permalink
Merge pull request #1046 from nextcloud/dep_getHTTPHelper
Browse files Browse the repository at this point in the history
Do not use deprecated HTTPHelper
  • Loading branch information
blizzz committed Aug 25, 2016
2 parents 15bda3c + cae87d0 commit 00c767f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private function legacyMountPublicLink($token, $remote, $password, $name, $owner
\OC::$server->getDatabaseConnection(),
\OC\Files\Filesystem::getMountManager(),
\OC\Files\Filesystem::getLoader(),
\OC::$server->getHTTPHelper(),
\OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(),
$discoveryManager,
\OC::$server->getUserSession()->getUser()->getUID()
Expand Down
2 changes: 1 addition & 1 deletion apps/federatedfilesharing/lib/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function createShare($params) {
\OC::$server->getDatabaseConnection(),
\OC\Files\Filesystem::getMountManager(),
\OC\Files\Filesystem::getLoader(),
\OC::$server->getHTTPHelper(),
\OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(),
$discoveryManager,
$shareWith
Expand Down
2 changes: 1 addition & 1 deletion apps/federatedfilesharing/tests/RequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function testDeleteUser($toDelete, $expected, $remainingUsers) {
\OC::$server->getDatabaseConnection(),
Filesystem::getMountManager(),
Filesystem::getLoader(),
\OC::$server->getHTTPHelper(),
\OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(),
$discoveryManager,
$toDelete
Expand Down
12 changes: 6 additions & 6 deletions apps/files_sharing/lib/API/Remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function getOpenShares($params) {
\OC::$server->getDatabaseConnection(),
Filesystem::getMountManager(),
Filesystem::getLoader(),
\OC::$server->getHTTPHelper(),
\OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(),
$discoveryManager,
\OC_User::getUser()
Expand All @@ -69,7 +69,7 @@ public static function acceptShare($params) {
\OC::$server->getDatabaseConnection(),
Filesystem::getMountManager(),
Filesystem::getLoader(),
\OC::$server->getHTTPHelper(),
\OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(),
$discoveryManager,
\OC_User::getUser()
Expand Down Expand Up @@ -100,7 +100,7 @@ public static function declineShare($params) {
\OC::$server->getDatabaseConnection(),
Filesystem::getMountManager(),
Filesystem::getLoader(),
\OC::$server->getHTTPHelper(),
\OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(),
$discoveryManager,
\OC_User::getUser()
Expand Down Expand Up @@ -148,7 +148,7 @@ public static function getShares($params) {
\OC::$server->getDatabaseConnection(),
Filesystem::getMountManager(),
Filesystem::getLoader(),
\OC::$server->getHTTPHelper(),
\OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(),
$discoveryManager,
\OC_User::getUser()
Expand Down Expand Up @@ -176,7 +176,7 @@ public static function getShare($params) {
\OC::$server->getDatabaseConnection(),
Filesystem::getMountManager(),
Filesystem::getLoader(),
\OC::$server->getHTTPHelper(),
\OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(),
$discoveryManager,
\OC_User::getUser()
Expand Down Expand Up @@ -207,7 +207,7 @@ public static function unshare($params) {
\OC::$server->getDatabaseConnection(),
Filesystem::getMountManager(),
Filesystem::getLoader(),
\OC::$server->getHTTPHelper(),
\OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(),
$discoveryManager,
\OC_User::getUser()
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function __construct(array $urlParams = array()) {
$server->getDatabaseConnection(),
\OC\Files\Filesystem::getMountManager(),
\OC\Files\Filesystem::getLoader(),
$server->getHTTPHelper(),
$server->getHTTPClientService(),
$server->getNotificationManager(),
$discoveryManager,
$uid
Expand Down
30 changes: 22 additions & 8 deletions apps/files_sharing/lib/External/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OC\Files\Filesystem;
use OCA\FederatedFileSharing\DiscoveryManager;
use OCP\Files;
use OCP\Http\Client\IClientService;
use OCP\Notification\IManager;

class Manager {
Expand All @@ -58,9 +59,9 @@ class Manager {
private $storageLoader;

/**
* @var \OC\HTTPHelper
* @var IClientService
*/
private $httpHelper;
private $clientService;

/**
* @var IManager
Expand All @@ -73,22 +74,22 @@ class Manager {
* @param \OCP\IDBConnection $connection
* @param \OC\Files\Mount\Manager $mountManager
* @param \OCP\Files\Storage\IStorageFactory $storageLoader
* @param \OC\HTTPHelper $httpHelper
* @param IClientService $clientService
* @param IManager $notificationManager
* @param DiscoveryManager $discoveryManager
* @param string $uid
*/
public function __construct(\OCP\IDBConnection $connection,
\OC\Files\Mount\Manager $mountManager,
\OCP\Files\Storage\IStorageFactory $storageLoader,
\OC\HTTPHelper $httpHelper,
IClientService $clientService,
IManager $notificationManager,
DiscoveryManager $discoveryManager,
$uid) {
$this->connection = $connection;
$this->mountManager = $mountManager;
$this->storageLoader = $storageLoader;
$this->httpHelper = $httpHelper;
$this->clientService = $clientService;
$this->uid = $uid;
$this->notificationManager = $notificationManager;
$this->discoveryManager = $discoveryManager;
Expand Down Expand Up @@ -262,10 +263,23 @@ private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) {
$url = rtrim($remote, '/') . $this->discoveryManager->getShareEndpoint($remote) . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT;
$fields = array('token' => $token);

$result = $this->httpHelper->post($url, $fields);
$status = json_decode($result['result'], true);
$client = $this->clientService->newClient();

try {
$response = $client->post(
$url,
[
'body' => $fields,
'connect_timeout' => 10,
]
);
} catch (\Exception $e) {
return false;
}

$status = json_decode($response->getBody(), true);

return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200));
return ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function deleteUser($params) {
\OC::$server->getDatabaseConnection(),
\OC\Files\Filesystem::getMountManager(),
\OC\Files\Filesystem::getLoader(),
\OC::$server->getHTTPHelper(),
\OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(),
$discoveryManager,
$params['uid']);
Expand Down
57 changes: 44 additions & 13 deletions apps/files_sharing/tests/External/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Files_Sharing\External\Manager;
use OCA\Files_Sharing\External\MountProvider;
use OCA\Files_Sharing\Tests\TestCase;
use OCP\Http\Client\IClientService;
use Test\Traits\UserTrait;

/**
Expand All @@ -48,8 +49,8 @@ class ManagerTest extends TestCase {
/** @var \OC\Files\Mount\Manager */
private $mountManager;

/** @var \PHPUnit_Framework_MockObject_MockObject */
private $httpHelper;
/** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */
private $clientService;

private $uid;

Expand All @@ -66,17 +67,17 @@ protected function setUp() {
$this->createUser($this->uid, '');
$this->user = \OC::$server->getUserManager()->get($this->uid);
$this->mountManager = new \OC\Files\Mount\Manager();
$this->httpHelper = $httpHelper = $this->getMockBuilder('\OC\HTTPHelper')->disableOriginalConstructor()->getMock();
$this->clientService = $this->getMockBuilder('\OCP\Http\Client\IClientService')
->disableOriginalConstructor()->getMock();
$discoveryManager = new DiscoveryManager(
\OC::$server->getMemCacheFactory(),
\OC::$server->getHTTPClientService()
);
/** @var \OC\HTTPHelper $httpHelper */
$this->manager = new Manager(
\OC::$server->getDatabaseConnection(),
$this->mountManager,
new StorageFactory(),
$httpHelper,
$this->clientService,
\OC::$server->getNotificationManager(),
$discoveryManager,
$this->uid
Expand Down Expand Up @@ -132,9 +133,17 @@ public function testAddShare() {
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');

$this->httpHelper->expects($this->at(0))
$client = $this->getMockBuilder('OCP\Http\Client\IClient')
->disableOriginalConstructor()->getMock();
$this->clientService->expects($this->at(0))
->method('newClient')
->willReturn($client);
$response = $this->getMockBuilder('OCP\Http\Client\IResponse')
->disableOriginalConstructor()->getMock();
$client->expects($this->once())
->method('post')
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything());
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything())
->willReturn($response);

// Accept the first share
$this->manager->acceptShare($openShares[0]['id']);
Expand Down Expand Up @@ -167,9 +176,17 @@ public function testAddShare() {
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');

$this->httpHelper->expects($this->at(0))
$client = $this->getMockBuilder('OCP\Http\Client\IClient')
->disableOriginalConstructor()->getMock();
$this->clientService->expects($this->at(0))
->method('newClient')
->willReturn($client);
$response = $this->getMockBuilder('OCP\Http\Client\IResponse')
->disableOriginalConstructor()->getMock();
$client->expects($this->once())
->method('post')
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything());
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything())
->willReturn($response);

// Decline the third share
$this->manager->declineShare($openShares[1]['id']);
Expand All @@ -194,12 +211,26 @@ public function testAddShare() {
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');

$this->httpHelper->expects($this->at(0))
$client1 = $this->getMockBuilder('OCP\Http\Client\IClient')
->disableOriginalConstructor()->getMock();
$client2 = $this->getMockBuilder('OCP\Http\Client\IClient')
->disableOriginalConstructor()->getMock();
$this->clientService->expects($this->at(0))
->method('newClient')
->willReturn($client1);
$this->clientService->expects($this->at(1))
->method('newClient')
->willReturn($client2);
$response = $this->getMockBuilder('OCP\Http\Client\IResponse')
->disableOriginalConstructor()->getMock();
$client1->expects($this->once())
->method('post')
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything());
$this->httpHelper->expects($this->at(1))
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything())
->willReturn($response);
$client2->expects($this->once())
->method('post')
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything());
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything())
->willReturn($response);

$this->manager->removeUserShares($this->uid);
$this->assertEmpty(self::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted');
Expand Down

0 comments on commit 00c767f

Please sign in to comment.