parser_test.go 3.6 KB

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