You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rewriter_test.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package rewrite // import "miniflux.app/reader/rewrite"
  5. import "testing"
  6. func TestReplaceTextLinks(t *testing.T) {
  7. scenarios := map[string]string{
  8. `This is a link to example.org`: `This is a link to example.org`,
  9. `This is a link to ftp://example.org`: `This is a link to ftp://example.org`,
  10. `This is a link to www.example.org`: `This is a link to www.example.org`,
  11. `This is a link to http://example.org`: `This is a link to <a href="http://example.org">http://example.org</a>`,
  12. `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.`,
  13. `This is a link to https://example.org`: `This is a link to <a href="https://example.org">https://example.org</a>`,
  14. `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>`,
  15. `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>.`,
  16. }
  17. for input, expected := range scenarios {
  18. actual := replaceTextLinks(input)
  19. if actual != expected {
  20. t.Errorf(`Unexpected link replacement, got "%s" instead of "%s"`, actual, expected)
  21. }
  22. }
  23. }
  24. func TestRewriteWithNoMatchingRule(t *testing.T) {
  25. output := Rewriter("https://example.org/article", `Some text.`, ``)
  26. expected := `Some text.`
  27. if expected != output {
  28. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  29. }
  30. }
  31. func TestRewriteWithYoutubeLink(t *testing.T) {
  32. output := Rewriter("https://www.youtube.com/watch?v=1234", "Video Description\nhttp://example.org/path", ``)
  33. expected := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><p>Video Description<br><a href="http://example.org/path">http://example.org/path</a></p>`
  34. if expected != output {
  35. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  36. }
  37. }
  38. func TestRewriteWithInexistingCustomRule(t *testing.T) {
  39. output := Rewriter("https://www.youtube.com/watch?v=1234", `Video Description`, `some rule`)
  40. expected := `Video Description`
  41. if expected != output {
  42. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  43. }
  44. }
  45. func TestRewriteWithXkcdLink(t *testing.T) {
  46. description := `<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." />`
  47. output := Rewriter("https://xkcd.com/1912/", description, ``)
  48. expected := `<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>`
  49. if expected != output {
  50. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  51. }
  52. }
  53. func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) {
  54. description := `<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." />`
  55. output := Rewriter("https://xkcd.com/1912/", description, ``)
  56. expected := description
  57. if expected != output {
  58. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  59. }
  60. }
  61. func TestRewriteWithXkcdLinkAndNoImage(t *testing.T) {
  62. description := "test"
  63. output := Rewriter("https://xkcd.com/1912/", description, ``)
  64. expected := description
  65. if expected != output {
  66. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  67. }
  68. }
  69. func TestRewriteWithXkcdAndNoImage(t *testing.T) {
  70. description := "test"
  71. output := Rewriter("https://xkcd.com/1912/", description, ``)
  72. expected := description
  73. if expected != output {
  74. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  75. }
  76. }
  77. func TestRewriteWithPDFLink(t *testing.T) {
  78. description := "test"
  79. output := Rewriter("https://example.org/document.pdf", description, ``)
  80. expected := `<a href="https://example.org/document.pdf">PDF</a><br>test`
  81. if expected != output {
  82. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  83. }
  84. }
  85. func TestRewriteWithNoLazyImage(t *testing.T) {
  86. description := `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`
  87. output := Rewriter("https://example.org/article", description, "add_dynamic_image")
  88. expected := description
  89. if expected != output {
  90. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  91. }
  92. }
  93. func TestRewriteWithLazyImage(t *testing.T) {
  94. description := `<img src="" data-url="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`
  95. output := Rewriter("https://example.org/article", description, "add_dynamic_image")
  96. expected := `<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>`
  97. if expected != output {
  98. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  99. }
  100. }
  101. func TestRewriteWithLazyDivImage(t *testing.T) {
  102. description := `<div data-url="https://example.org/image.jpg" alt="Image"></div><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`
  103. output := Rewriter("https://example.org/article", description, "add_dynamic_image")
  104. expected := `<img src="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`
  105. if expected != output {
  106. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  107. }
  108. }
  109. func TestRewriteWithUnknownLazyNoScriptImage(t *testing.T) {
  110. description := `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`
  111. output := Rewriter("https://example.org/article", description, "add_dynamic_image")
  112. expected := `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"/><img src="https://example.org/fallback.jpg" alt="Fallback"/>`
  113. if expected != output {
  114. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  115. }
  116. }