finder_test.go 7.6 KB

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