finder_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package subscription
  4. import (
  5. "strings"
  6. "testing"
  7. )
  8. func TestFindYoutubeChannelFeed(t *testing.T) {
  9. scenarios := map[string]string{
  10. "https://www.youtube.com/channel/UC-Qj80avWItNRjkZ41rzHyw": "https://www.youtube.com/feeds/videos.xml?channel_id=UC-Qj80avWItNRjkZ41rzHyw",
  11. "http://example.org/feed": "http://example.org/feed",
  12. }
  13. for websiteURL, expectedFeedURL := range scenarios {
  14. result := findYoutubeChannelFeed(websiteURL)
  15. if result != expectedFeedURL {
  16. t.Errorf(`Unexpected Feed, got %s, instead of %s`, result, expectedFeedURL)
  17. }
  18. }
  19. }
  20. func TestParseWebPageWithRssFeed(t *testing.T) {
  21. htmlPage := `
  22. <!doctype html>
  23. <html>
  24. <head>
  25. <link href="http://example.org/rss" rel="alternate" type="application/rss+xml" title="Some Title">
  26. </head>
  27. <body>
  28. </body>
  29. </html>`
  30. subscriptions, err := parseWebPage("http://example.org/", strings.NewReader(htmlPage))
  31. if err != nil {
  32. t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
  33. }
  34. if len(subscriptions) != 1 {
  35. t.Fatal(`Incorrect number of subscriptions returned`)
  36. }
  37. if subscriptions[0].Title != "Some Title" {
  38. t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
  39. }
  40. if subscriptions[0].URL != "http://example.org/rss" {
  41. t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
  42. }
  43. if subscriptions[0].Type != "rss" {
  44. t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
  45. }
  46. }
  47. func TestParseWebPageWithAtomFeed(t *testing.T) {
  48. htmlPage := `
  49. <!doctype html>
  50. <html>
  51. <head>
  52. <link href="http://example.org/atom.xml" rel="alternate" type="application/atom+xml" title="Some Title">
  53. </head>
  54. <body>
  55. </body>
  56. </html>`
  57. subscriptions, err := parseWebPage("http://example.org/", strings.NewReader(htmlPage))
  58. if err != nil {
  59. t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
  60. }
  61. if len(subscriptions) != 1 {
  62. t.Fatal(`Incorrect number of subscriptions returned`)
  63. }
  64. if subscriptions[0].Title != "Some Title" {
  65. t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
  66. }
  67. if subscriptions[0].URL != "http://example.org/atom.xml" {
  68. t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
  69. }
  70. if subscriptions[0].Type != "atom" {
  71. t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
  72. }
  73. }
  74. func TestParseWebPageWithJSONFeed(t *testing.T) {
  75. htmlPage := `
  76. <!doctype html>
  77. <html>
  78. <head>
  79. <link href="http://example.org/feed.json" rel="alternate" type="application/feed+json" title="Some Title">
  80. </head>
  81. <body>
  82. </body>
  83. </html>`
  84. subscriptions, err := parseWebPage("http://example.org/", strings.NewReader(htmlPage))
  85. if err != nil {
  86. t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
  87. }
  88. if len(subscriptions) != 1 {
  89. t.Fatal(`Incorrect number of subscriptions returned`)
  90. }
  91. if subscriptions[0].Title != "Some Title" {
  92. t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
  93. }
  94. if subscriptions[0].URL != "http://example.org/feed.json" {
  95. t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
  96. }
  97. if subscriptions[0].Type != "json" {
  98. t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
  99. }
  100. }
  101. func TestParseWebPageWithOldJSONFeedMimeType(t *testing.T) {
  102. htmlPage := `
  103. <!doctype html>
  104. <html>
  105. <head>
  106. <link href="http://example.org/feed.json" rel="alternate" type="application/json" title="Some Title">
  107. </head>
  108. <body>
  109. </body>
  110. </html>`
  111. subscriptions, err := parseWebPage("http://example.org/", strings.NewReader(htmlPage))
  112. if err != nil {
  113. t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
  114. }
  115. if len(subscriptions) != 1 {
  116. t.Fatal(`Incorrect number of subscriptions returned`)
  117. }
  118. if subscriptions[0].Title != "Some Title" {
  119. t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
  120. }
  121. if subscriptions[0].URL != "http://example.org/feed.json" {
  122. t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
  123. }
  124. if subscriptions[0].Type != "json" {
  125. t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
  126. }
  127. }
  128. func TestParseWebPageWithRelativeFeedURL(t *testing.T) {
  129. htmlPage := `
  130. <!doctype html>
  131. <html>
  132. <head>
  133. <link href="/feed.json" rel="alternate" type="application/feed+json" title="Some Title">
  134. </head>
  135. <body>
  136. </body>
  137. </html>`
  138. subscriptions, err := parseWebPage("http://example.org/", strings.NewReader(htmlPage))
  139. if err != nil {
  140. t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
  141. }
  142. if len(subscriptions) != 1 {
  143. t.Fatal(`Incorrect number of subscriptions returned`)
  144. }
  145. if subscriptions[0].Title != "Some Title" {
  146. t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
  147. }
  148. if subscriptions[0].URL != "http://example.org/feed.json" {
  149. t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
  150. }
  151. if subscriptions[0].Type != "json" {
  152. t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
  153. }
  154. }
  155. func TestParseWebPageWithEmptyTitle(t *testing.T) {
  156. htmlPage := `
  157. <!doctype html>
  158. <html>
  159. <head>
  160. <link href="/feed.json" rel="alternate" type="application/feed+json">
  161. </head>
  162. <body>
  163. </body>
  164. </html>`
  165. subscriptions, err := parseWebPage("http://example.org/", strings.NewReader(htmlPage))
  166. if err != nil {
  167. t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
  168. }
  169. if len(subscriptions) != 1 {
  170. t.Fatal(`Incorrect number of subscriptions returned`)
  171. }
  172. if subscriptions[0].Title != "http://example.org/feed.json" {
  173. t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
  174. }
  175. if subscriptions[0].URL != "http://example.org/feed.json" {
  176. t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
  177. }
  178. if subscriptions[0].Type != "json" {
  179. t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
  180. }
  181. }
  182. func TestParseWebPageWithMultipleFeeds(t *testing.T) {
  183. htmlPage := `
  184. <!doctype html>
  185. <html>
  186. <head>
  187. <link href="http://example.org/atom.xml" rel="alternate" type="application/atom+xml" title="Atom Feed">
  188. <link href="http://example.org/feed.json" rel="alternate" type="application/feed+json" title="JSON Feed">
  189. </head>
  190. <body>
  191. </body>
  192. </html>`
  193. subscriptions, err := parseWebPage("http://example.org/", strings.NewReader(htmlPage))
  194. if err != nil {
  195. t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
  196. }
  197. if len(subscriptions) != 2 {
  198. t.Fatal(`Incorrect number of subscriptions returned`)
  199. }
  200. }
  201. func TestParseWebPageWithEmptyFeedURL(t *testing.T) {
  202. htmlPage := `
  203. <!doctype html>
  204. <html>
  205. <head>
  206. <link href rel="alternate" type="application/feed+json" title="Some Title">
  207. </head>
  208. <body>
  209. </body>
  210. </html>`
  211. subscriptions, err := parseWebPage("http://example.org/", strings.NewReader(htmlPage))
  212. if err != nil {
  213. t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
  214. }
  215. if len(subscriptions) != 0 {
  216. t.Fatal(`Incorrect number of subscriptions returned`)
  217. }
  218. }
  219. func TestParseWebPageWithNoHref(t *testing.T) {
  220. htmlPage := `
  221. <!doctype html>
  222. <html>
  223. <head>
  224. <link rel="alternate" type="application/feed+json" title="Some Title">
  225. </head>
  226. <body>
  227. </body>
  228. </html>`
  229. subscriptions, err := parseWebPage("http://example.org/", strings.NewReader(htmlPage))
  230. if err != nil {
  231. t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
  232. }
  233. if len(subscriptions) != 0 {
  234. t.Fatal(`Incorrect number of subscriptions returned`)
  235. }
  236. }