parser_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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 TestParseItemWithLegacyAuthorField(t *testing.T) {
  354. data := `{
  355. "version": "https://jsonfeed.org/version/1",
  356. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  357. "title": "Brent Simmons’s Microblog",
  358. "home_page_url": "https://example.org/",
  359. "feed_url": "https://example.org/feed.json",
  360. "author": {
  361. "name": "Brent Simmons",
  362. "url": "http://example.org/",
  363. "avatar": "https://example.org/avatar.png"
  364. },
  365. "items": [
  366. {
  367. "id": "2347259",
  368. "url": "https://example.org/2347259",
  369. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  370. "date_published": "2016-02-09T14:22:00-07:00"
  371. }
  372. ]
  373. }`
  374. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  375. if err != nil {
  376. t.Fatal(err)
  377. }
  378. if len(feed.Entries) != 1 {
  379. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  380. }
  381. if feed.Entries[0].Author != "Brent Simmons" {
  382. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  383. }
  384. }
  385. func TestParseItemWithMultipleAuthorFields(t *testing.T) {
  386. data := `{
  387. "version": "https://jsonfeed.org/version/1.1",
  388. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  389. "title": "Brent Simmons’s Microblog",
  390. "home_page_url": "https://example.org/",
  391. "feed_url": "https://example.org/feed.json",
  392. "author": {
  393. "name": "Deprecated Author Field",
  394. "url": "http://example.org/",
  395. "avatar": "https://example.org/avatar.png"
  396. },
  397. "authors": [
  398. {
  399. "name": "Brent Simmons",
  400. "url": "http://example.org/",
  401. "avatar": "https://example.org/avatar.png"
  402. }
  403. ],
  404. "items": [
  405. {
  406. "id": "2347259",
  407. "url": "https://example.org/2347259",
  408. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  409. "date_published": "2016-02-09T14:22:00-07:00"
  410. }
  411. ]
  412. }`
  413. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  414. if err != nil {
  415. t.Fatal(err)
  416. }
  417. if len(feed.Entries) != 1 {
  418. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  419. }
  420. if feed.Entries[0].Author != "Brent Simmons, Deprecated Author Field" {
  421. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  422. }
  423. }
  424. func TestParseItemWithMultipleDuplicateAuthors(t *testing.T) {
  425. data := `{
  426. "version": "https://jsonfeed.org/version/1.1",
  427. "title": "Example",
  428. "home_page_url": "https://example.org/",
  429. "feed_url": "https://example.org/feed.json",
  430. "items": [
  431. {
  432. "id": "2347259",
  433. "url": "https://example.org/2347259",
  434. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  435. "date_published": "2016-02-09T14:22:00-07:00",
  436. "authors": [
  437. {
  438. "name": "Author B",
  439. "url": "http://example.org/",
  440. "avatar": "https://example.org/avatar.png"
  441. },
  442. {
  443. "name": "Author A",
  444. "url": "http://example.org/",
  445. "avatar": "https://example.org/avatar.png"
  446. },
  447. {
  448. "name": "Author B",
  449. "url": "http://example.org/",
  450. "avatar": "https://example.org/avatar.png"
  451. }
  452. ]
  453. }
  454. ]
  455. }`
  456. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  457. if err != nil {
  458. t.Fatal(err)
  459. }
  460. if len(feed.Entries) != 1 {
  461. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  462. }
  463. if feed.Entries[0].Author != "Author A, Author B" {
  464. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  465. }
  466. }
  467. func TestParseItemWithInvalidDate(t *testing.T) {
  468. data := `{
  469. "version": "https://jsonfeed.org/version/1",
  470. "title": "My Example Feed",
  471. "home_page_url": "https://example.org/",
  472. "feed_url": "https://example.org/feed.json",
  473. "items": [
  474. {
  475. "id": "2347259",
  476. "url": "https://example.org/2347259",
  477. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  478. "date_published": "Tomorrow"
  479. }
  480. ]
  481. }`
  482. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  483. if err != nil {
  484. t.Fatal(err)
  485. }
  486. if len(feed.Entries) != 1 {
  487. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  488. }
  489. duration := time.Since(feed.Entries[0].Date)
  490. if duration.Seconds() > 1 {
  491. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  492. }
  493. }
  494. func TestParseItemWithoutTitleButWithURL(t *testing.T) {
  495. data := `{
  496. "version": "https://jsonfeed.org/version/1",
  497. "title": "My Example Feed",
  498. "home_page_url": "https://example.org/",
  499. "feed_url": "https://example.org/feed.json",
  500. "items": [
  501. {
  502. "url": "https://example.org/item"
  503. }
  504. ]
  505. }`
  506. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  507. if err != nil {
  508. t.Fatal(err)
  509. }
  510. if len(feed.Entries) != 1 {
  511. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  512. }
  513. if feed.Entries[0].Title != "https://example.org/item" {
  514. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  515. }
  516. }
  517. func TestParseItemWithoutTitleButWithSummary(t *testing.T) {
  518. data := `{
  519. "version": "https://jsonfeed.org/version/1",
  520. "title": "My Example Feed",
  521. "home_page_url": "https://example.org/",
  522. "feed_url": "https://example.org/feed.json",
  523. "items": [
  524. {
  525. "summary": "This is some text content."
  526. }
  527. ]
  528. }`
  529. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  530. if err != nil {
  531. t.Fatal(err)
  532. }
  533. if len(feed.Entries) != 1 {
  534. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  535. }
  536. if feed.Entries[0].Title != "This is some text content." {
  537. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  538. }
  539. }
  540. func TestParseItemWithoutTitleButWithHTMLContent(t *testing.T) {
  541. data := `{
  542. "version": "https://jsonfeed.org/version/1",
  543. "title": "My Example Feed",
  544. "home_page_url": "https://example.org/",
  545. "feed_url": "https://example.org/feed.json",
  546. "items": [
  547. {
  548. "content_html": "This is <strong>HTML</strong>."
  549. }
  550. ]
  551. }`
  552. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  553. if err != nil {
  554. t.Fatal(err)
  555. }
  556. if len(feed.Entries) != 1 {
  557. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  558. }
  559. if feed.Entries[0].Title != "This is HTML." {
  560. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  561. }
  562. }
  563. func TestParseItemWithoutTitleButWithTextContent(t *testing.T) {
  564. data := `{
  565. "version": "https://jsonfeed.org/version/1",
  566. "title": "My Example Feed",
  567. "home_page_url": "https://example.org/",
  568. "feed_url": "https://example.org/feed.json",
  569. "items": [
  570. {
  571. "content_text": "` + strings.Repeat("a", 200) + `"
  572. }
  573. ]
  574. }`
  575. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  576. if err != nil {
  577. t.Fatal(err)
  578. }
  579. if len(feed.Entries) != 1 {
  580. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  581. }
  582. if len(feed.Entries[0].Title) != 103 {
  583. t.Errorf("Incorrect entry title, got: %d", len(feed.Entries[0].Title))
  584. }
  585. if len([]rune(feed.Entries[0].Title)) != 101 {
  586. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  587. }
  588. }
  589. func TestParseItemWithTooLongUnicodeTitle(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. "title": "I’m riding my electric bike and came across this castle. It’s called “Schloss Richmond”. 🚴‍♂️"
  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 len(feed.Entries[0].Title) != 110 {
  609. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  610. }
  611. if len([]rune(feed.Entries[0].Title)) != 93 {
  612. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  613. }
  614. }
  615. func TestParseItemTitleWithXMLTags(t *testing.T) {
  616. data := `{
  617. "version": "https://jsonfeed.org/version/1",
  618. "title": "My Example Feed",
  619. "home_page_url": "https://example.org/",
  620. "feed_url": "https://example.org/feed.json",
  621. "items": [
  622. {
  623. "title": "</example>"
  624. }
  625. ]
  626. }`
  627. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  628. if err != nil {
  629. t.Fatal(err)
  630. }
  631. if len(feed.Entries) != 1 {
  632. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  633. }
  634. if feed.Entries[0].Title != "</example>" {
  635. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  636. }
  637. }
  638. func TestParseItemWithoutID(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. "content_text": "Some text."
  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 feed.Entries[0].Hash != "13b4c5aecd1b6d749afcee968fbf9c80f1ed1bbdbe1aaf25cb34ebd01144bbe9" {
  658. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  659. }
  660. }
  661. func TestParseItemTags(t *testing.T) {
  662. data := `{
  663. "version": "https://jsonfeed.org/version/1",
  664. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  665. "title": "Brent Simmons’s Microblog",
  666. "home_page_url": "https://example.org/",
  667. "feed_url": "https://example.org/feed.json",
  668. "author": {
  669. "name": "Brent Simmons",
  670. "url": "http://example.org/",
  671. "avatar": "https://example.org/avatar.png"
  672. },
  673. "items": [
  674. {
  675. "id": "2347259",
  676. "url": "https://example.org/2347259",
  677. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  678. "date_published": "2016-02-09T14:22:00-07:00",
  679. "tags": [
  680. " tag 1",
  681. " ",
  682. "tag 2"
  683. ]
  684. }
  685. ]
  686. }`
  687. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  688. if err != nil {
  689. t.Fatal(err)
  690. }
  691. if len(feed.Entries[0].Tags) != 2 {
  692. t.Errorf("Incorrect number of Tags, got: %d", len(feed.Entries[0].Tags))
  693. }
  694. expected := "tag 2"
  695. result := feed.Entries[0].Tags[1]
  696. if result != expected {
  697. t.Errorf("Incorrect entry tag, got %q instead of %q", result, expected)
  698. }
  699. }
  700. func TestParseFeedFavicon(t *testing.T) {
  701. data := `{
  702. "version": "https://jsonfeed.org/version/1",
  703. "title": "My Example Feed",
  704. "favicon": "https://example.org/jsonfeed/favicon.png",
  705. "home_page_url": "https://example.org/",
  706. "feed_url": "https://example.org/feed.json",
  707. "items": [
  708. {
  709. "id": "2",
  710. "content_text": "This is a second item.",
  711. "url": "https://example.org/second-item"
  712. },
  713. {
  714. "id": "1",
  715. "content_html": "<p>Hello, world!</p>",
  716. "url": "https://example.org/initial-post"
  717. }
  718. ]
  719. }`
  720. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  721. if err != nil {
  722. t.Fatal(err)
  723. }
  724. if feed.IconURL != "https://example.org/jsonfeed/favicon.png" {
  725. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  726. }
  727. }
  728. func TestParseFeedIcon(t *testing.T) {
  729. data := `{
  730. "version": "https://jsonfeed.org/version/1",
  731. "title": "My Example Feed",
  732. "icon": "https://example.org/jsonfeed/icon.png",
  733. "home_page_url": "https://example.org/",
  734. "feed_url": "https://example.org/feed.json",
  735. "items": [
  736. {
  737. "id": "2",
  738. "content_text": "This is a second item.",
  739. "url": "https://example.org/second-item"
  740. },
  741. {
  742. "id": "1",
  743. "content_html": "<p>Hello, world!</p>",
  744. "url": "https://example.org/initial-post"
  745. }
  746. ]
  747. }`
  748. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  749. if err != nil {
  750. t.Fatal(err)
  751. }
  752. if feed.IconURL != "https://example.org/jsonfeed/icon.png" {
  753. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  754. }
  755. }
  756. func TestParseFeedWithRelativeAttachmentURL(t *testing.T) {
  757. data := `{
  758. "version": "https://jsonfeed.org/version/1",
  759. "title": "My Example Feed",
  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. "attachments": [
  768. {
  769. "url": " /attachment.mp3 ",
  770. "mime_type": "audio/mpeg",
  771. "size_in_bytes": 123456
  772. }
  773. ]
  774. }
  775. ]
  776. }`
  777. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  778. if err != nil {
  779. t.Fatal(err)
  780. }
  781. if len(feed.Entries[0].Enclosures) != 1 {
  782. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  783. }
  784. if feed.Entries[0].Enclosures[0].URL != "https://example.org/attachment.mp3" {
  785. t.Errorf("Incorrect enclosure URL, got: %q", feed.Entries[0].Enclosures[0].URL)
  786. }
  787. }
  788. func TestParseInvalidJSON(t *testing.T) {
  789. data := `garbage`
  790. _, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  791. if err == nil {
  792. t.Error("Parse should returns an error")
  793. }
  794. }