diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2016-05-19 09:38:52 +0200 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2016-05-19 09:38:52 +0200 |
commit | 9a4253ef7c34f9dc71a6a9f7828a10df769f0c32 (patch) | |
tree | febe8415af243c7f69867d095a3f281c65071d40 /tests/lib/HTTPHelperTest.php | |
parent | 55fc6536d33bd044a72437ac61d3c5ade09111cb (diff) | |
download | nextcloud-server-9a4253ef7c34f9dc71a6a9f7828a10df769f0c32.tar.gz nextcloud-server-9a4253ef7c34f9dc71a6a9f7828a10df769f0c32.zip |
Fix lib/
Diffstat (limited to 'tests/lib/HTTPHelperTest.php')
-rw-r--r-- | tests/lib/HTTPHelperTest.php | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/tests/lib/HTTPHelperTest.php b/tests/lib/HTTPHelperTest.php new file mode 100644 index 00000000000..6d9c4788735 --- /dev/null +++ b/tests/lib/HTTPHelperTest.php @@ -0,0 +1,120 @@ +<?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; + +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('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + $this->clientService = $this->getMock('\OCP\Http\Client\IClientService'); + $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); + } + +} |