2023-06-19 17:42:47 -04:00
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
2017-11-20 00:10:04 -05:00
2023-08-10 22:46:45 -04:00
package rewrite // import "miniflux.app/v2/internal/reader/rewrite"
2017-11-20 00:10:04 -05:00
2020-09-30 01:22:25 -04:00
import (
2023-07-05 11:11:56 -04:00
"os"
2021-09-01 17:42:23 -04:00
"reflect"
2020-09-30 01:22:25 -04:00
"strings"
"testing"
2023-04-08 05:02:36 -04:00
2023-08-10 22:46:45 -04:00
"miniflux.app/v2/internal/config"
"miniflux.app/v2/internal/model"
2020-09-30 01:22:25 -04:00
)
2017-11-20 00:10:04 -05:00
2021-09-01 17:42:23 -04:00
func TestParseRules ( t * testing . T ) {
rulesText := ` add_dynamic_image,replace("article/(.*).svg"|"article/$1.png"),remove(".spam, .ads:not(.keep)") `
expected := [ ] rule {
{ name : "add_dynamic_image" } ,
{ name : "replace" , args : [ ] string { "article/(.*).svg" , "article/$1.png" } } ,
{ name : "remove" , args : [ ] string { ".spam, .ads:not(.keep)" } } ,
}
actual := parseRules ( rulesText )
if ! reflect . DeepEqual ( expected , actual ) {
t . Errorf ( ` Parsed rules do not match expected rules: got %v instead of %v ` , actual , expected )
}
}
2018-10-08 23:47:10 -04:00
func TestReplaceTextLinks ( t * testing . T ) {
scenarios := map [ string ] string {
` This is a link to example.org ` : ` This is a link to example.org ` ,
` This is a link to ftp://example.org ` : ` This is a link to ftp://example.org ` ,
` This is a link to www.example.org ` : ` This is a link to www.example.org ` ,
` This is a link to http://example.org ` : ` This is a link to <a href="http://example.org">http://example.org</a> ` ,
` 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. ` ,
` This is a link to https://example.org ` : ` This is a link to <a href="https://example.org">https://example.org</a> ` ,
` 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> ` ,
` 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>. ` ,
}
for input , expected := range scenarios {
actual := replaceTextLinks ( input )
if actual != expected {
t . Errorf ( ` Unexpected link replacement, got "%s" instead of "%s" ` , actual , expected )
}
}
}
2017-11-20 00:10:04 -05:00
func TestRewriteWithNoMatchingRule ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` Some text. ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` Some text. ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` ` )
2017-11-20 00:10:04 -05:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2017-11-20 00:10:04 -05:00
}
}
func TestRewriteWithYoutubeLink ( t * testing . T ) {
2023-07-05 11:11:56 -04:00
config . Opts = config . NewOptions ( )
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><br>Video Description ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` Video Description ` ,
}
Rewriter ( "https://www.youtube.com/watch?v=1234" , testEntry , ` ` )
2017-11-20 00:10:04 -05:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2017-11-20 00:10:04 -05:00
}
}
2023-07-05 11:11:56 -04:00
func TestRewriteWithYoutubeLinkAndCustomEmbedURL ( t * testing . T ) {
os . Clearenv ( )
os . Setenv ( "YOUTUBE_EMBED_URL_OVERRIDE" , "https://invidious.custom/embed/" )
var err error
parser := config . NewParser ( )
config . Opts , err = parser . ParseEnvironmentVariables ( )
if err != nil {
t . Fatalf ( ` Parsing failure: %v ` , err )
}
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <iframe width="650" height="350" frameborder="0" src="https://invidious.custom/embed/1234" allowfullscreen></iframe><br>Video Description ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` Video Description ` ,
}
Rewriter ( "https://www.youtube.com/watch?v=1234" , testEntry , ` ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
}
}
2017-12-12 01:16:32 -05:00
func TestRewriteWithInexistingCustomRule ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` Video Description ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` Video Description ` ,
}
Rewriter ( "https://www.youtube.com/watch?v=1234" , testEntry , ` some rule ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2017-12-12 01:16:32 -05:00
}
}
2017-11-20 00:10:04 -05:00
func TestRewriteWithXkcdLink ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
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> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
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." /> ` ,
}
Rewriter ( "https://xkcd.com/1912/" , testEntry , ` ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2018-06-26 17:39:56 -04:00
}
}
2018-07-09 01:22:48 -04:00
2019-08-14 03:33:54 -04:00
func TestRewriteWithXkcdLinkHtmlInjection ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <figure><img src="https://imgs.xkcd.com/comics/thermostat.png" alt="<foo>"/><figcaption><p><foo></p></figcaption></figure> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="https://imgs.xkcd.com/comics/thermostat.png" title="<foo>" alt="<foo>" /> ` ,
}
Rewriter ( "https://xkcd.com/1912/" , testEntry , ` ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2019-08-14 03:33:54 -04:00
}
}
2018-06-26 17:39:56 -04:00
func TestRewriteWithXkcdLinkAndImageNoTitle ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
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." /> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
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." /> ` ,
}
Rewriter ( "https://xkcd.com/1912/" , testEntry , ` ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2017-11-20 00:10:04 -05:00
}
}
2018-07-09 01:22:48 -04:00
2017-12-12 01:16:32 -05:00
func TestRewriteWithXkcdLinkAndNoImage ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` test ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` test ` ,
}
Rewriter ( "https://xkcd.com/1912/" , testEntry , ` ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2017-12-12 01:16:32 -05:00
}
}
func TestRewriteWithXkcdAndNoImage ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` test ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` test ` ,
}
Rewriter ( "https://xkcd.com/1912/" , testEntry , ` ` )
2017-12-12 01:16:32 -05:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2017-12-12 01:16:32 -05:00
}
}
2017-12-14 00:30:40 -05:00
2019-08-13 11:44:23 -04:00
func TestRewriteMailtoLink ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact [blah blah]</a> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <a href="mailto:ryan@qwantz.com?subject=blah%20blah">contact</a> ` ,
}
Rewriter ( "https://www.qwantz.com/" , testEntry , ` ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2019-08-13 11:44:23 -04:00
}
}
2017-12-14 00:30:40 -05:00
func TestRewriteWithPDFLink ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <a href="https://example.org/document.pdf">PDF</a><br>test ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` test ` ,
}
Rewriter ( "https://example.org/document.pdf" , testEntry , ` ` )
2017-12-14 00:30:40 -05:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2017-12-14 00:30:40 -05:00
}
}
2018-07-09 01:22:48 -04:00
func TestRewriteWithNoLazyImage ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "add_dynamic_image" )
2018-07-09 01:22:48 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2018-07-09 01:22:48 -04:00
}
}
func TestRewriteWithLazyImage ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
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> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="" data-url="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "add_dynamic_image" )
2018-07-09 01:22:48 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2018-07-09 01:22:48 -04:00
}
}
func TestRewriteWithLazyDivImage ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <div data-url="https://example.org/image.jpg" alt="Image"></div><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "add_dynamic_image" )
2018-07-09 01:22:48 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2018-07-09 01:22:48 -04:00
}
}
func TestRewriteWithUnknownLazyNoScriptImage ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"/><img src="https://example.org/fallback.jpg" alt="Fallback"/> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "add_dynamic_image" )
2018-07-09 01:22:48 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2018-07-09 01:22:48 -04:00
}
}
2019-11-28 23:11:39 -05:00
2021-10-22 21:12:23 -04:00
func TestRewriteWithLazySrcset ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img srcset="" data-srcset="https://example.org/image.jpg" alt="Image"> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "add_dynamic_image" )
2021-10-22 21:12:23 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2021-10-22 21:12:23 -04:00
}
}
func TestRewriteWithImageAndLazySrcset ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="meow" srcset="https://example.org/image.jpg" data-srcset="https://example.org/image.jpg" alt="Image"/> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="meow" srcset="" data-srcset="https://example.org/image.jpg" alt="Image"> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "add_dynamic_image" )
2021-10-22 21:12:23 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2021-10-22 21:12:23 -04:00
}
}
2024-01-21 21:28:21 -05:00
func TestRewriteWithNoLazyIframe ( t * testing . T ) {
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <iframe src="https://example.org/embed" allowfullscreen></iframe> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <iframe src="https://example.org/embed" allowfullscreen></iframe> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "add_dynamic_iframe" )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
}
}
func TestRewriteWithLazyIframe ( t * testing . T ) {
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <iframe data-src="https://example.org/embed" allowfullscreen="" src="https://example.org/embed"></iframe> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <iframe data-src="https://example.org/embed" allowfullscreen></iframe> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "add_dynamic_iframe" )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
}
}
func TestRewriteWithLazyIframeAndSrc ( t * testing . T ) {
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <iframe src="https://example.org/embed" data-src="https://example.org/embed" allowfullscreen=""></iframe> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <iframe src="about:blank" data-src="https://example.org/embed" allowfullscreen></iframe> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "add_dynamic_iframe" )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
}
}
2019-11-28 23:11:39 -05:00
func TestNewLineRewriteRule ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` A<br>B<br>C ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : "A\nB\nC" ,
}
Rewriter ( "https://example.org/article" , testEntry , "nl2br" )
2019-11-28 23:11:39 -05:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2019-11-28 23:11:39 -05:00
}
}
func TestConvertTextLinkRewriteRule ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` Test: <a href="http://example.org/a/b">http://example.org/a/b</a> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` Test: http://example.org/a/b ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "convert_text_link" )
2019-11-28 23:11:39 -05:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2019-11-28 23:11:39 -05:00
}
}
2020-09-30 01:22:25 -04:00
func TestMediumImage ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
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"/> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : `
2020-09-30 01:22:25 -04:00
< figure class = "ht hu hv hw hx hy cy cz paragraph-image" >
< div class = "hz ia ib ic aj" >
< div class = "cy cz hs" >
< div class = "ii s ib ij" >
< div class = "ik il s" >
< div class = "id ie t u v if aj bk ig ih" >
< 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" / >
< / div >
< img alt = "Image for post" class = "id ie t u v if aj c" width = "1280" height = "720" / >
< noscript >
< 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"/>
< / noscript >
< / div >
< / div >
< / div >
< / div >
< / figure >
2023-04-08 05:02:36 -04:00
` ,
}
Rewriter ( "https://example.org/article" , testEntry , "fix_medium_images" )
testEntry . Content = strings . TrimSpace ( testEntry . Content )
2020-09-30 01:22:25 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2020-09-30 01:22:25 -04:00
}
}
2020-10-20 00:04:14 -04:00
func TestRewriteNoScriptImageWithoutNoScriptTag ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."/><figcaption>MDN Logo</figcaption></figure> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><figcaption>MDN Logo</figcaption></figure> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "use_noscript_figure_images" )
testEntry . Content = strings . TrimSpace ( testEntry . Content )
2020-10-20 00:04:14 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2020-10-20 00:04:14 -04:00
}
}
func TestRewriteNoScriptImageWithNoScriptTag ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <figure><img src="http://example.org/logo.svg"/><figcaption>MDN Logo</figcaption></figure> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
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> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , "use_noscript_figure_images" )
testEntry . Content = strings . TrimSpace ( testEntry . Content )
2020-10-20 00:04:14 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2020-10-20 00:04:14 -04:00
}
}
2020-11-25 17:51:54 -05:00
func TestRewriteReplaceCustom ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.png"> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <img src="http://example.org/logo.svg"><img src="https://example.org/article/picture.svg"> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` replace("article/(.*).svg"|"article/$1.png") ` )
2021-09-01 17:42:23 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2021-09-01 17:42:23 -04:00
}
}
2023-09-06 23:09:54 -04:00
func TestRewriteReplaceTitleCustom ( t * testing . T ) {
controlEntry := & model . Entry {
Title : ` Ouch, a thistle ` ,
Content : ` The replace_title rewrite rule should not modify the content. ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` The replace_title rewrite rule should not modify the content. ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` replace_title("(?i)^a\\s*ti"|"Ouch, a this") ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
}
}
2021-09-01 17:42:23 -04:00
func TestRewriteRemoveCustom ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <div>Lorem Ipsum <span class="ads keep">Super important info</span></div> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <div>Lorem Ipsum <span class="spam">I dont want to see this</span><span class="ads keep">Super important info</span></div> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` remove(".spam, .ads:not(.keep)") ` )
2020-11-25 17:51:54 -05:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2020-11-25 17:51:54 -05:00
}
}
2022-01-30 03:11:43 -05:00
func TestRewriteAddCastopodEpisode ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <iframe width="650" frameborder="0" src="https://podcast.demo/@demo/episodes/test/embed/light"></iframe><br>Episode Description ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` Episode Description ` ,
}
Rewriter ( "https://podcast.demo/@demo/episodes/test" , testEntry , ` add_castopod_episode ` )
2022-01-30 03:11:43 -05:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2022-01-30 03:11:43 -05:00
}
}
2022-05-25 23:44:04 -04:00
func TestRewriteBase64Decode ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` This is some base64 encoded content ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ= ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` base64_decode ` )
2022-05-25 23:44:04 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2022-05-25 23:44:04 -04:00
}
}
func TestRewriteBase64DecodeInHTML ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <div>Lorem Ipsum not valid base64<span class="base64">This is some base64 encoded content</span></div> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <div>Lorem Ipsum not valid base64<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` base64_decode ` )
2022-05-25 23:44:04 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2022-05-25 23:44:04 -04:00
}
}
func TestRewriteBase64DecodeArgs ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <div>Lorem Ipsum<span class="base64">This is some base64 encoded content</span></div> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <div>Lorem Ipsum<span class="base64">VGhpcyBpcyBzb21lIGJhc2U2NCBlbmNvZGVkIGNvbnRlbnQ=</span></div> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` base64_decode(".base64") ` )
2022-05-25 23:44:04 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2022-05-25 23:44:04 -04:00
}
}
2023-03-31 14:23:31 -04:00
func TestRewriteRemoveTables ( t * testing . T ) {
2023-04-08 05:02:36 -04:00
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` <p>Test</p><p>Hello World!</p><p>Test</p> ` ,
}
testEntry := & model . Entry {
Title : ` A title ` ,
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> ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` remove_tables ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
}
}
func TestRemoveClickbait ( t * testing . T ) {
controlEntry := & model . Entry {
Title : ` This Is Amazing ` ,
Content : ` Some description ` ,
}
testEntry := & model . Entry {
Title : ` THIS IS AMAZING ` ,
Content : ` Some description ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` remove_clickbait ` )
2023-03-31 14:23:31 -04:00
2023-04-08 05:02:36 -04:00
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
2023-03-31 14:23:31 -04:00
}
}
2023-09-23 16:54:48 -04:00
func TestAddHackerNewsLinksUsingHack ( t * testing . T ) {
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` < p > Article URL : < a href = "https://example.org/url" > https : //example.org/article</a></p>
< p > Comments URL : < a href = "https://news.ycombinator.com/item?id=37620043" > https : //news.ycombinator.com/item?id=37620043</a></p>
< p > Points : 23 < / p >
< p > # Comments : 38 < / p > ` ,
}
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` < p > Article URL : < a href = "https://example.org/url" > https : //example.org/article</a></p>
< 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>
< p > Points : 23 < / p >
< p > # Comments : 38 < / p > ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` add_hn_links_using_hack ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
}
}
func TestAddHackerNewsLinksUsingOpener ( t * testing . T ) {
testEntry := & model . Entry {
Title : ` A title ` ,
Content : ` < p > Article URL : < a href = "https://example.org/url" > https : //example.org/article</a></p>
< p > Comments URL : < a href = "https://news.ycombinator.com/item?id=37620043" > https : //news.ycombinator.com/item?id=37620043</a></p>
< p > Points : 23 < / p >
< p > # Comments : 38 < / p > ` ,
}
controlEntry := & model . Entry {
Title : ` A title ` ,
Content : ` < p > Article URL : < a href = "https://example.org/url" > https : //example.org/article</a></p>
< 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>
< p > Points : 23 < / p >
< p > # Comments : 38 < / p > ` ,
}
Rewriter ( "https://example.org/article" , testEntry , ` add_hn_links_using_opener ` )
if ! reflect . DeepEqual ( testEntry , controlEntry ) {
t . Errorf ( ` Not expected output: got "%+v" instead of "%+v" ` , testEntry , controlEntry )
}
}