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

html_test.go 9.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. func TestRender_Commits(t *testing.T) {
  15. setting.AppURL = AppURL
  16. setting.AppSubURL = AppSubURL
  17. test := func(input, expected string) {
  18. buffer := RenderString(".md", input, setting.AppSubURL, nil)
  19. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  20. }
  21. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  22. var commit = util.URLJoin(AppSubURL, "commit", sha)
  23. var subtree = util.URLJoin(commit, "src")
  24. var tree = strings.Replace(subtree, "/commit/", "/tree/", -1)
  25. test(sha, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  26. test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow">b6dd621</a></p>`)
  27. test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow">b6dd6210ea</a></p>`)
  28. test(commit, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  29. test(tree, `<p><a href="`+tree+`" rel="nofollow">b6dd6210ea/src</a></p>`)
  30. test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  31. }
  32. func TestRender_CrossReferences(t *testing.T) {
  33. setting.AppURL = AppURL
  34. setting.AppSubURL = AppSubURL
  35. test := func(input, expected string) {
  36. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  37. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  38. }
  39. test(
  40. "gogits/gogs#12345",
  41. `<p><a href="`+util.URLJoin(AppURL, "gogits", "gogs", "issues", "12345")+`" rel="nofollow">gogits/gogs#12345</a></p>`)
  42. test(
  43. "go-gitea/gitea#12345",
  44. `<p><a href="`+util.URLJoin(AppURL, "go-gitea", "gitea", "issues", "12345")+`" rel="nofollow">go-gitea/gitea#12345</a></p>`)
  45. }
  46. func TestMisc_IsSameDomain(t *testing.T) {
  47. setting.AppURL = AppURL
  48. setting.AppSubURL = AppSubURL
  49. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  50. var commit = util.URLJoin(AppSubURL, "commit", sha)
  51. assert.True(t, IsSameDomain(commit))
  52. assert.False(t, IsSameDomain("http://google.com/ncr"))
  53. assert.False(t, IsSameDomain("favicon.ico"))
  54. }
  55. func TestRender_links(t *testing.T) {
  56. setting.AppURL = AppURL
  57. setting.AppSubURL = AppSubURL
  58. test := func(input, expected string) {
  59. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  60. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  61. }
  62. // Text that should be turned into URL
  63. test(
  64. "https://www.example.com",
  65. `<p><a href="https://www.example.com" rel="nofollow">https://www.example.com</a></p>`)
  66. test(
  67. "http://www.example.com",
  68. `<p><a href="http://www.example.com" rel="nofollow">http://www.example.com</a></p>`)
  69. test(
  70. "https://example.com",
  71. `<p><a href="https://example.com" rel="nofollow">https://example.com</a></p>`)
  72. test(
  73. "http://example.com",
  74. `<p><a href="http://example.com" rel="nofollow">http://example.com</a></p>`)
  75. test(
  76. "http://foo.com/blah_blah",
  77. `<p><a href="http://foo.com/blah_blah" rel="nofollow">http://foo.com/blah_blah</a></p>`)
  78. test(
  79. "http://foo.com/blah_blah/",
  80. `<p><a href="http://foo.com/blah_blah/" rel="nofollow">http://foo.com/blah_blah/</a></p>`)
  81. test(
  82. "http://www.example.com/wpstyle/?p=364",
  83. `<p><a href="http://www.example.com/wpstyle/?p=364" rel="nofollow">http://www.example.com/wpstyle/?p=364</a></p>`)
  84. test(
  85. "https://www.example.com/foo/?bar=baz&inga=42&quux",
  86. `<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>`)
  87. test(
  88. "http://142.42.1.1/",
  89. `<p><a href="http://142.42.1.1/" rel="nofollow">http://142.42.1.1/</a></p>`)
  90. // Test that should *not* be turned into URL
  91. test(
  92. "www.example.com",
  93. `<p>www.example.com</p>`)
  94. test(
  95. "example.com",
  96. `<p>example.com</p>`)
  97. test(
  98. "test.example.com",
  99. `<p>test.example.com</p>`)
  100. test(
  101. "http://",
  102. `<p>http://</p>`)
  103. test(
  104. "https://",
  105. `<p>https://</p>`)
  106. test(
  107. "://",
  108. `<p>://</p>`)
  109. test(
  110. "www",
  111. `<p>www</p>`)
  112. }
  113. func TestRender_ShortLinks(t *testing.T) {
  114. setting.AppURL = AppURL
  115. setting.AppSubURL = AppSubURL
  116. tree := util.URLJoin(AppSubURL, "src", "master")
  117. test := func(input, expected, expectedWiki string) {
  118. buffer := markdown.RenderString(input, tree, nil)
  119. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  120. buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, nil)
  121. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
  122. }
  123. rawtree := util.URLJoin(AppSubURL, "raw", "master")
  124. url := util.URLJoin(tree, "Link")
  125. otherURL := util.URLJoin(tree, "Other-Link")
  126. encodedURL := util.URLJoin(tree, "Link%3F")
  127. imgurl := util.URLJoin(rawtree, "Link.jpg")
  128. otherImgurl := util.URLJoin(rawtree, "Link+Other.jpg")
  129. encodedImgurl := util.URLJoin(rawtree, "Link+%23.jpg")
  130. notencodedImgurl := util.URLJoin(rawtree, "some", "path", "Link+#.jpg")
  131. urlWiki := util.URLJoin(AppSubURL, "wiki", "Link")
  132. otherURLWiki := util.URLJoin(AppSubURL, "wiki", "Other-Link")
  133. encodedURLWiki := util.URLJoin(AppSubURL, "wiki", "Link%3F")
  134. imgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
  135. otherImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+Other.jpg")
  136. encodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+%23.jpg")
  137. notencodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "some", "path", "Link+#.jpg")
  138. favicon := "http://google.com/favicon.ico"
  139. test(
  140. "[[Link]]",
  141. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  142. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  143. test(
  144. "[[Link.jpg]]",
  145. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Link.jpg" alt="Link.jpg"/></a></p>`,
  146. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Link.jpg" alt="Link.jpg"/></a></p>`)
  147. test(
  148. "[["+favicon+"]]",
  149. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
  150. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
  151. test(
  152. "[[Name|Link]]",
  153. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  154. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  155. test(
  156. "[[Name|Link.jpg]]",
  157. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Name" alt="Name"/></a></p>`,
  158. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Name" alt="Name"/></a></p>`)
  159. test(
  160. "[[Name|Link.jpg|alt=AltName]]",
  161. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="AltName" alt="AltName"/></a></p>`,
  162. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="AltName" alt="AltName"/></a></p>`)
  163. test(
  164. "[[Name|Link.jpg|title=Title]]",
  165. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="Title"/></a></p>`,
  166. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="Title"/></a></p>`)
  167. test(
  168. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  169. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  170. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  171. test(
  172. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  173. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  174. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  175. test(
  176. "[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]",
  177. `<p><a href="`+otherImgurl+`" rel="nofollow"><img src="`+otherImgurl+`" title="Title" alt="AltName"/></a></p>`,
  178. `<p><a href="`+otherImgurlWiki+`" rel="nofollow"><img src="`+otherImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  179. test(
  180. "[[Link]] [[Other Link]]",
  181. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
  182. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a></p>`)
  183. test(
  184. "[[Link?]]",
  185. `<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  186. `<p><a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  187. test(
  188. "[[Link]] [[Other Link]] [[Link?]]",
  189. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  190. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a> <a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  191. test(
  192. "[[Link #.jpg]]",
  193. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`"/></a></p>`,
  194. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`"/></a></p>`)
  195. test(
  196. "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
  197. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
  198. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  199. test(
  200. "[[some/path/Link #.jpg]]",
  201. `<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`"/></a></p>`,
  202. `<p><a href="`+notencodedImgurlWiki+`" rel="nofollow"><img src="`+notencodedImgurlWiki+`"/></a></p>`)
  203. }