From 343e322a0afccded8f1d29ea77e2da601f155ff0 Mon Sep 17 00:00:00 2001 From: Drago Draganov Date: Fri, 10 Nov 2017 15:03:13 -0800 Subject: [PATCH] Category is omitted in RSS Item. --- src/Rss/RssConstants.cs | 1 + src/Rss/RssFormatter.cs | 9 +++++++- src/Rss/RssParser.cs | 5 ++++- tests/RssReader.cs | 46 +++++++++++++++++++++++++++++++++++++-- tests/RssWriter.cs | 15 ++++++++++--- tests/TestFeeds/rss20.xml | 3 +++ 6 files changed, 72 insertions(+), 7 deletions(-) diff --git a/src/Rss/RssConstants.cs b/src/Rss/RssConstants.cs index 864166e..28c2008 100644 --- a/src/Rss/RssConstants.cs +++ b/src/Rss/RssConstants.cs @@ -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"; } } diff --git a/src/Rss/RssFormatter.cs b/src/Rss/RssFormatter.cs index 6a70f79..4cf84e2 100644 --- a/src/Rss/RssFormatter.cs +++ b/src/Rss/RssFormatter.cs @@ -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) diff --git a/src/Rss/RssParser.cs b/src/Rss/RssParser.cs index 64821b6..b553058 100644 --- a/src/Rss/RssParser.cs +++ b/src/Rss/RssParser.cs @@ -141,6 +141,7 @@ public virtual ISyndicationItem CreateItem(ISyndicationContent content) // // Category case RssElementNames.Category: + item.AddCategory(CreateCategory(field)); break; // @@ -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) diff --git a/tests/RssReader.cs b/tests/RssReader.cs index 1bbda50..6882d73 100644 --- a/tests/RssReader.cs +++ b/tests/RssReader.cs @@ -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)); @@ -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() { diff --git a/tests/RssWriter.cs b/tests/RssWriter.cs index a3f74fe..75b87ff 100644 --- a/tests/RssWriter.cs +++ b/tests/RssWriter.cs @@ -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 == "Test Category"); + Assert.True(res == $"{cat1.Name}{cat2.Name}"); } [Fact] @@ -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); @@ -178,7 +187,7 @@ public async Task WriteItem() } string res = sw.ToString(); - Assert.True(res == $"First item on ItemWriter{url}{url}Anonymous Blog{item.Id}Brief description of an itemperson@email.com{item.Published.ToRfc1123()}", res); + Assert.True(res == $"First item on ItemWriter{url}{url}Anonymous Blog{item.Id}Brief description of an itemperson@email.comTest Category{item.Published.ToRfc1123()}", res); } [Fact] diff --git a/tests/TestFeeds/rss20.xml b/tests/TestFeeds/rss20.xml index fbba0c0..dfc6957 100644 --- a/tests/TestFeeds/rss20.xml +++ b/tests/TestFeeds/rss20.xml @@ -5,6 +5,7 @@ <![CDATA[Lorem ipsum feed for an interval of 1 minutes]]> http://example.com/ + Newspapers http://2.bp.blogspot.com/-NA5Jb-64eUg/URx8CSdcj_I/AAAAAAAAAUo/eCx0irI0rq0/s1600/bg_Microsoft_logo3-20120824073001907469-620x349.jpg Microsoft News @@ -24,6 +25,7 @@ http://example.com/test/1499372700 Thu, 06 Jul 2017 20:25:00 GMT + Newspapers <![CDATA[Lorem ipsum 2017-07-06T20:24:00+00:00]]> @@ -32,6 +34,7 @@ http://example.com/test/1499372640 Thu, 06 Jul 2017 20:24:00 GMT + Newspapers <![CDATA[Lorem ipsum 2017-07-06T20:23:00+00:00]]>