parser_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 TestParseAuthor(t *testing.T) {
  145. data := `{
  146. "version": "https://jsonfeed.org/version/1",
  147. "user_comment": "This is a microblog feed. You can add this to your feed reader using the following URL: https://example.org/feed.json",
  148. "title": "Brent Simmons’s Microblog",
  149. "home_page_url": "https://example.org/",
  150. "feed_url": "https://example.org/feed.json",
  151. "author": {
  152. "name": "Brent Simmons",
  153. "url": "http://example.org/",
  154. "avatar": "https://example.org/avatar.png"
  155. },
  156. "items": [
  157. {
  158. "id": "2347259",
  159. "url": "https://example.org/2347259",
  160. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  161. "date_published": "2016-02-09T14:22:00-07:00"
  162. }
  163. ]
  164. }`
  165. feed, err := Parse(bytes.NewBufferString(data))
  166. if err != nil {
  167. t.Error(err)
  168. }
  169. if len(feed.Entries) != 1 {
  170. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  171. }
  172. if feed.Entries[0].Author != "Brent Simmons" {
  173. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  174. }
  175. }
  176. func TestParseFeedWithoutTitle(t *testing.T) {
  177. data := `{
  178. "version": "https://jsonfeed.org/version/1",
  179. "home_page_url": "https://example.org/",
  180. "feed_url": "https://example.org/feed.json",
  181. "items": [
  182. {
  183. "id": "2347259",
  184. "url": "https://example.org/2347259",
  185. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  186. "date_published": "2016-02-09T14:22:00-07:00"
  187. }
  188. ]
  189. }`
  190. feed, err := Parse(bytes.NewBufferString(data))
  191. if err != nil {
  192. t.Error(err)
  193. }
  194. if feed.Title != "https://example.org/" {
  195. t.Errorf("Incorrect title, got: %s", feed.Title)
  196. }
  197. }
  198. func TestParseFeedItemWithInvalidDate(t *testing.T) {
  199. data := `{
  200. "version": "https://jsonfeed.org/version/1",
  201. "title": "My Example Feed",
  202. "home_page_url": "https://example.org/",
  203. "feed_url": "https://example.org/feed.json",
  204. "items": [
  205. {
  206. "id": "2347259",
  207. "url": "https://example.org/2347259",
  208. "content_text": "Cats are neat. \n\nhttps://example.org/cats",
  209. "date_published": "Tomorrow"
  210. }
  211. ]
  212. }`
  213. feed, err := Parse(bytes.NewBufferString(data))
  214. if err != nil {
  215. t.Error(err)
  216. }
  217. if len(feed.Entries) != 1 {
  218. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  219. }
  220. if !feed.Entries[0].Date.Before(time.Now()) {
  221. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  222. }
  223. }
  224. func TestParseFeedItemWithoutID(t *testing.T) {
  225. data := `{
  226. "version": "https://jsonfeed.org/version/1",
  227. "title": "My Example Feed",
  228. "home_page_url": "https://example.org/",
  229. "feed_url": "https://example.org/feed.json",
  230. "items": [
  231. {
  232. "content_text": "Some text."
  233. }
  234. ]
  235. }`
  236. feed, err := Parse(bytes.NewBufferString(data))
  237. if err != nil {
  238. t.Error(err)
  239. }
  240. if len(feed.Entries) != 1 {
  241. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  242. }
  243. if feed.Entries[0].Hash != "13b4c5aecd1b6d749afcee968fbf9c80f1ed1bbdbe1aaf25cb34ebd01144bbe9" {
  244. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  245. }
  246. }
  247. func TestParseFeedItemWithoutTitle(t *testing.T) {
  248. data := `{
  249. "version": "https://jsonfeed.org/version/1",
  250. "title": "My Example Feed",
  251. "home_page_url": "https://example.org/",
  252. "feed_url": "https://example.org/feed.json",
  253. "items": [
  254. {
  255. "url": "https://example.org/item"
  256. }
  257. ]
  258. }`
  259. feed, err := Parse(bytes.NewBufferString(data))
  260. if err != nil {
  261. t.Error(err)
  262. }
  263. if len(feed.Entries) != 1 {
  264. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  265. }
  266. if feed.Entries[0].Title != "https://example.org/item" {
  267. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  268. }
  269. }
  270. func TestParseTruncateItemTitle(t *testing.T) {
  271. data := `{
  272. "version": "https://jsonfeed.org/version/1",
  273. "title": "My Example Feed",
  274. "home_page_url": "https://example.org/",
  275. "feed_url": "https://example.org/feed.json",
  276. "items": [
  277. {
  278. "title": "` + strings.Repeat("a", 200) + `"
  279. }
  280. ]
  281. }`
  282. feed, err := Parse(bytes.NewBufferString(data))
  283. if err != nil {
  284. t.Error(err)
  285. }
  286. if len(feed.Entries) != 1 {
  287. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  288. }
  289. if len(feed.Entries[0].Title) != 103 {
  290. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  291. }
  292. }
  293. func TestParseInvalidJSON(t *testing.T) {
  294. data := `garbage`
  295. _, err := Parse(bytes.NewBufferString(data))
  296. if err == nil {
  297. t.Error("Parse should returns an error")
  298. }
  299. if _, ok := err.(errors.LocalizedError); !ok {
  300. t.Error("The error returned must be a LocalizedError")
  301. }
  302. }