content_rewrite_test.go 56 KB

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