parser_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package json // import "miniflux.app/reader/json"
  5. import (
  6. "bytes"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. func TestParseJsonFeed(t *testing.T) {
  12. data := `{
  13. "version": "https://jsonfeed.org/version/1",
  14. "title": "My Example Feed",
  15. "icon": "https://micro.blog/jsonfeed/avatar.jpg",
  16. "favicon": "https://micro.blog/jsonfeed/favicon.png",
  17. "home_page_url": "https://example.org/",
  18. "feed_url": "https://example.org/feed.json",
  19. "items": [
  20. {
  21. "id": "2",
  22. "content_text": "This is a second item.",
  23. "url": "https://example.org/second-item"
  24. },
  25. {
  26. "id": "1",
  27. "content_html": "<p>Hello, world!</p>",
  28. "url": "https://example.org/initial-post"
  29. }
  30. ]
  31. }`
  32. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if feed.Title != "My Example Feed" {
  37. t.Errorf("Incorrect title, got: %s", feed.Title)
  38. }
  39. if feed.FeedURL != "https://example.org/feed.json" {
  40. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  41. }
  42. if feed.SiteURL != "https://example.org/" {
  43. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  44. }
  45. if feed.IconURL != "https://micro.blog/jsonfeed/avatar.jpg" {
  46. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  47. }
  48. if len(feed.Entries) != 2 {
  49. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  50. }
  51. if feed.Entries[0].Hash != "d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" {
  52. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  53. }
  54. if feed.Entries[0].URL != "https://example.org/second-item" {
  55. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  56. }
  57. if feed.Entries[0].Title != "This is a second item." {
  58. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
  59. }
  60. if feed.Entries[0].Content != "This is a second item." {
  61. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  62. }
  63. if feed.Entries[1].Hash != "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b" {
  64. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[1].Hash)
  65. }
  66. if feed.Entries[1].URL != "https://example.org/initial-post" {
  67. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[1].URL)
  68. }
  69. if feed.Entries[1].Title != "Hello, world!" {
  70. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[1].Title)
  71. }
  72. if feed.Entries[1].Content != "<p>Hello, world!</p>" {
  73. t.Errorf("Incorrect entry content, got: %s", feed.Entries[1].Content)
  74. }
  75. }
  76. func TestParsePodcast(t *testing.T) {
  77. data := `{
  78. "version": "https://jsonfeed.org/version/1",
  79. "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",
  80. "title": "The Record",
  81. "home_page_url": "http://therecord.co/",
  82. "feed_url": "http://therecord.co/feed.json",
  83. "items": [
  84. {
  85. "id": "http://therecord.co/chris-parrish",
  86. "title": "Special #1 - Chris Parrish",
  87. "url": "http://therecord.co/chris-parrish",
  88. "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.",
  89. "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.",
  90. "summary": "Brent interviews Chris Parrish, co-host of The Record and one-half of Aged & Distilled.",
  91. "date_published": "2014-05-09T14:04:00-07:00",
  92. "attachments": [
  93. {
  94. "url": "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a",
  95. "mime_type": "audio/x-m4a",
  96. "size_in_bytes": 89970236,
  97. "duration_in_seconds": 6629
  98. }
  99. ]
  100. }
  101. ]
  102. }`
  103. feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if feed.Title != "The Record" {
  108. t.Errorf("Incorrect title, got: %s", feed.Title)
  109. }
  110. if feed.FeedURL != "http://therecord.co/feed.json" {
  111. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  112. }
  113. if feed.SiteURL != "http://therecord.co/" {
  114. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  115. }
  116. if len(feed.Entries) != 1 {
  117. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  118. }
  119. if feed.Entries[0].Hash != "6b678e57962a1b001e4e873756563cdc08bbd06ca561e764e0baa9a382485797" {
  120. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  121. }
  122. if feed.Entries[0].URL != "http://therecord.co/chris-parrish" {
  123. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  124. }
  125. if feed.Entries[0].Title != "Special #1 - Chris Parrish" {
  126. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
  127. }
  128. 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.` {
  129. t.Errorf(`Incorrect entry content, got: "%s"`, feed.Entries[0].Content)
  130. }
  131. location, _ := time.LoadLocation("America/Vancouver")
  132. if !feed.Entries[0].Date.Equal(time.Date(2014, time.May, 9, 14, 4, 0, 0, location)) {
  133. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  134. }
  135. if len(feed.Entries[0].Enclosures) != 1 {
  136. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  137. }
  138. if feed.Entries[0].Enclosures[0].URL != "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a" {
  139. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  140. }
  141. if feed.Entries[0].Enclosures[0].MimeType != "audio/x-m4a" {
  142. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  143. }
  144. if feed.Entries[0].Enclosures[0].Size != 89970236 {
  145. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  146. }
  147. }
  148. func TestParseEntryWithoutAttachmentURL(t *testing.T) {
  149. data := `{
  150. "version": "https://jsonfeed.org/version/1",
  151. "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",
  152. "title": "The Record",
  153. "home_page_url": "http://therecord.co/",
  154. "feed_url": "http://therecord.co/feed.json",
  155. "items": [
  156. {
  157. "id": "http://therecord.co/chris-parrish",
  158. "title": "Special #1 - Chris Parrish",
  159. "url": "http://therecord.co/chris-parrish",
  160. "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.",
  161. "date_published": "2014-05-09T14:04:00-07:00",
  162. "attachments": [
  163. {
  164. "url": "",
  165. "mime_type": "audio/x-m4a",
  166. "size_in_bytes": 0
  167. }
  168. ]
  169. }
  170. ]
  171. }`
  172. feed, err := Parse("http://therecord.co/feed.json", bytes.NewBufferString(data))
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. if len(feed.Entries) != 1 {
  177. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  178. }
  179. if len(feed.Entries[0].Enclosures) != 0 {
  180. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  181. }
  182. }
  183. func TestParseFeedWithRelativeURL(t *testing.T) {
  184. data := `{
  185. "version": "https://jsonfeed.org/version/1",
  186. "title": "Example",
  187. "home_page_url": "https://example.org/",
  188. "feed_url": "https://example.org/feed.json",
  189. "items": [
  190. {
  191. "id": "2347259",
  192. "url": "something.html",
  193. "date_published": "2016-02-09T14:22:00-07:00"
  194. }
  195. ]
  196. }`
  197. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. if feed.Entries[0].URL != "https://example.org/something.html" {
  202. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  203. }
  204. }
  205. func TestParseAuthor(t *testing.T) {
  206. data := `{
  207. "version": "https://jsonfeed.org/version/1",
  208. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  209. "title": "Brent Simmons’s Microblog",
  210. "home_page_url": "https://example.org/",
  211. "feed_url": "https://example.org/feed.json",
  212. "author": {
  213. "name": "Brent Simmons",
  214. "url": "http://example.org/",
  215. "avatar": "https://example.org/avatar.png"
  216. },
  217. "items": [
  218. {
  219. "id": "2347259",
  220. "url": "https://example.org/2347259",
  221. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  222. "date_published": "2016-02-09T14:22:00-07:00"
  223. }
  224. ]
  225. }`
  226. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  227. if err != nil {
  228. t.Fatal(err)
  229. }
  230. if len(feed.Entries) != 1 {
  231. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  232. }
  233. if feed.Entries[0].Author != "Brent Simmons" {
  234. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  235. }
  236. }
  237. func TestParseAuthors(t *testing.T) {
  238. data := `{
  239. "version": "https://jsonfeed.org/version/1.1",
  240. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  241. "title": "Brent Simmons’s Microblog",
  242. "home_page_url": "https://example.org/",
  243. "feed_url": "https://example.org/feed.json",
  244. "author": {
  245. "name": "This field is deprecated, use authors",
  246. "url": "http://example.org/",
  247. "avatar": "https://example.org/avatar.png"
  248. },
  249. "authors": [
  250. {
  251. "name": "Brent Simmons",
  252. "url": "http://example.org/",
  253. "avatar": "https://example.org/avatar.png"
  254. }
  255. ],
  256. "items": [
  257. {
  258. "id": "2347259",
  259. "url": "https://example.org/2347259",
  260. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  261. "date_published": "2016-02-09T14:22:00-07:00"
  262. }
  263. ]
  264. }`
  265. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  266. if err != nil {
  267. t.Fatal(err)
  268. }
  269. if len(feed.Entries) != 1 {
  270. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  271. }
  272. if feed.Entries[0].Author != "Brent Simmons" {
  273. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  274. }
  275. }
  276. func TestParseFeedWithoutTitle(t *testing.T) {
  277. data := `{
  278. "version": "https://jsonfeed.org/version/1",
  279. "home_page_url": "https://example.org/",
  280. "feed_url": "https://example.org/feed.json",
  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 feed.Title != "https://example.org/" {
  295. t.Errorf("Incorrect title, got: %s", feed.Title)
  296. }
  297. }
  298. func TestParseFeedItemWithInvalidDate(t *testing.T) {
  299. data := `{
  300. "version": "https://jsonfeed.org/version/1",
  301. "title": "My Example Feed",
  302. "home_page_url": "https://example.org/",
  303. "feed_url": "https://example.org/feed.json",
  304. "items": [
  305. {
  306. "id": "2347259",
  307. "url": "https://example.org/2347259",
  308. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  309. "date_published": "Tomorrow"
  310. }
  311. ]
  312. }`
  313. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  314. if err != nil {
  315. t.Fatal(err)
  316. }
  317. if len(feed.Entries) != 1 {
  318. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  319. }
  320. duration := time.Since(feed.Entries[0].Date)
  321. if duration.Seconds() > 1 {
  322. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  323. }
  324. }
  325. func TestParseFeedItemWithoutID(t *testing.T) {
  326. data := `{
  327. "version": "https://jsonfeed.org/version/1",
  328. "title": "My Example Feed",
  329. "home_page_url": "https://example.org/",
  330. "feed_url": "https://example.org/feed.json",
  331. "items": [
  332. {
  333. "content_text": "Some text."
  334. }
  335. ]
  336. }`
  337. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  338. if err != nil {
  339. t.Fatal(err)
  340. }
  341. if len(feed.Entries) != 1 {
  342. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  343. }
  344. if feed.Entries[0].Hash != "13b4c5aecd1b6d749afcee968fbf9c80f1ed1bbdbe1aaf25cb34ebd01144bbe9" {
  345. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  346. }
  347. }
  348. func TestParseFeedItemWithoutTitleButWithURL(t *testing.T) {
  349. data := `{
  350. "version": "https://jsonfeed.org/version/1",
  351. "title": "My Example Feed",
  352. "home_page_url": "https://example.org/",
  353. "feed_url": "https://example.org/feed.json",
  354. "items": [
  355. {
  356. "url": "https://example.org/item"
  357. }
  358. ]
  359. }`
  360. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  361. if err != nil {
  362. t.Fatal(err)
  363. }
  364. if len(feed.Entries) != 1 {
  365. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  366. }
  367. if feed.Entries[0].Title != "https://example.org/item" {
  368. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  369. }
  370. }
  371. func TestParseFeedItemWithoutTitleButWithSummary(t *testing.T) {
  372. data := `{
  373. "version": "https://jsonfeed.org/version/1",
  374. "title": "My Example Feed",
  375. "home_page_url": "https://example.org/",
  376. "feed_url": "https://example.org/feed.json",
  377. "items": [
  378. {
  379. "summary": "This is some text content."
  380. }
  381. ]
  382. }`
  383. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  384. if err != nil {
  385. t.Fatal(err)
  386. }
  387. if len(feed.Entries) != 1 {
  388. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  389. }
  390. if feed.Entries[0].Title != "This is some text content." {
  391. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  392. }
  393. }
  394. func TestParseFeedItemWithoutTitleButWithHTMLContent(t *testing.T) {
  395. data := `{
  396. "version": "https://jsonfeed.org/version/1",
  397. "title": "My Example Feed",
  398. "home_page_url": "https://example.org/",
  399. "feed_url": "https://example.org/feed.json",
  400. "items": [
  401. {
  402. "content_html": "This is <strong>HTML</strong>."
  403. }
  404. ]
  405. }`
  406. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  407. if err != nil {
  408. t.Fatal(err)
  409. }
  410. if len(feed.Entries) != 1 {
  411. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  412. }
  413. if feed.Entries[0].Title != "This is HTML." {
  414. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  415. }
  416. }
  417. func TestParseFeedItemWithoutTitleButWithTextContent(t *testing.T) {
  418. data := `{
  419. "version": "https://jsonfeed.org/version/1",
  420. "title": "My Example Feed",
  421. "home_page_url": "https://example.org/",
  422. "feed_url": "https://example.org/feed.json",
  423. "items": [
  424. {
  425. "content_text": "` + strings.Repeat("a", 200) + `"
  426. }
  427. ]
  428. }`
  429. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  430. if err != nil {
  431. t.Fatal(err)
  432. }
  433. if len(feed.Entries) != 1 {
  434. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  435. }
  436. if len(feed.Entries[0].Title) != 103 {
  437. t.Errorf("Incorrect entry title, got: %d", len(feed.Entries[0].Title))
  438. }
  439. if len([]rune(feed.Entries[0].Title)) != 101 {
  440. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  441. }
  442. }
  443. func TestParseTruncateItemTitleUnicode(t *testing.T) {
  444. data := `{
  445. "version": "https://jsonfeed.org/version/1",
  446. "title": "My Example Feed",
  447. "home_page_url": "https://example.org/",
  448. "feed_url": "https://example.org/feed.json",
  449. "items": [
  450. {
  451. "title": "I’m riding my electric bike and came across this castle. It’s called “Schloss Richmond”. 🚴‍♂️"
  452. }
  453. ]
  454. }`
  455. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  456. if err != nil {
  457. t.Fatal(err)
  458. }
  459. if len(feed.Entries) != 1 {
  460. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  461. }
  462. if len(feed.Entries[0].Title) != 110 {
  463. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  464. }
  465. if len([]rune(feed.Entries[0].Title)) != 93 {
  466. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  467. }
  468. }
  469. func TestParseItemTitleWithXMLTags(t *testing.T) {
  470. data := `{
  471. "version": "https://jsonfeed.org/version/1",
  472. "title": "My Example Feed",
  473. "home_page_url": "https://example.org/",
  474. "feed_url": "https://example.org/feed.json",
  475. "items": [
  476. {
  477. "title": "</example>"
  478. }
  479. ]
  480. }`
  481. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  482. if err != nil {
  483. t.Fatal(err)
  484. }
  485. if len(feed.Entries) != 1 {
  486. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  487. }
  488. if feed.Entries[0].Title != "</example>" {
  489. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  490. }
  491. }
  492. func TestParseInvalidJSON(t *testing.T) {
  493. data := `garbage`
  494. _, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  495. if err == nil {
  496. t.Error("Parse should returns an error")
  497. }
  498. }
  499. func TestParseTags(t *testing.T) {
  500. data := `{
  501. "version": "https://jsonfeed.org/version/1",
  502. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  503. "title": "Brent Simmons’s Microblog",
  504. "home_page_url": "https://example.org/",
  505. "feed_url": "https://example.org/feed.json",
  506. "author": {
  507. "name": "Brent Simmons",
  508. "url": "http://example.org/",
  509. "avatar": "https://example.org/avatar.png"
  510. },
  511. "items": [
  512. {
  513. "id": "2347259",
  514. "url": "https://example.org/2347259",
  515. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  516. "date_published": "2016-02-09T14:22:00-07:00",
  517. "tags": [
  518. "tag 1",
  519. "tag 2"
  520. ]
  521. }
  522. ]
  523. }`
  524. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  525. if err != nil {
  526. t.Fatal(err)
  527. }
  528. if len(feed.Entries[0].Tags) != 2 {
  529. t.Errorf("Incorrect number of Tags, got: %d", len(feed.Entries[0].Tags))
  530. }
  531. expected := "tag 2"
  532. result := feed.Entries[0].Tags[1]
  533. if result != expected {
  534. t.Errorf("Incorrect entry tag, got %q instead of %q", result, expected)
  535. }
  536. }
  537. func TestParseFavicon(t *testing.T) {
  538. data := `{
  539. "version": "https://jsonfeed.org/version/1",
  540. "title": "My Example Feed",
  541. "favicon": "https://micro.blog/jsonfeed/favicon.png",
  542. "home_page_url": "https://example.org/",
  543. "feed_url": "https://example.org/feed.json",
  544. "items": [
  545. {
  546. "id": "2",
  547. "content_text": "This is a second item.",
  548. "url": "https://example.org/second-item"
  549. },
  550. {
  551. "id": "1",
  552. "content_html": "<p>Hello, world!</p>",
  553. "url": "https://example.org/initial-post"
  554. }
  555. ]
  556. }`
  557. feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
  558. if err != nil {
  559. t.Fatal(err)
  560. }
  561. if feed.IconURL != "https://micro.blog/jsonfeed/favicon.png" {
  562. t.Errorf("Incorrect icon URL, got: %s", feed.IconURL)
  563. }
  564. }