本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

184 lines
4.8KB

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