parser_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 TestParseFeedWithoutTitle(t *testing.T) {
  148. data := `{
  149. "version": "https://jsonfeed.org/version/1",
  150. "home_page_url": "https://example.org/",
  151. "feed_url": "https://example.org/feed.json",
  152. "items": [
  153. {
  154. "id": "2347259",
  155. "url": "https://example.org/2347259",
  156. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  157. "date_published": "2016-02-09T14:22:00-07:00"
  158. }
  159. ]
  160. }`
  161. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. if feed.Title != "https://example.org/" {
  166. t.Errorf("Incorrect title, got: %s", feed.Title)
  167. }
  168. }
  169. func TestParseFeedWithoutHomePage(t *testing.T) {
  170. data := `{
  171. "version": "https://jsonfeed.org/version/1",
  172. "feed_url": "https://example.org/feed.json",
  173. "title": "Some test",
  174. "items": [
  175. {
  176. "id": "2347259",
  177. "url": "https://example.org/2347259",
  178. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  179. "date_published": "2016-02-09T14:22:00-07:00"
  180. }
  181. ]
  182. }`
  183. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. if feed.SiteURL != "https://example.org/feed.json" {
  188. t.Errorf("Incorrect title, got: %s", feed.Title)
  189. }
  190. }
  191. func TestParseFeedWithoutFeedURL(t *testing.T) {
  192. data := `{
  193. "version": "https://jsonfeed.org/version/1",
  194. "title": "Some test",
  195. "items": [
  196. {
  197. "id": "2347259",
  198. "url": "https://example.org/2347259",
  199. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  200. "date_published": "2016-02-09T14:22:00-07:00"
  201. }
  202. ]
  203. }`
  204. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. if feed.SiteURL != "https://example.org/feed.json" {
  209. t.Errorf("Incorrect title, got: %s", feed.Title)
  210. }
  211. }
  212. func TestParseItemWithoutAttachmentURL(t *testing.T) {
  213. data := `{
  214. "version": "https://jsonfeed.org/version/1",
  215. "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",
  216. "title": "The Record",
  217. "home_page_url": "http://therecord.co/",
  218. "feed_url": "http://therecord.co/feed.json",
  219. "items": [
  220. {
  221. "id": "http://therecord.co/chris-parrish",
  222. "title": "Special #1 - Chris Parrish",
  223. "url": "http://therecord.co/chris-parrish",
  224. "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.",
  225. "date_published": "2014-05-09T14:04:00-07:00",
  226. "attachments": [
  227. {
  228. "url": "",
  229. "mime_type": "audio/x-m4a",
  230. "size_in_bytes": 0
  231. }
  232. ]
  233. }
  234. ]
  235. }`
  236. feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
  237. if err != nil {
  238. t.Fatal(err)
  239. }
  240. if len(feed.Entries) != 1 {
  241. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  242. }
  243. if len(feed.Entries[0].Enclosures) != 0 {
  244. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  245. }
  246. }
  247. func TestParseItemWithRelativeURL(t *testing.T) {
  248. data := `{
  249. "version": "https://jsonfeed.org/version/1",
  250. "title": "Example",
  251. "home_page_url": "https://example.org/",
  252. "feed_url": "https://example.org/feed.json",
  253. "items": [
  254. {
  255. "id": "2347259",
  256. "url": "something.html",
  257. "date_published": "2016-02-09T14:22:00-07:00"
  258. }
  259. ]
  260. }`
  261. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  262. if err != nil {
  263. t.Fatal(err)
  264. }
  265. if feed.Entries[0].URL != "https://example.org/something.html" {
  266. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  267. }
  268. }
  269. func TestParseItemWithLegacyAuthorField(t *testing.T) {
  270. data := `{
  271. "version": "https://jsonfeed.org/version/1",
  272. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  273. "title": "Brent Simmons’s Microblog",
  274. "home_page_url": "https://example.org/",
  275. "feed_url": "https://example.org/feed.json",
  276. "author": {
  277. "name": "Brent Simmons",
  278. "url": "http://example.org/",
  279. "avatar": "https://example.org/avatar.png"
  280. },
  281. "items": [
  282. {
  283. "id": "2347259",
  284. "url": "https://example.org/2347259",
  285. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  286. "date_published": "2016-02-09T14:22:00-07:00"
  287. }
  288. ]
  289. }`
  290. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  291. if err != nil {
  292. t.Fatal(err)
  293. }
  294. if len(feed.Entries) != 1 {
  295. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  296. }
  297. if feed.Entries[0].Author != "Brent Simmons" {
  298. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  299. }
  300. }
  301. func TestParseItemWithMultipleAuthorFields(t *testing.T) {
  302. data := `{
  303. "version": "https://jsonfeed.org/version/1.1",
  304. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  305. "title": "Brent Simmons’s Microblog",
  306. "home_page_url": "https://example.org/",
  307. "feed_url": "https://example.org/feed.json",
  308. "author": {
  309. "name": "Deprecated Author Field",
  310. "url": "http://example.org/",
  311. "avatar": "https://example.org/avatar.png"
  312. },
  313. "authors": [
  314. {
  315. "name": "Brent Simmons",
  316. "url": "http://example.org/",
  317. "avatar": "https://example.org/avatar.png"
  318. }
  319. ],
  320. "items": [
  321. {
  322. "id": "2347259",
  323. "url": "https://example.org/2347259",
  324. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  325. "date_published": "2016-02-09T14:22:00-07:00"
  326. }
  327. ]
  328. }`
  329. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  330. if err != nil {
  331. t.Fatal(err)
  332. }
  333. if len(feed.Entries) != 1 {
  334. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  335. }
  336. if feed.Entries[0].Author != "Brent Simmons, Deprecated Author Field" {
  337. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  338. }
  339. }
  340. func TestParseItemWithMultipleDuplicateAuthors(t *testing.T) {
  341. data := `{
  342. "version": "https://jsonfeed.org/version/1.1",
  343. "title": "Example",
  344. "home_page_url": "https://example.org/",
  345. "feed_url": "https://example.org/feed.json",
  346. "items": [
  347. {
  348. "id": "2347259",
  349. "url": "https://example.org/2347259",
  350. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  351. "date_published": "2016-02-09T14:22:00-07:00",
  352. "authors": [
  353. {
  354. "name": "Author B",
  355. "url": "http://example.org/",
  356. "avatar": "https://example.org/avatar.png"
  357. },
  358. {
  359. "name": "Author A",
  360. "url": "http://example.org/",
  361. "avatar": "https://example.org/avatar.png"
  362. },
  363. {
  364. "name": "Author B",
  365. "url": "http://example.org/",
  366. "avatar": "https://example.org/avatar.png"
  367. }
  368. ]
  369. }
  370. ]
  371. }`
  372. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  373. if err != nil {
  374. t.Fatal(err)
  375. }
  376. if len(feed.Entries) != 1 {
  377. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  378. }
  379. if feed.Entries[0].Author != "Author A, Author B" {
  380. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  381. }
  382. }
  383. func TestParseItemWithInvalidDate(t *testing.T) {
  384. data := `{
  385. "version": "https://jsonfeed.org/version/1",
  386. "title": "My Example Feed",
  387. "home_page_url": "https://example.org/",
  388. "feed_url": "https://example.org/feed.json",
  389. "items": [
  390. {
  391. "id": "2347259",
  392. "url": "https://example.org/2347259",
  393. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  394. "date_published": "Tomorrow"
  395. }
  396. ]
  397. }`
  398. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  399. if err != nil {
  400. t.Fatal(err)
  401. }
  402. if len(feed.Entries) != 1 {
  403. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  404. }
  405. duration := time.Since(feed.Entries[0].Date)
  406. if duration.Seconds() > 1 {
  407. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  408. }
  409. }
  410. func TestParseItemWithoutTitleButWithURL(t *testing.T) {
  411. data := `{
  412. "version": "https://jsonfeed.org/version/1",
  413. "title": "My Example Feed",
  414. "home_page_url": "https://example.org/",
  415. "feed_url": "https://example.org/feed.json",
  416. "items": [
  417. {
  418. "url": "https://example.org/item"
  419. }
  420. ]
  421. }`
  422. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  423. if err != nil {
  424. t.Fatal(err)
  425. }
  426. if len(feed.Entries) != 1 {
  427. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  428. }
  429. if feed.Entries[0].Title != "https://example.org/item" {
  430. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  431. }
  432. }
  433. func TestParseItemWithoutTitleButWithSummary(t *testing.T) {
  434. data := `{
  435. "version": "https://jsonfeed.org/version/1",
  436. "title": "My Example Feed",
  437. "home_page_url": "https://example.org/",
  438. "feed_url": "https://example.org/feed.json",
  439. "items": [
  440. {
  441. "summary": "This is some text content."
  442. }
  443. ]
  444. }`
  445. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  446. if err != nil {
  447. t.Fatal(err)
  448. }
  449. if len(feed.Entries) != 1 {
  450. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  451. }
  452. if feed.Entries[0].Title != "This is some text content." {
  453. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  454. }
  455. }
  456. func TestParseItemWithoutTitleButWithHTMLContent(t *testing.T) {
  457. data := `{
  458. "version": "https://jsonfeed.org/version/1",
  459. "title": "My Example Feed",
  460. "home_page_url": "https://example.org/",
  461. "feed_url": "https://example.org/feed.json",
  462. "items": [
  463. {
  464. "content_html": "This is <strong>HTML</strong>."
  465. }
  466. ]
  467. }`
  468. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  469. if err != nil {
  470. t.Fatal(err)
  471. }
  472. if len(feed.Entries) != 1 {
  473. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  474. }
  475. if feed.Entries[0].Title != "This is HTML." {
  476. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  477. }
  478. }
  479. func TestParseItemWithoutTitleButWithTextContent(t *testing.T) {
  480. data := `{
  481. "version": "https://jsonfeed.org/version/1",
  482. "title": "My Example Feed",
  483. "home_page_url": "https://example.org/",
  484. "feed_url": "https://example.org/feed.json",
  485. "items": [
  486. {
  487. "content_text": "` + strings.Repeat("a", 200) + `"
  488. }
  489. ]
  490. }`
  491. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  492. if err != nil {
  493. t.Fatal(err)
  494. }
  495. if len(feed.Entries) != 1 {
  496. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  497. }
  498. if len(feed.Entries[0].Title) != 103 {
  499. t.Errorf("Incorrect entry title, got: %d", len(feed.Entries[0].Title))
  500. }
  501. if len([]rune(feed.Entries[0].Title)) != 101 {
  502. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  503. }
  504. }
  505. func TestParseItemWithTooLongUnicodeTitle(t *testing.T) {
  506. data := `{
  507. "version": "https://jsonfeed.org/version/1",
  508. "title": "My Example Feed",
  509. "home_page_url": "https://example.org/",
  510. "feed_url": "https://example.org/feed.json",
  511. "items": [
  512. {
  513. "title": "I’m riding my electric bike and came across this castle. It’s called “Schloss Richmond”. 🚴‍♂️"
  514. }
  515. ]
  516. }`
  517. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  518. if err != nil {
  519. t.Fatal(err)
  520. }
  521. if len(feed.Entries) != 1 {
  522. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  523. }
  524. if len(feed.Entries[0].Title) != 110 {
  525. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  526. }
  527. if len([]rune(feed.Entries[0].Title)) != 93 {
  528. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  529. }
  530. }
  531. func TestParseItemTitleWithXMLTags(t *testing.T) {
  532. data := `{
  533. "version": "https://jsonfeed.org/version/1",
  534. "title": "My Example Feed",
  535. "home_page_url": "https://example.org/",
  536. "feed_url": "https://example.org/feed.json",
  537. "items": [
  538. {
  539. "title": "</example>"
  540. }
  541. ]
  542. }`
  543. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  544. if err != nil {
  545. t.Fatal(err)
  546. }
  547. if len(feed.Entries) != 1 {
  548. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  549. }
  550. if feed.Entries[0].Title != "</example>" {
  551. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  552. }
  553. }
  554. func TestParseItemWithoutID(t *testing.T) {
  555. data := `{
  556. "version": "https://jsonfeed.org/version/1",
  557. "title": "My Example Feed",
  558. "home_page_url": "https://example.org/",
  559. "feed_url": "https://example.org/feed.json",
  560. "items": [
  561. {
  562. "content_text": "Some text."
  563. }
  564. ]
  565. }`
  566. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  567. if err != nil {
  568. t.Fatal(err)
  569. }
  570. if len(feed.Entries) != 1 {
  571. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  572. }
  573. if feed.Entries[0].Hash != "13b4c5aecd1b6d749afcee968fbf9c80f1ed1bbdbe1aaf25cb34ebd01144bbe9" {
  574. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  575. }
  576. }
  577. func TestParseItemTags(t *testing.T) {
  578. data := `{
  579. "version": "https://jsonfeed.org/version/1",
  580. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  581. "title": "Brent Simmons’s Microblog",
  582. "home_page_url": "https://example.org/",
  583. "feed_url": "https://example.org/feed.json",
  584. "author": {
  585. "name": "Brent Simmons",
  586. "url": "http://example.org/",
  587. "avatar": "https://example.org/avatar.png"
  588. },
  589. "items": [
  590. {
  591. "id": "2347259",
  592. "url": "https://example.org/2347259",
  593. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  594. "date_published": "2016-02-09T14:22:00-07:00",
  595. "tags": [
  596. " tag 1",
  597. " ",
  598. "tag 2"
  599. ]
  600. }
  601. ]
  602. }`
  603. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  604. if err != nil {
  605. t.Fatal(err)
  606. }
  607. if len(feed.Entries[0].Tags) != 2 {
  608. t.Errorf("Incorrect number of Tags, got: %d", len(feed.Entries[0].Tags))
  609. }
  610. expected := "tag 2"
  611. result := feed.Entries[0].Tags[1]
  612. if result != expected {
  613. t.Errorf("Incorrect entry tag, got %q instead of %q", result, expected)
  614. }
  615. }
  616. func TestParseFeedFavicon(t *testing.T) {
  617. data := `{
  618. "version": "https://jsonfeed.org/version/1",
  619. "title": "My Example Feed",
  620. "favicon": "https://example.org/jsonfeed/favicon.png",
  621. "home_page_url": "https://example.org/",
  622. "feed_url": "https://example.org/feed.json",
  623. "items": [
  624. {
  625. "id": "2",
  626. "content_text": "This is a second item.",
  627. "url": "https://example.org/second-item"
  628. },
  629. {
  630. "id": "1",
  631. "content_html": "<p>Hello, world!</p>",
  632. "url": "https://example.org/initial-post"
  633. }
  634. ]
  635. }`
  636. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  637. if err != nil {
  638. t.Fatal(err)
  639. }
  640. if feed.IconURL != "https://example.org/jsonfeed/favicon.png" {
  641. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  642. }
  643. }
  644. func TestParseFeedIcon(t *testing.T) {
  645. data := `{
  646. "version": "https://jsonfeed.org/version/1",
  647. "title": "My Example Feed",
  648. "icon": "https://example.org/jsonfeed/icon.png",
  649. "home_page_url": "https://example.org/",
  650. "feed_url": "https://example.org/feed.json",
  651. "items": [
  652. {
  653. "id": "2",
  654. "content_text": "This is a second item.",
  655. "url": "https://example.org/second-item"
  656. },
  657. {
  658. "id": "1",
  659. "content_html": "<p>Hello, world!</p>",
  660. "url": "https://example.org/initial-post"
  661. }
  662. ]
  663. }`
  664. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  665. if err != nil {
  666. t.Fatal(err)
  667. }
  668. if feed.IconURL != "https://example.org/jsonfeed/icon.png" {
  669. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  670. }
  671. }
  672. func TestParseInvalidJSON(t *testing.T) {
  673. data := `garbage`
  674. _, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  675. if err == nil {
  676. t.Error("Parse should returns an error")
  677. }
  678. }