parser_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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("https://example.org/feed.json", bytes.NewBufferString(data))
  31. if err != nil {
  32. t.Fatal(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 != "https://example.org/initial-post" {
  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("http://therecord.co/feed.json", bytes.NewBufferString(data))
  99. if err != nil {
  100. t.Fatal(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.Fatalf("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.Fatalf("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 TestParseEntryWithoutAttachmentURL(t *testing.T) {
  144. data := `{
  145. "version": "https://jsonfeed.org/version/1",
  146. "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",
  147. "title": "The Record",
  148. "home_page_url": "http://therecord.co/",
  149. "feed_url": "http://therecord.co/feed.json",
  150. "items": [
  151. {
  152. "id": "http://therecord.co/chris-parrish",
  153. "title": "Special #1 - Chris Parrish",
  154. "url": "http://therecord.co/chris-parrish",
  155. "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.",
  156. "date_published": "2014-05-09T14:04:00-07:00",
  157. "attachments": [
  158. {
  159. "url": "",
  160. "mime_type": "audio/x-m4a",
  161. "size_in_bytes": 0
  162. }
  163. ]
  164. }
  165. ]
  166. }`
  167. feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. if len(feed.Entries) != 1 {
  172. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  173. }
  174. if len(feed.Entries[0].Enclosures) != 0 {
  175. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  176. }
  177. }
  178. func TestParseFeedWithRelativeURL(t *testing.T) {
  179. data := `{
  180. "version": "https://jsonfeed.org/version/1",
  181. "title": "Example",
  182. "home_page_url": "https://example.org/",
  183. "feed_url": "https://example.org/feed.json",
  184. "items": [
  185. {
  186. "id": "2347259",
  187. "url": "something.html",
  188. "date_published": "2016-02-09T14:22:00-07:00"
  189. }
  190. ]
  191. }`
  192. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. if feed.Entries[0].URL != "https://example.org/something.html" {
  197. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  198. }
  199. }
  200. func TestParseAuthor(t *testing.T) {
  201. data := `{
  202. "version": "https://jsonfeed.org/version/1",
  203. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  204. "title": "Brent Simmons’s Microblog",
  205. "home_page_url": "https://example.org/",
  206. "feed_url": "https://example.org/feed.json",
  207. "author": {
  208. "name": "Brent Simmons",
  209. "url": "http://example.org/",
  210. "avatar": "https://example.org/avatar.png"
  211. },
  212. "items": [
  213. {
  214. "id": "2347259",
  215. "url": "https://example.org/2347259",
  216. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  217. "date_published": "2016-02-09T14:22:00-07:00"
  218. }
  219. ]
  220. }`
  221. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. if len(feed.Entries) != 1 {
  226. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  227. }
  228. if feed.Entries[0].Author != "Brent Simmons" {
  229. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  230. }
  231. }
  232. func TestParseFeedWithoutTitle(t *testing.T) {
  233. data := `{
  234. "version": "https://jsonfeed.org/version/1",
  235. "home_page_url": "https://example.org/",
  236. "feed_url": "https://example.org/feed.json",
  237. "items": [
  238. {
  239. "id": "2347259",
  240. "url": "https://example.org/2347259",
  241. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  242. "date_published": "2016-02-09T14:22:00-07:00"
  243. }
  244. ]
  245. }`
  246. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  247. if err != nil {
  248. t.Fatal(err)
  249. }
  250. if feed.Title != "https://example.org/" {
  251. t.Errorf("Incorrect title, got: %s", feed.Title)
  252. }
  253. }
  254. func TestParseFeedItemWithInvalidDate(t *testing.T) {
  255. data := `{
  256. "version": "https://jsonfeed.org/version/1",
  257. "title": "My Example Feed",
  258. "home_page_url": "https://example.org/",
  259. "feed_url": "https://example.org/feed.json",
  260. "items": [
  261. {
  262. "id": "2347259",
  263. "url": "https://example.org/2347259",
  264. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  265. "date_published": "Tomorrow"
  266. }
  267. ]
  268. }`
  269. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. if len(feed.Entries) != 1 {
  274. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  275. }
  276. duration := time.Since(feed.Entries[0].Date)
  277. if duration.Seconds() > 1 {
  278. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  279. }
  280. }
  281. func TestParseFeedItemWithoutID(t *testing.T) {
  282. data := `{
  283. "version": "https://jsonfeed.org/version/1",
  284. "title": "My Example Feed",
  285. "home_page_url": "https://example.org/",
  286. "feed_url": "https://example.org/feed.json",
  287. "items": [
  288. {
  289. "content_text": "Some text."
  290. }
  291. ]
  292. }`
  293. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  294. if err != nil {
  295. t.Fatal(err)
  296. }
  297. if len(feed.Entries) != 1 {
  298. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  299. }
  300. if feed.Entries[0].Hash != "13b4c5aecd1b6d749afcee968fbf9c80f1ed1bbdbe1aaf25cb34ebd01144bbe9" {
  301. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  302. }
  303. }
  304. func TestParseFeedItemWithoutTitle(t *testing.T) {
  305. data := `{
  306. "version": "https://jsonfeed.org/version/1",
  307. "title": "My Example Feed",
  308. "home_page_url": "https://example.org/",
  309. "feed_url": "https://example.org/feed.json",
  310. "items": [
  311. {
  312. "url": "https://example.org/item"
  313. }
  314. ]
  315. }`
  316. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  317. if err != nil {
  318. t.Fatal(err)
  319. }
  320. if len(feed.Entries) != 1 {
  321. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  322. }
  323. if feed.Entries[0].Title != "https://example.org/item" {
  324. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  325. }
  326. }
  327. func TestParseTruncateItemTitle(t *testing.T) {
  328. data := `{
  329. "version": "https://jsonfeed.org/version/1",
  330. "title": "My Example Feed",
  331. "home_page_url": "https://example.org/",
  332. "feed_url": "https://example.org/feed.json",
  333. "items": [
  334. {
  335. "title": "` + strings.Repeat("a", 200) + `"
  336. }
  337. ]
  338. }`
  339. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  340. if err != nil {
  341. t.Fatal(err)
  342. }
  343. if len(feed.Entries) != 1 {
  344. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  345. }
  346. if len(feed.Entries[0].Title) != 103 {
  347. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  348. }
  349. }
  350. func TestParseItemTitleWithXMLTags(t *testing.T) {
  351. data := `{
  352. "version": "https://jsonfeed.org/version/1",
  353. "title": "My Example Feed",
  354. "home_page_url": "https://example.org/",
  355. "feed_url": "https://example.org/feed.json",
  356. "items": [
  357. {
  358. "title": "</example>"
  359. }
  360. ]
  361. }`
  362. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  363. if err != nil {
  364. t.Fatal(err)
  365. }
  366. if len(feed.Entries) != 1 {
  367. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  368. }
  369. if feed.Entries[0].Title != "</example>" {
  370. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  371. }
  372. }
  373. func TestParseInvalidJSON(t *testing.T) {
  374. data := `garbage`
  375. _, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  376. if err == nil {
  377. t.Error("Parse should returns an error")
  378. }
  379. }