rewriter_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package rewrite // import "miniflux.app/v2/internal/reader/rewrite"
  4. import (
  5. "os"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "miniflux.app/v2/internal/config"
  10. "miniflux.app/v2/internal/model"
  11. )
  12. func TestParseRules(t *testing.T) {
  13. rulesText := `add_dynamic_image,replace("article/(.*).svg"|"article/$1.png"),remove(".spam, .ads:not(.keep)")`
  14. expected := []rule{
  15. {name: "add_dynamic_image"},
  16. {name: "replace", args: []string{"article/(.*).svg", "article/$1.png"}},
  17. {name: "remove", args: []string{".spam, .ads:not(.keep)"}},
  18. }
  19. actual := parseRules(rulesText)
  20. if !reflect.DeepEqual(expected, actual) {
  21. t.Errorf(`Parsed rules do not match expected rules: got %v instead of %v`, actual, expected)
  22. }
  23. }
  24. func TestReplaceTextLinks(t *testing.T) {
  25. scenarios := map[string]string{
  26. `This is a link to example.org`: `This is a link to example.org`,
  27. `This is a link to ftp://example.org`: `This is a link to ftp://example.org`,
  28. `This is a link to www.example.org`: `This is a link to www.example.org`,
  29. `This is a link to http://example.org`: `This is a link to <a href="http://example.org">http://example.org</a>`,
  30. `This is a link to http://example.org, end of sentence.`: `This is a link to <a href="http://example.org">http://example.org</a>, end of sentence.`,
  31. `This is a link to https://example.org`: `This is a link to <a href="https://example.org">https://example.org</a>`,
  32. `This is a link to https://www.example.org/path/to?q=s`: `This is a link to <a href="https://www.example.org/path/to?q=s">https://www.example.org/path/to?q=s</a>`,
  33. `This is a link to https://example.org/index#hash-tag, http://example.org/.`: `This is a link to <a href="https://example.org/index#hash-tag">https://example.org/index#hash-tag</a>, <a href="http://example.org/">http://example.org/</a>.`,
  34. }
  35. for input, expected := range scenarios {
  36. actual := replaceTextLinks(input)
  37. if actual != expected {
  38. t.Errorf(`Unexpected link replacement, got "%s" instead of "%s"`, actual, expected)
  39. }
  40. }
  41. }
  42. func TestRewriteWithNoMatchingRule(t *testing.T) {
  43. controlEntry := &model.Entry{
  44. Title: `A title`,
  45. Content: `Some text.`,
  46. }
  47. testEntry := &model.Entry{
  48. Title: `A title`,
  49. Content: `Some text.`,
  50. }
  51. Rewriter("https://example.org/article", testEntry, ``)
  52. if !reflect.DeepEqual(testEntry, controlEntry) {
  53. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  54. }
  55. }
  56. func TestRewriteWithYoutubeLink(t *testing.T) {
  57. config.Opts = config.NewOptions()
  58. controlEntry := &model.Entry{
  59. Title: `A title`,
  60. Content: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><br>Video Description`,
  61. }
  62. testEntry := &model.Entry{
  63. Title: `A title`,
  64. Content: `Video Description`,
  65. }
  66. Rewriter("https://www.youtube.com/watch?v=1234", testEntry, ``)
  67. if !reflect.DeepEqual(testEntry, controlEntry) {
  68. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  69. }
  70. }
  71. func TestRewriteWithYoutubeLinkAndCustomEmbedURL(t *testing.T) {
  72. os.Clearenv()
  73. os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://invidious.custom/embed/")
  74. var err error
  75. parser := config.NewParser()
  76. config.Opts, err = parser.ParseEnvironmentVariables()
  77. if err != nil {
  78. t.Fatalf(`Parsing failure: %v`, err)
  79. }
  80. controlEntry := &model.Entry{
  81. Title: `A title`,
  82. Content: `<iframe width="650" height="350" frameborder="0" src="https://invidious.custom/embed/1234" allowfullscreen></iframe><br>Video Description`,
  83. }
  84. testEntry := &model.Entry{
  85. Title: `A title`,
  86. Content: `Video Description`,
  87. }
  88. Rewriter("https://www.youtube.com/watch?v=1234", testEntry, ``)
  89. if !reflect.DeepEqual(testEntry, controlEntry) {
  90. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  91. }
  92. }
  93. func TestRewriteWithInexistingCustomRule(t *testing.T) {
  94. controlEntry := &model.Entry{
  95. Title: `A title`,
  96. Content: `Video Description`,
  97. }
  98. testEntry := &model.Entry{
  99. Title: `A title`,
  100. Content: `Video Description`,
  101. }
  102. Rewriter("https://www.youtube.com/watch?v=1234", testEntry, `some rule`)
  103. if !reflect.DeepEqual(testEntry, controlEntry) {
  104. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  105. }
  106. }
  107. func TestRewriteWithXkcdLink(t *testing.T) {
  108. controlEntry := &model.Entry{
  109. Title: `A title`,
  110. Content: `<figure><img src="https://imgs.xkcd.com/comics/thermostat.png" alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you."/><figcaption><p>Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you.</p></figcaption></figure>`,
  111. }
  112. testEntry := &model.Entry{
  113. Title: `A title`,
  114. Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" title="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." />`,
  115. }
  116. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  117. if !reflect.DeepEqual(testEntry, controlEntry) {
  118. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  119. }
  120. }
  121. func TestRewriteWithXkcdLinkHtmlInjection(t *testing.T) {
  122. controlEntry := &model.Entry{
  123. Title: `A title`,
  124. Content: `<figure><img src="https://imgs.xkcd.com/comics/thermostat.png" alt="&lt;foo&gt;"/><figcaption><p>&lt;foo&gt;</p></figcaption></figure>`,
  125. }
  126. testEntry := &model.Entry{
  127. Title: `A title`,
  128. Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" title="<foo>" alt="<foo>" />`,
  129. }
  130. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  131. if !reflect.DeepEqual(testEntry, controlEntry) {
  132. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  133. }
  134. }
  135. func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) {
  136. controlEntry := &model.Entry{
  137. Title: `A title`,
  138. Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." />`,
  139. }
  140. testEntry := &model.Entry{
  141. Title: `A title`,
  142. Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." />`,
  143. }
  144. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  145. if !reflect.DeepEqual(testEntry, controlEntry) {
  146. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  147. }
  148. }
  149. func TestRewriteWithXkcdLinkAndNoImage(t *testing.T) {
  150. controlEntry := &model.Entry{
  151. Title: `A title`,
  152. Content: `test`,
  153. }
  154. testEntry := &model.Entry{
  155. Title: `A title`,
  156. Content: `test`,
  157. }
  158. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  159. if !reflect.DeepEqual(testEntry, controlEntry) {
  160. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  161. }
  162. }
  163. func TestRewriteWithXkcdAndNoImage(t *testing.T) {
  164. controlEntry := &model.Entry{
  165. Title: `A title`,
  166. Content: `test`,
  167. }
  168. testEntry := &model.Entry{
  169. Title: `A title`,
  170. Content: `test`,
  171. }
  172. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  173. if !reflect.DeepEqual(testEntry, controlEntry) {
  174. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  175. }
  176. }
  177. func TestRewriteMailtoLink(t *testing.T) {
  178. controlEntry := &model.Entry{
  179. Title: `A title`,
  180. Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact [blah blah]</a>`,
  181. }
  182. testEntry := &model.Entry{
  183. Title: `A title`,
  184. Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact</a>`,
  185. }
  186. Rewriter("https://www.qwantz.com/", testEntry, ``)
  187. if !reflect.DeepEqual(testEntry, controlEntry) {
  188. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  189. }
  190. }
  191. func TestRewriteWithPDFLink(t *testing.T) {
  192. controlEntry := &model.Entry{
  193. Title: `A title`,
  194. Content: `<a href="https://example.org/document.pdf">PDF</a><br>test`,
  195. }
  196. testEntry := &model.Entry{
  197. Title: `A title`,
  198. Content: `test`,
  199. }
  200. Rewriter("https://example.org/document.pdf", testEntry, ``)
  201. if !reflect.DeepEqual(testEntry, controlEntry) {
  202. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  203. }
  204. }
  205. func TestRewriteWithNoLazyImage(t *testing.T) {
  206. controlEntry := &model.Entry{
  207. Title: `A title`,
  208. Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
  209. }
  210. testEntry := &model.Entry{
  211. Title: `A title`,
  212. Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
  213. }
  214. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  215. if !reflect.DeepEqual(testEntry, controlEntry) {
  216. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  217. }
  218. }
  219. func TestRewriteWithLazyImage(t *testing.T) {
  220. controlEntry := &model.Entry{
  221. Title: `A title`,
  222. Content: `<img src="https://example.org/image.jpg" data-url="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  223. }
  224. testEntry := &model.Entry{
  225. Title: `A title`,
  226. Content: `<img src="" data-url="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  227. }
  228. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  229. if !reflect.DeepEqual(testEntry, controlEntry) {
  230. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  231. }
  232. }
  233. func TestRewriteWithLazyDivImage(t *testing.T) {
  234. controlEntry := &model.Entry{
  235. Title: `A title`,
  236. Content: `<img src="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  237. }
  238. testEntry := &model.Entry{
  239. Title: `A title`,
  240. Content: `<div data-url="https://example.org/image.jpg" alt="Image"></div><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  241. }
  242. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  243. if !reflect.DeepEqual(testEntry, controlEntry) {
  244. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  245. }
  246. }
  247. func TestRewriteWithUnknownLazyNoScriptImage(t *testing.T) {
  248. controlEntry := &model.Entry{
  249. Title: `A title`,
  250. Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"/><img src="https://example.org/fallback.jpg" alt="Fallback"/>`,
  251. }
  252. testEntry := &model.Entry{
  253. Title: `A title`,
  254. Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  255. }
  256. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  257. if !reflect.DeepEqual(testEntry, controlEntry) {
  258. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  259. }
  260. }
  261. func TestRewriteWithLazySrcset(t *testing.T) {
  262. controlEntry := &model.Entry{
  263. Title: `A title`,
  264. Content: `<img srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
  265. }
  266. testEntry := &model.Entry{
  267. Title: `A title`,
  268. Content: `<img srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
  269. }
  270. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  271. if !reflect.DeepEqual(testEntry, controlEntry) {
  272. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  273. }
  274. }
  275. func TestRewriteWithImageAndLazySrcset(t *testing.T) {
  276. controlEntry := &model.Entry{
  277. Title: `A title`,
  278. Content: `<img src="meow" srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
  279. }
  280. testEntry := &model.Entry{
  281. Title: `A title`,
  282. Content: `<img src="meow" srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
  283. }
  284. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  285. if !reflect.DeepEqual(testEntry, controlEntry) {
  286. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  287. }
  288. }
  289. func TestNewLineRewriteRule(t *testing.T) {
  290. controlEntry := &model.Entry{
  291. Title: `A title`,
  292. Content: `A<br>B<br>C`,
  293. }
  294. testEntry := &model.Entry{
  295. Title: `A title`,
  296. Content: "A\nB\nC",
  297. }
  298. Rewriter("https://example.org/article", testEntry, "nl2br")
  299. if !reflect.DeepEqual(testEntry, controlEntry) {
  300. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  301. }
  302. }
  303. func TestConvertTextLinkRewriteRule(t *testing.T) {
  304. controlEntry := &model.Entry{
  305. Title: `A title`,
  306. Content: `Test: <a href="http://example.org/a/b">http://example.org/a/b</a>`,
  307. }
  308. testEntry := &model.Entry{
  309. Title: `A title`,
  310. Content: `Test: http://example.org/a/b`,
  311. }
  312. Rewriter("https://example.org/article", testEntry, "convert_text_link")
  313. if !reflect.DeepEqual(testEntry, controlEntry) {
  314. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  315. }
  316. }
  317. func TestMediumImage(t *testing.T) {
  318. controlEntry := &model.Entry{
  319. Title: `A title`,
  320. Content: `<img alt="Image for post" class="t u v if aj" src="https://miro.medium.com/max/2560/1*ephLSqSzQYLvb7faDwzRbw.jpeg" width="1280" height="720" srcset="https://miro.medium.com/max/552/1*ephLSqSzQYLvb7faDwzRbw.jpeg 276w, https://miro.medium.com/max/1104/1*ephLSqSzQYLvb7faDwzRbw.jpeg 552w, https://miro.medium.com/max/1280/1*ephLSqSzQYLvb7faDwzRbw.jpeg 640w, https://miro.medium.com/max/1400/1*ephLSqSzQYLvb7faDwzRbw.jpeg 700w" sizes="700px"/>`,
  321. }
  322. testEntry := &model.Entry{
  323. Title: `A title`,
  324. Content: `
  325. <figure class="ht hu hv hw hx hy cy cz paragraph-image">
  326. <div class="hz ia ib ic aj">
  327. <div class="cy cz hs">
  328. <div class="ii s ib ij">
  329. <div class="ik il s">
  330. <div class="id ie t u v if aj bk ig ih">
  331. <img alt="Image for post" class="t u v if aj im in io" src="https://miro.medium.com/max/60/1*ephLSqSzQYLvb7faDwzRbw.jpeg?q=20" width="1280" height="720"/>
  332. </div>
  333. <img alt="Image for post" class="id ie t u v if aj c" width="1280" height="720"/>
  334. <noscript>
  335. <img alt="Image for post" class="t u v if aj" src="https://miro.medium.com/max/2560/1*ephLSqSzQYLvb7faDwzRbw.jpeg" width="1280" height="720" srcSet="https://miro.medium.com/max/552/1*ephLSqSzQYLvb7faDwzRbw.jpeg 276w, https://miro.medium.com/max/1104/1*ephLSqSzQYLvb7faDwzRbw.jpeg 552w, https://miro.medium.com/max/1280/1*ephLSqSzQYLvb7faDwzRbw.jpeg 640w, https://miro.medium.com/max/1400/1*ephLSqSzQYLvb7faDwzRbw.jpeg 700w" sizes="700px"/>
  336. </noscript>
  337. </div>
  338. </div>
  339. </div>
  340. </div>
  341. </figure>
  342. `,
  343. }
  344. Rewriter("https://example.org/article", testEntry, "fix_medium_images")
  345. testEntry.Content = strings.TrimSpace(testEntry.Content)
  346. if !reflect.DeepEqual(testEntry, controlEntry) {
  347. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  348. }
  349. }
  350. func TestRewriteNoScriptImageWithoutNoScriptTag(t *testing.T) {
  351. controlEntry := &model.Entry{
  352. Title: `A title`,
  353. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."/><figcaption>MDN Logo</figcaption></figure>`,
  354. }
  355. testEntry := &model.Entry{
  356. Title: `A title`,
  357. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><figcaption>MDN Logo</figcaption></figure>`,
  358. }
  359. Rewriter("https://example.org/article", testEntry, "use_noscript_figure_images")
  360. testEntry.Content = strings.TrimSpace(testEntry.Content)
  361. if !reflect.DeepEqual(testEntry, controlEntry) {
  362. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  363. }
  364. }
  365. func TestRewriteNoScriptImageWithNoScriptTag(t *testing.T) {
  366. controlEntry := &model.Entry{
  367. Title: `A title`,
  368. Content: `<figure><img src="http://example.org/logo.svg"/><figcaption>MDN Logo</figcaption></figure>`,
  369. }
  370. testEntry := &model.Entry{
  371. Title: `A title`,
  372. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><noscript><img src="http://example.org/logo.svg"></noscript><figcaption>MDN Logo</figcaption></figure>`,
  373. }
  374. Rewriter("https://example.org/article", testEntry, "use_noscript_figure_images")
  375. testEntry.Content = strings.TrimSpace(testEntry.Content)
  376. if !reflect.DeepEqual(testEntry, controlEntry) {
  377. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  378. }
  379. }
  380. func TestRewriteReplaceCustom(t *testing.T) {
  381. controlEntry := &model.Entry{
  382. Title: `A title`,
  383. Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.png">`,
  384. }
  385. testEntry := &model.Entry{
  386. Title: `A title`,
  387. Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.svg">`,
  388. }
  389. Rewriter("https://example.org/article", testEntry, `replace("article/(.*).svg"|"article/$1.png")`)
  390. if !reflect.DeepEqual(testEntry, controlEntry) {
  391. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  392. }
  393. }
  394. func TestRewriteReplaceTitleCustom(t *testing.T) {
  395. controlEntry := &model.Entry{
  396. Title: `Ouch, a thistle`,
  397. Content: `The replace_title rewrite rule should not modify the content.`,
  398. }
  399. testEntry := &model.Entry{
  400. Title: `A title`,
  401. Content: `The replace_title rewrite rule should not modify the content.`,
  402. }
  403. Rewriter("https://example.org/article", testEntry, `replace_title("(?i)^a\\s*ti"|"Ouch, a this")`)
  404. if !reflect.DeepEqual(testEntry, controlEntry) {
  405. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  406. }
  407. }
  408. func TestRewriteRemoveCustom(t *testing.T) {
  409. controlEntry := &model.Entry{
  410. Title: `A title`,
  411. Content: `<div>Lorem Ipsum <span class="ads keep">Super important info</span></div>`,
  412. }
  413. testEntry := &model.Entry{
  414. Title: `A title`,
  415. Content: `<div>Lorem Ipsum <span class="spam">I dont want to see this</span><span class="ads keep">Super important info</span></div>`,
  416. }
  417. Rewriter("https://example.org/article", testEntry, `remove(".spam, .ads:not(.keep)")`)
  418. if !reflect.DeepEqual(testEntry, controlEntry) {
  419. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  420. }
  421. }
  422. func TestRewriteAddCastopodEpisode(t *testing.T) {
  423. controlEntry := &model.Entry{
  424. Title: `A title`,
  425. Content: `<iframe width="650" frameborder="0" src="https://podcast.demo/@demo/episodes/test/embed/light"></iframe><br>Episode Description`,
  426. }
  427. testEntry := &model.Entry{
  428. Title: `A title`,
  429. Content: `Episode Description`,
  430. }
  431. Rewriter("https://podcast.demo/@demo/episodes/test", testEntry, `add_castopod_episode`)
  432. if !reflect.DeepEqual(testEntry, controlEntry) {
  433. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  434. }
  435. }
  436. func TestRewriteBase64Decode(t *testing.T) {
  437. controlEntry := &model.Entry{
  438. Title: `A title`,
  439. Content: `This is some base64 encoded content`,
  440. }
  441. testEntry := &model.Entry{
  442. Title: `A title`,
  443. Content: `VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=`,
  444. }
  445. Rewriter("https://example.org/article", testEntry, `base64_decode`)
  446. if !reflect.DeepEqual(testEntry, controlEntry) {
  447. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  448. }
  449. }
  450. func TestRewriteBase64DecodeInHTML(t *testing.T) {
  451. controlEntry := &model.Entry{
  452. Title: `A title`,
  453. Content: `<div>Lorem Ipsum not valid base64<span class="base64">This is some base64 encoded content</span></div>`,
  454. }
  455. testEntry := &model.Entry{
  456. Title: `A title`,
  457. Content: `<div>Lorem Ipsum not valid base64<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
  458. }
  459. Rewriter("https://example.org/article", testEntry, `base64_decode`)
  460. if !reflect.DeepEqual(testEntry, controlEntry) {
  461. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  462. }
  463. }
  464. func TestRewriteBase64DecodeArgs(t *testing.T) {
  465. controlEntry := &model.Entry{
  466. Title: `A title`,
  467. Content: `<div>Lorem Ipsum<span class="base64">This is some base64 encoded content</span></div>`,
  468. }
  469. testEntry := &model.Entry{
  470. Title: `A title`,
  471. Content: `<div>Lorem Ipsum<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
  472. }
  473. Rewriter("https://example.org/article", testEntry, `base64_decode(".base64")`)
  474. if !reflect.DeepEqual(testEntry, controlEntry) {
  475. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  476. }
  477. }
  478. func TestRewriteRemoveTables(t *testing.T) {
  479. controlEntry := &model.Entry{
  480. Title: `A title`,
  481. Content: `<p>Test</p><p>Hello World!</p><p>Test</p>`,
  482. }
  483. testEntry := &model.Entry{
  484. Title: `A title`,
  485. Content: `<table class="container"><tbody><tr><td><p>Test</p><table class="row"><tbody><tr><td><p>Hello World!</p></td><td><p>Test</p></td></tr></tbody></table></td></tr></tbody></table>`,
  486. }
  487. Rewriter("https://example.org/article", testEntry, `remove_tables`)
  488. if !reflect.DeepEqual(testEntry, controlEntry) {
  489. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  490. }
  491. }
  492. func TestRemoveClickbait(t *testing.T) {
  493. controlEntry := &model.Entry{
  494. Title: `This Is Amazing`,
  495. Content: `Some description`,
  496. }
  497. testEntry := &model.Entry{
  498. Title: `THIS IS AMAZING`,
  499. Content: `Some description`,
  500. }
  501. Rewriter("https://example.org/article", testEntry, `remove_clickbait`)
  502. if !reflect.DeepEqual(testEntry, controlEntry) {
  503. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  504. }
  505. }
  506. func TestAddHackerNewsLinksUsingHack(t *testing.T) {
  507. testEntry := &model.Entry{
  508. Title: `A title`,
  509. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  510. <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
  511. <p>Points: 23</p>
  512. <p># Comments: 38</p>`,
  513. }
  514. controlEntry := &model.Entry{
  515. Title: `A title`,
  516. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  517. <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a> <a href="hack://item?id=37620043">Open with HACK</a></p>
  518. <p>Points: 23</p>
  519. <p># Comments: 38</p>`,
  520. }
  521. Rewriter("https://example.org/article", testEntry, `add_hn_links_using_hack`)
  522. if !reflect.DeepEqual(testEntry, controlEntry) {
  523. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  524. }
  525. }
  526. func TestAddHackerNewsLinksUsingOpener(t *testing.T) {
  527. testEntry := &model.Entry{
  528. Title: `A title`,
  529. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  530. <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
  531. <p>Points: 23</p>
  532. <p># Comments: 38</p>`,
  533. }
  534. controlEntry := &model.Entry{
  535. Title: `A title`,
  536. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  537. <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a> <a href="opener://x-callback-url/show-options?url=https%3A%2F%2Fnews.ycombinator.com%2Fitem%3Fid%3D37620043">Open with Opener</a></p>
  538. <p>Points: 23</p>
  539. <p># Comments: 38</p>`,
  540. }
  541. Rewriter("https://example.org/article", testEntry, `add_hn_links_using_opener`)
  542. if !reflect.DeepEqual(testEntry, controlEntry) {
  543. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  544. }
  545. }