Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Wiki: true,
  47. }
  48. requrl, _ := url.Parse(markdown.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">#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>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>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" alt="images/icon-bug.png" title="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(markdown.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(markdown.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. }