diff --git a/lib/Feed.php b/lib/Feed.php index dffbccb..81256a6 100644 --- a/lib/Feed.php +++ b/lib/Feed.php @@ -47,6 +47,17 @@ class Feed { return $out; } + public static function discoverAll(string $url, string $username = '', string $password = ''): array { + // fetch the candidate feed + $f = self::download($url, "", "", $username, $password); + if ($f->reader->detectFormat($f->getContent())) { + // if the prospective URL is a feed, use it + return [$url]; + } else { + return $f->reader->find($f->getUrl(), $f->getContent()); + } + } + public function __construct(int $feedID = null, string $url, string $lastModified = '', string $etag = '', string $username = '', string $password = '', bool $scrape = false) { // fetch the feed $this->resource = self::download($url, $lastModified, $etag, $username, $password); diff --git a/tests/cases/Feed/TestFeed.php b/tests/cases/Feed/TestFeed.php index a5036d2..f9a422e 100644 --- a/tests/cases/Feed/TestFeed.php +++ b/tests/cases/Feed/TestFeed.php @@ -150,6 +150,27 @@ class TestFeed extends \JKingWeb\Arsse\Test\AbstractTest { Feed::discover($this->base."Discovery/Invalid"); } + public function testDiscoverAMissingFeed(): void { + $this->assertException("invalidUrl", "Feed"); + Feed::discover($this->base."Discovery/Missing"); + } + + public function testDiscoverMultipleFeedsSuccessfully(): void { + $exp1 = [$this->base."Discovery/Feed", $this->base."Discovery/Missing"]; + $exp2 = [$this->base."Discovery/Feed"]; + $this->assertSame($exp1, Feed::discoverAll($this->base."Discovery/Valid")); + $this->assertSame($exp2, Feed::discoverAll($this->base."Discovery/Feed")); + } + + public function testDiscoverMultipleFeedsUnsuccessfully(): void { + $this->assertSame([], Feed::discoverAll($this->base."Discovery/Invalid")); + } + + public function testDiscoverMultipleMissingFeeds(): void { + $this->assertException("invalidUrl", "Feed"); + Feed::discoverAll($this->base."Discovery/Missing"); + } + public function testParseEntityExpansionAttack(): void { $this->assertException("xmlEntity", "Feed"); new Feed(null, $this->base."Parsing/XEEAttack"); diff --git a/tests/docroot/Feed/Discovery/Missing.php b/tests/docroot/Feed/Discovery/Missing.php new file mode 100644 index 0000000..666eb03 --- /dev/null +++ b/tests/docroot/Feed/Discovery/Missing.php @@ -0,0 +1,3 @@ + 404, +]; diff --git a/tests/docroot/Feed/Discovery/Valid.php b/tests/docroot/Feed/Discovery/Valid.php index 9f34f71..af7b9c1 100644 --- a/tests/docroot/Feed/Discovery/Valid.php +++ b/tests/docroot/Feed/Discovery/Valid.php @@ -4,6 +4,7 @@ Example article + MESSAGE_BODY ];