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.

182 lines
5.0 KiB

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