parser_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 feed
  5. import (
  6. "bytes"
  7. "testing"
  8. )
  9. func TestDetectRDF(t *testing.T) {
  10. data := `<?xml version="1.0"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://my.netscape.com/rdf/simple/0.9/"></rdf:RDF>`
  11. format := DetectFeedFormat(bytes.NewBufferString(data))
  12. if format != FormatRDF {
  13. t.Errorf("Wrong format detected: %s instead of %s", format, FormatRDF)
  14. }
  15. }
  16. func TestDetectRSS(t *testing.T) {
  17. data := `<?xml version="1.0"?><rss version="2.0"><channel></channel></rss>`
  18. format := DetectFeedFormat(bytes.NewBufferString(data))
  19. if format != FormatRSS {
  20. t.Errorf("Wrong format detected: %s instead of %s", format, FormatRSS)
  21. }
  22. }
  23. func TestDetectAtom(t *testing.T) {
  24. data := `<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"></feed>`
  25. format := DetectFeedFormat(bytes.NewBufferString(data))
  26. if format != FormatAtom {
  27. t.Errorf("Wrong format detected: %s instead of %s", format, FormatAtom)
  28. }
  29. }
  30. func TestDetectAtomWithISOCharset(t *testing.T) {
  31. data := `<?xml version="1.0" encoding="ISO-8859-15"?><feed xmlns="http://www.w3.org/2005/Atom"></feed>`
  32. format := DetectFeedFormat(bytes.NewBufferString(data))
  33. if format != FormatAtom {
  34. t.Errorf("Wrong format detected: %s instead of %s", format, FormatAtom)
  35. }
  36. }
  37. func TestDetectJSON(t *testing.T) {
  38. data := `
  39. {
  40. "version" : "https://jsonfeed.org/version/1",
  41. "title" : "Example"
  42. }
  43. `
  44. format := DetectFeedFormat(bytes.NewBufferString(data))
  45. if format != FormatJSON {
  46. t.Errorf("Wrong format detected: %s instead of %s", format, FormatJSON)
  47. }
  48. }
  49. func TestDetectUnknown(t *testing.T) {
  50. data := `
  51. <!DOCTYPE html> <html> </html>
  52. `
  53. format := DetectFeedFormat(bytes.NewBufferString(data))
  54. if format != FormatUnknown {
  55. t.Errorf("Wrong format detected: %s instead of %s", format, FormatUnknown)
  56. }
  57. }
  58. func TestParseAtom(t *testing.T) {
  59. data := `<?xml version="1.0" encoding="utf-8"?>
  60. <feed xmlns="http://www.w3.org/2005/Atom">
  61. <title>Example Feed</title>
  62. <link href="http://example.org/"/>
  63. <updated>2003-12-13T18:30:02Z</updated>
  64. <author>
  65. <name>John Doe</name>
  66. </author>
  67. <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  68. <entry>
  69. <title>Atom-Powered Robots Run Amok</title>
  70. <link href="http://example.org/2003/12/13/atom03"/>
  71. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  72. <updated>2003-12-13T18:30:02Z</updated>
  73. <summary>Some text.</summary>
  74. </entry>
  75. </feed>`
  76. feed, err := parseFeed(bytes.NewBufferString(data))
  77. if err != nil {
  78. t.Error(err)
  79. }
  80. if feed.Title != "Example Feed" {
  81. t.Errorf("Incorrect title, got: %s", feed.Title)
  82. }
  83. }
  84. func TestParseRSS(t *testing.T) {
  85. data := `<?xml version="1.0"?>
  86. <rss version="2.0">
  87. <channel>
  88. <title>Liftoff News</title>
  89. <link>http://liftoff.msfc.nasa.gov/</link>
  90. <item>
  91. <title>Star City</title>
  92. <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
  93. <description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &lt;a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm"&gt;Star City&lt;/a&gt;.</description>
  94. <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
  95. <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
  96. </item>
  97. </channel>
  98. </rss>`
  99. feed, err := parseFeed(bytes.NewBufferString(data))
  100. if err != nil {
  101. t.Error(err)
  102. }
  103. if feed.Title != "Liftoff News" {
  104. t.Errorf("Incorrect title, got: %s", feed.Title)
  105. }
  106. }
  107. func TestParseRDF(t *testing.T) {
  108. data := `<?xml version="1.0" encoding="utf-8"?>
  109. <rdf:RDF
  110. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  111. xmlns="http://purl.org/rss/1.0/"
  112. >
  113. <channel>
  114. <title>RDF Example</title>
  115. <link>http://example.org/</link>
  116. </channel>
  117. <item>
  118. <title>Title</title>
  119. <link>http://example.org/item</link>
  120. <description>Test</description>
  121. </item>
  122. </rdf:RDF>`
  123. feed, err := parseFeed(bytes.NewBufferString(data))
  124. if err != nil {
  125. t.Error(err)
  126. }
  127. if feed.Title != "RDF Example" {
  128. t.Errorf("Incorrect title, got: %s", feed.Title)
  129. }
  130. }
  131. func TestParseJson(t *testing.T) {
  132. data := `{
  133. "version": "https://jsonfeed.org/version/1",
  134. "title": "My Example Feed",
  135. "home_page_url": "https://example.org/",
  136. "feed_url": "https://example.org/feed.json",
  137. "items": [
  138. {
  139. "id": "2",
  140. "content_text": "This is a second item.",
  141. "url": "https://example.org/second-item"
  142. },
  143. {
  144. "id": "1",
  145. "content_html": "<p>Hello, world!</p>",
  146. "url": "https://example.org/initial-post"
  147. }
  148. ]
  149. }`
  150. feed, err := parseFeed(bytes.NewBufferString(data))
  151. if err != nil {
  152. t.Error(err)
  153. }
  154. if feed.Title != "My Example Feed" {
  155. t.Errorf("Incorrect title, got: %s", feed.Title)
  156. }
  157. }
  158. func TestParseUnknownFeed(t *testing.T) {
  159. data := `
  160. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  161. <html xmlns="http://www.w3.org/1999/xhtml">
  162. <head>
  163. <title>Title of document</title>
  164. </head>
  165. <body>
  166. some content
  167. </body>
  168. </html>
  169. `
  170. _, err := parseFeed(bytes.NewBufferString(data))
  171. if err == nil {
  172. t.Error("ParseFeed must returns an error")
  173. }
  174. }