parser_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 json // import "miniflux.app/reader/json"
  5. import (
  6. "bytes"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. func TestParseJsonFeed(t *testing.T) {
  12. data := `{
  13. "version": "https://jsonfeed.org/version/1",
  14. "title": "My Example Feed",
  15. "home_page_url": "https://example.org/",
  16. "feed_url": "https://example.org/feed.json",
  17. "items": [
  18. {
  19. "id": "2",
  20. "content_text": "This is a second item.",
  21. "url": "https://example.org/second-item"
  22. },
  23. {
  24. "id": "1",
  25. "content_html": "<p>Hello, world!</p>",
  26. "url": "https://example.org/initial-post"
  27. }
  28. ]
  29. }`
  30. feed, err := Parse(bytes.NewBufferString(data))
  31. if err != nil {
  32. t.Error(err)
  33. }
  34. if feed.Title != "My Example Feed" {
  35. t.Errorf("Incorrect title, got: %s", feed.Title)
  36. }
  37. if feed.FeedURL != "https://example.org/feed.json" {
  38. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  39. }
  40. if feed.SiteURL != "https://example.org/" {
  41. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  42. }
  43. if len(feed.Entries) != 2 {
  44. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  45. }
  46. if feed.Entries[0].Hash != "d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" {
  47. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  48. }
  49. if feed.Entries[0].URL != "https://example.org/second-item" {
  50. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  51. }
  52. if feed.Entries[0].Title != "This is a second item." {
  53. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
  54. }
  55. if feed.Entries[0].Content != "This is a second item." {
  56. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  57. }
  58. if feed.Entries[1].Hash != "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b" {
  59. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[1].Hash)
  60. }
  61. if feed.Entries[1].URL != "https://example.org/initial-post" {
  62. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[1].URL)
  63. }
  64. if feed.Entries[1].Title != "Hello, world!" {
  65. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[1].Title)
  66. }
  67. if feed.Entries[1].Content != "<p>Hello, world!</p>" {
  68. t.Errorf("Incorrect entry content, got: %s", feed.Entries[1].Content)
  69. }
  70. }
  71. func TestParsePodcast(t *testing.T) {
  72. data := `{
  73. "version": "https://jsonfeed.org/version/1",
  74. "user_comment": "This is a podcast feed. You can add this feed to your podcast client using the following URL: http://therecord.co/feed.json",
  75. "title": "The Record",
  76. "home_page_url": "http://therecord.co/",
  77. "feed_url": "http://therecord.co/feed.json",
  78. "items": [
  79. {
  80. "id": "http://therecord.co/chris-parrish",
  81. "title": "Special #1 - Chris Parrish",
  82. "url": "http://therecord.co/chris-parrish",
  83. "content_text": "Chris has worked at Adobe and as a founder of Rogue Sheep, which won an Apple Design Award for Postage. Chris’s new company is Aged & Distilled with Guy English — which shipped Napkin, a Mac app for visual collaboration. Chris is also the co-host of The Record. He lives on Bainbridge Island, a quick ferry ride from Seattle.",
  84. "content_html": "Chris has worked at <a href=\"http://adobe.com/\">Adobe</a> and as a founder of Rogue Sheep, which won an Apple Design Award for Postage. Chris’s new company is Aged & Distilled with Guy English — which shipped <a href=\"http://aged-and-distilled.com/napkin/\">Napkin</a>, a Mac app for visual collaboration. Chris is also the co-host of The Record. He lives on <a href=\"http://www.ci.bainbridge-isl.wa.us/\">Bainbridge Island</a>, a quick ferry ride from Seattle.",
  85. "summary": "Brent interviews Chris Parrish, co-host of The Record and one-half of Aged & Distilled.",
  86. "date_published": "2014-05-09T14:04:00-07:00",
  87. "attachments": [
  88. {
  89. "url": "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a",
  90. "mime_type": "audio/x-m4a",
  91. "size_in_bytes": 89970236,
  92. "duration_in_seconds": 6629
  93. }
  94. ]
  95. }
  96. ]
  97. }`
  98. feed, err := Parse(bytes.NewBufferString(data))
  99. if err != nil {
  100. t.Error(err)
  101. }
  102. if feed.Title != "The Record" {
  103. t.Errorf("Incorrect title, got: %s", feed.Title)
  104. }
  105. if feed.FeedURL != "http://therecord.co/feed.json" {
  106. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  107. }
  108. if feed.SiteURL != "http://therecord.co/" {
  109. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  110. }
  111. if len(feed.Entries) != 1 {
  112. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  113. }
  114. if feed.Entries[0].Hash != "6b678e57962a1b001e4e873756563cdc08bbd06ca561e764e0baa9a382485797" {
  115. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  116. }
  117. if feed.Entries[0].URL != "http://therecord.co/chris-parrish" {
  118. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  119. }
  120. if feed.Entries[0].Title != "Special #1 - Chris Parrish" {
  121. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
  122. }
  123. if feed.Entries[0].Content != `Chris has worked at <a href="http://adobe.com/">Adobe</a> and as a founder of Rogue Sheep, which won an Apple Design Award for Postage. Chris’s new company is Aged & Distilled with Guy English — which shipped <a href="http://aged-and-distilled.com/napkin/">Napkin</a>, a Mac app for visual collaboration. Chris is also the co-host of The Record. He lives on <a href="http://www.ci.bainbridge-isl.wa.us/">Bainbridge Island</a>, a quick ferry ride from Seattle.` {
  124. t.Errorf(`Incorrect entry content, got: "%s"`, feed.Entries[0].Content)
  125. }
  126. location, _ := time.LoadLocation("America/Vancouver")
  127. if !feed.Entries[0].Date.Equal(time.Date(2014, time.May, 9, 14, 4, 0, 0, location)) {
  128. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  129. }
  130. if len(feed.Entries[0].Enclosures) != 1 {
  131. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  132. }
  133. if feed.Entries[0].Enclosures[0].URL != "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a" {
  134. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  135. }
  136. if feed.Entries[0].Enclosures[0].MimeType != "audio/x-m4a" {
  137. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  138. }
  139. if feed.Entries[0].Enclosures[0].Size != 89970236 {
  140. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  141. }
  142. }
  143. func TestParseFeedWithRelativeURL(t *testing.T) {
  144. data := `{
  145. "version": "https://jsonfeed.org/version/1",
  146. "title": "Example",
  147. "home_page_url": "https://example.org/",
  148. "feed_url": "https://example.org/feed.json",
  149. "items": [
  150. {
  151. "id": "2347259",
  152. "url": "something.html",
  153. "date_published": "2016-02-09T14:22:00-07:00"
  154. }
  155. ]
  156. }`
  157. feed, err := Parse(bytes.NewBufferString(data))
  158. if err != nil {
  159. t.Error(err)
  160. }
  161. if feed.Entries[0].URL != "https://example.org/something.html" {
  162. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  163. }
  164. }
  165. func TestParseAuthor(t *testing.T) {
  166. data := `{
  167. "version": "https://jsonfeed.org/version/1",
  168. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  169. "title": "Brent Simmons’s Microblog",
  170. "home_page_url": "https://example.org/",
  171. "feed_url": "https://example.org/feed.json",
  172. "author": {
  173. "name": "Brent Simmons",
  174. "url": "http://example.org/",
  175. "avatar": "https://example.org/avatar.png"
  176. },
  177. "items": [
  178. {
  179. "id": "2347259",
  180. "url": "https://example.org/2347259",
  181. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  182. "date_published": "2016-02-09T14:22:00-07:00"
  183. }
  184. ]
  185. }`
  186. feed, err := Parse(bytes.NewBufferString(data))
  187. if err != nil {
  188. t.Error(err)
  189. }
  190. if len(feed.Entries) != 1 {
  191. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  192. }
  193. if feed.Entries[0].Author != "Brent Simmons" {
  194. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  195. }
  196. }
  197. func TestParseFeedWithoutTitle(t *testing.T) {
  198. data := `{
  199. "version": "https://jsonfeed.org/version/1",
  200. "home_page_url": "https://example.org/",
  201. "feed_url": "https://example.org/feed.json",
  202. "items": [
  203. {
  204. "id": "2347259",
  205. "url": "https://example.org/2347259",
  206. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  207. "date_published": "2016-02-09T14:22:00-07:00"
  208. }
  209. ]
  210. }`
  211. feed, err := Parse(bytes.NewBufferString(data))
  212. if err != nil {
  213. t.Error(err)
  214. }
  215. if feed.Title != "https://example.org/" {
  216. t.Errorf("Incorrect title, got: %s", feed.Title)
  217. }
  218. }
  219. func TestParseFeedItemWithInvalidDate(t *testing.T) {
  220. data := `{
  221. "version": "https://jsonfeed.org/version/1",
  222. "title": "My Example Feed",
  223. "home_page_url": "https://example.org/",
  224. "feed_url": "https://example.org/feed.json",
  225. "items": [
  226. {
  227. "id": "2347259",
  228. "url": "https://example.org/2347259",
  229. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  230. "date_published": "Tomorrow"
  231. }
  232. ]
  233. }`
  234. feed, err := Parse(bytes.NewBufferString(data))
  235. if err != nil {
  236. t.Error(err)
  237. }
  238. if len(feed.Entries) != 1 {
  239. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  240. }
  241. duration := time.Since(feed.Entries[0].Date)
  242. if duration.Seconds() > 1 {
  243. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  244. }
  245. }
  246. func TestParseFeedItemWithoutID(t *testing.T) {
  247. data := `{
  248. "version": "https://jsonfeed.org/version/1",
  249. "title": "My Example Feed",
  250. "home_page_url": "https://example.org/",
  251. "feed_url": "https://example.org/feed.json",
  252. "items": [
  253. {
  254. "content_text": "Some text."
  255. }
  256. ]
  257. }`
  258. feed, err := Parse(bytes.NewBufferString(data))
  259. if err != nil {
  260. t.Error(err)
  261. }
  262. if len(feed.Entries) != 1 {
  263. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  264. }
  265. if feed.Entries[0].Hash != "13b4c5aecd1b6d749afcee968fbf9c80f1ed1bbdbe1aaf25cb34ebd01144bbe9" {
  266. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  267. }
  268. }
  269. func TestParseFeedItemWithoutTitle(t *testing.T) {
  270. data := `{
  271. "version": "https://jsonfeed.org/version/1",
  272. "title": "My Example Feed",
  273. "home_page_url": "https://example.org/",
  274. "feed_url": "https://example.org/feed.json",
  275. "items": [
  276. {
  277. "url": "https://example.org/item"
  278. }
  279. ]
  280. }`
  281. feed, err := Parse(bytes.NewBufferString(data))
  282. if err != nil {
  283. t.Error(err)
  284. }
  285. if len(feed.Entries) != 1 {
  286. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  287. }
  288. if feed.Entries[0].Title != "https://example.org/item" {
  289. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  290. }
  291. }
  292. func TestParseTruncateItemTitle(t *testing.T) {
  293. data := `{
  294. "version": "https://jsonfeed.org/version/1",
  295. "title": "My Example Feed",
  296. "home_page_url": "https://example.org/",
  297. "feed_url": "https://example.org/feed.json",
  298. "items": [
  299. {
  300. "title": "` + strings.Repeat("a", 200) + `"
  301. }
  302. ]
  303. }`
  304. feed, err := Parse(bytes.NewBufferString(data))
  305. if err != nil {
  306. t.Error(err)
  307. }
  308. if len(feed.Entries) != 1 {
  309. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  310. }
  311. if len(feed.Entries[0].Title) != 103 {
  312. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  313. }
  314. }
  315. func TestParseInvalidJSON(t *testing.T) {
  316. data := `garbage`
  317. _, err := Parse(bytes.NewBufferString(data))
  318. if err == nil {
  319. t.Error("Parse should returns an error")
  320. }
  321. }