parser_test.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package json // import "miniflux.app/v2/internal/reader/json"
  4. import (
  5. "bytes"
  6. "crypto/sha256"
  7. "encoding/hex"
  8. "testing"
  9. "time"
  10. )
  11. func TestParseJsonFeedVersion1(t *testing.T) {
  12. data := `{
  13. "version": "https://jsonfeed.org/version/1",
  14. "title": "My Example Feed",
  15. "icon": "https://micro.blog/jsonfeed/avatar.jpg",
  16. "favicon": "https://micro.blog/jsonfeed/favicon.png",
  17. "home_page_url": "https://example.org/",
  18. "feed_url": "https://example.org/feed.json",
  19. "items": [
  20. {
  21. "id": "2",
  22. "content_text": "This is a second item.",
  23. "url": "https://example.org/second-item"
  24. },
  25. {
  26. "id": "1",
  27. "content_html": "<p>Hello, world!</p>",
  28. "url": "https://example.org/initial-post"
  29. }
  30. ]
  31. }`
  32. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if feed.Title != "My Example Feed" {
  37. t.Errorf("Incorrect title, got: %s", feed.Title)
  38. }
  39. if feed.Description != "" {
  40. t.Errorf("Incorrect description, got: %s", feed.Description)
  41. }
  42. if feed.FeedURL != "https://example.org/feed.json" {
  43. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  44. }
  45. if feed.SiteURL != "https://example.org/" {
  46. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  47. }
  48. if feed.IconURL != "https://micro.blog/jsonfeed/favicon.png" {
  49. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  50. }
  51. if len(feed.Entries) != 2 {
  52. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  53. }
  54. if feed.Entries[0].Hash != "d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" {
  55. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  56. }
  57. if feed.Entries[0].URL != "https://example.org/second-item" {
  58. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  59. }
  60. if feed.Entries[0].Title != "This is a second item." {
  61. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
  62. }
  63. if feed.Entries[0].Content != "This is a second item." {
  64. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  65. }
  66. if feed.Entries[1].Hash != "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b" {
  67. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[1].Hash)
  68. }
  69. if feed.Entries[1].URL != "https://example.org/initial-post" {
  70. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[1].URL)
  71. }
  72. if feed.Entries[1].Title != "Hello, world!" {
  73. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[1].Title)
  74. }
  75. if feed.Entries[1].Content != "<p>Hello, world!</p>" {
  76. t.Errorf("Incorrect entry content, got: %s", feed.Entries[1].Content)
  77. }
  78. }
  79. func TestParseFeedWithDescription(t *testing.T) {
  80. data := `{
  81. "version": "https://jsonfeed.org/version/1",
  82. "title": "My Example Feed",
  83. "description": "This is a sample feed description.",
  84. "home_page_url": "https://example.org/",
  85. "feed_url": "https://example.org/feed.json",
  86. "items": []
  87. }`
  88. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. if feed.Description != "This is a sample feed description." {
  93. t.Errorf("Incorrect description, got: %s", feed.Description)
  94. }
  95. }
  96. func TestParsePodcast(t *testing.T) {
  97. data := `{
  98. "version": "https://jsonfeed.org/version/1",
  99. "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",
  100. "title": "The Record",
  101. "home_page_url": "http://therecord.co/",
  102. "feed_url": "http://therecord.co/feed.json",
  103. "items": [
  104. {
  105. "id": "http://therecord.co/chris-parrish",
  106. "title": "Special #1 - Chris Parrish",
  107. "url": "http://therecord.co/chris-parrish",
  108. "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.",
  109. "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.",
  110. "summary": "Brent interviews Chris Parrish, co-host of The Record and one-half of Aged & Distilled.",
  111. "date_published": "2014-05-09T14:04:00-07:00",
  112. "attachments": [
  113. {
  114. "url": "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a",
  115. "mime_type": "audio/x-m4a",
  116. "size_in_bytes": 89970236,
  117. "duration_in_seconds": 6629
  118. }
  119. ]
  120. }
  121. ]
  122. }`
  123. feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. if feed.Title != "The Record" {
  128. t.Errorf("Incorrect title, got: %s", feed.Title)
  129. }
  130. if feed.FeedURL != "http://therecord.co/feed.json" {
  131. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  132. }
  133. if feed.SiteURL != "http://therecord.co/" {
  134. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  135. }
  136. if len(feed.Entries) != 1 {
  137. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  138. }
  139. if feed.Entries[0].Hash != "6b678e57962a1b001e4e873756563cdc08bbd06ca561e764e0baa9a382485797" {
  140. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  141. }
  142. if feed.Entries[0].URL != "http://therecord.co/chris-parrish" {
  143. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  144. }
  145. if feed.Entries[0].Title != "Special #1 - Chris Parrish" {
  146. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
  147. }
  148. 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.` {
  149. t.Errorf(`Incorrect entry content, got: "%s"`, feed.Entries[0].Content)
  150. }
  151. location, _ := time.LoadLocation("America/Vancouver")
  152. if !feed.Entries[0].Date.Equal(time.Date(2014, time.May, 9, 14, 4, 0, 0, location)) {
  153. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  154. }
  155. if len(feed.Entries[0].Enclosures) != 1 {
  156. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  157. }
  158. if feed.Entries[0].Enclosures[0].URL != "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a" {
  159. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  160. }
  161. if feed.Entries[0].Enclosures[0].MimeType != "audio/x-m4a" {
  162. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  163. }
  164. if feed.Entries[0].Enclosures[0].Size != 89970236 {
  165. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  166. }
  167. }
  168. func TestParseFeedWithFeedURLWithTrailingSpace(t *testing.T) {
  169. data := `{
  170. "version": "https://jsonfeed.org/version/1",
  171. "title": "My Example Feed",
  172. "home_page_url": "https://example.org/",
  173. "feed_url": "https://example.org/feed.json ",
  174. "items": []
  175. }`
  176. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. if feed.FeedURL != "https://example.org/feed.json" {
  181. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  182. }
  183. }
  184. func TestParseFeedWithRelativeFeedURL(t *testing.T) {
  185. data := `{
  186. "version": "https://jsonfeed.org/version/1",
  187. "title": "My Example Feed",
  188. "home_page_url": "https://example.org/",
  189. "feed_url": "/feed.json",
  190. "items": []
  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.FeedURL != "https://example.org/feed.json" {
  197. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  198. }
  199. }
  200. func TestParseFeedSiteURLWithTrailingSpace(t *testing.T) {
  201. data := `{
  202. "version": "https://jsonfeed.org/version/1",
  203. "title": "My Example Feed",
  204. "home_page_url": "https://example.org/ ",
  205. "feed_url": "https://example.org/feed.json",
  206. "items": []
  207. }`
  208. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  209. if err != nil {
  210. t.Fatal(err)
  211. }
  212. if feed.SiteURL != "https://example.org/" {
  213. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  214. }
  215. }
  216. func TestParseFeedWithRelativeSiteURL(t *testing.T) {
  217. data := `{
  218. "version": "https://jsonfeed.org/version/1",
  219. "title": "My Example Feed",
  220. "home_page_url": "/home ",
  221. "feed_url": "https://example.org/feed.json",
  222. "items": []
  223. }`
  224. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. if feed.SiteURL != "https://example.org/home" {
  229. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  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 TestParseFeedWithoutHomePage(t *testing.T) {
  255. data := `{
  256. "version": "https://jsonfeed.org/version/1",
  257. "feed_url": "https://example.org/feed.json",
  258. "title": "Some test",
  259. "items": [
  260. {
  261. "id": "2347259",
  262. "url": "https://example.org/2347259",
  263. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  264. "date_published": "2016-02-09T14:22:00-07:00"
  265. }
  266. ]
  267. }`
  268. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  269. if err != nil {
  270. t.Fatal(err)
  271. }
  272. if feed.SiteURL != "https://example.org/feed.json" {
  273. t.Errorf("Incorrect title, got: %s", feed.Title)
  274. }
  275. }
  276. func TestParseFeedWithoutFeedURL(t *testing.T) {
  277. data := `{
  278. "version": "https://jsonfeed.org/version/1",
  279. "title": "Some test",
  280. "items": [
  281. {
  282. "id": "2347259",
  283. "url": "https://example.org/2347259",
  284. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  285. "date_published": "2016-02-09T14:22:00-07:00"
  286. }
  287. ]
  288. }`
  289. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  290. if err != nil {
  291. t.Fatal(err)
  292. }
  293. if feed.SiteURL != "https://example.org/feed.json" {
  294. t.Errorf("Incorrect title, got: %s", feed.Title)
  295. }
  296. }
  297. func TestParseItemWithoutAttachmentURL(t *testing.T) {
  298. data := `{
  299. "version": "https://jsonfeed.org/version/1",
  300. "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",
  301. "title": "The Record",
  302. "home_page_url": "http://therecord.co/",
  303. "feed_url": "http://therecord.co/feed.json",
  304. "items": [
  305. {
  306. "id": "http://therecord.co/chris-parrish",
  307. "title": "Special #1 - Chris Parrish",
  308. "url": "http://therecord.co/chris-parrish",
  309. "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.",
  310. "date_published": "2014-05-09T14:04:00-07:00",
  311. "attachments": [
  312. {
  313. "url": "",
  314. "mime_type": "audio/x-m4a",
  315. "size_in_bytes": 0
  316. }
  317. ]
  318. }
  319. ]
  320. }`
  321. feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
  322. if err != nil {
  323. t.Fatal(err)
  324. }
  325. if len(feed.Entries) != 1 {
  326. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  327. }
  328. if len(feed.Entries[0].Enclosures) != 0 {
  329. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  330. }
  331. }
  332. func TestParseItemWithRelativeURL(t *testing.T) {
  333. data := `{
  334. "version": "https://jsonfeed.org/version/1",
  335. "title": "Example",
  336. "home_page_url": "https://example.org/",
  337. "feed_url": "https://example.org/feed.json",
  338. "items": [
  339. {
  340. "id": "2347259",
  341. "url": "something.html",
  342. "date_published": "2016-02-09T14:22:00-07:00"
  343. }
  344. ]
  345. }`
  346. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  347. if err != nil {
  348. t.Fatal(err)
  349. }
  350. if feed.Entries[0].URL != "https://example.org/something.html" {
  351. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  352. }
  353. }
  354. func TestParseItemWithExternalURLAndNoURL(t *testing.T) {
  355. data := `{
  356. "version": "https://jsonfeed.org/version/1",
  357. "title": "Example",
  358. "home_page_url": "https://example.org/",
  359. "feed_url": "https://example.org/feed.json",
  360. "items": [
  361. {
  362. "id": "1234259",
  363. "external_url": "some_page.html"
  364. }
  365. ]
  366. }`
  367. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  368. if err != nil {
  369. t.Fatal(err)
  370. }
  371. if len(feed.Entries) != 1 {
  372. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  373. }
  374. if feed.Entries[0].URL != "https://example.org/some_page.html" {
  375. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  376. }
  377. }
  378. func TestParseItemWithExternalURLAndURL(t *testing.T) {
  379. data := `{
  380. "version": "https://jsonfeed.org/version/1",
  381. "title": "Example",
  382. "home_page_url": "https://example.org/",
  383. "feed_url": "https://example.org/feed.json",
  384. "items": [
  385. {
  386. "id": "1234259",
  387. "url": "https://example.org/article",
  388. "external_url": "https://example.org/another-article"
  389. }
  390. ]
  391. }`
  392. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  393. if err != nil {
  394. t.Fatal(err)
  395. }
  396. if len(feed.Entries) != 1 {
  397. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  398. }
  399. if feed.Entries[0].URL != "https://example.org/article" {
  400. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  401. }
  402. }
  403. func TestParseItemWithLegacyAuthorField(t *testing.T) {
  404. data := `{
  405. "version": "https://jsonfeed.org/version/1",
  406. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  407. "title": "Brent Simmons’s Microblog",
  408. "home_page_url": "https://example.org/",
  409. "feed_url": "https://example.org/feed.json",
  410. "author": {
  411. "name": "Brent Simmons",
  412. "url": "http://example.org/",
  413. "avatar": "https://example.org/avatar.png"
  414. },
  415. "items": [
  416. {
  417. "id": "2347259",
  418. "url": "https://example.org/2347259",
  419. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  420. "date_published": "2016-02-09T14:22:00-07:00"
  421. }
  422. ]
  423. }`
  424. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  425. if err != nil {
  426. t.Fatal(err)
  427. }
  428. if len(feed.Entries) != 1 {
  429. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  430. }
  431. if feed.Entries[0].Author != "Brent Simmons" {
  432. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  433. }
  434. }
  435. func TestParseItemWithMultipleAuthorFields(t *testing.T) {
  436. data := `{
  437. "version": "https://jsonfeed.org/version/1.1",
  438. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  439. "title": "Brent Simmons’s Microblog",
  440. "home_page_url": "https://example.org/",
  441. "feed_url": "https://example.org/feed.json",
  442. "author": {
  443. "name": "Deprecated Author Field",
  444. "url": "http://example.org/",
  445. "avatar": "https://example.org/avatar.png"
  446. },
  447. "authors": [
  448. {
  449. "name": "Brent Simmons",
  450. "url": "http://example.org/",
  451. "avatar": "https://example.org/avatar.png"
  452. }
  453. ],
  454. "items": [
  455. {
  456. "id": "2347259",
  457. "url": "https://example.org/2347259",
  458. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  459. "date_published": "2016-02-09T14:22:00-07:00"
  460. }
  461. ]
  462. }`
  463. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  464. if err != nil {
  465. t.Fatal(err)
  466. }
  467. if len(feed.Entries) != 1 {
  468. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  469. }
  470. if feed.Entries[0].Author != "Brent Simmons, Deprecated Author Field" {
  471. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  472. }
  473. }
  474. func TestParseItemWithMultipleDuplicateAuthors(t *testing.T) {
  475. data := `{
  476. "version": "https://jsonfeed.org/version/1.1",
  477. "title": "Example",
  478. "home_page_url": "https://example.org/",
  479. "feed_url": "https://example.org/feed.json",
  480. "items": [
  481. {
  482. "id": "2347259",
  483. "url": "https://example.org/2347259",
  484. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  485. "date_published": "2016-02-09T14:22:00-07:00",
  486. "authors": [
  487. {
  488. "name": "Author B",
  489. "url": "http://example.org/",
  490. "avatar": "https://example.org/avatar.png"
  491. },
  492. {
  493. "name": "Author A",
  494. "url": "http://example.org/",
  495. "avatar": "https://example.org/avatar.png"
  496. },
  497. {
  498. "name": "Author B",
  499. "url": "http://example.org/",
  500. "avatar": "https://example.org/avatar.png"
  501. }
  502. ]
  503. }
  504. ]
  505. }`
  506. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  507. if err != nil {
  508. t.Fatal(err)
  509. }
  510. if len(feed.Entries) != 1 {
  511. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  512. }
  513. if feed.Entries[0].Author != "Author A, Author B" {
  514. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  515. }
  516. }
  517. func TestParseItemWithAuthorsObject(t *testing.T) {
  518. data := `{
  519. "version": "https://jsonfeed.org/version/1.1",
  520. "title": "Example",
  521. "home_page_url": "https://example.org/",
  522. "feed_url": "https://example.org/feed.json",
  523. "items": [
  524. {
  525. "id": "1",
  526. "title": "Example Item",
  527. "url": "https://example.org/item",
  528. "date_published": "2020-01-02T03:04:05Z",
  529. "authors": {
  530. "name": "Example Author"
  531. }
  532. }
  533. ]
  534. }`
  535. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  536. if err != nil {
  537. t.Fatal(err)
  538. }
  539. if len(feed.Entries) != 1 {
  540. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  541. }
  542. if feed.Entries[0].Author != "Example Author" {
  543. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  544. }
  545. }
  546. func TestParseItemWithInvalidDate(t *testing.T) {
  547. data := `{
  548. "version": "https://jsonfeed.org/version/1",
  549. "title": "My Example Feed",
  550. "home_page_url": "https://example.org/",
  551. "feed_url": "https://example.org/feed.json",
  552. "items": [
  553. {
  554. "id": "2347259",
  555. "url": "https://example.org/2347259",
  556. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  557. "date_published": "Tomorrow"
  558. }
  559. ]
  560. }`
  561. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  562. if err != nil {
  563. t.Fatal(err)
  564. }
  565. if len(feed.Entries) != 1 {
  566. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  567. }
  568. duration := time.Since(feed.Entries[0].Date)
  569. if duration.Seconds() > 1 {
  570. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  571. }
  572. }
  573. func TestParseItemWithMissingTitleUsesSummaryFallback(t *testing.T) {
  574. data := `{
  575. "version": "https://jsonfeed.org/version/1",
  576. "title": "My Example Feed",
  577. "home_page_url": "https://example.org/",
  578. "feed_url": "https://example.org/feed.json",
  579. "items": [
  580. {
  581. "summary": "Summary title",
  582. "content_text": "Content text title",
  583. "content_html": "<p>HTML title</p>"
  584. }
  585. ]
  586. }`
  587. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  588. if err != nil {
  589. t.Fatal(err)
  590. }
  591. if len(feed.Entries) != 1 {
  592. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  593. }
  594. if feed.Entries[0].Title != "Summary title" {
  595. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  596. }
  597. }
  598. func TestParseItemWithMissingTitleUsesContentTextFallback(t *testing.T) {
  599. data := `{
  600. "version": "https://jsonfeed.org/version/1",
  601. "title": "My Example Feed",
  602. "home_page_url": "https://example.org/",
  603. "feed_url": "https://example.org/feed.json",
  604. "items": [
  605. {
  606. "summary": " ",
  607. "content_text": "Content text title",
  608. "content_html": "<p>HTML title</p>"
  609. }
  610. ]
  611. }`
  612. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  613. if err != nil {
  614. t.Fatal(err)
  615. }
  616. if len(feed.Entries) != 1 {
  617. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  618. }
  619. if feed.Entries[0].Title != "Content text title" {
  620. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  621. }
  622. }
  623. func TestParseItemWithMissingTitleUsesHTMLFallback(t *testing.T) {
  624. data := `{
  625. "version": "https://jsonfeed.org/version/1",
  626. "title": "My Example Feed",
  627. "home_page_url": "https://example.org/",
  628. "feed_url": "https://example.org/feed.json",
  629. "items": [
  630. {
  631. "summary": "",
  632. "content_text": "",
  633. "content_html": "<p>HTML title</p>"
  634. }
  635. ]
  636. }`
  637. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  638. if err != nil {
  639. t.Fatal(err)
  640. }
  641. if len(feed.Entries) != 1 {
  642. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  643. }
  644. if feed.Entries[0].Title != "HTML title" {
  645. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  646. }
  647. }
  648. func TestParseItemWithMissingTitleUsesURLFallback(t *testing.T) {
  649. data := `{
  650. "version": "https://jsonfeed.org/version/1",
  651. "title": "My Example Feed",
  652. "home_page_url": "https://example.org/",
  653. "feed_url": "https://example.org/feed.json",
  654. "items": [
  655. {
  656. "url": "https://example.org/item"
  657. }
  658. ]
  659. }`
  660. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  661. if err != nil {
  662. t.Fatal(err)
  663. }
  664. if len(feed.Entries) != 1 {
  665. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  666. }
  667. if feed.Entries[0].Title != "https://example.org/item" {
  668. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  669. }
  670. }
  671. func TestParseItemWithTooLongUnicodeTitle(t *testing.T) {
  672. data := `{
  673. "version": "https://jsonfeed.org/version/1",
  674. "title": "My Example Feed",
  675. "home_page_url": "https://example.org/",
  676. "feed_url": "https://example.org/feed.json",
  677. "items": [
  678. {
  679. "title": "I’m riding my electric bike and came across this castle. It’s called “Schloss Richmond”. 🚴‍♂️"
  680. }
  681. ]
  682. }`
  683. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  684. if err != nil {
  685. t.Fatal(err)
  686. }
  687. if len(feed.Entries) != 1 {
  688. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  689. }
  690. if len(feed.Entries[0].Title) != 110 {
  691. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  692. }
  693. if len([]rune(feed.Entries[0].Title)) != 93 {
  694. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  695. }
  696. }
  697. func TestParseItemTitleWithXMLTags(t *testing.T) {
  698. data := `{
  699. "version": "https://jsonfeed.org/version/1",
  700. "title": "My Example Feed",
  701. "home_page_url": "https://example.org/",
  702. "feed_url": "https://example.org/feed.json",
  703. "items": [
  704. {
  705. "title": "</example>"
  706. }
  707. ]
  708. }`
  709. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  710. if err != nil {
  711. t.Fatal(err)
  712. }
  713. if len(feed.Entries) != 1 {
  714. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  715. }
  716. if feed.Entries[0].Title != "</example>" {
  717. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  718. }
  719. }
  720. func TestParseItemHashPrefersIDOverURL(t *testing.T) {
  721. data := `{
  722. "version": "https://jsonfeed.org/version/1",
  723. "title": "Example",
  724. "home_page_url": "https://example.org/",
  725. "feed_url": "https://example.org/feed.json",
  726. "items": [
  727. {
  728. "id": "id-value",
  729. "url": "https://example.org/article"
  730. }
  731. ]
  732. }`
  733. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  734. if err != nil {
  735. t.Fatal(err)
  736. }
  737. expected := sha256.Sum256([]byte("id-value"))
  738. if feed.Entries[0].Hash != hex.EncodeToString(expected[:]) {
  739. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  740. }
  741. }
  742. func TestParseItemHashUsesURLWhenNoID(t *testing.T) {
  743. data := `{
  744. "version": "https://jsonfeed.org/version/1",
  745. "title": "Example",
  746. "home_page_url": "https://example.org/",
  747. "feed_url": "https://example.org/feed.json",
  748. "items": [
  749. {
  750. "url": "https://example.org/article"
  751. }
  752. ]
  753. }`
  754. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  755. if err != nil {
  756. t.Fatal(err)
  757. }
  758. expected := sha256.Sum256([]byte("https://example.org/article"))
  759. if feed.Entries[0].Hash != hex.EncodeToString(expected[:]) {
  760. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  761. }
  762. }
  763. func TestParseItemHashUsesExternalURLFallback(t *testing.T) {
  764. data := `{
  765. "version": "https://jsonfeed.org/version/1",
  766. "title": "Example",
  767. "home_page_url": "https://example.org/",
  768. "feed_url": "https://example.org/feed.json",
  769. "items": [
  770. {
  771. "external_url": "https://example.org/external"
  772. }
  773. ]
  774. }`
  775. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  776. if err != nil {
  777. t.Fatal(err)
  778. }
  779. if len(feed.Entries) != 1 {
  780. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  781. }
  782. expected := sha256.Sum256([]byte("https://example.org/external"))
  783. if feed.Entries[0].Hash != hex.EncodeToString(expected[:]) {
  784. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  785. }
  786. }
  787. func TestParseItemHashFallsBackToContent(t *testing.T) {
  788. data := `{
  789. "version": "https://jsonfeed.org/version/1",
  790. "title": "Example",
  791. "home_page_url": "https://example.org/",
  792. "feed_url": "https://example.org/feed.json",
  793. "items": [
  794. {
  795. "content_text": "Text",
  796. "content_html": "<p>HTML</p>",
  797. "summary": "Summary"
  798. }
  799. ]
  800. }`
  801. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  802. if err != nil {
  803. t.Fatal(err)
  804. }
  805. expected := sha256.Sum256([]byte("Text<p>HTML</p>Summary"))
  806. if feed.Entries[0].Hash != hex.EncodeToString(expected[:]) {
  807. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  808. }
  809. }
  810. func TestParseItemTags(t *testing.T) {
  811. data := `{
  812. "version": "https://jsonfeed.org/version/1",
  813. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  814. "title": "Brent Simmons’s Microblog",
  815. "home_page_url": "https://example.org/",
  816. "feed_url": "https://example.org/feed.json",
  817. "author": {
  818. "name": "Brent Simmons",
  819. "url": "http://example.org/",
  820. "avatar": "https://example.org/avatar.png"
  821. },
  822. "items": [
  823. {
  824. "id": "2347259",
  825. "url": "https://example.org/2347259",
  826. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  827. "date_published": "2016-02-09T14:22:00-07:00",
  828. "tags": [
  829. " tag 1",
  830. " ",
  831. "tag 2",
  832. "tag 2",
  833. "aaa"
  834. ]
  835. }
  836. ]
  837. }`
  838. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  839. if err != nil {
  840. t.Fatal(err)
  841. }
  842. if len(feed.Entries) != 1 {
  843. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  844. }
  845. if len(feed.Entries[0].Tags) != 3 {
  846. t.Errorf("Incorrect number of Tags, got: %d", len(feed.Entries[0].Tags))
  847. }
  848. expected := []string{"aaa", "tag 1", "tag 2"}
  849. for i, tag := range feed.Entries[0].Tags {
  850. if tag != expected[i] {
  851. t.Errorf("Incorrect entry tag, got %q instead of %q", tag, expected[i])
  852. }
  853. }
  854. }
  855. func TestParseFeedFavicon(t *testing.T) {
  856. data := `{
  857. "version": "https://jsonfeed.org/version/1",
  858. "title": "My Example Feed",
  859. "favicon": "https://example.org/jsonfeed/favicon.png",
  860. "home_page_url": "https://example.org/",
  861. "feed_url": "https://example.org/feed.json",
  862. "items": [
  863. {
  864. "id": "2",
  865. "content_text": "This is a second item.",
  866. "url": "https://example.org/second-item"
  867. },
  868. {
  869. "id": "1",
  870. "content_html": "<p>Hello, world!</p>",
  871. "url": "https://example.org/initial-post"
  872. }
  873. ]
  874. }`
  875. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  876. if err != nil {
  877. t.Fatal(err)
  878. }
  879. if feed.IconURL != "https://example.org/jsonfeed/favicon.png" {
  880. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  881. }
  882. }
  883. func TestParseFeedIcon(t *testing.T) {
  884. data := `{
  885. "version": "https://jsonfeed.org/version/1",
  886. "title": "My Example Feed",
  887. "icon": "https://example.org/jsonfeed/icon.png",
  888. "home_page_url": "https://example.org/",
  889. "feed_url": "https://example.org/feed.json",
  890. "items": [
  891. {
  892. "id": "2",
  893. "content_text": "This is a second item.",
  894. "url": "https://example.org/second-item"
  895. },
  896. {
  897. "id": "1",
  898. "content_html": "<p>Hello, world!</p>",
  899. "url": "https://example.org/initial-post"
  900. }
  901. ]
  902. }`
  903. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  904. if err != nil {
  905. t.Fatal(err)
  906. }
  907. if feed.IconURL != "https://example.org/jsonfeed/icon.png" {
  908. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  909. }
  910. }
  911. func TestParseFeedWithRelativeAttachmentURL(t *testing.T) {
  912. data := `{
  913. "version": "https://jsonfeed.org/version/1",
  914. "title": "My Example Feed",
  915. "home_page_url": "https://example.org/",
  916. "feed_url": "https://example.org/feed.json",
  917. "items": [
  918. {
  919. "id": "2",
  920. "content_text": "This is a second item.",
  921. "url": "https://example.org/second-item",
  922. "attachments": [
  923. {
  924. "url": " /attachment.mp3 ",
  925. "mime_type": "audio/mpeg",
  926. "size_in_bytes": 123456
  927. }
  928. ]
  929. }
  930. ]
  931. }`
  932. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  933. if err != nil {
  934. t.Fatal(err)
  935. }
  936. if len(feed.Entries[0].Enclosures) != 1 {
  937. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  938. }
  939. if feed.Entries[0].Enclosures[0].URL != "https://example.org/attachment.mp3" {
  940. t.Errorf("Incorrect enclosure URL, got: %q", feed.Entries[0].Enclosures[0].URL)
  941. }
  942. }
  943. func TestParseInvalidJSON(t *testing.T) {
  944. data := `garbage`
  945. _, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  946. if err == nil {
  947. t.Error("Parse should returns an error")
  948. }
  949. }
  950. func TestParseNullJSONFeed(t *testing.T) {
  951. data := `null`
  952. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  953. if err != nil {
  954. t.Fatalf("Unexpected error when parsing null feed: %v", err)
  955. }
  956. if feed == nil {
  957. t.Fatalf("Feed should not be nil")
  958. }
  959. }