parser_test.go 28 KB

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