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.

markdown_test.go 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 markdown_test
  5. import (
  6. "fmt"
  7. "strconv"
  8. "testing"
  9. "strings"
  10. . "code.gitea.io/gitea/modules/markdown"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. const AppURL = "http://localhost:3000/"
  15. const Repo = "gogits/gogs"
  16. const AppSubURL = AppURL + Repo + "/"
  17. var numericMetas = map[string]string{
  18. "format": "https://someurl.com/{user}/{repo}/{index}",
  19. "user": "someUser",
  20. "repo": "someRepo",
  21. "style": IssueNameStyleNumeric,
  22. }
  23. var alphanumericMetas = map[string]string{
  24. "format": "https://someurl.com/{user}/{repo}/{index}",
  25. "user": "someUser",
  26. "repo": "someRepo",
  27. "style": IssueNameStyleAlphanumeric,
  28. }
  29. // numericLink an HTML to a numeric-style issue
  30. func numericIssueLink(baseURL string, index int) string {
  31. return link(URLJoin(baseURL, strconv.Itoa(index)), fmt.Sprintf("#%d", index))
  32. }
  33. // alphanumLink an HTML link to an alphanumeric-style issue
  34. func alphanumIssueLink(baseURL string, name string) string {
  35. return link(URLJoin(baseURL, name), name)
  36. }
  37. // urlContentsLink an HTML link whose contents is the target URL
  38. func urlContentsLink(href string) string {
  39. return link(href, href)
  40. }
  41. // link an HTML link
  42. func link(href, contents string) string {
  43. return fmt.Sprintf("<a href=\"%s\">%s</a>", href, contents)
  44. }
  45. func testRenderIssueIndexPattern(t *testing.T, input, expected string, metas map[string]string) {
  46. assert.Equal(t, expected,
  47. string(RenderIssueIndexPattern([]byte(input), AppSubURL, metas)))
  48. }
  49. func TestURLJoin(t *testing.T) {
  50. type test struct {
  51. Expected string
  52. Base string
  53. Elements []string
  54. }
  55. newTest := func(expected, base string, elements ...string) test {
  56. return test{Expected: expected, Base: base, Elements: elements}
  57. }
  58. for _, test := range []test{
  59. newTest("https://try.gitea.io/a/b/c",
  60. "https://try.gitea.io", "a/b", "c"),
  61. newTest("https://try.gitea.io/a/b/c",
  62. "https://try.gitea.io/", "/a/b/", "/c/"),
  63. newTest("https://try.gitea.io/a/c",
  64. "https://try.gitea.io/", "/a/./b/", "../c/"),
  65. newTest("a/b/c",
  66. "a", "b/c/"),
  67. newTest("a/b/d",
  68. "a/", "b/c/", "/../d/"),
  69. } {
  70. assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
  71. }
  72. }
  73. func TestRender_IssueIndexPattern(t *testing.T) {
  74. // numeric: render inputs without valid mentions
  75. test := func(s string) {
  76. testRenderIssueIndexPattern(t, s, s, nil)
  77. testRenderIssueIndexPattern(t, s, s, numericMetas)
  78. }
  79. // should not render anything when there are no mentions
  80. test("")
  81. test("this is a test")
  82. test("test 123 123 1234")
  83. test("#")
  84. test("# # #")
  85. test("# 123")
  86. test("#abcd")
  87. test("##1234")
  88. test("test#1234")
  89. test("#1234test")
  90. test(" test #1234test")
  91. // should not render issue mention without leading space
  92. test("test#54321 issue")
  93. // should not render issue mention without trailing space
  94. test("test #54321issue")
  95. }
  96. func TestRender_IssueIndexPattern2(t *testing.T) {
  97. setting.AppURL = AppURL
  98. setting.AppSubURL = AppSubURL
  99. // numeric: render inputs with valid mentions
  100. test := func(s, expectedFmt string, indices ...int) {
  101. links := make([]interface{}, len(indices))
  102. for i, index := range indices {
  103. links[i] = numericIssueLink(URLJoin(setting.AppSubURL, "issues"), index)
  104. }
  105. expectedNil := fmt.Sprintf(expectedFmt, links...)
  106. testRenderIssueIndexPattern(t, s, expectedNil, nil)
  107. for i, index := range indices {
  108. links[i] = numericIssueLink("https://someurl.com/someUser/someRepo/", index)
  109. }
  110. expectedNum := fmt.Sprintf(expectedFmt, links...)
  111. testRenderIssueIndexPattern(t, s, expectedNum, numericMetas)
  112. }
  113. // should render freestanding mentions
  114. test("#1234 test", "%s test", 1234)
  115. test("test #8 issue", "test %s issue", 8)
  116. test("test issue #1234", "test issue %s", 1234)
  117. // should render mentions in parentheses
  118. test("(#54321 issue)", "(%s issue)", 54321)
  119. test("test (#9801 extra) issue", "test (%s extra) issue", 9801)
  120. test("test (#1)", "test (%s)", 1)
  121. // should render multiple issue mentions in the same line
  122. test("#54321 #1243", "%s %s", 54321, 1243)
  123. test("wow (#54321 #1243)", "wow (%s %s)", 54321, 1243)
  124. test("(#4)(#5)", "(%s)(%s)", 4, 5)
  125. test("#1 (#4321) test", "%s (%s) test", 1, 4321)
  126. }
  127. func TestRender_IssueIndexPattern3(t *testing.T) {
  128. setting.AppURL = AppURL
  129. setting.AppSubURL = AppSubURL
  130. // alphanumeric: render inputs without valid mentions
  131. test := func(s string) {
  132. testRenderIssueIndexPattern(t, s, s, alphanumericMetas)
  133. }
  134. test("")
  135. test("this is a test")
  136. test("test 123 123 1234")
  137. test("#")
  138. test("##1234")
  139. test("# 123")
  140. test("#abcd")
  141. test("test #123")
  142. test("abc-1234") // issue prefix must be capital
  143. test("ABc-1234") // issue prefix must be _all_ capital
  144. test("ABCDEFGHIJK-1234") // the limit is 10 characters in the prefix
  145. test("ABC1234") // dash is required
  146. test("test ABC- test") // number is required
  147. test("test -1234 test") // prefix is required
  148. test("testABC-123 test") // leading space is required
  149. test("test ABC-123test") // trailing space is required
  150. test("ABC-0123") // no leading zero
  151. }
  152. func TestRender_IssueIndexPattern4(t *testing.T) {
  153. setting.AppURL = AppURL
  154. setting.AppSubURL = AppSubURL
  155. // alphanumeric: render inputs with valid mentions
  156. test := func(s, expectedFmt string, names ...string) {
  157. links := make([]interface{}, len(names))
  158. for i, name := range names {
  159. links[i] = alphanumIssueLink("https://someurl.com/someUser/someRepo/", name)
  160. }
  161. expected := fmt.Sprintf(expectedFmt, links...)
  162. testRenderIssueIndexPattern(t, s, expected, alphanumericMetas)
  163. }
  164. test("OTT-1234 test", "%s test", "OTT-1234")
  165. test("test T-12 issue", "test %s issue", "T-12")
  166. test("test issue ABCDEFGHIJ-1234567890", "test issue %s", "ABCDEFGHIJ-1234567890")
  167. }
  168. func TestRender_AutoLink(t *testing.T) {
  169. setting.AppURL = AppURL
  170. setting.AppSubURL = AppSubURL
  171. test := func(input, expected string) {
  172. buffer := RenderSpecialLink([]byte(input), setting.AppSubURL, nil, false)
  173. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  174. buffer = RenderSpecialLink([]byte(input), setting.AppSubURL, nil, true)
  175. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  176. }
  177. // render valid issue URLs
  178. test(URLJoin(setting.AppSubURL, "issues", "3333"),
  179. numericIssueLink(URLJoin(setting.AppSubURL, "issues"), 3333))
  180. // render external issue URLs
  181. for _, externalURL := range []string{
  182. "http://1111/2222/ssss-issues/3333?param=blah&blahh=333",
  183. "http://test.com/issues/33333",
  184. "https://issues/333"} {
  185. test(externalURL, externalURL)
  186. }
  187. // render valid commit URLs
  188. tmp := URLJoin(AppSubURL, "commit", "d8a994ef243349f321568f9e36d5c3f444b99cae")
  189. test(tmp, "<a href=\""+tmp+"\">d8a994ef24</a>")
  190. tmp += "#diff-2"
  191. test(tmp, "<a href=\""+tmp+"\">d8a994ef24 (diff-2)</a>")
  192. // render other commit URLs
  193. tmp = "https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2"
  194. test(tmp, "<a href=\""+tmp+"\">d8a994ef24 (diff-2)</a>")
  195. }
  196. func TestRender_StandardLinks(t *testing.T) {
  197. setting.AppURL = AppURL
  198. setting.AppSubURL = AppSubURL
  199. test := func(input, expected, expectedWiki string) {
  200. buffer := RenderString(input, setting.AppSubURL, nil)
  201. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  202. bufferWiki := RenderWiki([]byte(input), setting.AppSubURL, nil)
  203. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(bufferWiki))
  204. }
  205. googleRendered := `<p><a href="https://google.com/" rel="nofollow">https://google.com/</a></p>`
  206. test("<https://google.com/>", googleRendered, googleRendered)
  207. lnk := URLJoin(AppSubURL, "WikiPage")
  208. lnkWiki := URLJoin(AppSubURL, "wiki", "WikiPage")
  209. test("[WikiPage](WikiPage)",
  210. `<p><a href="`+lnk+`" rel="nofollow">WikiPage</a></p>`,
  211. `<p><a href="`+lnkWiki+`" rel="nofollow">WikiPage</a></p>`)
  212. }
  213. func TestRender_ShortLinks(t *testing.T) {
  214. setting.AppURL = AppURL
  215. setting.AppSubURL = AppSubURL
  216. tree := URLJoin(AppSubURL, "src", "master")
  217. test := func(input, expected, expectedWiki string) {
  218. buffer := RenderString(input, tree, nil)
  219. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  220. buffer = RenderWiki([]byte(input), setting.AppSubURL, nil)
  221. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
  222. }
  223. rawtree := URLJoin(AppSubURL, "raw", "master")
  224. url := URLJoin(tree, "Link")
  225. otherUrl := URLJoin(tree, "OtherLink")
  226. imgurl := URLJoin(rawtree, "Link.jpg")
  227. urlWiki := URLJoin(AppSubURL, "wiki", "Link")
  228. otherUrlWiki := URLJoin(AppSubURL, "wiki", "OtherLink")
  229. imgurlWiki := URLJoin(AppSubURL, "wiki", "raw", "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+`" alt="Link.jpg" title="Link.jpg"/></a></p>`,
  238. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Link.jpg" title="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+`" alt="Name" title="Name"/></a></p>`,
  250. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Name" title="Name"/></a></p>`)
  251. test(
  252. "[[Name|Link.jpg|alt=AltName]]",
  253. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="AltName"/></a></p>`,
  254. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="AltName"/></a></p>`)
  255. test(
  256. "[[Name|Link.jpg|title=Title]]",
  257. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="Title" title="Title"/></a></p>`,
  258. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="Title" title="Title"/></a></p>`)
  259. test(
  260. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  261. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="Title"/></a></p>`,
  262. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="Title"/></a></p>`)
  263. test(
  264. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  265. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" alt="AltName" title="Title"/></a></p>`,
  266. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" alt="AltName" title="Title"/></a></p>`)
  267. test(
  268. "[[Link]] [[OtherLink]]",
  269. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherUrl+`" rel="nofollow">OtherLink</a></p>`,
  270. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherUrlWiki+`" rel="nofollow">OtherLink</a></p>`)
  271. }
  272. func TestRender_Commits(t *testing.T) {
  273. setting.AppURL = AppURL
  274. setting.AppSubURL = AppSubURL
  275. test := func(input, expected string) {
  276. buffer := RenderString(input, setting.AppSubURL, nil)
  277. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  278. }
  279. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  280. var commit = URLJoin(AppSubURL, "commit", sha)
  281. var subtree = URLJoin(commit, "src")
  282. var tree = strings.Replace(subtree, "/commit/", "/tree/", -1)
  283. var src = strings.Replace(subtree, "/commit/", "/src/", -1)
  284. test(sha, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  285. test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow">b6dd621</a></p>`)
  286. test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow">b6dd6210ea</a></p>`)
  287. test(commit, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  288. test(tree, `<p><a href="`+src+`" rel="nofollow">b6dd6210ea/src</a></p>`)
  289. test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  290. }
  291. func TestRender_Images(t *testing.T) {
  292. setting.AppURL = AppURL
  293. setting.AppSubURL = AppSubURL
  294. test := func(input, expected string) {
  295. buffer := RenderString(input, setting.AppSubURL, nil)
  296. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  297. }
  298. url := "../../.images/src/02/train.jpg"
  299. title := "Train"
  300. result := URLJoin(AppSubURL, url)
  301. test(
  302. "!["+title+"]("+url+")",
  303. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"></a></p>`)
  304. test(
  305. "[["+title+"|"+url+"]]",
  306. `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`" title="`+title+`"/></a></p>`)
  307. }
  308. func TestRender_CrossReferences(t *testing.T) {
  309. setting.AppURL = AppURL
  310. setting.AppSubURL = AppSubURL
  311. test := func(input, expected string) {
  312. buffer := RenderString(input, setting.AppSubURL, nil)
  313. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  314. }
  315. test(
  316. "gogits/gogs#12345",
  317. `<p><a href="`+URLJoin(AppURL, "gogits", "gogs", "issues", "12345")+`" rel="nofollow">gogits/gogs#12345</a></p>`)
  318. }
  319. func TestRender_FullIssueURLs(t *testing.T) {
  320. setting.AppURL = AppURL
  321. setting.AppSubURL = AppSubURL
  322. test := func(input, expected string) {
  323. result := RenderFullIssuePattern([]byte(input))
  324. assert.Equal(t, expected, string(result))
  325. }
  326. test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6",
  327. "Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6")
  328. test("Look here http://localhost:3000/person/repo/issues/4",
  329. `Look here <a href="http://localhost:3000/person/repo/issues/4">#4</a>`)
  330. test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
  331. `<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234">#4</a>`)
  332. }
  333. func TestRegExp_MentionPattern(t *testing.T) {
  334. trueTestCases := []string{
  335. "@Unknwon",
  336. "@ANT_123",
  337. "@xxx-DiN0-z-A..uru..s-xxx",
  338. " @lol ",
  339. " @Te/st",
  340. }
  341. falseTestCases := []string{
  342. "@ 0",
  343. "@ ",
  344. "@",
  345. "",
  346. "ABC",
  347. }
  348. for _, testCase := range trueTestCases {
  349. res := MentionPattern.MatchString(testCase)
  350. if !res {
  351. println()
  352. println(testCase)
  353. }
  354. assert.True(t, res)
  355. }
  356. for _, testCase := range falseTestCases {
  357. res := MentionPattern.MatchString(testCase)
  358. if res {
  359. println()
  360. println(testCase)
  361. }
  362. assert.False(t, res)
  363. }
  364. }
  365. func TestRegExp_IssueNumericPattern(t *testing.T) {
  366. trueTestCases := []string{
  367. "#1234",
  368. "#0",
  369. "#1234567890987654321",
  370. }
  371. falseTestCases := []string{
  372. "# 1234",
  373. "# 0",
  374. "# ",
  375. "#",
  376. "#ABC",
  377. "#1A2B",
  378. "",
  379. "ABC",
  380. }
  381. for _, testCase := range trueTestCases {
  382. assert.True(t, IssueNumericPattern.MatchString(testCase))
  383. }
  384. for _, testCase := range falseTestCases {
  385. assert.False(t, IssueNumericPattern.MatchString(testCase))
  386. }
  387. }
  388. func TestRegExp_IssueAlphanumericPattern(t *testing.T) {
  389. trueTestCases := []string{
  390. "ABC-1234",
  391. "A-1",
  392. "RC-80",
  393. "ABCDEFGHIJ-1234567890987654321234567890",
  394. }
  395. falseTestCases := []string{
  396. "RC-08",
  397. "PR-0",
  398. "ABCDEFGHIJK-1",
  399. "PR_1",
  400. "",
  401. "#ABC",
  402. "",
  403. "ABC",
  404. "GG-",
  405. "rm-1",
  406. }
  407. for _, testCase := range trueTestCases {
  408. assert.True(t, IssueAlphanumericPattern.MatchString(testCase))
  409. }
  410. for _, testCase := range falseTestCases {
  411. assert.False(t, IssueAlphanumericPattern.MatchString(testCase))
  412. }
  413. }
  414. func TestRegExp_Sha1CurrentPattern(t *testing.T) {
  415. trueTestCases := []string{
  416. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  417. "abcdefabcdefabcdefabcdefabcdefabcdefabcd",
  418. }
  419. falseTestCases := []string{
  420. "test",
  421. "abcdefg",
  422. "abcdefghijklmnopqrstuvwxyzabcdefghijklmn",
  423. "abcdefghijklmnopqrstuvwxyzabcdefghijklmO",
  424. }
  425. for _, testCase := range trueTestCases {
  426. assert.True(t, Sha1CurrentPattern.MatchString(testCase))
  427. }
  428. for _, testCase := range falseTestCases {
  429. assert.False(t, Sha1CurrentPattern.MatchString(testCase))
  430. }
  431. }
  432. func TestRegExp_ShortLinkPattern(t *testing.T) {
  433. trueTestCases := []string{
  434. "[[stuff]]",
  435. "[[]]",
  436. "[[stuff|title=Difficult name with spaces*!]]",
  437. }
  438. falseTestCases := []string{
  439. "test",
  440. "abcdefg",
  441. "[[]",
  442. "[[",
  443. "[]",
  444. "]]",
  445. "abcdefghijklmnopqrstuvwxyz",
  446. }
  447. for _, testCase := range trueTestCases {
  448. assert.True(t, ShortLinkPattern.MatchString(testCase))
  449. }
  450. for _, testCase := range falseTestCases {
  451. assert.False(t, ShortLinkPattern.MatchString(testCase))
  452. }
  453. }
  454. func TestRegExp_AnySHA1Pattern(t *testing.T) {
  455. testCases := map[string][]string{
  456. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js#L2703": {
  457. "https",
  458. "github.com",
  459. "jquery",
  460. "jquery",
  461. "blob",
  462. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  463. "test/unit/event.js",
  464. "L2703",
  465. },
  466. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js": {
  467. "https",
  468. "github.com",
  469. "jquery",
  470. "jquery",
  471. "blob",
  472. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  473. "test/unit/event.js",
  474. "",
  475. },
  476. "https://github.com/jquery/jquery/commit/0705be475092aede1eddae01319ec931fb9c65fc": {
  477. "https",
  478. "github.com",
  479. "jquery",
  480. "jquery",
  481. "commit",
  482. "0705be475092aede1eddae01319ec931fb9c65fc",
  483. "",
  484. "",
  485. },
  486. "https://github.com/jquery/jquery/tree/0705be475092aede1eddae01319ec931fb9c65fc/src": {
  487. "https",
  488. "github.com",
  489. "jquery",
  490. "jquery",
  491. "tree",
  492. "0705be475092aede1eddae01319ec931fb9c65fc",
  493. "src",
  494. "",
  495. },
  496. "https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2": {
  497. "https",
  498. "try.gogs.io",
  499. "gogs",
  500. "gogs",
  501. "commit",
  502. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  503. "",
  504. "diff-2",
  505. },
  506. }
  507. for k, v := range testCases {
  508. assert.Equal(t, AnySHA1Pattern.FindStringSubmatch(k)[1:], v)
  509. }
  510. }
  511. func TestMisc_IsMarkdownFile(t *testing.T) {
  512. setting.Markdown.FileExtensions = []string{".md", ".markdown", ".mdown", ".mkd"}
  513. trueTestCases := []string{
  514. "test.md",
  515. "wow.MARKDOWN",
  516. "LOL.mDoWn",
  517. }
  518. falseTestCases := []string{
  519. "test",
  520. "abcdefg",
  521. "abcdefghijklmnopqrstuvwxyz",
  522. "test.md.test",
  523. }
  524. for _, testCase := range trueTestCases {
  525. assert.True(t, IsMarkdownFile(testCase))
  526. }
  527. for _, testCase := range falseTestCases {
  528. assert.False(t, IsMarkdownFile(testCase))
  529. }
  530. }
  531. func TestMisc_IsSameDomain(t *testing.T) {
  532. setting.AppURL = AppURL
  533. setting.AppSubURL = AppSubURL
  534. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  535. var commit = URLJoin(AppSubURL, "commit", sha)
  536. assert.True(t, IsSameDomain(commit))
  537. assert.False(t, IsSameDomain("http://google.com/ncr"))
  538. assert.False(t, IsSameDomain("favicon.ico"))
  539. }
  540. // Test cases without ambiguous links
  541. var sameCases = []string{
  542. // dear imgui wiki markdown extract: special wiki syntax
  543. `Wiki! Enjoy :)
  544. - [[Links, Language bindings, Engine bindings|Links]]
  545. - [[Tips]]
  546. Ideas and codes
  547. - Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786
  548. - Node graph editors https://github.com/ocornut/imgui/issues/306
  549. - [[Memory Editor|memory_editor_example]]
  550. - [[Plot var helper|plot_var_example]]`,
  551. // wine-staging wiki home extract: tables, special wiki syntax, images
  552. `## What is Wine Staging?
  553. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  554. ## Quick Links
  555. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  556. | [[images/icon-install.png]] | [[Installation]] |
  557. |--------------------------------|----------------------------------------------------------|
  558. | [[images/icon-usage.png]] | [[Usage]] |
  559. `,
  560. // libgdx wiki page: inline images with special syntax
  561. `[Excelsior JET](http://www.excelsiorjet.com/) allows you to create native executables for Windows, Linux and Mac OS X.
  562. 1. [Package your libGDX application](https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop)
  563. [[images/1.png]]
  564. 2. Perform a test run by hitting the Run! button.
  565. [[images/2.png]]`,
  566. }
  567. func testAnswers(baseURLContent, baseURLImages string) []string {
  568. return []string{
  569. `<p>Wiki! Enjoy :)</p>
  570. <ul>
  571. <li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
  572. <li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
  573. </ul>
  574. <p>Ideas and codes</p>
  575. <ul>
  576. <li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" rel="nofollow">#786</a></li>
  577. <li>Node graph editors https://github.com/ocornut/imgui/issues/306</li>
  578. <li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
  579. <li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
  580. </ul>
  581. `,
  582. `<h2>What is Wine Staging?</h2>
  583. <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
  584. <h2>Quick Links</h2>
  585. <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
  586. <table>
  587. <thead>
  588. <tr>
  589. <th><a href="` + baseURLImages + `/images/icon-install.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-install.png" alt="images/icon-install.png" title="icon-install.png"/></a></th>
  590. <th><a href="` + baseURLContent + `/Installation" rel="nofollow">Installation</a></th>
  591. </tr>
  592. </thead>
  593. <tbody>
  594. <tr>
  595. <td><a href="` + baseURLImages + `/images/icon-usage.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-usage.png" alt="images/icon-usage.png" title="icon-usage.png"/></a></td>
  596. <td><a href="` + baseURLContent + `/Usage" rel="nofollow">Usage</a></td>
  597. </tr>
  598. </tbody>
  599. </table>
  600. `,
  601. `<p><a href="http://www.excelsiorjet.com/" rel="nofollow">Excelsior JET</a> allows you to create native executables for Windows, Linux and Mac OS X.</p>
  602. <ol>
  603. <li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a>
  604. <a href="` + baseURLImages + `/images/1.png" rel="nofollow"><img src="` + baseURLImages + `/images/1.png" alt="images/1.png" title="1.png"/></a></li>
  605. <li>Perform a test run by hitting the Run! button.
  606. <a href="` + baseURLImages + `/images/2.png" rel="nofollow"><img src="` + baseURLImages + `/images/2.png" alt="images/2.png" title="2.png"/></a></li>
  607. </ol>
  608. `,
  609. }
  610. }
  611. func TestTotal_RenderString(t *testing.T) {
  612. answers := testAnswers(URLJoin(AppSubURL, "src", "master/"), URLJoin(AppSubURL, "raw", "master/"))
  613. for i := 0; i < len(sameCases); i++ {
  614. line := RenderString(sameCases[i], URLJoin(AppSubURL, "src", "master/"), nil)
  615. assert.Equal(t, answers[i], line)
  616. }
  617. testCases := []string{}
  618. for i := 0; i < len(testCases); i += 2 {
  619. line := RenderString(testCases[i], AppSubURL, nil)
  620. assert.Equal(t, testCases[i+1], line)
  621. }
  622. }
  623. func TestTotal_RenderWiki(t *testing.T) {
  624. answers := testAnswers(URLJoin(AppSubURL, "wiki/"), URLJoin(AppSubURL, "wiki", "raw/"))
  625. for i := 0; i < len(sameCases); i++ {
  626. line := RenderWiki([]byte(sameCases[i]), AppSubURL, nil)
  627. assert.Equal(t, answers[i], line)
  628. }
  629. testCases := []string{
  630. // Guard wiki sidebar: special syntax
  631. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  632. // rendered
  633. `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  634. `,
  635. // special syntax
  636. `[[Name|Link]]`,
  637. // rendered
  638. `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
  639. `,
  640. }
  641. for i := 0; i < len(testCases); i += 2 {
  642. line := RenderWiki([]byte(testCases[i]), AppSubURL, nil)
  643. assert.Equal(t, testCases[i+1], line)
  644. }
  645. }