parser_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 TestDetectRSS(t *testing.T) {
  10. data := `<?xml version="1.0"?><rss version="2.0"><channel></channel></rss>`
  11. format := DetectFeedFormat(bytes.NewBufferString(data))
  12. if format != FormatRss {
  13. t.Errorf("Wrong format detected: %s instead of %s", format, FormatRss)
  14. }
  15. }
  16. func TestDetectAtom(t *testing.T) {
  17. data := `<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"></feed>`
  18. format := DetectFeedFormat(bytes.NewBufferString(data))
  19. if format != FormatAtom {
  20. t.Errorf("Wrong format detected: %s instead of %s", format, FormatAtom)
  21. }
  22. }
  23. func TestDetectAtomWithISOCharset(t *testing.T) {
  24. data := `<?xml version="1.0" encoding="ISO-8859-15"?><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 TestDetectJSON(t *testing.T) {
  31. data := `
  32. {
  33. "version" : "https://jsonfeed.org/version/1",
  34. "title" : "Example"
  35. }
  36. `
  37. format := DetectFeedFormat(bytes.NewBufferString(data))
  38. if format != FormatJson {
  39. t.Errorf("Wrong format detected: %s instead of %s", format, FormatJson)
  40. }
  41. }
  42. func TestDetectUnknown(t *testing.T) {
  43. data := `
  44. <!DOCTYPE html> <html> </html>
  45. `
  46. format := DetectFeedFormat(bytes.NewBufferString(data))
  47. if format != FormatUnknown {
  48. t.Errorf("Wrong format detected: %s instead of %s", format, FormatUnknown)
  49. }
  50. }
  51. func TestParseAtom(t *testing.T) {
  52. data := `<?xml version="1.0" encoding="utf-8"?>
  53. <feed xmlns="http://www.w3.org/2005/Atom">
  54. <title>Example Feed</title>
  55. <link href="http://example.org/"/>
  56. <updated>2003-12-13T18:30:02Z</updated>
  57. <author>
  58. <name>John Doe</name>
  59. </author>
  60. <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  61. <entry>
  62. <title>Atom-Powered Robots Run Amok</title>
  63. <link href="http://example.org/2003/12/13/atom03"/>
  64. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  65. <updated>2003-12-13T18:30:02Z</updated>
  66. <summary>Some text.</summary>
  67. </entry>
  68. </feed>`
  69. feed, err := parseFeed(bytes.NewBufferString(data))
  70. if err != nil {
  71. t.Error(err)
  72. }
  73. if feed.Title != "Example Feed" {
  74. t.Errorf("Incorrect title, got: %s", feed.Title)
  75. }
  76. }
  77. func TestParseRss(t *testing.T) {
  78. data := `<?xml version="1.0"?>
  79. <rss version="2.0">
  80. <channel>
  81. <title>Liftoff News</title>
  82. <link>http://liftoff.msfc.nasa.gov/</link>
  83. <item>
  84. <title>Star City</title>
  85. <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
  86. <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>
  87. <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
  88. <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
  89. </item>
  90. </channel>
  91. </rss>`
  92. feed, err := parseFeed(bytes.NewBufferString(data))
  93. if err != nil {
  94. t.Error(err)
  95. }
  96. if feed.Title != "Liftoff News" {
  97. t.Errorf("Incorrect title, got: %s", feed.Title)
  98. }
  99. }
  100. func TestParseJson(t *testing.T) {
  101. data := `{
  102. "version": "https://jsonfeed.org/version/1",
  103. "title": "My Example Feed",
  104. "home_page_url": "https://example.org/",
  105. "feed_url": "https://example.org/feed.json",
  106. "items": [
  107. {
  108. "id": "2",
  109. "content_text": "This is a second item.",
  110. "url": "https://example.org/second-item"
  111. },
  112. {
  113. "id": "1",
  114. "content_html": "<p>Hello, world!</p>",
  115. "url": "https://example.org/initial-post"
  116. }
  117. ]
  118. }`
  119. feed, err := parseFeed(bytes.NewBufferString(data))
  120. if err != nil {
  121. t.Error(err)
  122. }
  123. if feed.Title != "My Example Feed" {
  124. t.Errorf("Incorrect title, got: %s", feed.Title)
  125. }
  126. }
  127. func TestParseUnknownFeed(t *testing.T) {
  128. data := `
  129. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  130. <html xmlns="http://www.w3.org/1999/xhtml">
  131. <head>
  132. <title>Title of document</title>
  133. </head>
  134. <body>
  135. some content
  136. </body>
  137. </html>
  138. `
  139. _, err := parseFeed(bytes.NewBufferString(data))
  140. if err == nil {
  141. t.Error("ParseFeed must returns an error")
  142. }
  143. }