content_rewrite_test.go 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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, errs := parseRules(rulesText)
  20. if len(errs) != 0 {
  21. t.Errorf(`Unexpected errors while parsing rules: %v`, errs)
  22. }
  23. if !reflect.DeepEqual(expected, actual) {
  24. t.Errorf(`Parsed rules do not match expected rules: got %v instead of %v`, actual, expected)
  25. }
  26. }
  27. func TestReplaceTextLinks(t *testing.T) {
  28. scenarios := map[string]string{
  29. `This is a link to example.org`: `This is a link to example.org`,
  30. `This is a link to ftp://example.org`: `This is a link to ftp://example.org`,
  31. `This is a link to www.example.org`: `This is a link to www.example.org`,
  32. `This is a link to http://example.org`: `This is a link to <a href="http://example.org">http://example.org</a>`,
  33. `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.`,
  34. `This is a link to https://example.org`: `This is a link to <a href="https://example.org">https://example.org</a>`,
  35. `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>`,
  36. `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>.`,
  37. }
  38. for input, expected := range scenarios {
  39. actual := replaceTextLinks(input)
  40. if actual != expected {
  41. t.Errorf(`Unexpected link replacement, got "%s" instead of "%s"`, actual, expected)
  42. }
  43. }
  44. }
  45. func TestRewriteWithNoMatchingRule(t *testing.T) {
  46. controlEntry := &model.Entry{
  47. URL: "https://example.org/article",
  48. Title: `A title`,
  49. Content: `Some text.`,
  50. }
  51. testEntry := &model.Entry{
  52. URL: "https://example.org/article",
  53. Title: `A title`,
  54. Content: `Some text.`,
  55. }
  56. ApplyContentRewriteRules(testEntry, ``)
  57. if !reflect.DeepEqual(testEntry, controlEntry) {
  58. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  59. }
  60. }
  61. func TestRewriteYoutubeVideoLink(t *testing.T) {
  62. config.Opts = config.NewConfigOptions()
  63. controlEntry := &model.Entry{
  64. URL: "https://www.youtube.com/watch?v=1234",
  65. Title: `A title`,
  66. Content: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><br>Video Description`,
  67. }
  68. testEntry := &model.Entry{
  69. URL: "https://www.youtube.com/watch?v=1234",
  70. Title: `A title`,
  71. Content: `Video Description`,
  72. }
  73. ApplyContentRewriteRules(testEntry, ``)
  74. if !reflect.DeepEqual(testEntry, controlEntry) {
  75. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  76. }
  77. }
  78. func TestRewriteYoutubeShortLink(t *testing.T) {
  79. config.Opts = config.NewConfigOptions()
  80. controlEntry := &model.Entry{
  81. URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
  82. Title: `A title`,
  83. Content: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1LUWKWZkPjo" allowfullscreen></iframe><br>Video Description`,
  84. }
  85. testEntry := &model.Entry{
  86. URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
  87. Title: `A title`,
  88. Content: `Video Description`,
  89. }
  90. ApplyContentRewriteRules(testEntry, ``)
  91. if !reflect.DeepEqual(testEntry, controlEntry) {
  92. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  93. }
  94. }
  95. func TestRewriteIncorrectYoutubeLink(t *testing.T) {
  96. config.Opts = config.NewConfigOptions()
  97. controlEntry := &model.Entry{
  98. URL: "https://www.youtube.com/some-page",
  99. Title: `A title`,
  100. Content: `Video Description`,
  101. }
  102. testEntry := &model.Entry{
  103. URL: "https://www.youtube.com/some-page",
  104. Title: `A title`,
  105. Content: `Video Description`,
  106. }
  107. ApplyContentRewriteRules(testEntry, ``)
  108. if !reflect.DeepEqual(testEntry, controlEntry) {
  109. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  110. }
  111. }
  112. func TestRewriteYoutubeLinkRejectsLookalikeDomain(t *testing.T) {
  113. config.Opts = config.NewConfigOptions()
  114. controlEntry := &model.Entry{
  115. URL: "https://notyoutube.com/watch?v=1234",
  116. Title: `A title`,
  117. Content: `Video Description`,
  118. }
  119. testEntry := &model.Entry{
  120. URL: "https://notyoutube.com/watch?v=1234",
  121. Title: `A title`,
  122. Content: `Video Description`,
  123. }
  124. ApplyContentRewriteRules(testEntry, `add_youtube_video`)
  125. if !reflect.DeepEqual(testEntry, controlEntry) {
  126. t.Errorf(`A domain that merely ends in "youtube.com" must not be treated as YouTube: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  127. }
  128. }
  129. func TestRewriteYoutubeLinkAndCustomEmbedURL(t *testing.T) {
  130. os.Clearenv()
  131. os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://invidious.custom/embed/")
  132. var err error
  133. parser := config.NewConfigParser()
  134. config.Opts, err = parser.ParseEnvironmentVariables()
  135. if err != nil {
  136. t.Fatalf(`Parsing failure: %v`, err)
  137. }
  138. controlEntry := &model.Entry{
  139. URL: "https://www.youtube.com/watch?v=1234",
  140. Title: `A title`,
  141. Content: `<iframe width="650" height="350" frameborder="0" src="https://invidious.custom/embed/1234" allowfullscreen></iframe><br>Video Description`,
  142. }
  143. testEntry := &model.Entry{
  144. URL: "https://www.youtube.com/watch?v=1234",
  145. Title: `A title`,
  146. Content: `Video Description`,
  147. }
  148. ApplyContentRewriteRules(testEntry, ``)
  149. if !reflect.DeepEqual(testEntry, controlEntry) {
  150. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  151. }
  152. }
  153. func TestRewriteYoutubeVideoLinkUsingInvidious(t *testing.T) {
  154. config.Opts = config.NewConfigOptions()
  155. controlEntry := &model.Entry{
  156. URL: "https://www.youtube.com/watch?v=1234",
  157. Title: `A title`,
  158. Content: `<iframe width="650" height="350" frameborder="0" src="https://yewtu.be/embed/1234" allowfullscreen></iframe><br>Video Description`,
  159. }
  160. testEntry := &model.Entry{
  161. URL: "https://www.youtube.com/watch?v=1234",
  162. Title: `A title`,
  163. Content: `Video Description`,
  164. }
  165. ApplyContentRewriteRules(testEntry, `add_youtube_video_using_invidious_player`)
  166. if !reflect.DeepEqual(testEntry, controlEntry) {
  167. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  168. }
  169. }
  170. func TestRewriteYoutubeShortLinkUsingInvidious(t *testing.T) {
  171. config.Opts = config.NewConfigOptions()
  172. controlEntry := &model.Entry{
  173. URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
  174. Title: `A title`,
  175. Content: `<iframe width="650" height="350" frameborder="0" src="https://yewtu.be/embed/1LUWKWZkPjo" allowfullscreen></iframe><br>Video Description`,
  176. }
  177. testEntry := &model.Entry{
  178. URL: "https://www.youtube.com/shorts/1LUWKWZkPjo",
  179. Title: `A title`,
  180. Content: `Video Description`,
  181. }
  182. ApplyContentRewriteRules(testEntry, `add_youtube_video_using_invidious_player`)
  183. if !reflect.DeepEqual(testEntry, controlEntry) {
  184. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  185. }
  186. }
  187. func TestAddYoutubeVideoFromId(t *testing.T) {
  188. config.Opts = config.NewConfigOptions()
  189. scenarios := map[string]string{
  190. // Test with single YouTube ID
  191. `Some content with youtube ID <script type="text/javascript" data-reactid="6">window.__APOLLO_STATE__ = {youtube_id: "9uASADiYe_8"}</script>`: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/9uASADiYe_8" allowfullscreen></iframe><br>Some content with youtube ID <script type="text/javascript" data-reactid="6">window.__APOLLO_STATE__ = {youtube_id: "9uASADiYe_8"}</script>`,
  192. // Test with multiple YouTube IDs
  193. `Content with youtube_id: "dQw4w9WgXcQ" and youtube_id: "jNQXAC9IVRw"`: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br><iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/jNQXAC9IVRw" allowfullscreen></iframe><br>Content with youtube_id: "dQw4w9WgXcQ" and youtube_id: "jNQXAC9IVRw"`,
  194. // Test with YouTube ID using equals sign
  195. `Some content with youtube_id = "dQw4w9WgXcQ"`: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Some content with youtube_id = "dQw4w9WgXcQ"`,
  196. // Test with spaces around delimiters
  197. `Some content with youtube_id : "dQw4w9WgXcQ"`: `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Some content with youtube_id : "dQw4w9WgXcQ"`,
  198. // Test with YouTube ID without quotes (regex requires quotes)
  199. `Some content with youtube_id: dQw4w9WgXcQ and more`: `Some content with youtube_id: dQw4w9WgXcQ and more`,
  200. // Test with no YouTube ID
  201. `Some regular content without any video ID`: `Some regular content without any video ID`,
  202. // Test with invalid YouTube ID (wrong length)
  203. `Some content with youtube_id: "invalid"`: `Some content with youtube_id: "invalid"`,
  204. // Test with empty content
  205. ``: ``,
  206. }
  207. for input, expected := range scenarios {
  208. actual := addYoutubeVideoFromId(input)
  209. if actual != expected {
  210. t.Errorf(`addYoutubeVideoFromId test failed for input "%s"`, input)
  211. t.Errorf(`Expected: "%s"`, expected)
  212. t.Errorf(`Actual: "%s"`, actual)
  213. }
  214. }
  215. }
  216. func TestAddYoutubeVideoFromIdWithCustomEmbedURL(t *testing.T) {
  217. os.Clearenv()
  218. os.Setenv("YOUTUBE_EMBED_URL_OVERRIDE", "https://invidious.custom/embed/")
  219. var err error
  220. parser := config.NewConfigParser()
  221. config.Opts, err = parser.ParseEnvironmentVariables()
  222. if err != nil {
  223. t.Fatalf(`Parsing failure: %v`, err)
  224. }
  225. input := `Some content with youtube_id: "dQw4w9WgXcQ"`
  226. expected := `<iframe width="650" height="350" frameborder="0" src="https://invidious.custom/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Some content with youtube_id: "dQw4w9WgXcQ"`
  227. actual := addYoutubeVideoFromId(input)
  228. if actual != expected {
  229. t.Errorf(`addYoutubeVideoFromId with custom embed URL failed`)
  230. t.Errorf(`Expected: "%s"`, expected)
  231. t.Errorf(`Actual: "%s"`, actual)
  232. }
  233. }
  234. func TestAddInvidiousVideo(t *testing.T) {
  235. scenarios := map[string][]string{
  236. // Test with various Invidious instances
  237. "https://invidious.io/watch?v=dQw4w9WgXcQ": {
  238. "Some video content",
  239. `<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Some video content`,
  240. },
  241. "https://yewtu.be/watch?v=jNQXAC9IVRw": {
  242. "Another video description",
  243. `<iframe width="650" height="350" frameborder="0" src="https://yewtu.be/embed/jNQXAC9IVRw" allowfullscreen></iframe><br>Another video description`,
  244. },
  245. "http://invidious.snopyta.org/watch?v=dQw4w9WgXcQ": {
  246. "HTTP instance test",
  247. `<iframe width="650" height="350" frameborder="0" src="https://invidious.snopyta.org/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>HTTP instance test`,
  248. },
  249. "https://youtube.com/watch?v=dQw4w9WgXcQ": {
  250. "YouTube URL (also matches regex)",
  251. `<iframe width="650" height="350" frameborder="0" src="https://youtube.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>YouTube URL (also matches regex)`,
  252. },
  253. "https://example.org/watch?v=dQw4w9WgXcQ": {
  254. "Any domain with watch pattern",
  255. `<iframe width="650" height="350" frameborder="0" src="https://example.org/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>Any domain with watch pattern`,
  256. },
  257. // Test with query parameters
  258. "https://invidious.io/watch?v=dQw4w9WgXcQ&t=30s": {
  259. "Video with timestamp",
  260. `<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ?t=30s" allowfullscreen></iframe><br>Video with timestamp`,
  261. },
  262. // Test with more complex query parameters
  263. "https://invidious.io/watch?v=dQw4w9WgXcQ&t=30s&autoplay=1": {
  264. "Video with multiple parameters",
  265. `<iframe width="650" height="350" frameborder="0" src="https://invidious.io/embed/dQw4w9WgXcQ?autoplay=1&t=30s" allowfullscreen></iframe><br>Video with multiple parameters`,
  266. },
  267. // Test with non-matching URLs (should return content unchanged)
  268. "https://invidious.io/": {
  269. "Invidious homepage",
  270. "Invidious homepage",
  271. },
  272. "https://invidious.io/some-other-page": {
  273. "Other page",
  274. "Other page",
  275. },
  276. "https://invidious.io/search?q=test": {
  277. "Search page",
  278. "Search page",
  279. },
  280. // Test with empty content
  281. "https://empty.invidious.io/watch?v=dQw4w9WgXcQ": {
  282. "",
  283. `<iframe width="650" height="350" frameborder="0" src="https://empty.invidious.io/embed/dQw4w9WgXcQ" allowfullscreen></iframe><br>`,
  284. },
  285. }
  286. for entryURL, testData := range scenarios {
  287. entryContent := testData[0]
  288. expected := testData[1]
  289. actual := addInvidiousVideo(entryURL, entryContent)
  290. if actual != expected {
  291. t.Errorf(`addInvidiousVideo test failed for URL "%s" and content "%s"`, entryURL, entryContent)
  292. t.Errorf(`Expected: "%s"`, expected)
  293. t.Errorf(`Actual: "%s"`, actual)
  294. }
  295. }
  296. }
  297. func TestRewriteWithInexistingCustomRule(t *testing.T) {
  298. controlEntry := &model.Entry{
  299. URL: "https://www.youtube.com/watch?v=1234",
  300. Title: `A title`,
  301. Content: `Video Description`,
  302. }
  303. testEntry := &model.Entry{
  304. URL: "https://www.youtube.com/watch?v=1234",
  305. Title: `A title`,
  306. Content: `Video Description`,
  307. }
  308. ApplyContentRewriteRules(testEntry, `some rule`)
  309. if !reflect.DeepEqual(testEntry, controlEntry) {
  310. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  311. }
  312. }
  313. func TestRewriteWithXkcdLink(t *testing.T) {
  314. controlEntry := &model.Entry{
  315. URL: "https://xkcd.com/1912/",
  316. Title: `A title`,
  317. 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>`,
  318. }
  319. testEntry := &model.Entry{
  320. URL: "https://xkcd.com/1912/",
  321. Title: `A title`,
  322. 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." />`,
  323. }
  324. ApplyContentRewriteRules(testEntry, ``)
  325. if !reflect.DeepEqual(testEntry, controlEntry) {
  326. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  327. }
  328. }
  329. func TestRewriteWithXkcdLinkHtmlInjection(t *testing.T) {
  330. controlEntry := &model.Entry{
  331. URL: "https://xkcd.com/1912/",
  332. Title: `A title`,
  333. Content: `<figure><img src="https://imgs.xkcd.com/comics/thermostat.png" alt="&lt;foo&gt;"/><figcaption><p>&lt;foo&gt;</p></figcaption></figure>`,
  334. }
  335. testEntry := &model.Entry{
  336. URL: "https://xkcd.com/1912/",
  337. Title: `A title`,
  338. Content: `<img src="https://imgs.xkcd.com/comics/thermostat.png" title="<foo>" alt="<foo>" />`,
  339. }
  340. ApplyContentRewriteRules(testEntry, ``)
  341. if !reflect.DeepEqual(testEntry, controlEntry) {
  342. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  343. }
  344. }
  345. func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) {
  346. controlEntry := &model.Entry{
  347. URL: "https://xkcd.com/1912/",
  348. Title: `A title`,
  349. 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." />`,
  350. }
  351. testEntry := &model.Entry{
  352. URL: "https://xkcd.com/1912/",
  353. Title: `A title`,
  354. 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." />`,
  355. }
  356. ApplyContentRewriteRules(testEntry, ``)
  357. if !reflect.DeepEqual(testEntry, controlEntry) {
  358. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  359. }
  360. }
  361. func TestRewriteWithXkcdLinkAndNoImage(t *testing.T) {
  362. controlEntry := &model.Entry{
  363. URL: "https://xkcd.com/1912/",
  364. Title: `A title`,
  365. Content: `test`,
  366. }
  367. testEntry := &model.Entry{
  368. URL: "https://xkcd.com/1912/",
  369. Title: `A title`,
  370. Content: `test`,
  371. }
  372. ApplyContentRewriteRules(testEntry, ``)
  373. if !reflect.DeepEqual(testEntry, controlEntry) {
  374. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  375. }
  376. }
  377. func TestRewriteWithXkcdAndNoImage(t *testing.T) {
  378. controlEntry := &model.Entry{
  379. URL: "https://xkcd.com/1912/",
  380. Title: `A title`,
  381. Content: `test`,
  382. }
  383. testEntry := &model.Entry{
  384. URL: "https://xkcd.com/1912/",
  385. Title: `A title`,
  386. Content: `test`,
  387. }
  388. ApplyContentRewriteRules(testEntry, ``)
  389. if !reflect.DeepEqual(testEntry, controlEntry) {
  390. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  391. }
  392. }
  393. func TestRewriteMailtoLink(t *testing.T) {
  394. controlEntry := &model.Entry{
  395. URL: "https://www.qwantz.com/",
  396. Title: `A title`,
  397. Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact [blah blah]</a>`,
  398. }
  399. testEntry := &model.Entry{
  400. URL: "https://www.qwantz.com/",
  401. Title: `A title`,
  402. Content: `<a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact</a>`,
  403. }
  404. ApplyContentRewriteRules(testEntry, ``)
  405. if !reflect.DeepEqual(testEntry, controlEntry) {
  406. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  407. }
  408. }
  409. func TestRewriteWithPDFLink(t *testing.T) {
  410. controlEntry := &model.Entry{
  411. URL: "https://example.org/document.pdf",
  412. Title: `A title`,
  413. Content: `<a href="https://example.org/document.pdf">PDF</a><br>test`,
  414. }
  415. testEntry := &model.Entry{
  416. URL: "https://example.org/document.pdf",
  417. Title: `A title`,
  418. Content: `test`,
  419. }
  420. ApplyContentRewriteRules(testEntry, ``)
  421. if !reflect.DeepEqual(testEntry, controlEntry) {
  422. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  423. }
  424. }
  425. func TestRewriteWithNoLazyImage(t *testing.T) {
  426. controlEntry := &model.Entry{
  427. URL: "https://example.org/article",
  428. Title: `A title`,
  429. Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
  430. }
  431. testEntry := &model.Entry{
  432. URL: "https://example.org/article",
  433. Title: `A title`,
  434. Content: `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`,
  435. }
  436. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  437. if !reflect.DeepEqual(testEntry, controlEntry) {
  438. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  439. }
  440. }
  441. func TestRewriteWithLazyImage(t *testing.T) {
  442. controlEntry := &model.Entry{
  443. URL: "https://example.org/article",
  444. Title: `A title`,
  445. 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>`,
  446. }
  447. testEntry := &model.Entry{
  448. URL: "https://example.org/article",
  449. Title: `A title`,
  450. Content: `<img src="" data-url="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  451. }
  452. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  453. if !reflect.DeepEqual(testEntry, controlEntry) {
  454. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  455. }
  456. }
  457. func TestRewriteWithLazyDivImage(t *testing.T) {
  458. controlEntry := &model.Entry{
  459. URL: "https://example.org/article",
  460. Title: `A title`,
  461. Content: `<img src="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"/></noscript>`,
  462. }
  463. testEntry := &model.Entry{
  464. URL: "https://example.org/article",
  465. Title: `A title`,
  466. Content: `<div data-url="https://example.org/image.jpg" alt="Image"></div><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  467. }
  468. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  469. if !reflect.DeepEqual(testEntry, controlEntry) {
  470. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  471. }
  472. }
  473. func TestRewriteWithUnknownLazyNoScriptImage(t *testing.T) {
  474. controlEntry := &model.Entry{
  475. URL: "https://example.org/article",
  476. Title: `A title`,
  477. Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"/><img src="https://example.org/fallback.jpg" alt="Fallback"/>`,
  478. }
  479. testEntry := &model.Entry{
  480. URL: "https://example.org/article",
  481. Title: `A title`,
  482. Content: `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`,
  483. }
  484. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  485. if !reflect.DeepEqual(testEntry, controlEntry) {
  486. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  487. }
  488. }
  489. func TestRewriteWithLazySrcset(t *testing.T) {
  490. controlEntry := &model.Entry{
  491. URL: "https://example.org/article",
  492. Title: `A title`,
  493. Content: `<img srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
  494. }
  495. testEntry := &model.Entry{
  496. URL: "https://example.org/article",
  497. Title: `A title`,
  498. Content: `<img srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
  499. }
  500. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  501. if !reflect.DeepEqual(testEntry, controlEntry) {
  502. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  503. }
  504. }
  505. func TestRewriteWithImageAndLazySrcset(t *testing.T) {
  506. controlEntry := &model.Entry{
  507. URL: "https://example.org/article",
  508. Title: `A title`,
  509. Content: `<img src="meow" srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/>`,
  510. }
  511. testEntry := &model.Entry{
  512. URL: "https://example.org/article",
  513. Title: `A title`,
  514. Content: `<img src="meow" srcset="" data-srcset="https://example.org/image.jpg" alt="Image">`,
  515. }
  516. ApplyContentRewriteRules(testEntry, "add_dynamic_image")
  517. if !reflect.DeepEqual(testEntry, controlEntry) {
  518. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  519. }
  520. }
  521. func TestRewriteWithNoLazyIframe(t *testing.T) {
  522. controlEntry := &model.Entry{
  523. URL: "https://example.org/article",
  524. Title: `A title`,
  525. Content: `<iframe src="https://example.org/embed" allowfullscreen></iframe>`,
  526. }
  527. testEntry := &model.Entry{
  528. URL: "https://example.org/article",
  529. Title: `A title`,
  530. Content: `<iframe src="https://example.org/embed" allowfullscreen></iframe>`,
  531. }
  532. ApplyContentRewriteRules(testEntry, "add_dynamic_iframe")
  533. if !reflect.DeepEqual(testEntry, controlEntry) {
  534. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  535. }
  536. }
  537. func TestRewriteWithLazyIframe(t *testing.T) {
  538. controlEntry := &model.Entry{
  539. URL: "https://example.org/article",
  540. Title: `A title`,
  541. Content: `<iframe data-src="https://example.org/embed" allowfullscreen="" src="https://example.org/embed"></iframe>`,
  542. }
  543. testEntry := &model.Entry{
  544. URL: "https://example.org/article",
  545. Title: `A title`,
  546. Content: `<iframe data-src="https://example.org/embed" allowfullscreen></iframe>`,
  547. }
  548. ApplyContentRewriteRules(testEntry, "add_dynamic_iframe")
  549. if !reflect.DeepEqual(testEntry, controlEntry) {
  550. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  551. }
  552. }
  553. func TestRewriteWithLazyIframeAndSrc(t *testing.T) {
  554. controlEntry := &model.Entry{
  555. URL: "https://example.org/article",
  556. Title: `A title`,
  557. Content: `<iframe src="https://example.org/embed" data-src="https://example.org/embed" allowfullscreen=""></iframe>`,
  558. }
  559. testEntry := &model.Entry{
  560. URL: "https://example.org/article",
  561. Title: `A title`,
  562. Content: `<iframe src="about:blank" data-src="https://example.org/embed" allowfullscreen></iframe>`,
  563. }
  564. ApplyContentRewriteRules(testEntry, "add_dynamic_iframe")
  565. if !reflect.DeepEqual(testEntry, controlEntry) {
  566. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  567. }
  568. }
  569. func TestNewLineRewriteRule(t *testing.T) {
  570. controlEntry := &model.Entry{
  571. URL: "https://example.org/article",
  572. Title: `A title`,
  573. Content: `A<br>B<br>C`,
  574. }
  575. testEntry := &model.Entry{
  576. URL: "https://example.org/article",
  577. Title: `A title`,
  578. Content: "A\nB\nC",
  579. }
  580. ApplyContentRewriteRules(testEntry, "nl2br")
  581. if !reflect.DeepEqual(testEntry, controlEntry) {
  582. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  583. }
  584. }
  585. func TestConvertTextLinkRewriteRule(t *testing.T) {
  586. controlEntry := &model.Entry{
  587. URL: "https://example.org/article",
  588. Title: `A title`,
  589. Content: `Test: <a href="http://example.org/a/b">http://example.org/a/b</a>`,
  590. }
  591. testEntry := &model.Entry{
  592. URL: "https://example.org/article",
  593. Title: `A title`,
  594. Content: `Test: http://example.org/a/b`,
  595. }
  596. ApplyContentRewriteRules(testEntry, "convert_text_link")
  597. if !reflect.DeepEqual(testEntry, controlEntry) {
  598. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  599. }
  600. }
  601. func TestMediumImage(t *testing.T) {
  602. controlEntry := &model.Entry{
  603. URL: "https://example.org/article",
  604. Title: `A title`,
  605. 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"/>`,
  606. }
  607. testEntry := &model.Entry{
  608. URL: "https://example.org/article",
  609. Title: `A title`,
  610. Content: `
  611. <figure class="ht hu hv hw hx hy cy cz paragraph-image">
  612. <div class="hz ia ib ic aj">
  613. <div class="cy cz hs">
  614. <div class="ii s ib ij">
  615. <div class="ik il s">
  616. <div class="id ie t u v if aj bk ig ih">
  617. <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"/>
  618. </div>
  619. <img alt="Image for post" class="id ie t u v if aj c" width="1280" height="720"/>
  620. <noscript>
  621. <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"/>
  622. </noscript>
  623. </div>
  624. </div>
  625. </div>
  626. </div>
  627. </figure>
  628. `,
  629. }
  630. ApplyContentRewriteRules(testEntry, "fix_medium_images")
  631. testEntry.Content = strings.TrimSpace(testEntry.Content)
  632. if !reflect.DeepEqual(testEntry, controlEntry) {
  633. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  634. }
  635. }
  636. func TestRewriteNoScriptImageWithoutNoScriptTag(t *testing.T) {
  637. controlEntry := &model.Entry{
  638. URL: "https://example.org/article",
  639. Title: `A title`,
  640. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."/><figcaption>MDN Logo</figcaption></figure>`,
  641. }
  642. testEntry := &model.Entry{
  643. URL: "https://example.org/article",
  644. Title: `A title`,
  645. Content: `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><figcaption>MDN Logo</figcaption></figure>`,
  646. }
  647. ApplyContentRewriteRules(testEntry, "use_noscript_figure_images")
  648. testEntry.Content = strings.TrimSpace(testEntry.Content)
  649. if !reflect.DeepEqual(testEntry, controlEntry) {
  650. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  651. }
  652. }
  653. func TestRewriteNoScriptImageWithNoScriptTag(t *testing.T) {
  654. controlEntry := &model.Entry{
  655. URL: "https://example.org/article",
  656. Title: `A title`,
  657. Content: `<figure><img src="http://example.org/logo.svg"/><figcaption>MDN Logo</figcaption></figure>`,
  658. }
  659. testEntry := &model.Entry{
  660. URL: "https://example.org/article",
  661. Title: `A title`,
  662. 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>`,
  663. }
  664. ApplyContentRewriteRules(testEntry, "use_noscript_figure_images")
  665. testEntry.Content = strings.TrimSpace(testEntry.Content)
  666. if !reflect.DeepEqual(testEntry, controlEntry) {
  667. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  668. }
  669. }
  670. func TestRewriteReplaceCustom(t *testing.T) {
  671. controlEntry := &model.Entry{
  672. URL: "https://example.org/article",
  673. Title: `A title`,
  674. Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.png">`,
  675. }
  676. testEntry := &model.Entry{
  677. URL: "https://example.org/article",
  678. Title: `A title`,
  679. Content: `<img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.svg">`,
  680. }
  681. ApplyContentRewriteRules(testEntry, `replace("article/(.*).svg"|"article/$1.png")`)
  682. if !reflect.DeepEqual(testEntry, controlEntry) {
  683. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  684. }
  685. }
  686. func TestRewriteReplaceTitleCustom(t *testing.T) {
  687. controlEntry := &model.Entry{
  688. URL: "https://example.org/article",
  689. Title: `Ouch, a thistle`,
  690. Content: `The replace_title rewrite rule should not modify the content.`,
  691. }
  692. testEntry := &model.Entry{
  693. URL: "https://example.org/article",
  694. Title: `A title`,
  695. Content: `The replace_title rewrite rule should not modify the content.`,
  696. }
  697. ApplyContentRewriteRules(testEntry, `replace_title("(?i)^a\\s*ti"|"Ouch, a this")`)
  698. if !reflect.DeepEqual(testEntry, controlEntry) {
  699. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  700. }
  701. }
  702. func TestRewriteRemoveCustom(t *testing.T) {
  703. controlEntry := &model.Entry{
  704. URL: "https://example.org/article",
  705. Title: `A title`,
  706. Content: `<div>Lorem Ipsum <span class="ads keep">Super important info</span></div>`,
  707. }
  708. testEntry := &model.Entry{
  709. URL: "https://example.org/article",
  710. Title: `A title`,
  711. Content: `<div>Lorem Ipsum <span class="spam">I dont want to see this</span><span class="ads keep">Super important info</span></div>`,
  712. }
  713. ApplyContentRewriteRules(testEntry, `remove(".spam, .ads:not(.keep)")`)
  714. if !reflect.DeepEqual(testEntry, controlEntry) {
  715. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  716. }
  717. }
  718. func TestRewriteRemoveQuotedSelector(t *testing.T) {
  719. controlEntry := &model.Entry{
  720. URL: "https://example.org/article",
  721. Title: `A title`,
  722. Content: `<div>Lorem Ipsum</div>`,
  723. }
  724. testEntry := &model.Entry{
  725. URL: "https://example.org/article",
  726. Title: `A title`,
  727. Content: `<div>Lorem Ipsum<img alt="LINUX KERNEL" src="/assets/categories/linuxkernel.webp" width="100" height="100"></div>`,
  728. }
  729. ApplyContentRewriteRules(testEntry, `remove("img[src^='/assets/categories/']")`)
  730. if !reflect.DeepEqual(testEntry, controlEntry) {
  731. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  732. }
  733. }
  734. func TestRewriteAddCastopodEpisode(t *testing.T) {
  735. controlEntry := &model.Entry{
  736. URL: "https://podcast.demo/@demo/episodes/test",
  737. Title: `A title`,
  738. Content: `<iframe width="650" frameborder="0" src="https://podcast.demo/@demo/episodes/test/embed/light"></iframe><br>Episode Description`,
  739. }
  740. testEntry := &model.Entry{
  741. URL: "https://podcast.demo/@demo/episodes/test",
  742. Title: `A title`,
  743. Content: `Episode Description`,
  744. }
  745. ApplyContentRewriteRules(testEntry, `add_castopod_episode`)
  746. if !reflect.DeepEqual(testEntry, controlEntry) {
  747. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  748. }
  749. }
  750. func TestRewriteAddEnclosureLinks(t *testing.T) {
  751. controlEntry := &model.Entry{
  752. URL: "https://example.org/article",
  753. Title: `A title`,
  754. Content: `Article Content<hr/><ul>` +
  755. `<li><a href="https://example.org/episode.mp3?token=a&amp;b=c">https://example.org/episode.mp3?token=a&amp;b=c</a></li>` +
  756. `<li><a href="https://example.org/video.mp4">https://example.org/video.mp4</a></li></ul>`,
  757. Enclosures: model.EnclosureList{
  758. {URL: "https://example.org/episode.mp3?token=a&b=c", MimeType: "audio/mpeg"},
  759. {URL: "https://example.org/video.mp4", MimeType: "video/mp4"},
  760. {URL: "", MimeType: "application/pdf"},
  761. },
  762. }
  763. testEntry := &model.Entry{
  764. URL: "https://example.org/article",
  765. Title: `A title`,
  766. Content: `Article Content`,
  767. Enclosures: model.EnclosureList{
  768. {URL: "https://example.org/episode.mp3?token=a&b=c", MimeType: "audio/mpeg"},
  769. {URL: "https://example.org/video.mp4", MimeType: "video/mp4"},
  770. {URL: "", MimeType: "application/pdf"},
  771. },
  772. }
  773. ApplyContentRewriteRules(testEntry, `add_enclosure_links`)
  774. if !reflect.DeepEqual(testEntry, controlEntry) {
  775. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  776. }
  777. }
  778. func TestRewriteAddEnclosureLinksWithoutEnclosureURL(t *testing.T) {
  779. controlEntry := &model.Entry{
  780. URL: "https://example.org/article",
  781. Title: `A title`,
  782. Content: `Article Content`,
  783. Enclosures: model.EnclosureList{
  784. {URL: "", MimeType: "audio/mpeg"},
  785. },
  786. }
  787. testEntry := &model.Entry{
  788. URL: "https://example.org/article",
  789. Title: `A title`,
  790. Content: `Article Content`,
  791. Enclosures: model.EnclosureList{
  792. {URL: "", MimeType: "audio/mpeg"},
  793. },
  794. }
  795. ApplyContentRewriteRules(testEntry, `add_enclosure_links`)
  796. if !reflect.DeepEqual(testEntry, controlEntry) {
  797. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  798. }
  799. }
  800. func TestRewriteBase64Decode(t *testing.T) {
  801. controlEntry := &model.Entry{
  802. URL: "https://example.org/article",
  803. Title: `A title`,
  804. Content: `This is some base64 encoded content`,
  805. }
  806. testEntry := &model.Entry{
  807. URL: "https://example.org/article",
  808. Title: `A title`,
  809. Content: `VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=`,
  810. }
  811. ApplyContentRewriteRules(testEntry, `base64_decode`)
  812. if !reflect.DeepEqual(testEntry, controlEntry) {
  813. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  814. }
  815. }
  816. func TestRewriteBase64DecodeInHTML(t *testing.T) {
  817. controlEntry := &model.Entry{
  818. URL: "https://example.org/article",
  819. Title: `A title`,
  820. Content: `<div>Lorem Ipsum not valid base64<span class="base64">This is some base64 encoded content</span></div>`,
  821. }
  822. testEntry := &model.Entry{
  823. URL: "https://example.org/article",
  824. Title: `A title`,
  825. Content: `<div>Lorem Ipsum not valid base64<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
  826. }
  827. ApplyContentRewriteRules(testEntry, `base64_decode`)
  828. if !reflect.DeepEqual(testEntry, controlEntry) {
  829. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  830. }
  831. }
  832. func TestRewriteBase64DecodeArgs(t *testing.T) {
  833. controlEntry := &model.Entry{
  834. URL: "https://example.org/article",
  835. Title: `A title`,
  836. Content: `<div>Lorem Ipsum<span class="base64">This is some base64 encoded content</span></div>`,
  837. }
  838. testEntry := &model.Entry{
  839. URL: "https://example.org/article",
  840. Title: `A title`,
  841. Content: `<div>Lorem Ipsum<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div>`,
  842. }
  843. ApplyContentRewriteRules(testEntry, `base64_decode(".base64")`)
  844. if !reflect.DeepEqual(testEntry, controlEntry) {
  845. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  846. }
  847. }
  848. func TestRewriteRemoveTables(t *testing.T) {
  849. controlEntry := &model.Entry{
  850. URL: "https://example.org/article",
  851. Title: `A title`,
  852. Content: `<p>Test</p><p>Hello World!</p><p>Test</p>`,
  853. }
  854. testEntry := &model.Entry{
  855. URL: "https://example.org/article",
  856. Title: `A title`,
  857. 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>`,
  858. }
  859. ApplyContentRewriteRules(testEntry, `remove_tables`)
  860. if !reflect.DeepEqual(testEntry, controlEntry) {
  861. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  862. }
  863. }
  864. func TestRewriteRemoveTablesKeepsSurroundingContentOrder(t *testing.T) {
  865. // Regression test for https://github.com/miniflux/v2/issues/3110
  866. // When a table is unwrapped, its content and the content surrounding it
  867. // must keep their original document order. Previously the unwrapped
  868. // content was appended to the end of the parent, so anything after the
  869. // table (and the table content itself) ended up reordered.
  870. controlEntry := &model.Entry{
  871. URL: "https://example.org/article",
  872. Title: `A title`,
  873. Content: `<h1>Header</h1><p>Intro</p><img src="https://example.org/image.png"/><p>Outro</p>`,
  874. }
  875. testEntry := &model.Entry{
  876. URL: "https://example.org/article",
  877. Title: `A title`,
  878. Content: `<h1>Header</h1><table><tbody><tr><td><p>Intro</p><img src="https://example.org/image.png"/></td></tr></tbody></table><p>Outro</p>`,
  879. }
  880. ApplyContentRewriteRules(testEntry, `remove_tables`)
  881. if !reflect.DeepEqual(testEntry, controlEntry) {
  882. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  883. }
  884. }
  885. func TestRemoveClickbait(t *testing.T) {
  886. controlEntry := &model.Entry{
  887. URL: "https://example.org/article",
  888. Title: `This Is Amazing`,
  889. Content: `Some description`,
  890. }
  891. testEntry := &model.Entry{
  892. URL: "https://example.org/article",
  893. Title: `THIS IS AMAZING`,
  894. Content: `Some description`,
  895. }
  896. ApplyContentRewriteRules(testEntry, `remove_clickbait`)
  897. if !reflect.DeepEqual(testEntry, controlEntry) {
  898. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  899. }
  900. }
  901. func TestAddHackerNewsLinksUsingHack(t *testing.T) {
  902. testEntry := &model.Entry{
  903. URL: "https://example.org/article",
  904. Title: `A title`,
  905. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  906. <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
  907. <p>Points: 23</p>
  908. <p># Comments: 38</p>`,
  909. }
  910. controlEntry := &model.Entry{
  911. URL: "https://example.org/article",
  912. Title: `A title`,
  913. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  914. <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>
  915. <p>Points: 23</p>
  916. <p># Comments: 38</p>`,
  917. }
  918. ApplyContentRewriteRules(testEntry, `add_hn_links_using_hack`)
  919. if !reflect.DeepEqual(testEntry, controlEntry) {
  920. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  921. }
  922. }
  923. func TestAddHackerNewsLinksUsingOpener(t *testing.T) {
  924. testEntry := &model.Entry{
  925. URL: "https://example.org/article",
  926. Title: `A title`,
  927. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  928. <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
  929. <p>Points: 23</p>
  930. <p># Comments: 38</p>`,
  931. }
  932. controlEntry := &model.Entry{
  933. URL: "https://example.org/article",
  934. Title: `A title`,
  935. Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
  936. <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>
  937. <p>Points: 23</p>
  938. <p># Comments: 38</p>`,
  939. }
  940. ApplyContentRewriteRules(testEntry, `add_hn_links_using_opener`)
  941. if !reflect.DeepEqual(testEntry, controlEntry) {
  942. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  943. }
  944. }
  945. func TestAddImageTitle(t *testing.T) {
  946. testEntry := &model.Entry{
  947. URL: "https://example.org/article",
  948. Title: `A title`,
  949. Content: `
  950. <img src="pif" title="pouf">
  951. <img src="pif" title="pouf" alt='"onerror=alert(1) a="'>
  952. <img src="pif" title="pouf" alt='&quot;onerror=alert(1) a=&quot'>
  953. <img src="pif" title="pouf" alt=';&amp;quot;onerror=alert(1) a=;&amp;quot;'>
  954. <img src="pif" alt="pouf" title='"onerror=alert(1) a="'>
  955. <img src="pif" alt="pouf" title='&quot;onerror=alert(1) a=&quot'>
  956. <img src="pif" alt="pouf" title=';&amp;quot;onerror=alert(1) a=;&amp;quot;'>
  957. `,
  958. }
  959. controlEntry := &model.Entry{
  960. URL: "https://example.org/article",
  961. Title: `A title`,
  962. Content: `<figure><img src="pif" alt=""/><figcaption><p>pouf</p></figcaption></figure>
  963. <figure><img src="pif" alt="" onerror="alert(1)" a=""/><figcaption><p>pouf</p></figcaption></figure>
  964. <figure><img src="pif" alt="" onerror="alert(1)" a=""/><figcaption><p>pouf</p></figcaption></figure>
  965. <figure><img src="pif" alt=";&#34;onerror=alert(1) a=;&#34;"/><figcaption><p>pouf</p></figcaption></figure>
  966. <figure><img src="pif" alt="pouf"/><figcaption><p>&#34;onerror=alert(1) a=&#34;</p></figcaption></figure>
  967. <figure><img src="pif" alt="pouf"/><figcaption><p>&#34;onerror=alert(1) a=&#34;</p></figcaption></figure>
  968. <figure><img src="pif" alt="pouf"/><figcaption><p>;&amp;quot;onerror=alert(1) a=;&amp;quot;</p></figcaption></figure>
  969. `,
  970. }
  971. ApplyContentRewriteRules(testEntry, `add_image_title`)
  972. if !reflect.DeepEqual(testEntry, controlEntry) {
  973. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  974. }
  975. }
  976. func TestFixGhostCard(t *testing.T) {
  977. testEntry := &model.Entry{
  978. URL: "https://example.org/article",
  979. Title: `A title`,
  980. Content: `<figure class="kg-card kg-bookmark-card">
  981. <a class="kg-bookmark-container" href="https://example.org/article">
  982. <div class="kg-bookmark-content">
  983. <div class="kg-bookmark-title">Example Article</div>
  984. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  985. <div class="kg-bookmark-metadata">
  986. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  987. <span class="kg-bookmark-author">Example</span>
  988. <span class="kg-bookmark-publisher">Test Author</span>
  989. </div>
  990. </div>
  991. <div class="kg-bookmark-thumbnail">
  992. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  993. </div>
  994. </a>
  995. </figure>`,
  996. }
  997. controlEntry := &model.Entry{
  998. URL: "https://example.org/article",
  999. Title: `A title`,
  1000. Content: `<a href="https://example.org/article">Example Article - Example</a>`,
  1001. }
  1002. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  1003. if !reflect.DeepEqual(testEntry, controlEntry) {
  1004. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1005. }
  1006. }
  1007. func TestFixGhostCardNoCard(t *testing.T) {
  1008. testEntry := &model.Entry{
  1009. URL: "https://example.org/article",
  1010. Title: `A title`,
  1011. Content: `<a href="https://example.org/article">Example Article - Example</a>`,
  1012. }
  1013. controlEntry := &model.Entry{
  1014. URL: "https://example.org/article",
  1015. Title: `A title`,
  1016. Content: `<a href="https://example.org/article">Example Article - Example</a>`,
  1017. }
  1018. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  1019. if !reflect.DeepEqual(testEntry, controlEntry) {
  1020. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1021. }
  1022. }
  1023. func TestFixGhostCardInvalidCard(t *testing.T) {
  1024. testEntry := &model.Entry{
  1025. URL: "https://example.org/article",
  1026. Title: `A title`,
  1027. Content: `<figure class="kg-card kg-bookmark-card">
  1028. <a href="https://example.org/article">This card does not have the required fields</a>
  1029. </figure>`,
  1030. }
  1031. controlEntry := &model.Entry{
  1032. URL: "https://example.org/article",
  1033. Title: `A title`,
  1034. Content: `<figure class="kg-card kg-bookmark-card">
  1035. <a href="https://example.org/article">This card does not have the required fields</a>
  1036. </figure>`,
  1037. }
  1038. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  1039. if !reflect.DeepEqual(testEntry, controlEntry) {
  1040. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1041. }
  1042. }
  1043. func TestFixGhostCardMissingAuthor(t *testing.T) {
  1044. testEntry := &model.Entry{
  1045. URL: "https://example.org/article",
  1046. Title: `A title`,
  1047. Content: `<figure class="kg-card kg-bookmark-card">
  1048. <a class="kg-bookmark-container" href="https://example.org/article">
  1049. <div class="kg-bookmark-content">
  1050. <div class="kg-bookmark-title">Example Article</div>
  1051. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  1052. </div>
  1053. <div class="kg-bookmark-thumbnail">
  1054. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  1055. </div>
  1056. </a>
  1057. </figure>`,
  1058. }
  1059. controlEntry := &model.Entry{
  1060. URL: "https://example.org/article",
  1061. Title: `A title`,
  1062. Content: `<a href="https://example.org/article">Example Article</a>`,
  1063. }
  1064. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  1065. if !reflect.DeepEqual(testEntry, controlEntry) {
  1066. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1067. }
  1068. }
  1069. func TestFixGhostCardDuplicatedAuthor(t *testing.T) {
  1070. testEntry := &model.Entry{
  1071. URL: "https://example.org/article",
  1072. Title: `A title`,
  1073. Content: `<figure class="kg-card kg-bookmark-card">
  1074. <a class="kg-bookmark-container" href="https://example.org/article">
  1075. <div class="kg-bookmark-content">
  1076. <div class="kg-bookmark-title">Example Article - Example</div>
  1077. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  1078. <div class="kg-bookmark-metadata">
  1079. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  1080. <span class="kg-bookmark-author">Example</span>
  1081. <span class="kg-bookmark-publisher">Test Author</span>
  1082. </div>
  1083. </div>
  1084. <div class="kg-bookmark-thumbnail">
  1085. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  1086. </div>
  1087. </a>
  1088. </figure>`,
  1089. }
  1090. controlEntry := &model.Entry{
  1091. URL: "https://example.org/article",
  1092. Title: `A title`,
  1093. Content: `<a href="https://example.org/article">Example Article - Example</a>`,
  1094. }
  1095. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  1096. if !reflect.DeepEqual(testEntry, controlEntry) {
  1097. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1098. }
  1099. }
  1100. func TestFixGhostCardMultiple(t *testing.T) {
  1101. testEntry := &model.Entry{
  1102. URL: "https://example.org/article",
  1103. Title: `A title`,
  1104. Content: `<figure class="kg-card kg-bookmark-card">
  1105. <a class="kg-bookmark-container" href="https://example.org/article1">
  1106. <div class="kg-bookmark-content">
  1107. <div class="kg-bookmark-title">Example Article 1 - Example</div>
  1108. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  1109. <div class="kg-bookmark-metadata">
  1110. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  1111. <span class="kg-bookmark-author">Example</span>
  1112. <span class="kg-bookmark-publisher">Test Author</span>
  1113. </div>
  1114. </div>
  1115. <div class="kg-bookmark-thumbnail">
  1116. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  1117. </div>
  1118. </a>
  1119. </figure>
  1120. <figure class="kg-card kg-bookmark-card">
  1121. <a class="kg-bookmark-container" href="https://example.org/article2">
  1122. <div class="kg-bookmark-content">
  1123. <div class="kg-bookmark-title">Example Article 2 - Example</div>
  1124. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  1125. <div class="kg-bookmark-metadata">
  1126. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  1127. <span class="kg-bookmark-author">Example</span>
  1128. <span class="kg-bookmark-publisher">Test Author</span>
  1129. </div>
  1130. </div>
  1131. <div class="kg-bookmark-thumbnail">
  1132. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  1133. </div>
  1134. </a>
  1135. </figure>`,
  1136. }
  1137. controlEntry := &model.Entry{
  1138. URL: "https://example.org/article",
  1139. Title: `A title`,
  1140. Content: `<ul><li><a href="https://example.org/article1">Example Article 1 - Example</a></li><li><a href="https://example.org/article2">Example Article 2 - Example</a></li></ul>`,
  1141. }
  1142. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  1143. if !reflect.DeepEqual(testEntry, controlEntry) {
  1144. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1145. }
  1146. }
  1147. func TestFixGhostCardMultipleSplit(t *testing.T) {
  1148. testEntry := &model.Entry{
  1149. URL: "https://example.org/article",
  1150. Title: `A title`,
  1151. Content: `<figure class="kg-card kg-bookmark-card">
  1152. <a class="kg-bookmark-container" href="https://example.org/article1">
  1153. <div class="kg-bookmark-content">
  1154. <div class="kg-bookmark-title">Example Article 1 - Example</div>
  1155. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  1156. <div class="kg-bookmark-metadata">
  1157. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  1158. <span class="kg-bookmark-author">Example</span>
  1159. <span class="kg-bookmark-publisher">Test Author</span>
  1160. </div>
  1161. </div>
  1162. <div class="kg-bookmark-thumbnail">
  1163. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  1164. </div>
  1165. </a>
  1166. </figure>
  1167. <p>This separates the two cards</p>
  1168. <figure class="kg-card kg-bookmark-card">
  1169. <a class="kg-bookmark-container" href="https://example.org/article2">
  1170. <div class="kg-bookmark-content">
  1171. <div class="kg-bookmark-title">Example Article 2 - Example</div>
  1172. <div class="kg-bookmark-description">Lorem ipsum odor amet, consectetuer adipiscing elit. Pretium magnis luctus ligula conubia quam, donec orci vehicula efficitur...</div>
  1173. <div class="kg-bookmark-metadata">
  1174. <img class="kg-bookmark-icon" src="https://example.org/favicon.ico" alt="">
  1175. <span class="kg-bookmark-author">Example</span>
  1176. <span class="kg-bookmark-publisher">Test Author</span>
  1177. </div>
  1178. </div>
  1179. <div class="kg-bookmark-thumbnail">
  1180. <img src="https://example.org/article-image.jpg" alt="" onerror="this.style.display = 'none'">
  1181. </div>
  1182. </a>
  1183. </figure>`,
  1184. }
  1185. controlEntry := &model.Entry{
  1186. URL: "https://example.org/article",
  1187. Title: `A title`,
  1188. Content: `<a href="https://example.org/article1">Example Article 1 - Example</a>
  1189. <p>This separates the two cards</p>
  1190. <a href="https://example.org/article2">Example Article 2 - Example</a>`,
  1191. }
  1192. ApplyContentRewriteRules(testEntry, `fix_ghost_cards`)
  1193. if !reflect.DeepEqual(testEntry, controlEntry) {
  1194. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1195. }
  1196. }
  1197. func TestStripImageQueryParams(t *testing.T) {
  1198. testEntry := &model.Entry{
  1199. URL: "https://example.org/article",
  1200. Title: `News Article Title`,
  1201. Content: `
  1202. <article>
  1203. <p>Article content with images having query parameters:</p>
  1204. <img src="https://example.org/images/image1.jpg?width=200&height=113&q=80&blur=90" alt="Image with params">
  1205. <img src="https://example.org/images/image2.jpg?width=800&height=600&q=85" alt="Another image with params">
  1206. <p>More images with various query parameters:</p>
  1207. <img src="https://example.org/image123.jpg?blur=50&size=small&format=webp" alt="Complex query params">
  1208. <img src="https://example.org/image123.jpg?size=large&quality=95&cache=123" alt="Different params">
  1209. <p>Image without query parameters:</p>
  1210. <img src="https://example.org/single-image.jpg" alt="Clean image">
  1211. <p>Images with various other params:</p>
  1212. <img src="https://example.org/normal1.jpg?width=300&format=jpg" alt="Normal 1">
  1213. <img src="https://example.org/normal1.jpg?width=600&quality=high" alt="Normal 2">
  1214. </article>`,
  1215. }
  1216. controlEntry := &model.Entry{
  1217. URL: "https://example.org/article",
  1218. Title: `News Article Title`,
  1219. Content: `<article>
  1220. <p>Article content with images having query parameters:</p>
  1221. <img src="https://example.org/images/image1.jpg" alt="Image with params"/>
  1222. <img src="https://example.org/images/image2.jpg?width=800&amp;height=600&amp;q=85" alt="Another image with params"/>
  1223. <p>More images with various query parameters:</p>
  1224. <img src="https://example.org/image123.jpg" alt="Complex query params"/>
  1225. <img src="https://example.org/image123.jpg?size=large&amp;quality=95&amp;cache=123" alt="Different params"/>
  1226. <p>Image without query parameters:</p>
  1227. <img src="https://example.org/single-image.jpg" alt="Clean image"/>
  1228. <p>Images with various other params:</p>
  1229. <img src="https://example.org/normal1.jpg?width=300&amp;format=jpg" alt="Normal 1"/>
  1230. <img src="https://example.org/normal1.jpg?width=600&amp;quality=high" alt="Normal 2"/>
  1231. </article>`,
  1232. }
  1233. ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
  1234. if !reflect.DeepEqual(testEntry, controlEntry) {
  1235. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1236. }
  1237. }
  1238. func TestStripImageQueryParamsNoChanges(t *testing.T) {
  1239. testEntry := &model.Entry{
  1240. URL: "https://example.org/article",
  1241. Title: `Article Without Images`,
  1242. Content: `<p>No images here:</p>
  1243. <div>Just some text content</div>
  1244. <a href="https://example.org">A link</a>`,
  1245. }
  1246. controlEntry := &model.Entry{
  1247. URL: "https://example.org/article",
  1248. Title: `Article Without Images`,
  1249. Content: `<p>No images here:</p>
  1250. <div>Just some text content</div>
  1251. <a href="https://example.org">A link</a>`,
  1252. }
  1253. ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
  1254. if !reflect.DeepEqual(testEntry, controlEntry) {
  1255. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1256. }
  1257. }
  1258. func TestStripImageQueryParamsEdgeCases(t *testing.T) {
  1259. testEntry := &model.Entry{
  1260. URL: "https://example.org/article",
  1261. Title: `Edge Cases`,
  1262. Content: `
  1263. <p>Edge cases for image query parameter stripping:</p>
  1264. <!-- Various query parameters -->
  1265. <img src="https://example.org/image1.jpg?blur=80&width=300" alt="Multiple params">
  1266. <!-- Complex query parameters -->
  1267. <img src="https://example.org/image2.jpg?BLUR=60&format=webp&cache=123" alt="Complex params">
  1268. <img src="https://example.org/image3.jpg?quality=high&version=2" alt="Other params">
  1269. <!-- Query params in middle of string -->
  1270. <img src="https://example.org/image4.jpg?size=large&blur=30&format=webp&quality=90" alt="Middle params">
  1271. <!-- Image without query params -->
  1272. <img src="https://example.org/clean.jpg" alt="Clean image">
  1273. `,
  1274. }
  1275. controlEntry := &model.Entry{
  1276. URL: "https://example.org/article",
  1277. Title: `Edge Cases`,
  1278. Content: `<p>Edge cases for image query parameter stripping:</p>
  1279. <!-- Various query parameters -->
  1280. <img src="https://example.org/image1.jpg" alt="Multiple params"/>
  1281. <!-- Complex query parameters -->
  1282. <img src="https://example.org/image2.jpg?BLUR=60&amp;format=webp&amp;cache=123" alt="Complex params"/>
  1283. <img src="https://example.org/image3.jpg?quality=high&amp;version=2" alt="Other params"/>
  1284. <!-- Query params in middle of string -->
  1285. <img src="https://example.org/image4.jpg" alt="Middle params"/>
  1286. <!-- Image without query params -->
  1287. <img src="https://example.org/clean.jpg" alt="Clean image"/>
  1288. `,
  1289. }
  1290. ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
  1291. if !reflect.DeepEqual(testEntry, controlEntry) {
  1292. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1293. }
  1294. }
  1295. func TestStripImageQueryParamsSimple(t *testing.T) {
  1296. testEntry := &model.Entry{
  1297. URL: "https://example.org/article",
  1298. Title: `Simple Test`,
  1299. Content: `
  1300. <p>Testing query parameter stripping:</p>
  1301. <!-- Images with various query parameters -->
  1302. <img src="https://example.org/test1.jpg?blur=0&width=300" alt="With blur zero">
  1303. <img src="https://example.org/test2.jpg?blur=50&width=300&format=webp" alt="With blur fifty">
  1304. <img src="https://example.org/test3.jpg?width=800&quality=high" alt="No blur param">
  1305. <img src="https://example.org/test4.jpg" alt="No params at all">
  1306. `,
  1307. }
  1308. controlEntry := &model.Entry{
  1309. URL: "https://example.org/article",
  1310. Title: `Simple Test`,
  1311. Content: `<p>Testing query parameter stripping:</p>
  1312. <!-- Images with various query parameters -->
  1313. <img src="https://example.org/test1.jpg?blur=0&amp;width=300" alt="With blur zero"/>
  1314. <img src="https://example.org/test2.jpg" alt="With blur fifty"/>
  1315. <img src="https://example.org/test3.jpg?width=800&amp;quality=high" alt="No blur param"/>
  1316. <img src="https://example.org/test4.jpg" alt="No params at all"/>
  1317. `,
  1318. }
  1319. ApplyContentRewriteRules(testEntry, `remove_img_blur_params`)
  1320. if !reflect.DeepEqual(testEntry, controlEntry) {
  1321. t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
  1322. }
  1323. }