parser_test.go 17 KB

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