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 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package misc
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. macaron "gopkg.in/macaron.v1"
  7. "net/url"
  8. "io/ioutil"
  9. "strings"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/markdown"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/sdk/gitea"
  14. "github.com/go-macaron/inject"
  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{req},
  25. Resp: macaron.NewResponseWriter(resp),
  26. Render: &macaron.DummyRender{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. }
  47. requrl, _ := url.Parse(markdown.URLJoin(AppURL, "api", "v1", "markdown"))
  48. req := &http.Request{
  49. Method: "POST",
  50. URL: requrl,
  51. }
  52. m, resp := createContext(req)
  53. ctx := wrap(m)
  54. testCases := []string{
  55. // dear imgui wiki markdown extract: special wiki syntax
  56. `Wiki! Enjoy :)
  57. - [[Links, Language bindings, Engine bindings|Links]]
  58. - [[Tips]]
  59. - Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`,
  60. // rendered
  61. `<p>Wiki! Enjoy :)</p>
  62. <ul>
  63. <li><a href="` + AppSubURL + `wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
  64. <li><a href="` + AppSubURL + `wiki/Tips" rel="nofollow">Tips</a></li>
  65. <li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>)<a href="` + AppSubURL + `issues/786" rel="nofollow">#786</a></li>
  66. </ul>
  67. `,
  68. // wine-staging wiki home extract: special wiki syntax, images
  69. `## What is Wine Staging?
  70. **Wine Staging** on website [wine-staging.com](http://wine-staging.com).
  71. ## Quick Links
  72. Here are some links to the most important topics. You can find the full list of pages at the sidebar.
  73. [[Configuration]]
  74. [[images/icon-bug.png]]
  75. `,
  76. // rendered
  77. `<h2>What is Wine Staging?</h2>
  78. <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
  79. <h2>Quick Links</h2>
  80. <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
  81. <p><a href="` + AppSubURL + `wiki/Configuration" rel="nofollow">Configuration</a>
  82. <a href="` + AppSubURL + `wiki/raw/images%2Ficon-bug.png" rel="nofollow"><img src="` + AppSubURL + `wiki/raw/images%2Ficon-bug.png" alt="images/icon-bug.png" title="icon-bug.png"/></a></p>
  83. `,
  84. // Guard wiki sidebar: special syntax
  85. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  86. // rendered
  87. `<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
  88. `,
  89. // special syntax
  90. `[[Name|Link]]`,
  91. // rendered
  92. `<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
  93. `,
  94. // empty
  95. ``,
  96. // rendered
  97. ``,
  98. }
  99. for i := 0; i < len(testCases); i += 2 {
  100. options.Text = testCases[i]
  101. Markdown(ctx, options)
  102. assert.Equal(t, testCases[i+1], resp.Body.String())
  103. resp.Body.Reset()
  104. }
  105. }
  106. var simpleCases = []string{
  107. // Guard wiki sidebar: special syntax
  108. `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
  109. // rendered
  110. `<p>[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]</p>
  111. `,
  112. // special syntax
  113. `[[Name|Link]]`,
  114. // rendered
  115. `<p>[[Name|Link]]</p>
  116. `,
  117. // empty
  118. ``,
  119. // rendered
  120. ``,
  121. }
  122. func TestAPI_RenderSimple(t *testing.T) {
  123. setting.AppURL = AppURL
  124. options := api.MarkdownOption{
  125. Mode: "markdown",
  126. Text: "",
  127. Context: Repo,
  128. }
  129. requrl, _ := url.Parse(markdown.URLJoin(AppURL, "api", "v1", "markdown"))
  130. req := &http.Request{
  131. Method: "POST",
  132. URL: requrl,
  133. }
  134. m, resp := createContext(req)
  135. ctx := wrap(m)
  136. for i := 0; i < len(simpleCases); i += 2 {
  137. options.Text = simpleCases[i]
  138. Markdown(ctx, options)
  139. assert.Equal(t, simpleCases[i+1], resp.Body.String())
  140. resp.Body.Reset()
  141. }
  142. }
  143. func TestAPI_RenderRaw(t *testing.T) {
  144. setting.AppURL = AppURL
  145. requrl, _ := url.Parse(markdown.URLJoin(AppURL, "api", "v1", "markdown"))
  146. req := &http.Request{
  147. Method: "POST",
  148. URL: requrl,
  149. }
  150. m, resp := createContext(req)
  151. ctx := wrap(m)
  152. for i := 0; i < len(simpleCases); i += 2 {
  153. ctx.Req.Request.Body = ioutil.NopCloser(strings.NewReader(simpleCases[i]))
  154. MarkdownRaw(ctx)
  155. assert.Equal(t, simpleCases[i+1], resp.Body.String())
  156. resp.Body.Reset()
  157. }
  158. }