parser_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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.FeedURL != "https://example.org/feed.json" {
  39. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  40. }
  41. if feed.SiteURL != "https://example.org/" {
  42. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  43. }
  44. if feed.IconURL != "https://micro.blog/jsonfeed/favicon.png" {
  45. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  46. }
  47. if len(feed.Entries) != 2 {
  48. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  49. }
  50. if feed.Entries[0].Hash != "d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" {
  51. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  52. }
  53. if feed.Entries[0].URL != "https://example.org/second-item" {
  54. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  55. }
  56. if feed.Entries[0].Title != "This is a second item." {
  57. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
  58. }
  59. if feed.Entries[0].Content != "This is a second item." {
  60. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  61. }
  62. if feed.Entries[1].Hash != "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b" {
  63. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[1].Hash)
  64. }
  65. if feed.Entries[1].URL != "https://example.org/initial-post" {
  66. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[1].URL)
  67. }
  68. if feed.Entries[1].Title != "Hello, world!" {
  69. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[1].Title)
  70. }
  71. if feed.Entries[1].Content != "<p>Hello, world!</p>" {
  72. t.Errorf("Incorrect entry content, got: %s", feed.Entries[1].Content)
  73. }
  74. }
  75. func TestParsePodcast(t *testing.T) {
  76. data := `{
  77. "version": "https://jsonfeed.org/version/1",
  78. "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",
  79. "title": "The Record",
  80. "home_page_url": "http://therecord.co/",
  81. "feed_url": "http://therecord.co/feed.json",
  82. "items": [
  83. {
  84. "id": "http://therecord.co/chris-parrish",
  85. "title": "Special #1 - Chris Parrish",
  86. "url": "http://therecord.co/chris-parrish",
  87. "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.",
  88. "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.",
  89. "summary": "Brent interviews Chris Parrish, co-host of The Record and one-half of Aged & Distilled.",
  90. "date_published": "2014-05-09T14:04:00-07:00",
  91. "attachments": [
  92. {
  93. "url": "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a",
  94. "mime_type": "audio/x-m4a",
  95. "size_in_bytes": 89970236,
  96. "duration_in_seconds": 6629
  97. }
  98. ]
  99. }
  100. ]
  101. }`
  102. feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if feed.Title != "The Record" {
  107. t.Errorf("Incorrect title, got: %s", feed.Title)
  108. }
  109. if feed.FeedURL != "http://therecord.co/feed.json" {
  110. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  111. }
  112. if feed.SiteURL != "http://therecord.co/" {
  113. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  114. }
  115. if len(feed.Entries) != 1 {
  116. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  117. }
  118. if feed.Entries[0].Hash != "6b678e57962a1b001e4e873756563cdc08bbd06ca561e764e0baa9a382485797" {
  119. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  120. }
  121. if feed.Entries[0].URL != "http://therecord.co/chris-parrish" {
  122. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  123. }
  124. if feed.Entries[0].Title != "Special #1 - Chris Parrish" {
  125. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
  126. }
  127. 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.` {
  128. t.Errorf(`Incorrect entry content, got: "%s"`, feed.Entries[0].Content)
  129. }
  130. location, _ := time.LoadLocation("America/Vancouver")
  131. if !feed.Entries[0].Date.Equal(time.Date(2014, time.May, 9, 14, 4, 0, 0, location)) {
  132. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  133. }
  134. if len(feed.Entries[0].Enclosures) != 1 {
  135. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  136. }
  137. if feed.Entries[0].Enclosures[0].URL != "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a" {
  138. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  139. }
  140. if feed.Entries[0].Enclosures[0].MimeType != "audio/x-m4a" {
  141. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  142. }
  143. if feed.Entries[0].Enclosures[0].Size != 89970236 {
  144. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  145. }
  146. }
  147. func TestParseFeedWithFeedURLWithTrailingSpace(t *testing.T) {
  148. data := `{
  149. "version": "https://jsonfeed.org/version/1",
  150. "title": "My Example Feed",
  151. "home_page_url": "https://example.org/",
  152. "feed_url": "https://example.org/feed.json ",
  153. "items": []
  154. }`
  155. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. if feed.FeedURL != "https://example.org/feed.json" {
  160. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  161. }
  162. }
  163. func TestParseFeedWithRelativeFeedURL(t *testing.T) {
  164. data := `{
  165. "version": "https://jsonfeed.org/version/1",
  166. "title": "My Example Feed",
  167. "home_page_url": "https://example.org/",
  168. "feed_url": "/feed.json",
  169. "items": []
  170. }`
  171. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. if feed.FeedURL != "https://example.org/feed.json" {
  176. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  177. }
  178. }
  179. func TestParseFeedSiteURLWithTrailingSpace(t *testing.T) {
  180. data := `{
  181. "version": "https://jsonfeed.org/version/1",
  182. "title": "My Example Feed",
  183. "home_page_url": "https://example.org/ ",
  184. "feed_url": "https://example.org/feed.json",
  185. "items": []
  186. }`
  187. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. if feed.SiteURL != "https://example.org/" {
  192. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  193. }
  194. }
  195. func TestParseFeedWithRelativeSiteURL(t *testing.T) {
  196. data := `{
  197. "version": "https://jsonfeed.org/version/1",
  198. "title": "My Example Feed",
  199. "home_page_url": "/home ",
  200. "feed_url": "https://example.org/feed.json",
  201. "items": []
  202. }`
  203. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. if feed.SiteURL != "https://example.org/home" {
  208. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  209. }
  210. }
  211. func TestParseFeedWithoutTitle(t *testing.T) {
  212. data := `{
  213. "version": "https://jsonfeed.org/version/1",
  214. "home_page_url": "https://example.org/",
  215. "feed_url": "https://example.org/feed.json",
  216. "items": [
  217. {
  218. "id": "2347259",
  219. "url": "https://example.org/2347259",
  220. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  221. "date_published": "2016-02-09T14:22:00-07:00"
  222. }
  223. ]
  224. }`
  225. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. if feed.Title != "https://example.org/" {
  230. t.Errorf("Incorrect title, got: %s", feed.Title)
  231. }
  232. }
  233. func TestParseFeedWithoutHomePage(t *testing.T) {
  234. data := `{
  235. "version": "https://jsonfeed.org/version/1",
  236. "feed_url": "https://example.org/feed.json",
  237. "title": "Some test",
  238. "items": [
  239. {
  240. "id": "2347259",
  241. "url": "https://example.org/2347259",
  242. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  243. "date_published": "2016-02-09T14:22:00-07:00"
  244. }
  245. ]
  246. }`
  247. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  248. if err != nil {
  249. t.Fatal(err)
  250. }
  251. if feed.SiteURL != "https://example.org/feed.json" {
  252. t.Errorf("Incorrect title, got: %s", feed.Title)
  253. }
  254. }
  255. func TestParseFeedWithoutFeedURL(t *testing.T) {
  256. data := `{
  257. "version": "https://jsonfeed.org/version/1",
  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 TestParseItemWithoutAttachmentURL(t *testing.T) {
  277. data := `{
  278. "version": "https://jsonfeed.org/version/1",
  279. "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",
  280. "title": "The Record",
  281. "home_page_url": "http://therecord.co/",
  282. "feed_url": "http://therecord.co/feed.json",
  283. "items": [
  284. {
  285. "id": "http://therecord.co/chris-parrish",
  286. "title": "Special #1 - Chris Parrish",
  287. "url": "http://therecord.co/chris-parrish",
  288. "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.",
  289. "date_published": "2014-05-09T14:04:00-07:00",
  290. "attachments": [
  291. {
  292. "url": "",
  293. "mime_type": "audio/x-m4a",
  294. "size_in_bytes": 0
  295. }
  296. ]
  297. }
  298. ]
  299. }`
  300. feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
  301. if err != nil {
  302. t.Fatal(err)
  303. }
  304. if len(feed.Entries) != 1 {
  305. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  306. }
  307. if len(feed.Entries[0].Enclosures) != 0 {
  308. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  309. }
  310. }
  311. func TestParseItemWithRelativeURL(t *testing.T) {
  312. data := `{
  313. "version": "https://jsonfeed.org/version/1",
  314. "title": "Example",
  315. "home_page_url": "https://example.org/",
  316. "feed_url": "https://example.org/feed.json",
  317. "items": [
  318. {
  319. "id": "2347259",
  320. "url": "something.html",
  321. "date_published": "2016-02-09T14:22:00-07:00"
  322. }
  323. ]
  324. }`
  325. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  326. if err != nil {
  327. t.Fatal(err)
  328. }
  329. if feed.Entries[0].URL != "https://example.org/something.html" {
  330. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  331. }
  332. }
  333. func TestParseItemWithLegacyAuthorField(t *testing.T) {
  334. data := `{
  335. "version": "https://jsonfeed.org/version/1",
  336. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  337. "title": "Brent Simmons’s Microblog",
  338. "home_page_url": "https://example.org/",
  339. "feed_url": "https://example.org/feed.json",
  340. "author": {
  341. "name": "Brent Simmons",
  342. "url": "http://example.org/",
  343. "avatar": "https://example.org/avatar.png"
  344. },
  345. "items": [
  346. {
  347. "id": "2347259",
  348. "url": "https://example.org/2347259",
  349. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  350. "date_published": "2016-02-09T14:22:00-07:00"
  351. }
  352. ]
  353. }`
  354. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  355. if err != nil {
  356. t.Fatal(err)
  357. }
  358. if len(feed.Entries) != 1 {
  359. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  360. }
  361. if feed.Entries[0].Author != "Brent Simmons" {
  362. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  363. }
  364. }
  365. func TestParseItemWithMultipleAuthorFields(t *testing.T) {
  366. data := `{
  367. "version": "https://jsonfeed.org/version/1.1",
  368. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  369. "title": "Brent Simmons’s Microblog",
  370. "home_page_url": "https://example.org/",
  371. "feed_url": "https://example.org/feed.json",
  372. "author": {
  373. "name": "Deprecated Author Field",
  374. "url": "http://example.org/",
  375. "avatar": "https://example.org/avatar.png"
  376. },
  377. "authors": [
  378. {
  379. "name": "Brent Simmons",
  380. "url": "http://example.org/",
  381. "avatar": "https://example.org/avatar.png"
  382. }
  383. ],
  384. "items": [
  385. {
  386. "id": "2347259",
  387. "url": "https://example.org/2347259",
  388. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  389. "date_published": "2016-02-09T14:22:00-07:00"
  390. }
  391. ]
  392. }`
  393. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  394. if err != nil {
  395. t.Fatal(err)
  396. }
  397. if len(feed.Entries) != 1 {
  398. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  399. }
  400. if feed.Entries[0].Author != "Brent Simmons, Deprecated Author Field" {
  401. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  402. }
  403. }
  404. func TestParseItemWithMultipleDuplicateAuthors(t *testing.T) {
  405. data := `{
  406. "version": "https://jsonfeed.org/version/1.1",
  407. "title": "Example",
  408. "home_page_url": "https://example.org/",
  409. "feed_url": "https://example.org/feed.json",
  410. "items": [
  411. {
  412. "id": "2347259",
  413. "url": "https://example.org/2347259",
  414. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  415. "date_published": "2016-02-09T14:22:00-07:00",
  416. "authors": [
  417. {
  418. "name": "Author B",
  419. "url": "http://example.org/",
  420. "avatar": "https://example.org/avatar.png"
  421. },
  422. {
  423. "name": "Author A",
  424. "url": "http://example.org/",
  425. "avatar": "https://example.org/avatar.png"
  426. },
  427. {
  428. "name": "Author B",
  429. "url": "http://example.org/",
  430. "avatar": "https://example.org/avatar.png"
  431. }
  432. ]
  433. }
  434. ]
  435. }`
  436. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  437. if err != nil {
  438. t.Fatal(err)
  439. }
  440. if len(feed.Entries) != 1 {
  441. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  442. }
  443. if feed.Entries[0].Author != "Author A, Author B" {
  444. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  445. }
  446. }
  447. func TestParseItemWithInvalidDate(t *testing.T) {
  448. data := `{
  449. "version": "https://jsonfeed.org/version/1",
  450. "title": "My Example Feed",
  451. "home_page_url": "https://example.org/",
  452. "feed_url": "https://example.org/feed.json",
  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": "Tomorrow"
  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. duration := time.Since(feed.Entries[0].Date)
  470. if duration.Seconds() > 1 {
  471. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  472. }
  473. }
  474. func TestParseItemWithoutTitleButWithURL(t *testing.T) {
  475. data := `{
  476. "version": "https://jsonfeed.org/version/1",
  477. "title": "My Example Feed",
  478. "home_page_url": "https://example.org/",
  479. "feed_url": "https://example.org/feed.json",
  480. "items": [
  481. {
  482. "url": "https://example.org/item"
  483. }
  484. ]
  485. }`
  486. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  487. if err != nil {
  488. t.Fatal(err)
  489. }
  490. if len(feed.Entries) != 1 {
  491. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  492. }
  493. if feed.Entries[0].Title != "https://example.org/item" {
  494. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  495. }
  496. }
  497. func TestParseItemWithoutTitleButWithSummary(t *testing.T) {
  498. data := `{
  499. "version": "https://jsonfeed.org/version/1",
  500. "title": "My Example Feed",
  501. "home_page_url": "https://example.org/",
  502. "feed_url": "https://example.org/feed.json",
  503. "items": [
  504. {
  505. "summary": "This is some text content."
  506. }
  507. ]
  508. }`
  509. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  510. if err != nil {
  511. t.Fatal(err)
  512. }
  513. if len(feed.Entries) != 1 {
  514. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  515. }
  516. if feed.Entries[0].Title != "This is some text content." {
  517. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  518. }
  519. }
  520. func TestParseItemWithoutTitleButWithHTMLContent(t *testing.T) {
  521. data := `{
  522. "version": "https://jsonfeed.org/version/1",
  523. "title": "My Example Feed",
  524. "home_page_url": "https://example.org/",
  525. "feed_url": "https://example.org/feed.json",
  526. "items": [
  527. {
  528. "content_html": "This is <strong>HTML</strong>."
  529. }
  530. ]
  531. }`
  532. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  533. if err != nil {
  534. t.Fatal(err)
  535. }
  536. if len(feed.Entries) != 1 {
  537. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  538. }
  539. if feed.Entries[0].Title != "This is HTML." {
  540. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  541. }
  542. }
  543. func TestParseItemWithoutTitleButWithTextContent(t *testing.T) {
  544. data := `{
  545. "version": "https://jsonfeed.org/version/1",
  546. "title": "My Example Feed",
  547. "home_page_url": "https://example.org/",
  548. "feed_url": "https://example.org/feed.json",
  549. "items": [
  550. {
  551. "content_text": "` + strings.Repeat("a", 200) + `"
  552. }
  553. ]
  554. }`
  555. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  556. if err != nil {
  557. t.Fatal(err)
  558. }
  559. if len(feed.Entries) != 1 {
  560. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  561. }
  562. if len(feed.Entries[0].Title) != 103 {
  563. t.Errorf("Incorrect entry title, got: %d", len(feed.Entries[0].Title))
  564. }
  565. if len([]rune(feed.Entries[0].Title)) != 101 {
  566. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  567. }
  568. }
  569. func TestParseItemWithTooLongUnicodeTitle(t *testing.T) {
  570. data := `{
  571. "version": "https://jsonfeed.org/version/1",
  572. "title": "My Example Feed",
  573. "home_page_url": "https://example.org/",
  574. "feed_url": "https://example.org/feed.json",
  575. "items": [
  576. {
  577. "title": "I’m riding my electric bike and came across this castle. It’s called “Schloss Richmond”. 🚴‍♂️"
  578. }
  579. ]
  580. }`
  581. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  582. if err != nil {
  583. t.Fatal(err)
  584. }
  585. if len(feed.Entries) != 1 {
  586. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  587. }
  588. if len(feed.Entries[0].Title) != 110 {
  589. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  590. }
  591. if len([]rune(feed.Entries[0].Title)) != 93 {
  592. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  593. }
  594. }
  595. func TestParseItemTitleWithXMLTags(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. "title": "</example>"
  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 != "</example>" {
  615. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  616. }
  617. }
  618. func TestParseItemWithoutID(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_text": "Some text."
  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].Hash != "13b4c5aecd1b6d749afcee968fbf9c80f1ed1bbdbe1aaf25cb34ebd01144bbe9" {
  638. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  639. }
  640. }
  641. func TestParseItemTags(t *testing.T) {
  642. data := `{
  643. "version": "https://jsonfeed.org/version/1",
  644. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  645. "title": "Brent Simmons’s Microblog",
  646. "home_page_url": "https://example.org/",
  647. "feed_url": "https://example.org/feed.json",
  648. "author": {
  649. "name": "Brent Simmons",
  650. "url": "http://example.org/",
  651. "avatar": "https://example.org/avatar.png"
  652. },
  653. "items": [
  654. {
  655. "id": "2347259",
  656. "url": "https://example.org/2347259",
  657. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  658. "date_published": "2016-02-09T14:22:00-07:00",
  659. "tags": [
  660. " tag 1",
  661. " ",
  662. "tag 2"
  663. ]
  664. }
  665. ]
  666. }`
  667. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  668. if err != nil {
  669. t.Fatal(err)
  670. }
  671. if len(feed.Entries[0].Tags) != 2 {
  672. t.Errorf("Incorrect number of Tags, got: %d", len(feed.Entries[0].Tags))
  673. }
  674. expected := "tag 2"
  675. result := feed.Entries[0].Tags[1]
  676. if result != expected {
  677. t.Errorf("Incorrect entry tag, got %q instead of %q", result, expected)
  678. }
  679. }
  680. func TestParseFeedFavicon(t *testing.T) {
  681. data := `{
  682. "version": "https://jsonfeed.org/version/1",
  683. "title": "My Example Feed",
  684. "favicon": "https://example.org/jsonfeed/favicon.png",
  685. "home_page_url": "https://example.org/",
  686. "feed_url": "https://example.org/feed.json",
  687. "items": [
  688. {
  689. "id": "2",
  690. "content_text": "This is a second item.",
  691. "url": "https://example.org/second-item"
  692. },
  693. {
  694. "id": "1",
  695. "content_html": "<p>Hello, world!</p>",
  696. "url": "https://example.org/initial-post"
  697. }
  698. ]
  699. }`
  700. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  701. if err != nil {
  702. t.Fatal(err)
  703. }
  704. if feed.IconURL != "https://example.org/jsonfeed/favicon.png" {
  705. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  706. }
  707. }
  708. func TestParseFeedIcon(t *testing.T) {
  709. data := `{
  710. "version": "https://jsonfeed.org/version/1",
  711. "title": "My Example Feed",
  712. "icon": "https://example.org/jsonfeed/icon.png",
  713. "home_page_url": "https://example.org/",
  714. "feed_url": "https://example.org/feed.json",
  715. "items": [
  716. {
  717. "id": "2",
  718. "content_text": "This is a second item.",
  719. "url": "https://example.org/second-item"
  720. },
  721. {
  722. "id": "1",
  723. "content_html": "<p>Hello, world!</p>",
  724. "url": "https://example.org/initial-post"
  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 feed.IconURL != "https://example.org/jsonfeed/icon.png" {
  733. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  734. }
  735. }
  736. func TestParseFeedWithRelativeAttachmentURL(t *testing.T) {
  737. data := `{
  738. "version": "https://jsonfeed.org/version/1",
  739. "title": "My Example Feed",
  740. "home_page_url": "https://example.org/",
  741. "feed_url": "https://example.org/feed.json",
  742. "items": [
  743. {
  744. "id": "2",
  745. "content_text": "This is a second item.",
  746. "url": "https://example.org/second-item",
  747. "attachments": [
  748. {
  749. "url": " /attachment.mp3 ",
  750. "mime_type": "audio/mpeg",
  751. "size_in_bytes": 123456
  752. }
  753. ]
  754. }
  755. ]
  756. }`
  757. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  758. if err != nil {
  759. t.Fatal(err)
  760. }
  761. if len(feed.Entries[0].Enclosures) != 1 {
  762. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  763. }
  764. if feed.Entries[0].Enclosures[0].URL != "https://example.org/attachment.mp3" {
  765. t.Errorf("Incorrect enclosure URL, got: %q", feed.Entries[0].Enclosures[0].URL)
  766. }
  767. }
  768. func TestParseInvalidJSON(t *testing.T) {
  769. data := `garbage`
  770. _, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  771. if err == nil {
  772. t.Error("Parse should returns an error")
  773. }
  774. }