parser_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package rdf
  5. import (
  6. "bytes"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/miniflux/miniflux/errors"
  11. )
  12. func TestParseRDFSample(t *testing.T) {
  13. data := `
  14. <?xml version="1.0"?>
  15. <rdf:RDF
  16. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  17. xmlns="http://purl.org/rss/1.0/"
  18. >
  19. <channel rdf:about="http://www.xml.com/xml/news.rss">
  20. <title>XML.com</title>
  21. <link>http://xml.com/pub</link>
  22. <description>
  23. XML.com features a rich mix of information and services
  24. for the XML community.
  25. </description>
  26. <image rdf:resource="http://xml.com/universal/images/xml_tiny.gif" />
  27. <items>
  28. <rdf:Seq>
  29. <rdf:li resource="http://xml.com/pub/2000/08/09/xslt/xslt.html" />
  30. <rdf:li resource="http://xml.com/pub/2000/08/09/rdfdb/index.html" />
  31. </rdf:Seq>
  32. </items>
  33. <textinput rdf:resource="http://search.xml.com" />
  34. </channel>
  35. <image rdf:about="http://xml.com/universal/images/xml_tiny.gif">
  36. <title>XML.com</title>
  37. <link>http://www.xml.com</link>
  38. <url>http://xml.com/universal/images/xml_tiny.gif</url>
  39. </image>
  40. <item rdf:about="http://xml.com/pub/2000/08/09/xslt/xslt.html">
  41. <title>Processing Inclusions with XSLT</title>
  42. <link>http://xml.com/pub/2000/08/09/xslt/xslt.html</link>
  43. <description>
  44. Processing document inclusions with general XML tools can be
  45. problematic. This article proposes a way of preserving inclusion
  46. information through SAX-based processing.
  47. </description>
  48. </item>
  49. <item rdf:about="http://xml.com/pub/2000/08/09/rdfdb/index.html">
  50. <title>Putting RDF to Work</title>
  51. <link>http://xml.com/pub/2000/08/09/rdfdb/index.html</link>
  52. <description>
  53. Tool and API support for the Resource Description Framework
  54. is slowly coming of age. Edd Dumbill takes a look at RDFDB,
  55. one of the most exciting new RDF toolkits.
  56. </description>
  57. </item>
  58. <textinput rdf:about="http://search.xml.com">
  59. <title>Search XML.com</title>
  60. <description>Search XML.com's XML collection</description>
  61. <name>s</name>
  62. <link>http://search.xml.com</link>
  63. </textinput>
  64. </rdf:RDF>`
  65. feed, err := Parse(bytes.NewBufferString(data))
  66. if err != nil {
  67. t.Error(err)
  68. }
  69. if feed.Title != "XML.com" {
  70. t.Errorf("Incorrect title, got: %s", feed.Title)
  71. }
  72. if feed.FeedURL != "" {
  73. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  74. }
  75. if feed.SiteURL != "http://xml.com/pub" {
  76. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  77. }
  78. if len(feed.Entries) != 2 {
  79. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  80. }
  81. if feed.Entries[1].Hash != "8aaeee5d3ab50351422fbded41078ee88c73bf1441085b16a8c09fd90a7db321" {
  82. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  83. }
  84. if feed.Entries[1].URL != "http://xml.com/pub/2000/08/09/rdfdb/index.html" {
  85. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  86. }
  87. if feed.Entries[1].Title != "Putting RDF to Work" {
  88. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  89. }
  90. if strings.HasSuffix(feed.Entries[1].Content, "Tool and API support") {
  91. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  92. }
  93. if feed.Entries[1].Date.Year() != time.Now().Year() {
  94. t.Errorf("Entry date should not be empty")
  95. }
  96. }
  97. func TestParseRDFSampleWithDublinCore(t *testing.T) {
  98. data := `<?xml version="1.0" encoding="utf-8"?>
  99. <rdf:RDF
  100. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  101. xmlns:dc="http://purl.org/dc/elements/1.1/"
  102. xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  103. xmlns:co="http://purl.org/rss/1.0/modules/company/"
  104. xmlns:ti="http://purl.org/rss/1.0/modules/textinput/"
  105. xmlns="http://purl.org/rss/1.0/"
  106. >
  107. <channel rdf:about="http://meerkat.oreillynet.com/?_fl=rss1.0">
  108. <title>Meerkat</title>
  109. <link>http://meerkat.oreillynet.com</link>
  110. <description>Meerkat: An Open Wire Service</description>
  111. <dc:publisher>The O'Reilly Network</dc:publisher>
  112. <dc:creator>Rael Dornfest (mailto:rael@oreilly.com)</dc:creator>
  113. <dc:rights>Copyright &#169; 2000 O'Reilly &amp; Associates, Inc.</dc:rights>
  114. <dc:date>2000-01-01T12:00+00:00</dc:date>
  115. <sy:updatePeriod>hourly</sy:updatePeriod>
  116. <sy:updateFrequency>2</sy:updateFrequency>
  117. <sy:updateBase>2000-01-01T12:00+00:00</sy:updateBase>
  118. <image rdf:resource="http://meerkat.oreillynet.com/icons/meerkat-powered.jpg" />
  119. <items>
  120. <rdf:Seq>
  121. <rdf:li resource="http://c.moreover.com/click/here.pl?r123" />
  122. </rdf:Seq>
  123. </items>
  124. <textinput rdf:resource="http://meerkat.oreillynet.com" />
  125. </channel>
  126. <image rdf:about="http://meerkat.oreillynet.com/icons/meerkat-powered.jpg">
  127. <title>Meerkat Powered!</title>
  128. <url>http://meerkat.oreillynet.com/icons/meerkat-powered.jpg</url>
  129. <link>http://meerkat.oreillynet.com</link>
  130. </image>
  131. <item rdf:about="http://c.moreover.com/click/here.pl?r123">
  132. <title>XML: A Disruptive Technology</title>
  133. <link>http://c.moreover.com/click/here.pl?r123</link>
  134. <dc:description>
  135. XML is placing increasingly heavy loads on the existing technical
  136. infrastructure of the Internet.
  137. </dc:description>
  138. <dc:publisher>The O'Reilly Network</dc:publisher>
  139. <dc:creator>Simon St.Laurent (mailto:simonstl@simonstl.com)</dc:creator>
  140. <dc:rights>Copyright &#169; 2000 O'Reilly &amp; Associates, Inc.</dc:rights>
  141. <dc:subject>XML</dc:subject>
  142. <co:name>XML.com</co:name>
  143. <co:market>NASDAQ</co:market>
  144. <co:symbol>XML</co:symbol>
  145. </item>
  146. <textinput rdf:about="http://meerkat.oreillynet.com">
  147. <title>Search Meerkat</title>
  148. <description>Search Meerkat's RSS Database...</description>
  149. <name>s</name>
  150. <link>http://meerkat.oreillynet.com/</link>
  151. <ti:function>search</ti:function>
  152. <ti:inputType>regex</ti:inputType>
  153. </textinput>
  154. </rdf:RDF>`
  155. feed, err := Parse(bytes.NewBufferString(data))
  156. if err != nil {
  157. t.Error(err)
  158. }
  159. if feed.Title != "Meerkat" {
  160. t.Errorf("Incorrect title, got: %s", feed.Title)
  161. }
  162. if feed.FeedURL != "" {
  163. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  164. }
  165. if feed.SiteURL != "http://meerkat.oreillynet.com" {
  166. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  167. }
  168. if len(feed.Entries) != 1 {
  169. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  170. }
  171. if feed.Entries[0].Hash != "fa4ef7c300b175ca66f92f226b5dba5caa2a9619f031101bf56e5b884b02cd97" {
  172. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  173. }
  174. if feed.Entries[0].URL != "http://c.moreover.com/click/here.pl?r123" {
  175. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  176. }
  177. if feed.Entries[0].Title != "XML: A Disruptive Technology" {
  178. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  179. }
  180. if strings.HasSuffix(feed.Entries[0].Content, "XML is placing increasingly") {
  181. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  182. }
  183. if feed.Entries[0].Author != "Simon St.Laurent (mailto:simonstl@simonstl.com)" {
  184. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  185. }
  186. }
  187. func TestParseItemWithOnlyFeedAuthor(t *testing.T) {
  188. data := `<?xml version="1.0" encoding="utf-8"?>
  189. <rdf:RDF
  190. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  191. xmlns:dc="http://purl.org/dc/elements/1.1/"
  192. xmlns="http://purl.org/rss/1.0/"
  193. >
  194. <channel rdf:about="http://meerkat.oreillynet.com/?_fl=rss1.0">
  195. <title>Meerkat</title>
  196. <link>http://meerkat.oreillynet.com</link>
  197. <dc:creator>Rael Dornfest (mailto:rael@oreilly.com)</dc:creator>
  198. </channel>
  199. <item rdf:about="http://c.moreover.com/click/here.pl?r123">
  200. <title>XML: A Disruptive Technology</title>
  201. <link>http://c.moreover.com/click/here.pl?r123</link>
  202. <dc:description>
  203. XML is placing increasingly heavy loads on the existing technical
  204. infrastructure of the Internet.
  205. </dc:description>
  206. </item>
  207. </rdf:RDF>`
  208. feed, err := Parse(bytes.NewBufferString(data))
  209. if err != nil {
  210. t.Error(err)
  211. }
  212. if feed.Entries[0].Author != "Rael Dornfest (mailto:rael@oreilly.com)" {
  213. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  214. }
  215. }
  216. func TestParseItemWithoutLink(t *testing.T) {
  217. data := `<?xml version="1.0" encoding="utf-8"?>
  218. <rdf:RDF
  219. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  220. xmlns="http://purl.org/rss/1.0/"
  221. >
  222. <channel rdf:about="http://meerkat.oreillynet.com/?_fl=rss1.0">
  223. <title>Meerkat</title>
  224. <link>http://meerkat.oreillynet.com</link>
  225. </channel>
  226. <item rdf:about="http://c.moreover.com/click/here.pl?r123">
  227. <title>Title</title>
  228. <description>Test</description>
  229. </item>
  230. </rdf:RDF>`
  231. feed, err := Parse(bytes.NewBufferString(data))
  232. if err != nil {
  233. t.Error(err)
  234. }
  235. if feed.Entries[0].Hash != "37f5223ebd58639aa62a49afbb61df960efb7dc5db5181dfb3cedd9a49ad34c6" {
  236. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  237. }
  238. if feed.Entries[0].URL != "http://meerkat.oreillynet.com" {
  239. t.Errorf("Incorrect entry url, got: %s", feed.Entries[0].URL)
  240. }
  241. }
  242. func TestParseInvalidXml(t *testing.T) {
  243. data := `garbage`
  244. _, err := Parse(bytes.NewBufferString(data))
  245. if err == nil {
  246. t.Error("Parse should returns an error")
  247. }
  248. if _, ok := err.(errors.LocalizedError); !ok {
  249. t.Error("The error returned must be a LocalizedError")
  250. }
  251. }