parser_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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
  5. import (
  6. "bytes"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/miniflux/miniflux/errors"
  11. )
  12. func TestParseJsonFeed(t *testing.T) {
  13. data := `{
  14. "version": "https://jsonfeed.org/version/1",
  15. "title": "My Example Feed",
  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(bytes.NewBufferString(data))
  32. if err != nil {
  33. t.Error(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 len(feed.Entries) != 2 {
  45. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  46. }
  47. if feed.Entries[0].Hash != "d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" {
  48. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  49. }
  50. if feed.Entries[0].URL != "https://example.org/second-item" {
  51. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  52. }
  53. if feed.Entries[0].Title != "This is a second item." {
  54. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
  55. }
  56. if feed.Entries[0].Content != "This is a second item." {
  57. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  58. }
  59. if feed.Entries[1].Hash != "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b" {
  60. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[1].Hash)
  61. }
  62. if feed.Entries[1].URL != "https://example.org/initial-post" {
  63. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[1].URL)
  64. }
  65. if feed.Entries[1].Title != "Hello, world!" {
  66. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[1].Title)
  67. }
  68. if feed.Entries[1].Content != "<p>Hello, world!</p>" {
  69. t.Errorf("Incorrect entry content, got: %s", feed.Entries[1].Content)
  70. }
  71. }
  72. func TestParsePodcast(t *testing.T) {
  73. data := `{
  74. "version": "https://jsonfeed.org/version/1",
  75. "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",
  76. "title": "The Record",
  77. "home_page_url": "http://therecord.co/",
  78. "feed_url": "http://therecord.co/feed.json",
  79. "items": [
  80. {
  81. "id": "http://therecord.co/chris-parrish",
  82. "title": "Special #1 - Chris Parrish",
  83. "url": "http://therecord.co/chris-parrish",
  84. "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.",
  85. "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.",
  86. "summary": "Brent interviews Chris Parrish, co-host of The Record and one-half of Aged & Distilled.",
  87. "date_published": "2014-05-09T14:04:00-07:00",
  88. "attachments": [
  89. {
  90. "url": "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a",
  91. "mime_type": "audio/x-m4a",
  92. "size_in_bytes": 89970236,
  93. "duration_in_seconds": 6629
  94. }
  95. ]
  96. }
  97. ]
  98. }`
  99. feed, err := Parse(bytes.NewBufferString(data))
  100. if err != nil {
  101. t.Error(err)
  102. }
  103. if feed.Title != "The Record" {
  104. t.Errorf("Incorrect title, got: %s", feed.Title)
  105. }
  106. if feed.FeedURL != "http://therecord.co/feed.json" {
  107. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  108. }
  109. if feed.SiteURL != "http://therecord.co/" {
  110. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  111. }
  112. if len(feed.Entries) != 1 {
  113. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  114. }
  115. if feed.Entries[0].Hash != "6b678e57962a1b001e4e873756563cdc08bbd06ca561e764e0baa9a382485797" {
  116. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  117. }
  118. if feed.Entries[0].URL != "http://therecord.co/chris-parrish" {
  119. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  120. }
  121. if feed.Entries[0].Title != "Special #1 - Chris Parrish" {
  122. t.Errorf(`Incorrect entry title, got: "%s"`, feed.Entries[0].Title)
  123. }
  124. 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.` {
  125. t.Errorf(`Incorrect entry content, got: "%s"`, feed.Entries[0].Content)
  126. }
  127. location, _ := time.LoadLocation("America/Vancouver")
  128. if !feed.Entries[0].Date.Equal(time.Date(2014, time.May, 9, 14, 4, 0, 0, location)) {
  129. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  130. }
  131. if len(feed.Entries[0].Enclosures) != 1 {
  132. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  133. }
  134. if feed.Entries[0].Enclosures[0].URL != "http://therecord.co/downloads/The-Record-sp1e1-ChrisParrish.m4a" {
  135. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  136. }
  137. if feed.Entries[0].Enclosures[0].MimeType != "audio/x-m4a" {
  138. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  139. }
  140. if feed.Entries[0].Enclosures[0].Size != 89970236 {
  141. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  142. }
  143. }
  144. func TestParseFeedWithRelativeURL(t *testing.T) {
  145. data := `{
  146. "version": "https://jsonfeed.org/version/1",
  147. "title": "Example",
  148. "home_page_url": "https://example.org/",
  149. "feed_url": "https://example.org/feed.json",
  150. "items": [
  151. {
  152. "id": "2347259",
  153. "url": "something.html",
  154. "date_published": "2016-02-09T14:22:00-07:00"
  155. }
  156. ]
  157. }`
  158. feed, err := Parse(bytes.NewBufferString(data))
  159. if err != nil {
  160. t.Error(err)
  161. }
  162. if feed.Entries[0].URL != "https://example.org/something.html" {
  163. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  164. }
  165. }
  166. func TestParseAuthor(t *testing.T) {
  167. data := `{
  168. "version": "https://jsonfeed.org/version/1",
  169. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  170. "title": "Brent Simmons’s Microblog",
  171. "home_page_url": "https://example.org/",
  172. "feed_url": "https://example.org/feed.json",
  173. "author": {
  174. "name": "Brent Simmons",
  175. "url": "http://example.org/",
  176. "avatar": "https://example.org/avatar.png"
  177. },
  178. "items": [
  179. {
  180. "id": "2347259",
  181. "url": "https://example.org/2347259",
  182. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  183. "date_published": "2016-02-09T14:22:00-07:00"
  184. }
  185. ]
  186. }`
  187. feed, err := Parse(bytes.NewBufferString(data))
  188. if err != nil {
  189. t.Error(err)
  190. }
  191. if len(feed.Entries) != 1 {
  192. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  193. }
  194. if feed.Entries[0].Author != "Brent Simmons" {
  195. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  196. }
  197. }
  198. func TestParseFeedWithoutTitle(t *testing.T) {
  199. data := `{
  200. "version": "https://jsonfeed.org/version/1",
  201. "home_page_url": "https://example.org/",
  202. "feed_url": "https://example.org/feed.json",
  203. "items": [
  204. {
  205. "id": "2347259",
  206. "url": "https://example.org/2347259",
  207. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  208. "date_published": "2016-02-09T14:22:00-07:00"
  209. }
  210. ]
  211. }`
  212. feed, err := Parse(bytes.NewBufferString(data))
  213. if err != nil {
  214. t.Error(err)
  215. }
  216. if feed.Title != "https://example.org/" {
  217. t.Errorf("Incorrect title, got: %s", feed.Title)
  218. }
  219. }
  220. func TestParseFeedItemWithInvalidDate(t *testing.T) {
  221. data := `{
  222. "version": "https://jsonfeed.org/version/1",
  223. "title": "My Example Feed",
  224. "home_page_url": "https://example.org/",
  225. "feed_url": "https://example.org/feed.json",
  226. "items": [
  227. {
  228. "id": "2347259",
  229. "url": "https://example.org/2347259",
  230. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  231. "date_published": "Tomorrow"
  232. }
  233. ]
  234. }`
  235. feed, err := Parse(bytes.NewBufferString(data))
  236. if err != nil {
  237. t.Error(err)
  238. }
  239. if len(feed.Entries) != 1 {
  240. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  241. }
  242. if !feed.Entries[0].Date.Before(time.Now()) {
  243. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  244. }
  245. }
  246. func TestParseFeedItemWithoutID(t *testing.T) {
  247. data := `{
  248. "version": "https://jsonfeed.org/version/1",
  249. "title": "My Example Feed",
  250. "home_page_url": "https://example.org/",
  251. "feed_url": "https://example.org/feed.json",
  252. "items": [
  253. {
  254. "content_text": "Some text."
  255. }
  256. ]
  257. }`
  258. feed, err := Parse(bytes.NewBufferString(data))
  259. if err != nil {
  260. t.Error(err)
  261. }
  262. if len(feed.Entries) != 1 {
  263. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  264. }
  265. if feed.Entries[0].Hash != "13b4c5aecd1b6d749afcee968fbf9c80f1ed1bbdbe1aaf25cb34ebd01144bbe9" {
  266. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  267. }
  268. }
  269. func TestParseFeedItemWithoutTitle(t *testing.T) {
  270. data := `{
  271. "version": "https://jsonfeed.org/version/1",
  272. "title": "My Example Feed",
  273. "home_page_url": "https://example.org/",
  274. "feed_url": "https://example.org/feed.json",
  275. "items": [
  276. {
  277. "url": "https://example.org/item"
  278. }
  279. ]
  280. }`
  281. feed, err := Parse(bytes.NewBufferString(data))
  282. if err != nil {
  283. t.Error(err)
  284. }
  285. if len(feed.Entries) != 1 {
  286. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  287. }
  288. if feed.Entries[0].Title != "https://example.org/item" {
  289. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  290. }
  291. }
  292. func TestParseTruncateItemTitle(t *testing.T) {
  293. data := `{
  294. "version": "https://jsonfeed.org/version/1",
  295. "title": "My Example Feed",
  296. "home_page_url": "https://example.org/",
  297. "feed_url": "https://example.org/feed.json",
  298. "items": [
  299. {
  300. "title": "` + strings.Repeat("a", 200) + `"
  301. }
  302. ]
  303. }`
  304. feed, err := Parse(bytes.NewBufferString(data))
  305. if err != nil {
  306. t.Error(err)
  307. }
  308. if len(feed.Entries) != 1 {
  309. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  310. }
  311. if len(feed.Entries[0].Title) != 103 {
  312. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  313. }
  314. }
  315. func TestParseInvalidJSON(t *testing.T) {
  316. data := `garbage`
  317. _, err := Parse(bytes.NewBufferString(data))
  318. if err == nil {
  319. t.Error("Parse should returns an error")
  320. }
  321. if _, ok := err.(errors.LocalizedError); !ok {
  322. t.Error("The error returned must be a LocalizedError")
  323. }
  324. }