atom_03_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/", bytes.NewBufferString(data))
  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/" {
  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 TestParseAtom03WithoutFeedTitle(t *testing.T) {
  63. data := `<?xml version="1.0" encoding="utf-8"?>
  64. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  65. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  66. <modified>2003-12-13T18:30:02Z</modified>
  67. <author><name>Mark Pilgrim</name></author>
  68. <entry>
  69. <title>Atom 0.3 snapshot</title>
  70. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  71. <id>tag:diveintomark.org,2003:3.2397</id>
  72. </entry>
  73. </feed>`
  74. feed, err := Parse("http://diveintomark.org/", bytes.NewBufferString(data))
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if feed.Title != "http://diveintomark.org/" {
  79. t.Errorf("Incorrect title, got: %s", feed.Title)
  80. }
  81. }
  82. func TestParseAtom03WithoutEntryTitleButWithLink(t *testing.T) {
  83. data := `<?xml version="1.0" encoding="utf-8"?>
  84. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  85. <title>dive into mark</title>
  86. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  87. <modified>2003-12-13T18:30:02Z</modified>
  88. <author><name>Mark Pilgrim</name></author>
  89. <entry>
  90. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  91. <id>tag:diveintomark.org,2003:3.2397</id>
  92. </entry>
  93. </feed>`
  94. feed, err := Parse("http://diveintomark.org/", bytes.NewBufferString(data))
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. if len(feed.Entries) != 1 {
  99. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  100. }
  101. if feed.Entries[0].Title != "http://diveintomark.org/2003/12/13/atom03" {
  102. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  103. }
  104. }
  105. func TestParseAtom03WithoutEntryTitleButWithSummary(t *testing.T) {
  106. data := `<?xml version="1.0" encoding="utf-8"?>
  107. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  108. <title>dive into mark</title>
  109. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  110. <modified>2003-12-13T18:30:02Z</modified>
  111. <author><name>Mark Pilgrim</name></author>
  112. <entry>
  113. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  114. <id>tag:diveintomark.org,2003:3.2397</id>
  115. <summary type="text/plain">It&apos;s a test</summary>
  116. </entry>
  117. </feed>`
  118. feed, err := Parse("http://diveintomark.org/", bytes.NewBufferString(data))
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. if len(feed.Entries) != 1 {
  123. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  124. }
  125. if feed.Entries[0].Title != "It's a test" {
  126. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  127. }
  128. }
  129. func TestParseAtom03WithoutEntryTitleButWithXMLContent(t *testing.T) {
  130. data := `<?xml version="1.0" encoding="utf-8"?>
  131. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  132. <title>dive into mark</title>
  133. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  134. <modified>2003-12-13T18:30:02Z</modified>
  135. <author><name>Mark Pilgrim</name></author>
  136. <entry>
  137. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  138. <id>tag:diveintomark.org,2003:3.2397</id>
  139. <content mode="xml" type="text/html"><p>Some text.</p></content>
  140. </entry>
  141. </feed>`
  142. feed, err := Parse("http://diveintomark.org/", bytes.NewBufferString(data))
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. if len(feed.Entries) != 1 {
  147. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  148. }
  149. if feed.Entries[0].Title != "Some text." {
  150. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  151. }
  152. }
  153. func TestParseAtom03WithSummaryOnly(t *testing.T) {
  154. data := `<?xml version="1.0" encoding="utf-8"?>
  155. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  156. <title>dive into mark</title>
  157. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  158. <modified>2003-12-13T18:30:02Z</modified>
  159. <author><name>Mark Pilgrim</name></author>
  160. <entry>
  161. <title>Atom 0.3 snapshot</title>
  162. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  163. <id>tag:diveintomark.org,2003:3.2397</id>
  164. <issued>2003-12-13T08:29:29-04:00</issued>
  165. <modified>2003-12-13T18:30:02Z</modified>
  166. <summary type="text/plain">It&apos;s a test</summary>
  167. </entry>
  168. </feed>`
  169. feed, err := Parse("http://diveintomark.org/", bytes.NewBufferString(data))
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. if len(feed.Entries) != 1 {
  174. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  175. }
  176. if feed.Entries[0].Content != "It&#39;s a test" {
  177. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  178. }
  179. }
  180. func TestParseAtom03WithXMLContent(t *testing.T) {
  181. data := `<?xml version="1.0" encoding="utf-8"?>
  182. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  183. <title>dive into mark</title>
  184. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  185. <modified>2003-12-13T18:30:02Z</modified>
  186. <author><name>Mark Pilgrim</name></author>
  187. <entry>
  188. <title>Atom 0.3 snapshot</title>
  189. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  190. <id>tag:diveintomark.org,2003:3.2397</id>
  191. <issued>2003-12-13T08:29:29-04:00</issued>
  192. <modified>2003-12-13T18:30:02Z</modified>
  193. <content mode="xml" type="text/html"><p>Some text.</p></content>
  194. </entry>
  195. </feed>`
  196. feed, err := Parse("http://diveintomark.org/", bytes.NewBufferString(data))
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. if len(feed.Entries) != 1 {
  201. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  202. }
  203. if feed.Entries[0].Content != "<p>Some text.</p>" {
  204. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  205. }
  206. }
  207. func TestParseAtom03WithBase64Content(t *testing.T) {
  208. data := `<?xml version="1.0" encoding="utf-8"?>
  209. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  210. <title>dive into mark</title>
  211. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  212. <modified>2003-12-13T18:30:02Z</modified>
  213. <author><name>Mark Pilgrim</name></author>
  214. <entry>
  215. <title>Atom 0.3 snapshot</title>
  216. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  217. <id>tag:diveintomark.org,2003:3.2397</id>
  218. <issued>2003-12-13T08:29:29-04:00</issued>
  219. <modified>2003-12-13T18:30:02Z</modified>
  220. <content mode="base64" type="text/html">PHA+U29tZSB0ZXh0LjwvcD4=</content>
  221. </entry>
  222. </feed>`
  223. feed, err := Parse("http://diveintomark.org/", bytes.NewBufferString(data))
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. if len(feed.Entries) != 1 {
  228. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  229. }
  230. if feed.Entries[0].Content != "<p>Some text.</p>" {
  231. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  232. }
  233. }