rewriter_test.go 20 KB

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