atom_03_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package atom // import "miniflux.app/v2/internal/reader/atom"
  4. import (
  5. "bytes"
  6. "testing"
  7. "time"
  8. )
  9. func TestParseAtom03(t *testing.T) {
  10. data := `<?xml version="1.0" encoding="utf-8"?>
  11. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  12. <title>dive into mark</title>
  13. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  14. <modified>2003-12-13T18:30:02Z</modified>
  15. <author><name>Mark Pilgrim</name></author>
  16. <entry>
  17. <title>Atom 0.3 snapshot</title>
  18. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  19. <id>tag:diveintomark.org,2003:3.2397</id>
  20. <issued>2003-12-13T08:29:29-04:00</issued>
  21. <modified>2003-12-13T18:30:02Z</modified>
  22. <summary type="text/plain">It&apos;s a test</summary>
  23. <content type="text/html" mode="escaped"><![CDATA[<p>HTML content</p>]]></content>
  24. </entry>
  25. </feed>`
  26. feed, err := Parse("http://diveintomark.org/atom.xml", bytes.NewReader([]byte(data)), "0.3")
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. if feed.Title != "dive into mark" {
  31. t.Errorf("Incorrect title, got: %s", feed.Title)
  32. }
  33. if feed.FeedURL != "http://diveintomark.org/atom.xml" {
  34. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  35. }
  36. if feed.SiteURL != "http://diveintomark.org/" {
  37. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  38. }
  39. if len(feed.Entries) != 1 {
  40. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  41. }
  42. tz := time.FixedZone("Test Case Time", -int((4 * time.Hour).Seconds()))
  43. if !feed.Entries[0].Date.Equal(time.Date(2003, time.December, 13, 8, 29, 29, 0, tz)) {
  44. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  45. }
  46. if feed.Entries[0].Hash != "b70d30334b808f32e66eb19fabb263525cecd18f205720b583e84f7f295cf728" {
  47. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  48. }
  49. if feed.Entries[0].URL != "http://diveintomark.org/2003/12/13/atom03" {
  50. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  51. }
  52. if feed.Entries[0].Title != "Atom 0.3 snapshot" {
  53. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  54. }
  55. if feed.Entries[0].Content != "<p>HTML content</p>" {
  56. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  57. }
  58. if feed.Entries[0].Author != "Mark Pilgrim" {
  59. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  60. }
  61. }
  62. func TestParseAtom03WithoutSiteURL(t *testing.T) {
  63. data := `<?xml version="1.0" encoding="utf-8"?>
  64. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  65. <modified>2003-12-13T18:30:02Z</modified>
  66. <author><name>Mark Pilgrim</name></author>
  67. <entry>
  68. <title>Atom 0.3 snapshot</title>
  69. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  70. <id>tag:diveintomark.org,2003:3.2397</id>
  71. </entry>
  72. </feed>`
  73. feed, err := Parse("http://diveintomark.org/atom.xml", bytes.NewReader([]byte(data)), "0.3")
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. if feed.SiteURL != "http://diveintomark.org/atom.xml" {
  78. t.Errorf("Incorrect title, got: %s", feed.Title)
  79. }
  80. }
  81. func TestParseAtom03WithoutFeedTitle(t *testing.T) {
  82. data := `<?xml version="1.0" encoding="utf-8"?>
  83. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  84. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  85. <modified>2003-12-13T18:30:02Z</modified>
  86. <author><name>Mark Pilgrim</name></author>
  87. <entry>
  88. <title>Atom 0.3 snapshot</title>
  89. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  90. <id>tag:diveintomark.org,2003:3.2397</id>
  91. </entry>
  92. </feed>`
  93. feed, err := Parse("http://diveintomark.org/", bytes.NewReader([]byte(data)), "0.3")
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. if feed.Title != "http://diveintomark.org/" {
  98. t.Errorf("Incorrect title, got: %s", feed.Title)
  99. }
  100. }
  101. func TestParseAtom03WithoutEntryTitleButWithLink(t *testing.T) {
  102. data := `<?xml version="1.0" encoding="utf-8"?>
  103. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  104. <title>dive into mark</title>
  105. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  106. <modified>2003-12-13T18:30:02Z</modified>
  107. <author><name>Mark Pilgrim</name></author>
  108. <entry>
  109. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  110. <id>tag:diveintomark.org,2003:3.2397</id>
  111. </entry>
  112. </feed>`
  113. feed, err := Parse("http://diveintomark.org/", bytes.NewReader([]byte(data)), "0.3")
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. if len(feed.Entries) != 1 {
  118. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  119. }
  120. if feed.Entries[0].Title != "http://diveintomark.org/2003/12/13/atom03" {
  121. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  122. }
  123. }
  124. func TestParseAtom03WithoutEntryTitleButWithSummary(t *testing.T) {
  125. data := `<?xml version="1.0" encoding="utf-8"?>
  126. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  127. <title>dive into mark</title>
  128. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  129. <modified>2003-12-13T18:30:02Z</modified>
  130. <author><name>Mark Pilgrim</name></author>
  131. <entry>
  132. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  133. <id>tag:diveintomark.org,2003:3.2397</id>
  134. <summary type="text/plain">It&apos;s a test</summary>
  135. </entry>
  136. </feed>`
  137. feed, err := Parse("http://diveintomark.org/", bytes.NewReader([]byte(data)), "0.3")
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. if len(feed.Entries) != 1 {
  142. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  143. }
  144. if feed.Entries[0].Title != "It's a test" {
  145. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  146. }
  147. }
  148. func TestParseAtom03WithoutEntryTitleButWithXMLContent(t *testing.T) {
  149. data := `<?xml version="1.0" encoding="utf-8"?>
  150. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  151. <title>dive into mark</title>
  152. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  153. <modified>2003-12-13T18:30:02Z</modified>
  154. <author><name>Mark Pilgrim</name></author>
  155. <entry>
  156. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  157. <id>tag:diveintomark.org,2003:3.2397</id>
  158. <content mode="xml" type="text/html"><p>Some text.</p></content>
  159. </entry>
  160. </feed>`
  161. feed, err := Parse("http://diveintomark.org/", bytes.NewReader([]byte(data)), "0.3")
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. if len(feed.Entries) != 1 {
  166. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  167. }
  168. if feed.Entries[0].Title != "Some text." {
  169. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  170. }
  171. }
  172. func TestParseAtom03WithSummaryOnly(t *testing.T) {
  173. data := `<?xml version="1.0" encoding="utf-8"?>
  174. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  175. <title>dive into mark</title>
  176. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  177. <modified>2003-12-13T18:30:02Z</modified>
  178. <author><name>Mark Pilgrim</name></author>
  179. <entry>
  180. <title>Atom 0.3 snapshot</title>
  181. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  182. <id>tag:diveintomark.org,2003:3.2397</id>
  183. <issued>2003-12-13T08:29:29-04:00</issued>
  184. <modified>2003-12-13T18:30:02Z</modified>
  185. <summary type="text/plain">It&apos;s a test</summary>
  186. </entry>
  187. </feed>`
  188. feed, err := Parse("http://diveintomark.org/", bytes.NewReader([]byte(data)), "0.3")
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. if len(feed.Entries) != 1 {
  193. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  194. }
  195. if feed.Entries[0].Content != "It&#39;s a test" {
  196. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  197. }
  198. }
  199. func TestParseAtom03WithXMLContent(t *testing.T) {
  200. data := `<?xml version="1.0" encoding="utf-8"?>
  201. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  202. <title>dive into mark</title>
  203. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  204. <modified>2003-12-13T18:30:02Z</modified>
  205. <author><name>Mark Pilgrim</name></author>
  206. <entry>
  207. <title>Atom 0.3 snapshot</title>
  208. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  209. <id>tag:diveintomark.org,2003:3.2397</id>
  210. <issued>2003-12-13T08:29:29-04:00</issued>
  211. <modified>2003-12-13T18:30:02Z</modified>
  212. <content mode="xml" type="text/html"><p>Some text.</p></content>
  213. </entry>
  214. </feed>`
  215. feed, err := Parse("http://diveintomark.org/", bytes.NewReader([]byte(data)), "0.3")
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. if len(feed.Entries) != 1 {
  220. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  221. }
  222. if feed.Entries[0].Content != "<p>Some text.</p>" {
  223. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  224. }
  225. }
  226. func TestParseAtom03WithBase64Content(t *testing.T) {
  227. data := `<?xml version="1.0" encoding="utf-8"?>
  228. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  229. <title>dive into mark</title>
  230. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  231. <modified>2003-12-13T18:30:02Z</modified>
  232. <author><name>Mark Pilgrim</name></author>
  233. <entry>
  234. <title>Atom 0.3 snapshot</title>
  235. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  236. <id>tag:diveintomark.org,2003:3.2397</id>
  237. <issued>2003-12-13T08:29:29-04:00</issued>
  238. <modified>2003-12-13T18:30:02Z</modified>
  239. <content mode="base64" type="text/html">PHA+U29tZSB0ZXh0LjwvcD4=</content>
  240. </entry>
  241. </feed>`
  242. feed, err := Parse("http://diveintomark.org/", bytes.NewReader([]byte(data)), "0.3")
  243. if err != nil {
  244. t.Fatal(err)
  245. }
  246. if len(feed.Entries) != 1 {
  247. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  248. }
  249. if feed.Entries[0].Content != "<p>Some text.</p>" {
  250. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  251. }
  252. }