2014-09-11 17:21:56 +00:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 07:38:52 +00:00
|
|
|
namespace Test;
|
|
|
|
|
2016-09-07 18:03:57 +00:00
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
|
2016-05-19 07:38:52 +00:00
|
|
|
class HTTPHelperTest extends \Test\TestCase {
|
2014-09-11 17:21:56 +00:00
|
|
|
|
2014-11-27 17:19:14 +00:00
|
|
|
/** @var \OCP\IConfig*/
|
2014-09-11 17:21:56 +00:00
|
|
|
private $config;
|
|
|
|
/** @var \OC\HTTPHelper */
|
|
|
|
private $httpHelperMock;
|
2015-05-26 09:22:50 +00:00
|
|
|
/** @var \OCP\Http\Client\IClientService */
|
|
|
|
private $clientService;
|
2014-09-11 17:21:56 +00:00
|
|
|
|
2014-11-10 21:59:50 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2014-11-27 17:19:14 +00:00
|
|
|
$this->config = $this->getMockBuilder('\OCP\IConfig')
|
2014-09-11 17:21:56 +00:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-09-07 18:03:57 +00:00
|
|
|
$this->clientService = $this->createMock(IClientService::class);
|
2014-09-11 17:21:56 +00:00
|
|
|
$this->httpHelperMock = $this->getMockBuilder('\OC\HTTPHelper')
|
2015-05-26 09:22:50 +00:00
|
|
|
->setConstructorArgs(array($this->config, $this->clientService))
|
2014-09-11 17:21:56 +00:00
|
|
|
->setMethods(array('getHeaders'))
|
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
|
2014-11-21 09:48:05 +00:00
|
|
|
public function isHttpTestData() {
|
2014-09-11 17:21:56 +00:00
|
|
|
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),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-21 09:48:05 +00:00
|
|
|
* @dataProvider isHttpTestData
|
2014-09-11 17:21:56 +00:00
|
|
|
*/
|
|
|
|
public function testIsHTTP($url, $expected) {
|
|
|
|
$this->assertSame($expected, $this->httpHelperMock->isHTTPURL($url));
|
|
|
|
}
|
2015-05-26 09:22:50 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-09-11 17:21:56 +00:00
|
|
|
}
|