本站源代码
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

329 lines
13KB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package markup_test
  5. import (
  6. "strings"
  7. "testing"
  8. . "code.gitea.io/gitea/modules/markup"
  9. "code.gitea.io/gitea/modules/markup/markdown"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/util"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. var localMetas = map[string]string{
  15. "user": "gogits",
  16. "repo": "gogs",
  17. "repoPath": "../../integrations/gitea-repositories-meta/user13/repo11.git/",
  18. }
  19. func TestRender_Commits(t *testing.T) {
  20. setting.AppURL = AppURL
  21. setting.AppSubURL = AppSubURL
  22. test := func(input, expected string) {
  23. buffer := RenderString(".md", input, setting.AppSubURL, localMetas)
  24. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  25. }
  26. var sha = "65f1bf27bc3bf70f64657658635e66094edbcb4d"
  27. var commit = util.URLJoin(AppSubURL, "commit", sha)
  28. var subtree = util.URLJoin(commit, "src")
  29. var tree = strings.Replace(subtree, "/commit/", "/tree/", -1)
  30. test(sha, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  31. test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow"><code>65f1bf2</code></a></p>`)
  32. test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  33. test(commit, `<p><a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  34. test(tree, `<p><a href="`+tree+`" rel="nofollow"><code>65f1bf27bc/src</code></a></p>`)
  35. test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow"><code>65f1bf27bc</code></a></p>`)
  36. test("/home/gitea/"+sha, "<p>/home/gitea/"+sha+"</p>")
  37. test("deadbeef", `<p>deadbeef</p>`)
  38. test("d27ace93", `<p>d27ace93</p>`)
  39. }
  40. func TestRender_CrossReferences(t *testing.T) {
  41. setting.AppURL = AppURL
  42. setting.AppSubURL = AppSubURL
  43. test := func(input, expected string) {
  44. buffer := RenderString("a.md", input, setting.AppSubURL, localMetas)
  45. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  46. }
  47. test(
  48. "gogits/gogs#12345",
  49. `<p><a href="`+util.URLJoin(AppURL, "gogits", "gogs", "issues", "12345")+`" rel="nofollow">gogits/gogs#12345</a></p>`)
  50. test(
  51. "go-gitea/gitea#12345",
  52. `<p><a href="`+util.URLJoin(AppURL, "go-gitea", "gitea", "issues", "12345")+`" rel="nofollow">go-gitea/gitea#12345</a></p>`)
  53. test(
  54. "/home/gitea/go-gitea/gitea#12345",
  55. `<p>/home/gitea/go-gitea/gitea#12345</p>`)
  56. }
  57. func TestMisc_IsSameDomain(t *testing.T) {
  58. setting.AppURL = AppURL
  59. setting.AppSubURL = AppSubURL
  60. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  61. var commit = util.URLJoin(AppSubURL, "commit", sha)
  62. assert.True(t, IsSameDomain(commit))
  63. assert.False(t, IsSameDomain("http://google.com/ncr"))
  64. assert.False(t, IsSameDomain("favicon.ico"))
  65. }
  66. func TestRender_links(t *testing.T) {
  67. setting.AppURL = AppURL
  68. setting.AppSubURL = AppSubURL
  69. test := func(input, expected string) {
  70. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  71. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  72. }
  73. // Text that should be turned into URL
  74. defaultCustom := setting.Markdown.CustomURLSchemes
  75. setting.Markdown.CustomURLSchemes = []string{"ftp", "magnet"}
  76. ReplaceSanitizer()
  77. CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes)
  78. test(
  79. "https://www.example.com",
  80. `<p><a href="https://www.example.com" rel="nofollow">https://www.example.com</a></p>`)
  81. test(
  82. "http://www.example.com",
  83. `<p><a href="http://www.example.com" rel="nofollow">http://www.example.com</a></p>`)
  84. test(
  85. "https://example.com",
  86. `<p><a href="https://example.com" rel="nofollow">https://example.com</a></p>`)
  87. test(
  88. "http://example.com",
  89. `<p><a href="http://example.com" rel="nofollow">http://example.com</a></p>`)
  90. test(
  91. "http://foo.com/blah_blah",
  92. `<p><a href="http://foo.com/blah_blah" rel="nofollow">http://foo.com/blah_blah</a></p>`)
  93. test(
  94. "http://foo.com/blah_blah/",
  95. `<p><a href="http://foo.com/blah_blah/" rel="nofollow">http://foo.com/blah_blah/</a></p>`)
  96. test(
  97. "http://www.example.com/wpstyle/?p=364",
  98. `<p><a href="http://www.example.com/wpstyle/?p=364" rel="nofollow">http://www.example.com/wpstyle/?p=364</a></p>`)
  99. test(
  100. "https://www.example.com/foo/?bar=baz&inga=42&quux",
  101. `<p><a href="https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux" rel="nofollow">https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux</a></p>`)
  102. test(
  103. "http://142.42.1.1/",
  104. `<p><a href="http://142.42.1.1/" rel="nofollow">http://142.42.1.1/</a></p>`)
  105. test(
  106. "https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd",
  107. `<p><a href="https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd" rel="nofollow">https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd</a></p>`)
  108. test(
  109. "https://en.wikipedia.org/wiki/URL_(disambiguation)",
  110. `<p><a href="https://en.wikipedia.org/wiki/URL_(disambiguation)" rel="nofollow">https://en.wikipedia.org/wiki/URL_(disambiguation)</a></p>`)
  111. test(
  112. "https://foo_bar.example.com/",
  113. `<p><a href="https://foo_bar.example.com/" rel="nofollow">https://foo_bar.example.com/</a></p>`)
  114. test(
  115. "https://stackoverflow.com/questions/2896191/what-is-go-used-fore",
  116. `<p><a href="https://stackoverflow.com/questions/2896191/what-is-go-used-fore" rel="nofollow">https://stackoverflow.com/questions/2896191/what-is-go-used-fore</a></p>`)
  117. test(
  118. "https://username:password@gitea.com",
  119. `<p><a href="https://username:password@gitea.com" rel="nofollow">https://username:password@gitea.com</a></p>`)
  120. test(
  121. "ftp://gitea.com/file.txt",
  122. `<p><a href="ftp://gitea.com/file.txt" rel="nofollow">ftp://gitea.com/file.txt</a></p>`)
  123. test(
  124. "magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&dn=download",
  125. `<p><a href="magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&amp;dn=download" rel="nofollow">magnet:?xt=urn:btih:5dee65101db281ac9c46344cd6b175cdcadabcde&amp;dn=download</a></p>`)
  126. // Test that should *not* be turned into URL
  127. test(
  128. "www.example.com",
  129. `<p>www.example.com</p>`)
  130. test(
  131. "example.com",
  132. `<p>example.com</p>`)
  133. test(
  134. "test.example.com",
  135. `<p>test.example.com</p>`)
  136. test(
  137. "http://",
  138. `<p>http://</p>`)
  139. test(
  140. "https://",
  141. `<p>https://</p>`)
  142. test(
  143. "://",
  144. `<p>://</p>`)
  145. test(
  146. "www",
  147. `<p>www</p>`)
  148. test(
  149. "ftps://gitea.com",
  150. `<p>ftps://gitea.com</p>`)
  151. // Restore previous settings
  152. setting.Markdown.CustomURLSchemes = defaultCustom
  153. ReplaceSanitizer()
  154. CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes)
  155. }
  156. func TestRender_email(t *testing.T) {
  157. setting.AppURL = AppURL
  158. setting.AppSubURL = AppSubURL
  159. test := func(input, expected string) {
  160. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  161. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  162. }
  163. // Text that should be turned into email link
  164. test(
  165. "info@gitea.com",
  166. `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a></p>`)
  167. test(
  168. "(info@gitea.com)",
  169. `<p>(<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>)</p>`)
  170. test(
  171. "[info@gitea.com]",
  172. `<p>[<a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>]</p>`)
  173. test(
  174. "info@gitea.com.",
  175. `<p><a href="mailto:info@gitea.com" rel="nofollow">info@gitea.com</a>.</p>`)
  176. test(
  177. "firstname+lastname@gitea.com",
  178. `<p><a href="mailto:firstname+lastname@gitea.com" rel="nofollow">firstname+lastname@gitea.com</a></p>`)
  179. test(
  180. "send email to info@gitea.co.uk.",
  181. `<p>send email to <a href="mailto:info@gitea.co.uk" rel="nofollow">info@gitea.co.uk</a>.</p>`)
  182. // Test that should *not* be turned into email links
  183. test(
  184. "\"info@gitea.com\"",
  185. `<p>“info@gitea.com”</p>`)
  186. test(
  187. "/home/gitea/mailstore/info@gitea/com",
  188. `<p>/home/gitea/mailstore/info@gitea/com</p>`)
  189. test(
  190. "git@try.gitea.io:go-gitea/gitea.git",
  191. `<p>git@try.gitea.io:go-gitea/gitea.git</p>`)
  192. test(
  193. "gitea@3",
  194. `<p>gitea@3</p>`)
  195. test(
  196. "gitea@gmail.c",
  197. `<p>gitea@gmail.c</p>`)
  198. test(
  199. "email@domain@domain.com",
  200. `<p>email@domain@domain.com</p>`)
  201. test(
  202. "email@domain..com",
  203. `<p>email@domain..com</p>`)
  204. }
  205. func TestRender_ShortLinks(t *testing.T) {
  206. setting.AppURL = AppURL
  207. setting.AppSubURL = AppSubURL
  208. tree := util.URLJoin(AppSubURL, "src", "master")
  209. test := func(input, expected, expectedWiki string) {
  210. buffer := markdown.RenderString(input, tree, nil)
  211. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
  212. buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, localMetas)
  213. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer))
  214. }
  215. rawtree := util.URLJoin(AppSubURL, "raw", "master")
  216. url := util.URLJoin(tree, "Link")
  217. otherURL := util.URLJoin(tree, "Other-Link")
  218. encodedURL := util.URLJoin(tree, "Link%3F")
  219. imgurl := util.URLJoin(rawtree, "Link.jpg")
  220. otherImgurl := util.URLJoin(rawtree, "Link+Other.jpg")
  221. encodedImgurl := util.URLJoin(rawtree, "Link+%23.jpg")
  222. notencodedImgurl := util.URLJoin(rawtree, "some", "path", "Link+#.jpg")
  223. urlWiki := util.URLJoin(AppSubURL, "wiki", "Link")
  224. otherURLWiki := util.URLJoin(AppSubURL, "wiki", "Other-Link")
  225. encodedURLWiki := util.URLJoin(AppSubURL, "wiki", "Link%3F")
  226. imgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
  227. otherImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+Other.jpg")
  228. encodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+%23.jpg")
  229. notencodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "some", "path", "Link+#.jpg")
  230. favicon := "http://google.com/favicon.ico"
  231. test(
  232. "[[Link]]",
  233. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  234. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  235. test(
  236. "[[Link.jpg]]",
  237. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Link.jpg" alt="Link.jpg"/></a></p>`,
  238. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Link.jpg" alt="Link.jpg"/></a></p>`)
  239. test(
  240. "[["+favicon+"]]",
  241. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
  242. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
  243. test(
  244. "[[Name|Link]]",
  245. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  246. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  247. test(
  248. "[[Name|Link.jpg]]",
  249. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Name" alt="Name"/></a></p>`,
  250. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Name" alt="Name"/></a></p>`)
  251. test(
  252. "[[Name|Link.jpg|alt=AltName]]",
  253. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="AltName" alt="AltName"/></a></p>`,
  254. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="AltName" alt="AltName"/></a></p>`)
  255. test(
  256. "[[Name|Link.jpg|title=Title]]",
  257. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="Title"/></a></p>`,
  258. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="Title"/></a></p>`)
  259. test(
  260. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  261. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  262. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  263. test(
  264. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  265. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  266. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  267. test(
  268. "[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]",
  269. `<p><a href="`+otherImgurl+`" rel="nofollow"><img src="`+otherImgurl+`" title="Title" alt="AltName"/></a></p>`,
  270. `<p><a href="`+otherImgurlWiki+`" rel="nofollow"><img src="`+otherImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  271. test(
  272. "[[Link]] [[Other Link]]",
  273. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
  274. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a></p>`)
  275. test(
  276. "[[Link?]]",
  277. `<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  278. `<p><a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  279. test(
  280. "[[Link]] [[Other Link]] [[Link?]]",
  281. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  282. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a> <a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  283. test(
  284. "[[Link #.jpg]]",
  285. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`"/></a></p>`,
  286. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`"/></a></p>`)
  287. test(
  288. "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
  289. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
  290. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  291. test(
  292. "[[some/path/Link #.jpg]]",
  293. `<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`"/></a></p>`,
  294. `<p><a href="`+notencodedImgurlWiki+`" rel="nofollow"><img src="`+notencodedImgurlWiki+`"/></a></p>`)
  295. test(
  296. "<p><a href=\"https://example.org\">[[foobar]]</a></p>",
  297. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`,
  298. `<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`)
  299. }
上海开阖软件有限公司 沪ICP备12045867号-1