rewriter_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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
  5. import "testing"
  6. func TestRewriteWithNoMatchingRule(t *testing.T) {
  7. output := Rewriter("https://example.org/article", `Some text.`, ``)
  8. expected := `Some text.`
  9. if expected != output {
  10. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  11. }
  12. }
  13. func TestRewriteWithYoutubeLink(t *testing.T) {
  14. output := Rewriter("https://www.youtube.com/watch?v=1234", `Video Description`, ``)
  15. expected := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><p>Video Description</p>`
  16. if expected != output {
  17. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  18. }
  19. }
  20. func TestRewriteWithInexistingCustomRule(t *testing.T) {
  21. output := Rewriter("https://www.youtube.com/watch?v=1234", `Video Description`, `some rule`)
  22. expected := `Video Description`
  23. if expected != output {
  24. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  25. }
  26. }
  27. func TestRewriteWithXkcdLink(t *testing.T) {
  28. 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." />`
  29. output := Rewriter("https://xkcd.com/1912/", description, ``)
  30. 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>`
  31. if expected != output {
  32. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  33. }
  34. }
  35. func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) {
  36. 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." />`
  37. output := Rewriter("https://xkcd.com/1912/", description, ``)
  38. expected := description
  39. if expected != output {
  40. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  41. }
  42. }
  43. func TestRewriteWithXkcdLinkAndNoImage(t *testing.T) {
  44. description := "test"
  45. output := Rewriter("https://xkcd.com/1912/", description, ``)
  46. expected := description
  47. if expected != output {
  48. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  49. }
  50. }
  51. func TestRewriteWithXkcdAndNoImage(t *testing.T) {
  52. description := "test"
  53. output := Rewriter("https://xkcd.com/1912/", description, ``)
  54. expected := description
  55. if expected != output {
  56. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  57. }
  58. }
  59. func TestRewriteWithPDFLink(t *testing.T) {
  60. description := "test"
  61. output := Rewriter("https://example.org/document.pdf", description, ``)
  62. expected := `<a href="https://example.org/document.pdf">PDF</a><br>test`
  63. if expected != output {
  64. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  65. }
  66. }
  67. func TestRewriteWithNoLazyImage(t *testing.T) {
  68. description := `<img src="https://example.org/image.jpg" alt="Image"><noscript><p>Some text</p></noscript>`
  69. output := Rewriter("https://example.org/article", description, "add_dynamic_image")
  70. expected := description
  71. if expected != output {
  72. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  73. }
  74. }
  75. func TestRewriteWithLazyImage(t *testing.T) {
  76. description := `<img src="" data-url="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`
  77. output := Rewriter("https://example.org/article", description, "add_dynamic_image")
  78. 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>`
  79. if expected != output {
  80. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  81. }
  82. }
  83. func TestRewriteWithLazyDivImage(t *testing.T) {
  84. description := `<div data-url="https://example.org/image.jpg" alt="Image"></div><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`
  85. output := Rewriter("https://example.org/article", description, "add_dynamic_image")
  86. expected := `<img src="https://example.org/image.jpg" alt="Image"/><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`
  87. if expected != output {
  88. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  89. }
  90. }
  91. func TestRewriteWithUnknownLazyNoScriptImage(t *testing.T) {
  92. description := `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"><noscript><img src="https://example.org/fallback.jpg" alt="Fallback"></noscript>`
  93. output := Rewriter("https://example.org/article", description, "add_dynamic_image")
  94. expected := `<img src="" data-non-candidate="https://example.org/image.jpg" alt="Image"/><img src="https://example.org/fallback.jpg" alt="Fallback"/>`
  95. if expected != output {
  96. t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
  97. }
  98. }