parser_test.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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. }
  960. func TestParseFeedWithLanguage(t *testing.T) {
  961. data := `{
  962. "version": "https://jsonfeed.org/version/1.1",
  963. "title": "Example",
  964. "home_page_url": "https://example.org/",
  965. "feed_url": "https://example.org/feed.json",
  966. "language": "en-US",
  967. "items": []
  968. }`
  969. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  970. if err != nil {
  971. t.Fatal(err)
  972. }
  973. if feed.Language != "en-us" {
  974. t.Errorf("Incorrect language, got: %q", feed.Language)
  975. }
  976. }
  977. func TestParseFeedWithoutLanguage(t *testing.T) {
  978. data := `{
  979. "version": "https://jsonfeed.org/version/1.1",
  980. "title": "Example",
  981. "home_page_url": "https://example.org/",
  982. "feed_url": "https://example.org/feed.json",
  983. "items": []
  984. }`
  985. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  986. if err != nil {
  987. t.Fatal(err)
  988. }
  989. if feed.Language != "" {
  990. t.Errorf("Expected empty language, got: %q", feed.Language)
  991. }
  992. }
  993. func TestParseItemWithLanguage(t *testing.T) {
  994. data := `{
  995. "version": "https://jsonfeed.org/version/1.1",
  996. "title": "Example",
  997. "home_page_url": "https://example.org/",
  998. "feed_url": "https://example.org/feed.json",
  999. "language": "en-US",
  1000. "items": [
  1001. {
  1002. "id": "1",
  1003. "url": "https://example.org/item",
  1004. "content_text": "Bonjour",
  1005. "language": "fr-FR"
  1006. }
  1007. ]
  1008. }`
  1009. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  1010. if err != nil {
  1011. t.Fatal(err)
  1012. }
  1013. if len(feed.Entries) != 1 {
  1014. t.Fatalf("Expected 1 entry, got: %d", len(feed.Entries))
  1015. }
  1016. if feed.Entries[0].Language != "fr-fr" {
  1017. t.Errorf("Incorrect entry language, got: %q", feed.Entries[0].Language)
  1018. }
  1019. }
  1020. func TestParseItemWithoutLanguage(t *testing.T) {
  1021. data := `{
  1022. "version": "https://jsonfeed.org/version/1.1",
  1023. "title": "Example",
  1024. "home_page_url": "https://example.org/",
  1025. "feed_url": "https://example.org/feed.json",
  1026. "language": "en-US",
  1027. "items": [
  1028. {
  1029. "id": "1",
  1030. "url": "https://example.org/item",
  1031. "content_text": "Hello"
  1032. }
  1033. ]
  1034. }`
  1035. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  1036. if err != nil {
  1037. t.Fatal(err)
  1038. }
  1039. if len(feed.Entries) != 1 {
  1040. t.Fatalf("Expected 1 entry, got: %d", len(feed.Entries))
  1041. }
  1042. if feed.Entries[0].Language != "" {
  1043. t.Errorf("Expected empty entry language, got: %q", feed.Entries[0].Language)
  1044. }
  1045. }