4
0

parser_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 TestParseItemWithInvalidDate(t *testing.T) {
  517. data := `{
  518. "version": "https://jsonfeed.org/version/1",
  519. "title": "My Example Feed",
  520. "home_page_url": "https://example.org/",
  521. "feed_url": "https://example.org/feed.json",
  522. "items": [
  523. {
  524. "id": "2347259",
  525. "url": "https://example.org/2347259",
  526. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  527. "date_published": "Tomorrow"
  528. }
  529. ]
  530. }`
  531. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  532. if err != nil {
  533. t.Fatal(err)
  534. }
  535. if len(feed.Entries) != 1 {
  536. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  537. }
  538. duration := time.Since(feed.Entries[0].Date)
  539. if duration.Seconds() > 1 {
  540. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  541. }
  542. }
  543. func TestParseItemWithoutTitleButWithURL(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. "url": "https://example.org/item"
  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 feed.Entries[0].Title != "https://example.org/item" {
  563. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  564. }
  565. }
  566. func TestParseItemWithoutTitleButWithSummary(t *testing.T) {
  567. data := `{
  568. "version": "https://jsonfeed.org/version/1",
  569. "title": "My Example Feed",
  570. "home_page_url": "https://example.org/",
  571. "feed_url": "https://example.org/feed.json",
  572. "items": [
  573. {
  574. "summary": "This is some text content."
  575. }
  576. ]
  577. }`
  578. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  579. if err != nil {
  580. t.Fatal(err)
  581. }
  582. if len(feed.Entries) != 1 {
  583. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  584. }
  585. if feed.Entries[0].Title != "This is some text content." {
  586. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  587. }
  588. }
  589. func TestParseItemWithoutTitleButWithHTMLContent(t *testing.T) {
  590. data := `{
  591. "version": "https://jsonfeed.org/version/1",
  592. "title": "My Example Feed",
  593. "home_page_url": "https://example.org/",
  594. "feed_url": "https://example.org/feed.json",
  595. "items": [
  596. {
  597. "content_html": "This is <strong>HTML</strong>."
  598. }
  599. ]
  600. }`
  601. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  602. if err != nil {
  603. t.Fatal(err)
  604. }
  605. if len(feed.Entries) != 1 {
  606. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  607. }
  608. if feed.Entries[0].Title != "This is HTML." {
  609. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  610. }
  611. }
  612. func TestParseItemWithoutTitleButWithTextContent(t *testing.T) {
  613. data := `{
  614. "version": "https://jsonfeed.org/version/1",
  615. "title": "My Example Feed",
  616. "home_page_url": "https://example.org/",
  617. "feed_url": "https://example.org/feed.json",
  618. "items": [
  619. {
  620. "content_text": "` + strings.Repeat("a", 200) + `"
  621. }
  622. ]
  623. }`
  624. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  625. if err != nil {
  626. t.Fatal(err)
  627. }
  628. if len(feed.Entries) != 1 {
  629. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  630. }
  631. if len(feed.Entries[0].Title) != 103 {
  632. t.Errorf("Incorrect entry title, got: %d", len(feed.Entries[0].Title))
  633. }
  634. if len([]rune(feed.Entries[0].Title)) != 101 {
  635. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  636. }
  637. }
  638. func TestParseItemWithTooLongUnicodeTitle(t *testing.T) {
  639. data := `{
  640. "version": "https://jsonfeed.org/version/1",
  641. "title": "My Example Feed",
  642. "home_page_url": "https://example.org/",
  643. "feed_url": "https://example.org/feed.json",
  644. "items": [
  645. {
  646. "title": "I’m riding my electric bike and came across this castle. It’s called “Schloss Richmond”. 🚴‍♂️"
  647. }
  648. ]
  649. }`
  650. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  651. if err != nil {
  652. t.Fatal(err)
  653. }
  654. if len(feed.Entries) != 1 {
  655. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  656. }
  657. if len(feed.Entries[0].Title) != 110 {
  658. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  659. }
  660. if len([]rune(feed.Entries[0].Title)) != 93 {
  661. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  662. }
  663. }
  664. func TestParseItemTitleWithXMLTags(t *testing.T) {
  665. data := `{
  666. "version": "https://jsonfeed.org/version/1",
  667. "title": "My Example Feed",
  668. "home_page_url": "https://example.org/",
  669. "feed_url": "https://example.org/feed.json",
  670. "items": [
  671. {
  672. "title": "</example>"
  673. }
  674. ]
  675. }`
  676. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  677. if err != nil {
  678. t.Fatal(err)
  679. }
  680. if len(feed.Entries) != 1 {
  681. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  682. }
  683. if feed.Entries[0].Title != "</example>" {
  684. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  685. }
  686. }
  687. func TestParseItemWithoutID(t *testing.T) {
  688. data := `{
  689. "version": "https://jsonfeed.org/version/1",
  690. "title": "My Example Feed",
  691. "home_page_url": "https://example.org/",
  692. "feed_url": "https://example.org/feed.json",
  693. "items": [
  694. {
  695. "content_text": "Some text."
  696. }
  697. ]
  698. }`
  699. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  700. if err != nil {
  701. t.Fatal(err)
  702. }
  703. if len(feed.Entries) != 1 {
  704. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  705. }
  706. if feed.Entries[0].Hash != "13b4c5aecd1b6d749afcee968fbf9c80f1ed1bbdbe1aaf25cb34ebd01144bbe9" {
  707. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  708. }
  709. }
  710. func TestParseItemTags(t *testing.T) {
  711. data := `{
  712. "version": "https://jsonfeed.org/version/1",
  713. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  714. "title": "Brent Simmons’s Microblog",
  715. "home_page_url": "https://example.org/",
  716. "feed_url": "https://example.org/feed.json",
  717. "author": {
  718. "name": "Brent Simmons",
  719. "url": "http://example.org/",
  720. "avatar": "https://example.org/avatar.png"
  721. },
  722. "items": [
  723. {
  724. "id": "2347259",
  725. "url": "https://example.org/2347259",
  726. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  727. "date_published": "2016-02-09T14:22:00-07:00",
  728. "tags": [
  729. " tag 1",
  730. " ",
  731. "tag 2",
  732. "tag 2",
  733. "aaa"
  734. ]
  735. }
  736. ]
  737. }`
  738. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  739. if err != nil {
  740. t.Fatal(err)
  741. }
  742. if len(feed.Entries) != 1 {
  743. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  744. }
  745. if len(feed.Entries[0].Tags) != 3 {
  746. t.Errorf("Incorrect number of Tags, got: %d", len(feed.Entries[0].Tags))
  747. }
  748. expected := []string{"aaa", "tag 1", "tag 2"}
  749. for i, tag := range feed.Entries[0].Tags {
  750. if tag != expected[i] {
  751. t.Errorf("Incorrect entry tag, got %q instead of %q", tag, expected[i])
  752. }
  753. }
  754. }
  755. func TestParseFeedFavicon(t *testing.T) {
  756. data := `{
  757. "version": "https://jsonfeed.org/version/1",
  758. "title": "My Example Feed",
  759. "favicon": "https://example.org/jsonfeed/favicon.png",
  760. "home_page_url": "https://example.org/",
  761. "feed_url": "https://example.org/feed.json",
  762. "items": [
  763. {
  764. "id": "2",
  765. "content_text": "This is a second item.",
  766. "url": "https://example.org/second-item"
  767. },
  768. {
  769. "id": "1",
  770. "content_html": "<p>Hello, world!</p>",
  771. "url": "https://example.org/initial-post"
  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 feed.IconURL != "https://example.org/jsonfeed/favicon.png" {
  780. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  781. }
  782. }
  783. func TestParseFeedIcon(t *testing.T) {
  784. data := `{
  785. "version": "https://jsonfeed.org/version/1",
  786. "title": "My Example Feed",
  787. "icon": "https://example.org/jsonfeed/icon.png",
  788. "home_page_url": "https://example.org/",
  789. "feed_url": "https://example.org/feed.json",
  790. "items": [
  791. {
  792. "id": "2",
  793. "content_text": "This is a second item.",
  794. "url": "https://example.org/second-item"
  795. },
  796. {
  797. "id": "1",
  798. "content_html": "<p>Hello, world!</p>",
  799. "url": "https://example.org/initial-post"
  800. }
  801. ]
  802. }`
  803. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  804. if err != nil {
  805. t.Fatal(err)
  806. }
  807. if feed.IconURL != "https://example.org/jsonfeed/icon.png" {
  808. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  809. }
  810. }
  811. func TestParseFeedWithRelativeAttachmentURL(t *testing.T) {
  812. data := `{
  813. "version": "https://jsonfeed.org/version/1",
  814. "title": "My Example Feed",
  815. "home_page_url": "https://example.org/",
  816. "feed_url": "https://example.org/feed.json",
  817. "items": [
  818. {
  819. "id": "2",
  820. "content_text": "This is a second item.",
  821. "url": "https://example.org/second-item",
  822. "attachments": [
  823. {
  824. "url": " /attachment.mp3 ",
  825. "mime_type": "audio/mpeg",
  826. "size_in_bytes": 123456
  827. }
  828. ]
  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 len(feed.Entries[0].Enclosures) != 1 {
  837. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  838. }
  839. if feed.Entries[0].Enclosures[0].URL != "https://example.org/attachment.mp3" {
  840. t.Errorf("Incorrect enclosure URL, got: %q", feed.Entries[0].Enclosures[0].URL)
  841. }
  842. }
  843. func TestParseInvalidJSON(t *testing.T) {
  844. data := `garbage`
  845. _, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  846. if err == nil {
  847. t.Error("Parse should returns an error")
  848. }
  849. }