rewriter_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package rewrite // import "miniflux.app/reader/rewrite"
  4. import (
  5. "reflect"
  6. "strings"
  7. "testing"
  8. "miniflux.app/model"
  9. )
  10. func TestParseRules(t *testing.T) {
  11. rulesText := `add_dynamic_image,replace("article/(.*).svg"|"article/$1.png"),remove(".spam, .ads:not(.keep)")`
  12. expected := []rule{
  13. {name: "add_dynamic_image"},
  14. {name: "replace", args: []string{"article/(.*).svg", "article/$1.png"}},
  15. {name: "remove", args: []string{".spam, .ads:not(.keep)"}},
  16. }
  17. actual := parseRules(rulesText)
  18. if !reflect.DeepEqual(expected, actual) {
  19. t.Errorf(`Parsed rules do not match expected rules: got %v instead of %v`, actual, expected)
  20. }
  21. }
  22. func TestReplaceTextLinks(t *testing.T) {
  23. scenarios := map[string]string{
  24. `This is a link to example.org`: `This is a link to example.org`,
  25. `This is a link to ftp://example.org`: `This is a link to ftp://example.org`,
  26. `This is a link to www.example.org`: `This is a link to www.example.org`,
  27. `This is a link to http://example.org`: `This is a link to <a href="http://example.org">http://example.org</a>`,
  28. `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.`,
  29. `This is a link to https://example.org`: `This is a link to <a href="https://example.org">https://example.org</a>`,
  30. `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>`,
  31. `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>.`,
  32. }
  33. for input, expected := range scenarios {
  34. actual := replaceTextLinks(input)
  35. if actual != expected {
  36. t.Errorf(`Unexpected link replacement, got "%s" instead of "%s"`, actual, expected)
  37. }
  38. }
  39. }
  40. func TestRewriteWithNoMatchingRule(t *testing.T) {
  41. controlEntry := &model.Entry{
  42. Title: `A title`,
  43. Content: `Some text.`,
  44. }
  45. testEntry := &model.Entry{
  46. Title: `A title`,
  47. Content: `Some text.`,
  48. }
  49. Rewriter("https://example.org/article", testEntry, ``)
  50. if !reflect.DeepEqual(testEntry, controlEntry) {
  51. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  52. }
  53. }
  54. func TestRewriteWithYoutubeLink(t *testing.T) {
  55. controlEntry := &model.Entry{
  56. Title: `A title`,
  57. Content: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><br>Video Description`,
  58. }
  59. testEntry := &model.Entry{
  60. Title: `A title`,
  61. Content: `Video Description`,
  62. }
  63. Rewriter("https://www.youtube.com/watch?v=1234", testEntry, ``)
  64. if !reflect.DeepEqual(testEntry, controlEntry) {
  65. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  66. }
  67. }
  68. func TestRewriteWithInexistingCustomRule(t *testing.T) {
  69. controlEntry := &model.Entry{
  70. Title: `A title`,
  71. Content: `Video Description`,
  72. }
  73. testEntry := &model.Entry{
  74. Title: `A title`,
  75. Content: `Video Description`,
  76. }
  77. Rewriter("https://www.youtube.com/watch?v=1234", testEntry, `some rule`)
  78. if !reflect.DeepEqual(testEntry, controlEntry) {
  79. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  80. }
  81. }
  82. func TestRewriteWithXkcdLink(t *testing.T) {
  83. controlEntry := &model.Entry{
  84. Title: `A title`,
  85. 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>`,
  86. }
  87. testEntry := &model.Entry{
  88. Title: `A title`,
  89. 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." />`,
  90. }
  91. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  92. if !reflect.DeepEqual(testEntry, controlEntry) {
  93. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  94. }
  95. }
  96. func TestRewriteWithXkcdLinkHtmlInjection(t *testing.T) {
  97. controlEntry := &model.Entry{
  98. Title: `A title`,
  99. Content: `<figure><img src="https://imgs.xkcd.com/comics/thermostat.png" alt="&lt;foo&gt;"/><figcaption><p>&lt;foo&gt;</p></figcaption></figure>`,
  100. }
  101. testEntry := &model.Entry{
  102. Title: `A title`,
  103. Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" title="<foo>" alt="<foo>" />`,
  104. }
  105. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  106. if !reflect.DeepEqual(testEntry, controlEntry) {
  107. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  108. }
  109. }
  110. func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) {
  111. controlEntry := &model.Entry{
  112. Title: `A title`,
  113. 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." />`,
  114. }
  115. testEntry := &model.Entry{
  116. Title: `A title`,
  117. 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." />`,
  118. }
  119. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  120. if !reflect.DeepEqual(testEntry, controlEntry) {
  121. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  122. }
  123. }
  124. func TestRewriteWithXkcdLinkAndNoImage(t *testing.T) {
  125. controlEntry := &model.Entry{
  126. Title: `A title`,
  127. Content: `test`,
  128. }
  129. testEntry := &model.Entry{
  130. Title: `A title`,
  131. Content: `test`,
  132. }
  133. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  134. if !reflect.DeepEqual(testEntry, controlEntry) {
  135. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  136. }
  137. }
  138. func TestRewriteWithXkcdAndNoImage(t *testing.T) {
  139. controlEntry := &model.Entry{
  140. Title: `A title`,
  141. Content: `test`,
  142. }
  143. testEntry := &model.Entry{
  144. Title: `A title`,
  145. Content: `test`,
  146. }
  147. Rewriter("https://xkcd.com/1912/", testEntry, ``)
  148. if !reflect.DeepEqual(testEntry, controlEntry) {
  149. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  150. }
  151. }
  152. func TestRewriteMailtoLink(t *testing.T) {
  153. controlEntry := &model.Entry{
  154. Title: `A title`,
  155. Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact [blah blah]</a>`,
  156. }
  157. testEntry := &model.Entry{
  158. Title: `A title`,
  159. Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact</a>`,
  160. }
  161. Rewriter("https://www.qwantz.com/", testEntry, ``)
  162. if !reflect.DeepEqual(testEntry, controlEntry) {
  163. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  164. }
  165. }
  166. func TestRewriteWithPDFLink(t *testing.T) {
  167. controlEntry := &model.Entry{
  168. Title: `A title`,
  169. Content: `<a href="https://example.org/document.pdf">PDF</a><br>test`,
  170. }
  171. testEntry := &model.Entry{
  172. Title: `A title`,
  173. Content: `test`,
  174. }
  175. Rewriter("https://example.org/document.pdf", testEntry, ``)
  176. if !reflect.DeepEqual(testEntry, controlEntry) {
  177. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  178. }
  179. }
  180. func TestRewriteWithNoLazyImage(t *testing.T) {
  181. controlEntry := &model.Entry{
  182. Title: `A title`,
  183. Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
  184. }
  185. testEntry := &model.Entry{
  186. Title: `A title`,
  187. Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
  188. }
  189. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  190. if !reflect.DeepEqual(testEntry, controlEntry) {
  191. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  192. }
  193. }
  194. func TestRewriteWithLazyImage(t *testing.T) {
  195. controlEntry := &model.Entry{
  196. Title: `A title`,
  197. 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>`,
  198. }
  199. testEntry := &model.Entry{
  200. Title: `A title`,
  201. Content: `<img src="" data-url="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  202. }
  203. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  204. if !reflect.DeepEqual(testEntry, controlEntry) {
  205. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  206. }
  207. }
  208. func TestRewriteWithLazyDivImage(t *testing.T) {
  209. controlEntry := &model.Entry{
  210. Title: `A title`,
  211. Content: `<img src="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  212. }
  213. testEntry := &model.Entry{
  214. Title: `A title`,
  215. Content: `<div data-url="https://example.org/image.jpg" alt="Image"></div><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  216. }
  217. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  218. if !reflect.DeepEqual(testEntry, controlEntry) {
  219. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  220. }
  221. }
  222. func TestRewriteWithUnknownLazyNoScriptImage(t *testing.T) {
  223. controlEntry := &model.Entry{
  224. Title: `A title`,
  225. Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"/><img src="https://example.org/fallback.jpg" alt="Fallback"/>`,
  226. }
  227. testEntry := &model.Entry{
  228. Title: `A title`,
  229. Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  230. }
  231. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  232. if !reflect.DeepEqual(testEntry, controlEntry) {
  233. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  234. }
  235. }
  236. func TestRewriteWithLazySrcset(t *testing.T) {
  237. controlEntry := &model.Entry{
  238. Title: `A title`,
  239. Content: `<img srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
  240. }
  241. testEntry := &model.Entry{
  242. Title: `A title`,
  243. Content: `<img srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
  244. }
  245. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  246. if !reflect.DeepEqual(testEntry, controlEntry) {
  247. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  248. }
  249. }
  250. func TestRewriteWithImageAndLazySrcset(t *testing.T) {
  251. controlEntry := &model.Entry{
  252. Title: `A title`,
  253. Content: `<img src="meow" srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
  254. }
  255. testEntry := &model.Entry{
  256. Title: `A title`,
  257. Content: `<img src="meow" srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
  258. }
  259. Rewriter("https://example.org/article", testEntry, "add_dynamic_image")
  260. if !reflect.DeepEqual(testEntry, controlEntry) {
  261. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  262. }
  263. }
  264. func TestNewLineRewriteRule(t *testing.T) {
  265. controlEntry := &model.Entry{
  266. Title: `A title`,
  267. Content: `A<br>B<br>C`,
  268. }
  269. testEntry := &model.Entry{
  270. Title: `A title`,
  271. Content: "A\nB\nC",
  272. }
  273. Rewriter("https://example.org/article", testEntry, "nl2br")
  274. if !reflect.DeepEqual(testEntry, controlEntry) {
  275. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  276. }
  277. }
  278. func TestConvertTextLinkRewriteRule(t *testing.T) {
  279. controlEntry := &model.Entry{
  280. Title: `A title`,
  281. Content: `Test: <a href="http://example.org/a/b">http://example.org/a/b</a>`,
  282. }
  283. testEntry := &model.Entry{
  284. Title: `A title`,
  285. Content: `Test: http://example.org/a/b`,
  286. }
  287. Rewriter("https://example.org/article", testEntry, "convert_text_link")
  288. if !reflect.DeepEqual(testEntry, controlEntry) {
  289. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  290. }
  291. }
  292. func TestMediumImage(t *testing.T) {
  293. controlEntry := &model.Entry{
  294. Title: `A title`,
  295. 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"/>`,
  296. }
  297. testEntry := &model.Entry{
  298. Title: `A title`,
  299. Content: `
  300. <figure class="ht hu hv hw hx hy cy cz paragraph-image">
  301. <div class="hz ia ib ic aj">
  302. <div class="cy cz hs">
  303. <div class="ii s ib ij">
  304. <div class="ik il s">
  305. <div class="id ie t u v if aj bk ig ih">
  306. <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"/>
  307. </div>
  308. <img alt="Image for post" class="id ie t u v if aj c" width="1280" height="720"/>
  309. <noscript>
  310. <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"/>
  311. </noscript>
  312. </div>
  313. </div>
  314. </div>
  315. </div>
  316. </figure>
  317. `,
  318. }
  319. Rewriter("https://example.org/article", testEntry, "fix_medium_images")
  320. testEntry.Content = strings.TrimSpace(testEntry.Content)
  321. if !reflect.DeepEqual(testEntry, controlEntry) {
  322. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  323. }
  324. }
  325. func TestRewriteNoScriptImageWithoutNoScriptTag(t *testing.T) {
  326. controlEntry := &model.Entry{
  327. Title: `A title`,
  328. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."/><figcaption>MDN Logo</figcaption></figure>`,
  329. }
  330. testEntry := &model.Entry{
  331. Title: `A title`,
  332. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><figcaption>MDN Logo</figcaption></figure>`,
  333. }
  334. Rewriter("https://example.org/article", testEntry, "use_noscript_figure_images")
  335. testEntry.Content = strings.TrimSpace(testEntry.Content)
  336. if !reflect.DeepEqual(testEntry, controlEntry) {
  337. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  338. }
  339. }
  340. func TestRewriteNoScriptImageWithNoScriptTag(t *testing.T) {
  341. controlEntry := &model.Entry{
  342. Title: `A title`,
  343. Content: `<figure><img src="http://example.org/logo.svg"/><figcaption>MDN Logo</figcaption></figure>`,
  344. }
  345. testEntry := &model.Entry{
  346. Title: `A title`,
  347. 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>`,
  348. }
  349. Rewriter("https://example.org/article", testEntry, "use_noscript_figure_images")
  350. testEntry.Content = strings.TrimSpace(testEntry.Content)
  351. if !reflect.DeepEqual(testEntry, controlEntry) {
  352. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  353. }
  354. }
  355. func TestRewriteReplaceCustom(t *testing.T) {
  356. controlEntry := &model.Entry{
  357. Title: `A title`,
  358. Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.png">`,
  359. }
  360. testEntry := &model.Entry{
  361. Title: `A title`,
  362. Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.svg">`,
  363. }
  364. Rewriter("https://example.org/article", testEntry, `replace("article/(.*).svg"|"article/$1.png")`)
  365. if !reflect.DeepEqual(testEntry, controlEntry) {
  366. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  367. }
  368. }
  369. func TestRewriteRemoveCustom(t *testing.T) {
  370. controlEntry := &model.Entry{
  371. Title: `A title`,
  372. Content: `<div>Lorem Ipsum <span class="ads keep">Super important info</span></div>`,
  373. }
  374. testEntry := &model.Entry{
  375. Title: `A title`,
  376. Content: `<div>Lorem Ipsum <span class="spam">I dont want to see this</span><span class="ads keep">Super important info</span></div>`,
  377. }
  378. Rewriter("https://example.org/article", testEntry, `remove(".spam, .ads:not(.keep)")`)
  379. if !reflect.DeepEqual(testEntry, controlEntry) {
  380. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  381. }
  382. }
  383. func TestRewriteAddCastopodEpisode(t *testing.T) {
  384. controlEntry := &model.Entry{
  385. Title: `A title`,
  386. Content: `<iframe width="650" frameborder="0" src="https://podcast.demo/@demo/episodes/test/embed/light"></iframe><br>Episode Description`,
  387. }
  388. testEntry := &model.Entry{
  389. Title: `A title`,
  390. Content: `Episode Description`,
  391. }
  392. Rewriter("https://podcast.demo/@demo/episodes/test", testEntry, `add_castopod_episode`)
  393. if !reflect.DeepEqual(testEntry, controlEntry) {
  394. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  395. }
  396. }
  397. func TestRewriteBase64Decode(t *testing.T) {
  398. controlEntry := &model.Entry{
  399. Title: `A title`,
  400. Content: `This is some base64 encoded content`,
  401. }
  402. testEntry := &model.Entry{
  403. Title: `A title`,
  404. Content: `VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=`,
  405. }
  406. Rewriter("https://example.org/article", testEntry, `base64_decode`)
  407. if !reflect.DeepEqual(testEntry, controlEntry) {
  408. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  409. }
  410. }
  411. func TestRewriteBase64DecodeInHTML(t *testing.T) {
  412. controlEntry := &model.Entry{
  413. Title: `A title`,
  414. Content: `<div>Lorem Ipsum not valid base64<span class="base64">This is some base64 encoded content</span></div>`,
  415. }
  416. testEntry := &model.Entry{
  417. Title: `A title`,
  418. Content: `<div>Lorem Ipsum not valid base64<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
  419. }
  420. Rewriter("https://example.org/article", testEntry, `base64_decode`)
  421. if !reflect.DeepEqual(testEntry, controlEntry) {
  422. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  423. }
  424. }
  425. func TestRewriteBase64DecodeArgs(t *testing.T) {
  426. controlEntry := &model.Entry{
  427. Title: `A title`,
  428. Content: `<div>Lorem Ipsum<span class="base64">This is some base64 encoded content</span></div>`,
  429. }
  430. testEntry := &model.Entry{
  431. Title: `A title`,
  432. Content: `<div>Lorem Ipsum<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
  433. }
  434. Rewriter("https://example.org/article", testEntry, `base64_decode(".base64")`)
  435. if !reflect.DeepEqual(testEntry, controlEntry) {
  436. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  437. }
  438. }
  439. func TestRewriteRemoveTables(t *testing.T) {
  440. controlEntry := &model.Entry{
  441. Title: `A title`,
  442. Content: `<p>Test</p><p>Hello World!</p><p>Test</p>`,
  443. }
  444. testEntry := &model.Entry{
  445. Title: `A title`,
  446. 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>`,
  447. }
  448. Rewriter("https://example.org/article", testEntry, `remove_tables`)
  449. if !reflect.DeepEqual(testEntry, controlEntry) {
  450. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  451. }
  452. }
  453. func TestRemoveClickbait(t *testing.T) {
  454. controlEntry := &model.Entry{
  455. Title: `This Is Amazing`,
  456. Content: `Some description`,
  457. }
  458. testEntry := &model.Entry{
  459. Title: `THIS IS AMAZING`,
  460. Content: `Some description`,
  461. }
  462. Rewriter("https://example.org/article", testEntry, `remove_clickbait`)
  463. if !reflect.DeepEqual(testEntry, controlEntry) {
  464. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  465. }
  466. }