Skip to content

Commit

Permalink
Improve handling of feeds when discover encounters feel links without…
Browse files Browse the repository at this point in the history
… a protocol (#405)

Signed-off-by: Accalia <Accalia@Elementia.me>

Signed-off-by: Accalia <Accalia@Elementia.me>
Co-authored-by: alexdebril <alexdebril@users.noreply.github.com>
  • Loading branch information
AccaliaDeElementia and alexdebril committed Oct 26, 2022
1 parent 63dd6ea commit 5808bc2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/FeedIo/Explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ protected function extractFeeds(string $html): array
$feeds = [];
foreach ($links as $link) {
if ($this->isFeedLink($link)) {
$feeds[] = $link->getAttribute('href');
$href = $link->getAttribute('href');
if (strpos($href, '//') === 0) {
// Link href is protocol-less, Implies feed supports http
// and https. Consumers will often assume that feed url
// includes protocol, so we will assign a protocol before
// returning
$href = 'https:' . $href;
}
$feeds[] = $href;
}
}

Expand Down
34 changes: 31 additions & 3 deletions tests/FeedIo/ExplorerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@ class ExplorerTest extends TestCase
protected $object;

public function setUp(): void
{

}

protected function createTestObject(string $htmlPath)
{
$this->object = new Explorer(
$this->getClientMock(),
$this->getClientMock($htmlPath),
new NullLogger()
);
}

/**
* @return \FeedIo\Adapter\ClientInterface
*/
protected function getClientMock()
protected function getClientMock(string $htmlPath)
{
$html = file_get_contents(dirname(__FILE__)."/../samples/discovery.html");
$html = file_get_contents(dirname(__FILE__) . $htmlPath);
$client = $this->createMock('FeedIo\Adapter\ClientInterface');
$response = $this->createMock('FeedIo\Adapter\ResponseInterface');
$response->expects($this->any())->method('getBody')->will($this->returnValue($html));
Expand All @@ -52,8 +57,31 @@ protected function getClientMock()
*/
public function testDiscover()
{
$this->createTestObject("/../samples/discovery.html");
$feeds = $this->object->discover('https://exmple.org/feed.atom');

$this->assertEquals(['http://example.org/feed.xml', 'http://example.org/comments.xml'], $feeds);
}

/**
* @covers \FeedIo\Reader::addParser
*/
public function testDiscoverHttps()
{
$this->createTestObject("/../samples/discovery-https.html");
$feeds = $this->object->discover('https://exmple.org/feed.atom');

$this->assertEquals(['https://example.org/feed.xml', 'https://example.org/comments.xml'], $feeds);
}

/**
* @covers \FeedIo\Reader::addParser
*/
public function testDiscoverProtocolless()
{
$this->createTestObject("/../samples/discovery-mixed.html");
$feeds = $this->object->discover('https://exmple.org/feed.atom');

$this->assertEquals(['https://example.org/feed.xml', 'https://example.org/comments.xml'], $feeds);
}
}
16 changes: 16 additions & 0 deletions tests/samples/discovery-dualprotocol.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>

<link rel="shortcut icon" href="/favicon.ico">
<link rel="search" type="application/opensearchdescription+xml" href="/improvedsearch.src" title="Add search">
<link rel="alternate" type="application/atom+xml" href="//example.org/feed.xml" title="Main feed">
<link rel="alternate" type="application/atom+xml" href="//example.org/comments.xml" title="Comments feed">

<link rel="canonical" href="//example.org/index.php">
<link rel="shorturl" href="//example.org/index">
<link rel="alternate" href="//example.org/index" hreflang="x-default">

<link rel="stylesheet" type="text/css" href="//example.org/styles/home.css" media="screen">
</head>
<body></body>
</html>
16 changes: 16 additions & 0 deletions tests/samples/discovery-https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>

<link rel="shortcut icon" href="/favicon.ico">
<link rel="search" type="application/opensearchdescription+xml" href="/improvedsearch.src" title="Add search">
<link rel="alternate" type="application/atom+xml" href="https://example.org/feed.xml" title="Main feed">
<link rel="alternate" type="application/atom+xml" href="https://example.org/comments.xml" title="Comments feed">

<link rel="canonical" href="https://example.org/index.php">
<link rel="shorturl" href="https://example.org/index">
<link rel="alternate" href="https://example.org/index" hreflang="x-default">

<link rel="stylesheet" type="text/css" href="https://example.org/styles/home.css" media="screen">
</head>
<body></body>
</html>

0 comments on commit 5808bc2

Please sign in to comment.