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.

259 lines
7.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. defmodule Linkify.ParserTest do
  2. use ExUnit.Case, async: true
  3. doctest Linkify.Parser
  4. import Linkify.Parser
  5. describe "url?/2" do
  6. test "valid scheme true" do
  7. valid_scheme_urls()
  8. |> Enum.each(fn url ->
  9. assert url?(url, scheme: true, validate_tld: true)
  10. end)
  11. end
  12. test "invalid scheme true" do
  13. invalid_scheme_urls()
  14. |> Enum.each(fn url ->
  15. refute url?(url, scheme: true, validate_tld: true)
  16. end)
  17. end
  18. test "valid scheme false" do
  19. valid_non_scheme_urls()
  20. |> Enum.each(fn url ->
  21. assert url?(url, scheme: false, validate_tld: true)
  22. end)
  23. end
  24. test "invalid scheme false" do
  25. invalid_non_scheme_urls()
  26. |> Enum.each(fn url ->
  27. refute url?(url, scheme: false, validate_tld: true)
  28. end)
  29. end
  30. test "checks the tld for url with a scheme when validate_tld: true" do
  31. custom_tld_scheme_urls()
  32. |> Enum.each(fn url ->
  33. refute url?(url, scheme: true, validate_tld: true)
  34. end)
  35. end
  36. test "does not check the tld for url with a scheme when validate_tld: false" do
  37. custom_tld_scheme_urls()
  38. |> Enum.each(fn url ->
  39. assert url?(url, scheme: true, validate_tld: false)
  40. end)
  41. end
  42. test "does not check the tld for url with a scheme when validate_tld: :no_scheme" do
  43. custom_tld_scheme_urls()
  44. |> Enum.each(fn url ->
  45. assert url?(url, scheme: true, validate_tld: :no_scheme)
  46. end)
  47. end
  48. test "checks the tld for url without a scheme when validate_tld: true" do
  49. custom_tld_non_scheme_urls()
  50. |> Enum.each(fn url ->
  51. refute url?(url, scheme: false, validate_tld: true)
  52. end)
  53. end
  54. test "checks the tld for url without a scheme when validate_tld: :no_scheme" do
  55. custom_tld_non_scheme_urls()
  56. |> Enum.each(fn url ->
  57. refute url?(url, scheme: false, validate_tld: :no_scheme)
  58. end)
  59. end
  60. test "does not check the tld for url without a scheme when validate_tld: false" do
  61. custom_tld_non_scheme_urls()
  62. |> Enum.each(fn url ->
  63. assert url?(url, scheme: false, validate_tld: false)
  64. end)
  65. end
  66. end
  67. describe "email?" do
  68. test "identifies valid emails" do
  69. valid_emails()
  70. |> Enum.each(fn email ->
  71. assert email?(email, [])
  72. end)
  73. end
  74. test "identifies invalid emails" do
  75. invalid_emails()
  76. |> Enum.each(fn email ->
  77. refute email?(email, [])
  78. end)
  79. end
  80. test "does not validate tlds when validate_tld: false" do
  81. valid_custom_tld_emails()
  82. |> Enum.each(fn email ->
  83. assert email?(email, validate_tld: false)
  84. end)
  85. end
  86. test "validates tlds when validate_tld: true" do
  87. valid_custom_tld_emails()
  88. |> Enum.each(fn email ->
  89. refute email?(email, validate_tld: true)
  90. end)
  91. end
  92. end
  93. describe "parse" do
  94. test "handle line breakes" do
  95. text = "google.com\r\nssss"
  96. expected = "<a href=\"http://google.com\">google.com</a>\r\nssss"
  97. assert parse(text) == expected
  98. end
  99. test "does not link attributes" do
  100. text = "Check out <a href='google.com'>google</a>"
  101. assert parse(text) == text
  102. text = "Check out <img src='google.com' alt='google.com'/>"
  103. assert parse(text) == text
  104. text = "Check out <span><img src='google.com' alt='google.com'/></span>"
  105. assert parse(text) == text
  106. end
  107. test "does not link inside `<pre>` and `<code>`" do
  108. text = "<pre>google.com</pre>"
  109. assert parse(text) == text
  110. text = "<code>google.com</code>"
  111. assert parse(text) == text
  112. text = "<pre><code>google.com</code></pre>"
  113. assert parse(text) == text
  114. end
  115. test "links url inside html" do
  116. text = "<div>google.com</div>"
  117. expected = "<div><a href=\"http://google.com\">google.com</a></div>"
  118. assert parse(text, class: false, rel: false) == expected
  119. text = "Check out <div class='section'>google.com</div>"
  120. expected =
  121. "Check out <div class='section'><a href=\"http://google.com\">google.com</a></div>"
  122. assert parse(text, class: false, rel: false) == expected
  123. end
  124. test "links url inside nested html" do
  125. text = "<p><strong>google.com</strong></p>"
  126. expected = "<p><strong><a href=\"http://google.com\">google.com</a></strong></p>"
  127. assert parse(text, class: false, rel: false) == expected
  128. end
  129. test "do not link parens" do
  130. text = " foo (https://example.com/path/folder/), bar"
  131. expected =
  132. " foo (<a href=\"https://example.com/path/folder/\">https://example.com/path/folder/</a>), bar"
  133. assert parse(text, class: false, rel: false, scheme: true) == expected
  134. text = " foo (example.com/path/folder/), bar"
  135. expected =
  136. " foo (<a href=\"http://example.com/path/folder/\">example.com/path/folder/</a>), bar"
  137. assert parse(text, class: false, rel: false) == expected
  138. end
  139. test "do not link urls" do
  140. text = "google.com"
  141. assert parse(text, url: false) == text
  142. end
  143. test "do not link `:test.test`" do
  144. text = ":test.test"
  145. assert parse(text, %{
  146. scheme: true,
  147. extra: true,
  148. class: false,
  149. strip_prefix: false,
  150. new_window: false,
  151. rel: false
  152. }) == text
  153. end
  154. end
  155. def valid_number?([list], number) do
  156. assert List.last(list) == number
  157. end
  158. def valid_number?(_, _), do: false
  159. def valid_scheme_urls,
  160. do: [
  161. "https://www.example.com",
  162. "http://www2.example.com",
  163. "http://home.example-site.com",
  164. "http://blog.example.com",
  165. "http://www.example.com/product",
  166. "http://www.example.com/products?id=1&page=2",
  167. "http://www.example.com#up",
  168. "http://255.255.255.255",
  169. "http://www.site.com:8008"
  170. ]
  171. def invalid_scheme_urls,
  172. do: [
  173. "http://invalid.com/perl.cgi?key= | http://web-site.com/cgi-bin/perl.cgi?key1=value1&key2"
  174. ]
  175. def valid_non_scheme_urls,
  176. do: [
  177. "www.example.com",
  178. "www2.example.com",
  179. "www.example.com:2000",
  180. "www.example.com?abc=1",
  181. "example.example-site.com",
  182. "example.com",
  183. "example.ca",
  184. "example.tv",
  185. "example.com:999?one=one",
  186. "255.255.255.255",
  187. "255.255.255.255:3000?one=1&two=2"
  188. ]
  189. def invalid_non_scheme_urls,
  190. do: [
  191. "invalid.com/perl.cgi?key= | web-site.com/cgi-bin/perl.cgi?key1=value1&key2",
  192. "invalid.",
  193. "hi..there",
  194. "555.555.5555"
  195. ]
  196. def custom_tld_scheme_urls,
  197. do: [
  198. "http://whatever.null/",
  199. "https://example.o/index.html",
  200. "http://pleroma.i2p/test",
  201. "http://misskey.loki"
  202. ]
  203. def custom_tld_non_scheme_urls,
  204. do: [
  205. "whatever.null/",
  206. "example.o/index.html",
  207. "pleroma.i2p/test",
  208. "misskey.loki"
  209. ]
  210. def valid_emails, do: ["rms@ai.mit.edu", "vc@cock.li"]
  211. def invalid_emails, do: ["rms[at]ai.mit.edu", "vc@cock", "xmpp:lain@trashserver.net"]
  212. def valid_custom_tld_emails, do: ["guardian@33y6fjyhs3phzfjj.onion", "hi@company.null"]
  213. end