parser_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 TestParseJsonFeed(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/avatar.jpg" {
  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 TestParseEntryWithoutAttachmentURL(t *testing.T) {
  148. data := `{
  149. "version": "https://jsonfeed.org/version/1",
  150. "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",
  151. "title": "The Record",
  152. "home_page_url": "http://therecord.co/",
  153. "feed_url": "http://therecord.co/feed.json",
  154. "items": [
  155. {
  156. "id": "http://therecord.co/chris-parrish",
  157. "title": "Special #1 - Chris Parrish",
  158. "url": "http://therecord.co/chris-parrish",
  159. "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.",
  160. "date_published": "2014-05-09T14:04:00-07:00",
  161. "attachments": [
  162. {
  163. "url": "",
  164. "mime_type": "audio/x-m4a",
  165. "size_in_bytes": 0
  166. }
  167. ]
  168. }
  169. ]
  170. }`
  171. feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. if len(feed.Entries) != 1 {
  176. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  177. }
  178. if len(feed.Entries[0].Enclosures) != 0 {
  179. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  180. }
  181. }
  182. func TestParseFeedWithRelativeURL(t *testing.T) {
  183. data := `{
  184. "version": "https://jsonfeed.org/version/1",
  185. "title": "Example",
  186. "home_page_url": "https://example.org/",
  187. "feed_url": "https://example.org/feed.json",
  188. "items": [
  189. {
  190. "id": "2347259",
  191. "url": "something.html",
  192. "date_published": "2016-02-09T14:22:00-07:00"
  193. }
  194. ]
  195. }`
  196. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. if feed.Entries[0].URL != "https://example.org/something.html" {
  201. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  202. }
  203. }
  204. func TestParseAuthor(t *testing.T) {
  205. data := `{
  206. "version": "https://jsonfeed.org/version/1",
  207. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  208. "title": "Brent Simmons’s Microblog",
  209. "home_page_url": "https://example.org/",
  210. "feed_url": "https://example.org/feed.json",
  211. "author": {
  212. "name": "Brent Simmons",
  213. "url": "http://example.org/",
  214. "avatar": "https://example.org/avatar.png"
  215. },
  216. "items": [
  217. {
  218. "id": "2347259",
  219. "url": "https://example.org/2347259",
  220. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  221. "date_published": "2016-02-09T14:22:00-07:00"
  222. }
  223. ]
  224. }`
  225. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. if len(feed.Entries) != 1 {
  230. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  231. }
  232. if feed.Entries[0].Author != "Brent Simmons" {
  233. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  234. }
  235. }
  236. func TestParseAuthors(t *testing.T) {
  237. data := `{
  238. "version": "https://jsonfeed.org/version/1.1",
  239. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  240. "title": "Brent Simmons’s Microblog",
  241. "home_page_url": "https://example.org/",
  242. "feed_url": "https://example.org/feed.json",
  243. "author": {
  244. "name": "This field is deprecated, use authors",
  245. "url": "http://example.org/",
  246. "avatar": "https://example.org/avatar.png"
  247. },
  248. "authors": [
  249. {
  250. "name": "Brent Simmons",
  251. "url": "http://example.org/",
  252. "avatar": "https://example.org/avatar.png"
  253. }
  254. ],
  255. "items": [
  256. {
  257. "id": "2347259",
  258. "url": "https://example.org/2347259",
  259. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  260. "date_published": "2016-02-09T14:22:00-07:00"
  261. }
  262. ]
  263. }`
  264. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  265. if err != nil {
  266. t.Fatal(err)
  267. }
  268. if len(feed.Entries) != 1 {
  269. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  270. }
  271. if feed.Entries[0].Author != "Brent Simmons" {
  272. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  273. }
  274. }
  275. func TestParseFeedWithoutTitle(t *testing.T) {
  276. data := `{
  277. "version": "https://jsonfeed.org/version/1",
  278. "home_page_url": "https://example.org/",
  279. "feed_url": "https://example.org/feed.json",
  280. "items": [
  281. {
  282. "id": "2347259",
  283. "url": "https://example.org/2347259",
  284. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  285. "date_published": "2016-02-09T14:22:00-07:00"
  286. }
  287. ]
  288. }`
  289. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  290. if err != nil {
  291. t.Fatal(err)
  292. }
  293. if feed.Title != "https://example.org/" {
  294. t.Errorf("Incorrect title, got: %s", feed.Title)
  295. }
  296. }
  297. func TestParseFeedItemWithInvalidDate(t *testing.T) {
  298. data := `{
  299. "version": "https://jsonfeed.org/version/1",
  300. "title": "My Example Feed",
  301. "home_page_url": "https://example.org/",
  302. "feed_url": "https://example.org/feed.json",
  303. "items": [
  304. {
  305. "id": "2347259",
  306. "url": "https://example.org/2347259",
  307. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  308. "date_published": "Tomorrow"
  309. }
  310. ]
  311. }`
  312. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  313. if err != nil {
  314. t.Fatal(err)
  315. }
  316. if len(feed.Entries) != 1 {
  317. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  318. }
  319. duration := time.Since(feed.Entries[0].Date)
  320. if duration.Seconds() > 1 {
  321. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  322. }
  323. }
  324. func TestParseFeedItemWithoutID(t *testing.T) {
  325. data := `{
  326. "version": "https://jsonfeed.org/version/1",
  327. "title": "My Example Feed",
  328. "home_page_url": "https://example.org/",
  329. "feed_url": "https://example.org/feed.json",
  330. "items": [
  331. {
  332. "content_text": "Some text."
  333. }
  334. ]
  335. }`
  336. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  337. if err != nil {
  338. t.Fatal(err)
  339. }
  340. if len(feed.Entries) != 1 {
  341. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  342. }
  343. if feed.Entries[0].Hash != "13b4c5aecd1b6d749afcee968fbf9c80f1ed1bbdbe1aaf25cb34ebd01144bbe9" {
  344. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  345. }
  346. }
  347. func TestParseFeedItemWithoutTitleButWithURL(t *testing.T) {
  348. data := `{
  349. "version": "https://jsonfeed.org/version/1",
  350. "title": "My Example Feed",
  351. "home_page_url": "https://example.org/",
  352. "feed_url": "https://example.org/feed.json",
  353. "items": [
  354. {
  355. "url": "https://example.org/item"
  356. }
  357. ]
  358. }`
  359. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  360. if err != nil {
  361. t.Fatal(err)
  362. }
  363. if len(feed.Entries) != 1 {
  364. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  365. }
  366. if feed.Entries[0].Title != "https://example.org/item" {
  367. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  368. }
  369. }
  370. func TestParseFeedItemWithoutTitleButWithSummary(t *testing.T) {
  371. data := `{
  372. "version": "https://jsonfeed.org/version/1",
  373. "title": "My Example Feed",
  374. "home_page_url": "https://example.org/",
  375. "feed_url": "https://example.org/feed.json",
  376. "items": [
  377. {
  378. "summary": "This is some text content."
  379. }
  380. ]
  381. }`
  382. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  383. if err != nil {
  384. t.Fatal(err)
  385. }
  386. if len(feed.Entries) != 1 {
  387. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  388. }
  389. if feed.Entries[0].Title != "This is some text content." {
  390. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  391. }
  392. }
  393. func TestParseFeedItemWithoutTitleButWithHTMLContent(t *testing.T) {
  394. data := `{
  395. "version": "https://jsonfeed.org/version/1",
  396. "title": "My Example Feed",
  397. "home_page_url": "https://example.org/",
  398. "feed_url": "https://example.org/feed.json",
  399. "items": [
  400. {
  401. "content_html": "This is <strong>HTML</strong>."
  402. }
  403. ]
  404. }`
  405. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  406. if err != nil {
  407. t.Fatal(err)
  408. }
  409. if len(feed.Entries) != 1 {
  410. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  411. }
  412. if feed.Entries[0].Title != "This is HTML." {
  413. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  414. }
  415. }
  416. func TestParseFeedItemWithoutTitleButWithTextContent(t *testing.T) {
  417. data := `{
  418. "version": "https://jsonfeed.org/version/1",
  419. "title": "My Example Feed",
  420. "home_page_url": "https://example.org/",
  421. "feed_url": "https://example.org/feed.json",
  422. "items": [
  423. {
  424. "content_text": "` + strings.Repeat("a", 200) + `"
  425. }
  426. ]
  427. }`
  428. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  429. if err != nil {
  430. t.Fatal(err)
  431. }
  432. if len(feed.Entries) != 1 {
  433. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  434. }
  435. if len(feed.Entries[0].Title) != 103 {
  436. t.Errorf("Incorrect entry title, got: %d", len(feed.Entries[0].Title))
  437. }
  438. if len([]rune(feed.Entries[0].Title)) != 101 {
  439. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  440. }
  441. }
  442. func TestParseTruncateItemTitleUnicode(t *testing.T) {
  443. data := `{
  444. "version": "https://jsonfeed.org/version/1",
  445. "title": "My Example Feed",
  446. "home_page_url": "https://example.org/",
  447. "feed_url": "https://example.org/feed.json",
  448. "items": [
  449. {
  450. "title": "I’m riding my electric bike and came across this castle. It’s called “Schloss Richmond”. 🚴‍♂️"
  451. }
  452. ]
  453. }`
  454. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  455. if err != nil {
  456. t.Fatal(err)
  457. }
  458. if len(feed.Entries) != 1 {
  459. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  460. }
  461. if len(feed.Entries[0].Title) != 110 {
  462. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  463. }
  464. if len([]rune(feed.Entries[0].Title)) != 93 {
  465. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  466. }
  467. }
  468. func TestParseItemTitleWithXMLTags(t *testing.T) {
  469. data := `{
  470. "version": "https://jsonfeed.org/version/1",
  471. "title": "My Example Feed",
  472. "home_page_url": "https://example.org/",
  473. "feed_url": "https://example.org/feed.json",
  474. "items": [
  475. {
  476. "title": "</example>"
  477. }
  478. ]
  479. }`
  480. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  481. if err != nil {
  482. t.Fatal(err)
  483. }
  484. if len(feed.Entries) != 1 {
  485. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  486. }
  487. if feed.Entries[0].Title != "</example>" {
  488. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  489. }
  490. }
  491. func TestParseInvalidJSON(t *testing.T) {
  492. data := `garbage`
  493. _, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  494. if err == nil {
  495. t.Error("Parse should returns an error")
  496. }
  497. }
  498. func TestParseTags(t *testing.T) {
  499. data := `{
  500. "version": "https://jsonfeed.org/version/1",
  501. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  502. "title": "Brent Simmons’s Microblog",
  503. "home_page_url": "https://example.org/",
  504. "feed_url": "https://example.org/feed.json",
  505. "author": {
  506. "name": "Brent Simmons",
  507. "url": "http://example.org/",
  508. "avatar": "https://example.org/avatar.png"
  509. },
  510. "items": [
  511. {
  512. "id": "2347259",
  513. "url": "https://example.org/2347259",
  514. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  515. "date_published": "2016-02-09T14:22:00-07:00",
  516. "tags": [
  517. "tag 1",
  518. "tag 2"
  519. ]
  520. }
  521. ]
  522. }`
  523. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  524. if err != nil {
  525. t.Fatal(err)
  526. }
  527. if len(feed.Entries[0].Tags) != 2 {
  528. t.Errorf("Incorrect number of Tags, got: %d", len(feed.Entries[0].Tags))
  529. }
  530. expected := "tag 2"
  531. result := feed.Entries[0].Tags[1]
  532. if result != expected {
  533. t.Errorf("Incorrect entry tag, got %q instead of %q", result, expected)
  534. }
  535. }
  536. func TestParseFavicon(t *testing.T) {
  537. data := `{
  538. "version": "https://jsonfeed.org/version/1",
  539. "title": "My Example Feed",
  540. "favicon": "https://micro.blog/jsonfeed/favicon.png",
  541. "home_page_url": "https://example.org/",
  542. "feed_url": "https://example.org/feed.json",
  543. "items": [
  544. {
  545. "id": "2",
  546. "content_text": "This is a second item.",
  547. "url": "https://example.org/second-item"
  548. },
  549. {
  550. "id": "1",
  551. "content_html": "<p>Hello, world!</p>",
  552. "url": "https://example.org/initial-post"
  553. }
  554. ]
  555. }`
  556. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  557. if err != nil {
  558. t.Fatal(err)
  559. }
  560. if feed.IconURL != "https://micro.blog/jsonfeed/favicon.png" {
  561. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  562. }
  563. }