Browse Source

Add ability to discover multiple feeds

rpm
J. King 3 years ago
parent
commit
669e17a1f6
  1. 11
      lib/Feed.php
  2. 21
      tests/cases/Feed/TestFeed.php
  3. 3
      tests/docroot/Feed/Discovery/Missing.php
  4. 1
      tests/docroot/Feed/Discovery/Valid.php

11
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);

21
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");

3
tests/docroot/Feed/Discovery/Missing.php

@ -0,0 +1,3 @@
<?php return [
'code' => 404,
];

1
tests/docroot/Feed/Discovery/Valid.php

@ -4,6 +4,7 @@
<html>
<title>Example article</title>
<link rel="alternate" type="application/rss+xml" href="http://localhost:8000/Feed/Discovery/Feed">
<link rel="alternate" type="application/rss+xml" href="http://localhost:8000/Feed/Discovery/Missing">
</html>
MESSAGE_BODY
];

Loading…
Cancel
Save