atom_03_test.go 9.0 KB

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