summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-04-10 19:15:31 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-04-10 19:15:31 +0200
commit5befce51ef9f38609b713cfbc6095a3b60b1d574 (patch)
tree0bbe540c109afa83086ed7ad43adb8a215e54633 /tests
parentdb545b430a513e7fda3fba5859972b6c550958b5 (diff)
add proxy support based on simplepie pr, fix #491
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/articleenhancer/XPathArticleEnhancerTest.php106
-rw-r--r--tests/unit/fetcher/FeedFetcherTest.php67
-rw-r--r--tests/unit/utility/ConfigTest.php8
-rw-r--r--tests/unit/utility/FaviconFetcherTest.php102
4 files changed, 267 insertions, 16 deletions
diff --git a/tests/unit/articleenhancer/XPathArticleEnhancerTest.php b/tests/unit/articleenhancer/XPathArticleEnhancerTest.php
index a600d6739..dbd752518 100644
--- a/tests/unit/articleenhancer/XPathArticleEnhancerTest.php
+++ b/tests/unit/articleenhancer/XPathArticleEnhancerTest.php
@@ -38,17 +38,32 @@ class XPathArticleEnhancerTest extends \OCA\News\Utility\TestUtility {
private $redirects;
private $headers;
private $userAgent;
+ private $proxyHost;
+ private $proxyPort;
+ private $proxyAuth;
protected function setUp() {
$this->timeout = 30;
$this->fileFactory = $this->getMockBuilder('\OCA\News\Utility\SimplePieAPIFactory')
->disableOriginalConstructor()
->getMock();
- $config = $this->getMockBuilder(
+ $this->proxyHost = 'test';
+ $this->proxyPort = 3;
+ $this->proxyAuth = 'hi';
+ $this->config = $this->getMockBuilder(
'\OCA\News\Utility\Config')
->disableOriginalConstructor()
->getMock();
- $config->expects($this->any())
+ $this->config->expects($this->any())
+ ->method('getProxyHost')
+ ->will($this->returnValue(''));
+ $this->config->expects($this->any())
+ ->method('getProxyAuth')
+ ->will($this->returnValue($this->proxyAuth));
+ $this->config->expects($this->any())
+ ->method('getProxyPort')
+ ->will($this->returnValue($this->proxyPort));
+ $this->config->expects($this->any())
->method('getFeedFetcherTimeout')
->will($this->returnValue($this->timeout));
@@ -60,7 +75,7 @@ class XPathArticleEnhancerTest extends \OCA\News\Utility\TestUtility {
'/explosm.net\/all/' => '//body/*',
'/themerepublic.net/' => '//*[@class=\'post hentry\']'
),
- $config
+ $this->config
);
$this->redirects = 5;
$this->headers = null;
@@ -68,6 +83,91 @@ class XPathArticleEnhancerTest extends \OCA\News\Utility\TestUtility {
}
+ public function testXPathUsesNoProxy() {
+ $file = new \stdClass;
+ $file->headers = array("content-type"=>"text/html; charset=utf-8");
+ $file->body = '';
+ $item = new Item();
+ $item->setUrl('https://www.explosm.net/comics/312');
+ $item->setBody('Hello thar');
+
+ $this->fileFactory->expects($this->once())
+ ->method('getFile')
+ ->with($this->equalTo($item->getUrl()),
+ $this->equalTo($this->timeout),
+ $this->equalTo($this->redirects),
+ $this->equalTo($this->headers),
+ $this->equalTo($this->userAgent),
+ $this->equalTo(false),
+ $this->equalTo(null),
+ $this->equalTo(null),
+ $this->equalTo(null))
+ ->will($this->returnValue($file));
+
+ $result = $this->testEnhancer->enhance($item);
+ $this->assertEquals('Hello thar', $result->getBody());
+ }
+
+
+ public function testXPathUsesProxy() {
+ $this->config = $this->getMockBuilder(
+ '\OCA\News\Utility\Config')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->config->expects($this->any())
+ ->method('getProxyHost')
+ ->will($this->returnValue($this->proxyHost));
+ $this->config->expects($this->any())
+ ->method('getProxyAuth')
+ ->will($this->returnValue($this->proxyAuth));
+ $this->config->expects($this->any())
+ ->method('getProxyPort')
+ ->will($this->returnValue($this->proxyPort));
+ $this->config->expects($this->any())
+ ->method('getFeedFetcherTimeout')
+ ->will($this->returnValue($this->timeout));
+
+ $this->testEnhancer = new XPathArticleEnhancer(
+ $this->fileFactory,
+ array(
+ '/explosm.net\/comics/' => '//*[@id=\'maincontent\']/div[2]/div/span',
+ '/explosm.net\/shorts/' => '//*[@id=\'maincontent\']/div/div',
+ '/explosm.net\/all/' => '//body/*',
+ '/themerepublic.net/' => '//*[@class=\'post hentry\']'
+ ),
+ $this->config
+ );
+
+ $file = new \stdClass;
+ $file->headers = array("content-type"=>"text/html; charset=utf-8");
+ $file->body = '';
+ $item = new Item();
+ $item->setUrl('https://www.explosm.net/comics/312');
+ $item->setBody('Hello thar');
+
+ $this->config->expects($this->any())
+ ->method('getProxyHost')
+ ->will($this->returnValue($this->proxyHost));
+
+ $this->fileFactory->expects($this->once())
+ ->method('getFile')
+ ->with($this->equalTo($item->getUrl()),
+ $this->equalTo($this->timeout),
+ $this->equalTo($this->redirects),
+ $this->equalTo($this->headers),
+ $this->equalTo($this->userAgent),
+ $this->equalTo(false),
+ $this->equalTo($this->proxyHost),
+ $this->equalTo($this->proxyPort),
+ $this->equalTo($this->proxyAuth))
+ ->will($this->returnValue($file));
+
+ $result = $this->testEnhancer->enhance($item);
+ $this->assertEquals('Hello thar', $result->getBody());
+ }
+
+
+
public function testDoesNotModifiyNotMatchingResults() {
$item = new Item();
$item->setUrl('http://explosm.net');
diff --git a/tests/unit/fetcher/FeedFetcherTest.php b/tests/unit/fetcher/FeedFetcherTest.php
index ff076d11c..392d0972c 100644
--- a/tests/unit/fetcher/FeedFetcherTest.php
+++ b/tests/unit/fetcher/FeedFetcherTest.php
@@ -44,6 +44,10 @@ class FeedFetcherTest extends \OCA\News\Utility\TestUtility {
private $item;
private $purifier;
private $fetchTimeout;
+ private $proxyHost;
+ private $getProxyPort;
+ private $proxyAuth;
+ private $config;
// items
private $permalink;
@@ -62,10 +66,23 @@ class FeedFetcherTest extends \OCA\News\Utility\TestUtility {
private $webFavicon;
protected function setUp(){
- $this->core = $this->getMockBuilder(
- '\SimplePie_Core')
- ->disableOriginalConstructor()
- ->getMock();
+ $this->core = $this->getMock(
+ '\SimplePie_Core', array(
+ 'set_timeout',
+ 'set_feed_url',
+ 'enable_cache',
+ 'set_stupidly_fast',
+ 'set_cache_location',
+ 'set_cache_duration',
+ 'set_proxyhost',
+ 'set_proxyport',
+ 'set_proxyuserpwd',
+ 'init',
+ 'get_permalink',
+ 'get_items',
+ 'get_title',
+ 'get_image_url'
+ ));
$this->coreFactory = $this->getMockBuilder(
'\OCA\News\Utility\SimplePieAPIFactory')
->disableOriginalConstructor()
@@ -88,15 +105,27 @@ class FeedFetcherTest extends \OCA\News\Utility\TestUtility {
->will($this->returnValue($this->time));
$this->cacheDuration = 100;
$this->cacheDirectory = 'dir/';
+ $this->proxyHost = 'test';
+ $this->proxyPort = 30;
+ $this->proxyAuth = 'hi';
$this->fetchTimeout = 40;
- $config = $this->getMockBuilder(
+ $this->config = $this->getMockBuilder(
'\OCA\News\Utility\Config')
->disableOriginalConstructor()
->getMock();
- $config->expects($this->any())
+ $this->config->expects($this->any())
->method('getSimplePieCacheDuration')
->will($this->returnValue($this->cacheDuration));
- $config->expects($this->any())
+ $this->config->expects($this->any())
+ ->method('getProxyHost')
+ ->will($this->returnValue($this->proxyHost));
+ $this->config->expects($this->any())
+ ->method('getProxyAuth')
+ ->will($this->returnValue($this->proxyAuth));
+ $this->config->expects($this->any())
+ ->method('getProxyPort')
+ ->will($this->returnValue($this->proxyPort));
+ $this->config->expects($this->any())
->method('getFeedFetcherTimeout')
->will($this->returnValue($this->fetchTimeout));
$this->fetcher = new FeedFetcher($this->getAPIMock(),
@@ -104,7 +133,7 @@ class FeedFetcherTest extends \OCA\News\Utility\TestUtility {
$this->faviconFetcher,
$timeFactory,
$this->cacheDirectory,
- $config);
+ $this->config);
$this->url = 'http://tests';
$this->permalink = 'http://permalink';
@@ -131,6 +160,19 @@ class FeedFetcherTest extends \OCA\News\Utility\TestUtility {
}
+ public function testDoesNotUseProxyIfNotEnabled() {
+ $this->config->expects($this->any())
+ ->method('getProxyHost')
+ ->will($this->returnValue(''));
+ $this->core->expects($this->never())
+ ->method('set_proxyhost');
+ $this->core->expects($this->never())
+ ->method('set_proxyport');
+ $this->core->expects($this->never())
+ ->method('set_proxyuserpwd');
+ }
+
+
public function testFetchThrowsExceptionWhenInitFailed() {
$this->core->expects($this->once())
->method('set_feed_url')
@@ -145,6 +187,15 @@ class FeedFetcherTest extends \OCA\News\Utility\TestUtility {
->method('set_cache_location')
->with($this->equalTo($this->cacheDirectory));
$this->core->expects($this->once())
+ ->method('set_proxyhost')
+ ->with($this->equalTo($this->proxyHost));
+ $this->core->expects($this->once())
+ ->method('set_proxyport')
+ ->with($this->equalTo($this->proxyPort));
+ $this->core->expects($this->once())
+ ->method('set_proxyuserpwd')
+ ->with($this->equalTo($this->proxyAuth));
+ $this->core->expects($this->once())
->method('set_stupidly_fast')
->with($this->equalTo(true));
$this->core->expects($this->once())
diff --git a/tests/unit/utility/ConfigTest.php b/tests/unit/utility/ConfigTest.php
index d3f91b975..9cea0acf0 100644
--- a/tests/unit/utility/ConfigTest.php
+++ b/tests/unit/utility/ConfigTest.php
@@ -57,7 +57,7 @@ class ConfigFetcherTest extends \OCA\News\Utility\TestUtility {
$this->assertEquals(true, $this->config->getUseCronUpdates());
$this->assertEquals(8080, $this->config->getProxyPort());
$this->assertEquals('', $this->config->getProxyHost());
- $this->assertEquals('', $this->config->getProxyPassword());
+ $this->assertEquals('', $this->config->getProxyAuth());
}
@@ -124,11 +124,11 @@ class ConfigFetcherTest extends \OCA\News\Utility\TestUtility {
"useCronUpdates = true\n" .
"proxyHost = yo man\n" .
"proxyPort = 12\n" .
- "proxyPassword = this is a test";
+ "proxyAuth = this is a test";
$this->config->setAutoPurgeCount(3);
$this->config->setProxyHost("yo man");
$this->config->setProxyPort(12);
- $this->config->setProxyPassword("this is a test");
+ $this->config->setProxyAuth("this is a test");
$this->fileSystem->expects($this->once())
->method('file_put_contents')
@@ -154,7 +154,7 @@ class ConfigFetcherTest extends \OCA\News\Utility\TestUtility {
"useCronUpdates = false\n" .
"proxyHost = \n" .
"proxyPort = 8080\n" .
- "proxyPassword = ";
+ "proxyAuth = ";
$this->fileSystem->expects($this->once())
->method('file_put_contents')
diff --git a/tests/unit/utility/FaviconFetcherTest.php b/tests/unit/utility/FaviconFetcherTest.php
index 4f11c49e1..875fcf11d 100644
--- a/tests/unit/utility/FaviconFetcherTest.php
+++ b/tests/unit/utility/FaviconFetcherTest.php
@@ -33,14 +33,33 @@ class FaviconFetcherTest extends \PHPUnit_Framework_TestCase {
private $fetcher;
private $fileFactory;
private $png;
+ private $proxyHost;
+ private $proxyPort;
+ private $proxyAuth;
protected function setUp(){
$this->png = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
+ $this->proxyHost = 'test';
+ $this->proxyPort = 3;
+ $this->proxyAuth = 'hi';
$this->fileFactory = $this->getMockBuilder(
'\OCA\News\Utility\SimplePieAPIFactory')
->disableOriginalConstructor()
->getMock();
- $this->fetcher = new FaviconFetcher($this->fileFactory);
+ $this->config = $this->getMockBuilder(
+ '\OCA\News\Utility\Config')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->config->expects($this->any())
+ ->method('getProxyHost')
+ ->will($this->returnValue(''));
+ $this->config->expects($this->any())
+ ->method('getProxyAuth')
+ ->will($this->returnValue($this->proxyAuth));
+ $this->config->expects($this->any())
+ ->method('getProxyPort')
+ ->will($this->returnValue($this->proxyPort));
+ $this->fetcher = new FaviconFetcher($this->fileFactory, $this->config);
}
@@ -78,6 +97,87 @@ class FaviconFetcherTest extends \PHPUnit_Framework_TestCase {
}
+ public function testProxySettingsAreUsed() {
+ $this->config = $this->getMockBuilder(
+ '\OCA\News\Utility\Config')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->config->expects($this->any())
+ ->method('getProxyHost')
+ ->will($this->returnValue($this->proxyHost));
+ $this->config->expects($this->any())
+ ->method('getProxyAuth')
+ ->will($this->returnValue($this->proxyAuth));
+ $this->config->expects($this->any())
+ ->method('getProxyPort')
+ ->will($this->returnValue($this->proxyPort));
+ $this->fetcher = new FaviconFetcher($this->fileFactory, $this->config);
+
+ $faviconPath = "/owncloud/core/img/favicon.png";
+ $html = $this->getFaviconHTML($faviconPath);
+
+ $url = 'http://google.com';
+ $pageMock = $this->getFileMock($html);
+ $pngMock = $this->getFileMock($this->png);
+
+ $this->fileFactory->expects($this->at(0))
+ ->method('getFile')
+ ->with($this->equalTo('http://google.com'))
+ ->will($this->returnValue($pageMock));
+
+ $this->fileFactory->expects($this->at(1))
+ ->method('getFile')
+ ->with($this->equalTo(
+ 'http://google.com/owncloud/core/img/favicon.png'),
+ $this->equalTo(10),
+ $this->equalTo(5),
+ $this->equalTo(null),
+ $this->equalTo(null),
+ $this->equalTo(false),
+ $this->equalTo($this->proxyHost),
+ $this->equalTo($this->proxyPort),
+ $this->equalTo($this->proxyAuth))
+ ->will($this->returnValue($pngMock));
+
+ $favicon = $this->fetcher->fetch($url);
+
+ $this->assertEquals('http://google.com/owncloud/core/img/favicon.png', $favicon);
+ }
+
+
+ public function testNoProxySettingsAreUsed() {
+ $faviconPath = "/owncloud/core/img/favicon.png";
+ $html = $this->getFaviconHTML($faviconPath);
+
+ $url = 'http://google.com';
+ $pageMock = $this->getFileMock($html);
+ $pngMock = $this->getFileMock($this->png);
+
+ $this->fileFactory->expects($this->at(0))
+ ->method('getFile')
+ ->with($this->equalTo('http://google.com'))
+ ->will($this->returnValue($pageMock));
+
+ $this->fileFactory->expects($this->at(1))
+ ->method('getFile')
+ ->with($this->equalTo(
+ 'http://google.com/owncloud/core/img/favicon.png'),
+ $this->equalTo(10),
+ $this->equalTo(5),
+ $this->equalTo(null),
+ $this->equalTo(null),
+ $this->equalTo(false),
+ $this->equalTo(null),
+ $this->equalTo(null),
+ $this->equalTo(null))
+ ->will($this->returnValue($pngMock));
+
+ $favicon = $this->fetcher->fetch($url);
+
+ $this->assertEquals('http://google.com/owncloud/core/img/favicon.png', $favicon);
+ }
+
+
public function testFetchFaviconFaviconDotIcoHttp(){
$url = ' sub.google.com ';
$mock = $this->getFileMock($this->png);