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.

markdown_test.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2020 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 misc
  5. import (
  6. go_context "context"
  7. "io"
  8. "net/http"
  9. "net/http/httptest"
  10. "net/url"
  11. "strings"
  12. "testing"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/markup"
  15. "code.gitea.io/gitea/modules/setting"
  16. api "code.gitea.io/gitea/modules/structs"
  17. "code.gitea.io/gitea/modules/templates"
  18. "code.gitea.io/gitea/modules/util"
  19. "code.gitea.io/gitea/modules/web"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. const (
  23. AppURL = "http://localhost:3000/"
  24. Repo = "gogits/gogs"
  25. AppSubURL = AppURL + Repo + "/"
  26. )
  27. func createContext(req *http.Request) (*context.Context, *httptest.ResponseRecorder) {
  28. _, rnd := templates.HTMLRenderer(req.Context())
  29. resp := httptest.NewRecorder()
  30. c := &context.Context{
  31. Req: req,
  32. Resp: context.NewResponse(resp),
  33. Render: rnd,
  34. Data: make(map[string]interface{}),
  35. }
  36. defer c.Close()
  37. return c, resp
  38. }
  39. func wrap(ctx *context.Context) *context.APIContext {
  40. return &context.APIContext{
  41. Context: ctx,
  42. }
  43. }
  44. func TestAPI_RenderGFM(t *testing.T) {
  45. setting.AppURL = AppURL
  46. markup.Init(&markup.ProcessorHelper{
  47. IsUsernameMentionable: func(ctx go_context.Context, username string) bool {
  48. return username == "r-lyeh"
  49. },
  50. })
  51. options := api.MarkdownOption{
  52. Mode: "gfm",
  53. Text: "",
  54. Context: Repo,
  55. Wiki: true,
  56. }
  57. requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
  58. req := &http.Request{
  59. Method: "POST",
  60. URL: requrl,
  61. }
  62. m, resp := createContext(req)
  63. ctx := wrap(m)
  64. testCases := []string{
  65. // dear imgui wiki markdown extract: special wiki syntax
  66. `Wiki! Enjoy :)
  67. - [[Links, Language bindings, Engine bindings|Links]]
  68. - [[Tips]]
  69. - Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`,
  70. // rendered
  71. `<p>Wiki! Enjoy :)</p>
  72. <ul>
  73. <li><a href="` + AppSubURL + `wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
  74. <li><a href="` + AppSubURL + `wiki/Tips" rel="nofollow">Tips</a></li>
  75. <li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="https://github.com/ocornut/imgui/issues/786" rel="nofollow">https://github.com/ocornut/imgui/issues/786</a></li>
  76. </ul>
  77. `,
  78. // wine-staging wiki home extract: special wiki syntax, images
  79. `## What is Wine Staging?
  80. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  81. ## Quick Links
  82. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  83. [[Configuration]]
  84. [[images/icon-bug.png]]
  85. `,
  86. // rendered
  87. `<h2 id="user-content-what-is-wine-staging">What is Wine Staging?</h2>
  88. <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
  89. <h2 id="user-content-quick-links">Quick Links</h2>
  90. <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
  91. <p><a href="` + AppSubURL + `wiki/Configuration" rel="nofollow">Configuration</a>
  92. <a href="` + AppSubURL + `wiki/raw/images/icon-bug.png" rel="nofollow"><img src="` + AppSubURL + `wiki/raw/images/icon-bug.png" title="icon-bug.png" alt="images/icon-bug.png"/></a></p>
  93. `,
  94. // Guard wiki sidebar: special syntax
  95. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  96. // rendered
  97. `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  98. `,
  99. // special syntax
  100. `[[Name|Link]]`,
  101. // rendered
  102. `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
  103. `,
  104. // empty
  105. ``,
  106. // rendered
  107. ``,
  108. }
  109. for i := 0; i < len(testCases); i += 2 {
  110. options.Text = testCases[i]
  111. web.SetForm(ctx, &options)
  112. Markdown(ctx)
  113. assert.Equal(t, testCases[i+1], resp.Body.String())
  114. resp.Body.Reset()
  115. }
  116. }
  117. var simpleCases = []string{
  118. // Guard wiki sidebar: special syntax
  119. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  120. // rendered
  121. `<p>[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]</p>
  122. `,
  123. // special syntax
  124. `[[Name|Link]]`,
  125. // rendered
  126. `<p>[[Name|Link]]</p>
  127. `,
  128. // empty
  129. ``,
  130. // rendered
  131. ``,
  132. }
  133. func TestAPI_RenderSimple(t *testing.T) {
  134. setting.AppURL = AppURL
  135. options := api.MarkdownOption{
  136. Mode: "markdown",
  137. Text: "",
  138. Context: Repo,
  139. }
  140. requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
  141. req := &http.Request{
  142. Method: "POST",
  143. URL: requrl,
  144. }
  145. m, resp := createContext(req)
  146. ctx := wrap(m)
  147. for i := 0; i < len(simpleCases); i += 2 {
  148. options.Text = simpleCases[i]
  149. web.SetForm(ctx, &options)
  150. Markdown(ctx)
  151. assert.Equal(t, simpleCases[i+1], resp.Body.String())
  152. resp.Body.Reset()
  153. }
  154. }
  155. func TestAPI_RenderRaw(t *testing.T) {
  156. setting.AppURL = AppURL
  157. requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
  158. req := &http.Request{
  159. Method: "POST",
  160. URL: requrl,
  161. }
  162. m, resp := createContext(req)
  163. ctx := wrap(m)
  164. for i := 0; i < len(simpleCases); i += 2 {
  165. ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
  166. MarkdownRaw(ctx)
  167. assert.Equal(t, simpleCases[i+1], resp.Body.String())
  168. resp.Body.Reset()
  169. }
  170. }