// Copyright 2017 Frédéric Guillot. All rights reserved. // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. package rewrite // import "miniflux.app/reader/rewrite" import ( "reflect" "strings" "testing" ) 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) } } 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 http://example.org`, `This is a link to http://example.org, end of sentence.`: `This is a link to http://example.org, end of sentence.`, `This is a link to https://example.org`: `This is a link to https://example.org`, `This is a link to https://www.example.org/path/to?q=s`: `This is a link to https://www.example.org/path/to?q=s`, `This is a link to https://example.org/index#hash-tag, http://example.org/.`: `This is a link to https://example.org/index#hash-tag, http://example.org/.`, } for input, expected := range scenarios { actual := replaceTextLinks(input) if actual != expected { t.Errorf(`Unexpected link replacement, got "%s" instead of "%s"`, actual, expected) } } } func TestRewriteWithNoMatchingRule(t *testing.T) { output := Rewriter("https://example.org/article", `Some text.`, ``) expected := `Some text.` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithYoutubeLink(t *testing.T) { output := Rewriter("https://www.youtube.com/watch?v=1234", "Video Description", ``) expected := `
Video Description` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithInexistingCustomRule(t *testing.T) { output := Rewriter("https://www.youtube.com/watch?v=1234", `Video Description`, `some rule`) expected := `Video Description` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithXkcdLink(t *testing.T) { description := `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.` output := Rewriter("https://xkcd.com/1912/", description, ``) expected := `
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.

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.

` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithXkcdLinkHtmlInjection(t *testing.T) { description := `<foo>` output := Rewriter("https://xkcd.com/1912/", description, ``) expected := `
<foo>

<foo>

` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) { description := `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.` output := Rewriter("https://xkcd.com/1912/", description, ``) expected := description if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithXkcdLinkAndNoImage(t *testing.T) { description := "test" output := Rewriter("https://xkcd.com/1912/", description, ``) expected := description if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithXkcdAndNoImage(t *testing.T) { description := "test" output := Rewriter("https://xkcd.com/1912/", description, ``) expected := description if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteMailtoLink(t *testing.T) { description := `contact` output := Rewriter("https://www.qwantz.com/", description, ``) expected := `contact [blah blah]` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithPDFLink(t *testing.T) { description := "test" output := Rewriter("https://example.org/document.pdf", description, ``) expected := `PDF
test` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithNoLazyImage(t *testing.T) { description := `Image` output := Rewriter("https://example.org/article", description, "add_dynamic_image") expected := description if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithLazyImage(t *testing.T) { description := `Image` output := Rewriter("https://example.org/article", description, "add_dynamic_image") expected := `Image` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithLazyDivImage(t *testing.T) { description := `
` output := Rewriter("https://example.org/article", description, "add_dynamic_image") expected := `Image` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestRewriteWithUnknownLazyNoScriptImage(t *testing.T) { description := `Image` output := Rewriter("https://example.org/article", description, "add_dynamic_image") expected := `ImageFallback` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestNewLineRewriteRule(t *testing.T) { description := "A\nB\nC" output := Rewriter("https://example.org/article", description, "nl2br") expected := `A
B
C` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, output, expected) } } func TestConvertTextLinkRewriteRule(t *testing.T) { description := "Test: http://example.org/a/b" output := Rewriter("https://example.org/article", description, "convert_text_link") expected := `Test: http://example.org/a/b` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, output, expected) } } func TestMediumImage(t *testing.T) { content := `
Image for post
Image for post
` expected := `Image for post` output := Rewriter("https://example.org/article", content, "fix_medium_images") output = strings.TrimSpace(output) if expected != output { t.Errorf(`Not expected output: %s`, output) } } func TestRewriteNoScriptImageWithoutNoScriptTag(t *testing.T) { content := `
The beautiful MDN logo.
MDN Logo
` expected := `
The beautiful MDN logo.
MDN Logo
` output := Rewriter("https://example.org/article", content, "use_noscript_figure_images") output = strings.TrimSpace(output) if expected != output { t.Errorf(`Not expected output: %s`, output) } } func TestRewriteNoScriptImageWithNoScriptTag(t *testing.T) { content := `
The beautiful MDN logo.
MDN Logo
` expected := `
MDN Logo
` output := Rewriter("https://example.org/article", content, "use_noscript_figure_images") output = strings.TrimSpace(output) if expected != output { t.Errorf(`Not expected output: %s`, output) } } func TestRewriteReplaceCustom(t *testing.T) { content := `` expected := `` output := Rewriter("https://example.org/article", content, `replace("article/(.*).svg"|"article/$1.png")`) if expected != output { t.Errorf(`Not expected output: %s`, output) } } func TestRewriteRemoveCustom(t *testing.T) { content := `
Lorem Ipsum I dont want to see thisSuper important info
` expected := `
Lorem Ipsum Super important info
` output := Rewriter("https://example.org/article", content, `remove(".spam, .ads:not(.keep)")`) if expected != output { t.Errorf(`Not expected output: %s`, output) } }