Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #20 from drago-draganov/dev
Browse files Browse the repository at this point in the history
Category is omitted when reading RSS Item.
  • Loading branch information
jimmyca15 committed Nov 10, 2017
2 parents ff35f7a + 343e322 commit d085533
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Rss/RssConstants.cs
Expand Up @@ -13,5 +13,6 @@ static class RssConstants
public const string IsPermaLink = "isPermaLink";
public const string Length = "length";
public const string Type = "type";
public const string Domain = "domain";
}
}
9 changes: 8 additions & 1 deletion src/Rss/RssFormatter.cs
Expand Up @@ -150,7 +150,14 @@ public virtual ISyndicationContent CreateContent(ISyndicationCategory category)
throw new FormatException("Invalid category name");
}

return new SyndicationContent(RssElementNames.Category, category.Name);
var content = new SyndicationContent(RssElementNames.Category, category.Name);

if (category.Scheme != null)
{
content.AddAttribute(new SyndicationAttribute(RssConstants.Domain, category.Scheme));
}

return content;
}

public virtual ISyndicationContent CreateContent(ISyndicationPerson person)
Expand Down
5 changes: 4 additions & 1 deletion src/Rss/RssParser.cs
Expand Up @@ -141,6 +141,7 @@ public virtual ISyndicationItem CreateItem(ISyndicationContent content)
//
// Category
case RssElementNames.Category:
item.AddCategory(CreateCategory(field));
break;

//
Expand Down Expand Up @@ -331,7 +332,9 @@ public virtual ISyndicationCategory CreateCategory(ISyndicationContent content)
throw new FormatException("Invalid Rss category name");
}

return new SyndicationCategory(content.Value);
return new SyndicationCategory(content.Value) {
Scheme = content.Attributes.GetRss(RssConstants.Domain)
};
}

private static ISyndicationContent ReadSyndicationContent(XmlReader reader)
Expand Down
46 changes: 44 additions & 2 deletions tests/RssReader.cs
Expand Up @@ -42,10 +42,9 @@ public async Task ReadItemAsContent()

// Read as content
ISyndicationContent content = await reader.ReadContent();
//ISyndicationItem item = await reader.ReadItem();

var fields = content.Fields.ToArray();
Assert.Equal(fields.Length, 6);
Assert.True(fields.Length >= 6);

Assert.Equal("title", fields[0].Name);
Assert.False(string.IsNullOrEmpty(fields[0].Value));
Expand All @@ -71,6 +70,49 @@ public async Task ReadItemAsContent()
}
}

[Fact]
public async Task ReadCategory()
{
using (var xmlReader = XmlReader.Create(@"..\..\..\TestFeeds\rss20.xml", new XmlReaderSettings() { Async = true }))
{
var reader = new RssFeedReader(xmlReader);

while (await reader.Read())
{
if (reader.ElementType == SyndicationElementType.Category)
{
ISyndicationCategory category = await reader.ReadCategory();

Assert.True(category.Name == "Newspapers");
Assert.True(category.Scheme == "http://example.com/news");
}
}
}
}

[Fact]
public async Task ReadItemCategory()
{
using (var xmlReader = XmlReader.Create(@"..\..\..\TestFeeds\rss20.xml", new XmlReaderSettings() { Async = true }))
{
var reader = new RssFeedReader(xmlReader);

while (await reader.Read())
{
if (reader.ElementType == SyndicationElementType.Item)
{
ISyndicationItem item = await reader.ReadItem();

foreach (var c in item.Categories)
{
Assert.True(c.Name == "Newspapers");
Assert.True(c.Scheme == null || c.Scheme == "http://example.com/news/item");
}
}
}
}
}

[Fact]
public async Task CountItems()
{
Expand Down
15 changes: 12 additions & 3 deletions tests/RssWriter.cs
Expand Up @@ -35,16 +35,23 @@ public async Task WriteCategory()
{
var sw = new StringWriterWithEncoding(Encoding.UTF8);

var cat1 = new SyndicationCategory("Test Category 1") {
Scheme = "http://example.com/test"
};

var cat2 = new SyndicationCategory("Test Category 2");

using (var xmlWriter = XmlWriter.Create(sw))
{
var writer = new RssFeedWriter(xmlWriter);

await writer.Write(new SyndicationCategory("Test Category"));
await writer.Write(cat1);
await writer.Write(cat2);
await writer.Flush();
}

string res = sw.ToString();
Assert.True(res == "<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><channel><category>Test Category</category></channel></rss>");
Assert.True(res == $"<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><channel><category domain=\"{cat1.Scheme}\">{cat1.Name}</category><category>{cat2.Name}</category></channel></rss>");
}

[Fact]
Expand Down Expand Up @@ -165,6 +172,8 @@ public async Task WriteItem()

item.AddContributor(new SyndicationPerson("person", "person@email.com"));

item.AddCategory(new SyndicationCategory("Test Category"));

//
// Write
var sw = new StringWriterWithEncoding(Encoding.UTF8);
Expand All @@ -178,7 +187,7 @@ public async Task WriteItem()
}

string res = sw.ToString();
Assert.True(res == $"<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><channel><item><title>First item on ItemWriter</title><link>{url}</link><enclosure url=\"{url}\" length=\"4123\" type=\"audio/mpeg\" /><comments>{url}</comments><source url=\"{url}\">Anonymous Blog</source><guid>{item.Id}</guid><description>Brief description of an item</description><author>person@email.com</author><pubDate>{item.Published.ToRfc1123()}</pubDate></item></channel></rss>", res);
Assert.True(res == $"<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><channel><item><title>First item on ItemWriter</title><link>{url}</link><enclosure url=\"{url}\" length=\"4123\" type=\"audio/mpeg\" /><comments>{url}</comments><source url=\"{url}\">Anonymous Blog</source><guid>{item.Id}</guid><description>Brief description of an item</description><author>person@email.com</author><category>Test Category</category><pubDate>{item.Published.ToRfc1123()}</pubDate></item></channel></rss>", res);
}

[Fact]
Expand Down
3 changes: 3 additions & 0 deletions tests/TestFeeds/rss20.xml
Expand Up @@ -5,6 +5,7 @@
<title asd="123"><![CDATA[Lorem ipsum feed for an interval of 1 minutes]]></title>
<description><![CDATA[This is a constantly updating lorem ipsum feed]]></description>
<link length="123" type="testType" >http://example.com/</link>
<category domain="http://example.com/news">Newspapers</category>
<image>
<url>http://2.bp.blogspot.com/-NA5Jb-64eUg/URx8CSdcj_I/AAAAAAAAAUo/eCx0irI0rq0/s1600/bg_Microsoft_logo3-20120824073001907469-620x349.jpg</url>
<title>Microsoft News</title>
Expand All @@ -24,6 +25,7 @@
<guid isPermaLink="true">http://example.com/test/1499372700</guid>
<dc:creator><![CDATA[John Smith]]></dc:creator>
<pubDate>Thu, 06 Jul 2017 20:25:00 GMT</pubDate>
<category domain="http://example.com/news/item">Newspapers</category>
</item>
<item>
<title><![CDATA[Lorem ipsum 2017-07-06T20:24:00+00:00]]></title>
Expand All @@ -32,6 +34,7 @@
<guid isPermaLink="true">http://example.com/test/1499372640</guid>
<dc:creator><![CDATA[John Smith]]></dc:creator>
<pubDate>Thu, 06 Jul 2017 20:24:00 GMT</pubDate>
<category>Newspapers</category>
</item>
<item>
<title><![CDATA[Lorem ipsum 2017-07-06T20:23:00+00:00]]></title>
Expand Down

0 comments on commit d085533

Please sign in to comment.