2020-04-05 06:20:50 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-04-05 06:20:50 +00:00
|
|
|
|
2017-02-14 01:13:59 +00:00
|
|
|
package misc
|
|
|
|
|
|
|
|
import (
|
2022-10-22 17:15:52 +00:00
|
|
|
go_context "context"
|
2021-09-22 05:38:34 +00:00
|
|
|
"io"
|
2017-02-14 01:13:59 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2017-09-16 17:17:57 +00:00
|
|
|
"testing"
|
2017-02-14 01:13:59 +00:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-10-22 17:15:52 +00:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2017-02-14 01:13:59 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 10:21:34 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2018-02-20 12:50:42 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 15:36:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2023-05-04 06:36:34 +00:00
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2017-09-16 17:17:57 +00:00
|
|
|
|
2017-02-14 01:13:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-01-20 17:46:10 +00:00
|
|
|
const (
|
|
|
|
AppURL = "http://localhost:3000/"
|
|
|
|
Repo = "gogits/gogs"
|
|
|
|
AppSubURL = AppURL + Repo + "/"
|
|
|
|
)
|
2017-02-14 01:13:59 +00:00
|
|
|
|
2023-05-21 01:50:53 +00:00
|
|
|
func createAPIContext(req *http.Request) (*context.APIContext, *httptest.ResponseRecorder) {
|
2017-02-14 01:13:59 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2023-05-21 01:50:53 +00:00
|
|
|
base, baseCleanUp := context.NewBaseContext(resp, req)
|
|
|
|
base.Data = middleware.ContextData{}
|
|
|
|
c := &context.APIContext{Base: base}
|
|
|
|
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
|
2022-05-05 14:13:23 +00:00
|
|
|
|
2017-02-14 01:13:59 +00:00
|
|
|
return c, resp
|
|
|
|
}
|
|
|
|
|
2023-03-24 06:12:23 +00:00
|
|
|
func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, responseCode int) {
|
|
|
|
setting.AppURL = AppURL
|
|
|
|
|
|
|
|
options := api.MarkupOption{
|
|
|
|
Mode: mode,
|
|
|
|
Text: "",
|
|
|
|
Context: Repo,
|
|
|
|
Wiki: true,
|
|
|
|
FilePath: filePath,
|
|
|
|
}
|
|
|
|
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markup"))
|
|
|
|
req := &http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: requrl,
|
|
|
|
}
|
2023-05-21 01:50:53 +00:00
|
|
|
ctx, resp := createAPIContext(req)
|
2023-03-24 06:12:23 +00:00
|
|
|
|
|
|
|
options.Text = text
|
|
|
|
web.SetForm(ctx, &options)
|
|
|
|
Markup(ctx)
|
|
|
|
assert.Equal(t, responseBody, resp.Body.String())
|
|
|
|
assert.Equal(t, responseCode, resp.Code)
|
|
|
|
resp.Body.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRenderMarkdown(t *testing.T, mode, text, responseBody string, responseCode int) {
|
2017-02-14 01:13:59 +00:00
|
|
|
setting.AppURL = AppURL
|
|
|
|
|
|
|
|
options := api.MarkdownOption{
|
2023-03-24 06:12:23 +00:00
|
|
|
Mode: mode,
|
2017-02-14 01:13:59 +00:00
|
|
|
Text: "",
|
|
|
|
Context: Repo,
|
2017-02-24 14:59:56 +00:00
|
|
|
Wiki: true,
|
2017-02-14 01:13:59 +00:00
|
|
|
}
|
2018-02-20 12:50:42 +00:00
|
|
|
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
|
2017-02-14 01:13:59 +00:00
|
|
|
req := &http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: requrl,
|
|
|
|
}
|
2023-05-21 01:50:53 +00:00
|
|
|
ctx, resp := createAPIContext(req)
|
2017-02-14 01:13:59 +00:00
|
|
|
|
2023-03-24 06:12:23 +00:00
|
|
|
options.Text = text
|
|
|
|
web.SetForm(ctx, &options)
|
|
|
|
Markdown(ctx)
|
|
|
|
assert.Equal(t, responseBody, resp.Body.String())
|
|
|
|
assert.Equal(t, responseCode, resp.Code)
|
|
|
|
resp.Body.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_RenderGFM(t *testing.T) {
|
|
|
|
markup.Init(&markup.ProcessorHelper{
|
|
|
|
IsUsernameMentionable: func(ctx go_context.Context, username string) bool {
|
|
|
|
return username == "r-lyeh"
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
testCasesCommon := []string{
|
2017-02-14 01:13:59 +00:00
|
|
|
// dear imgui wiki markdown extract: special wiki syntax
|
|
|
|
`Wiki! Enjoy :)
|
|
|
|
- [[Links, Language bindings, Engine bindings|Links]]
|
|
|
|
- [[Tips]]
|
|
|
|
- Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`,
|
|
|
|
// rendered
|
|
|
|
`<p>Wiki! Enjoy :)</p>
|
|
|
|
<ul>
|
|
|
|
<li><a href="` + AppSubURL + `wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
|
|
|
|
<li><a href="` + AppSubURL + `wiki/Tips" rel="nofollow">Tips</a></li>
|
2018-02-27 07:09:18 +00:00
|
|
|
<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>
|
2017-02-14 01:13:59 +00:00
|
|
|
</ul>
|
|
|
|
`,
|
2023-03-24 06:12:23 +00:00
|
|
|
// Guard wiki sidebar: special syntax
|
|
|
|
`[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
|
|
|
|
// rendered
|
|
|
|
`<p><a href="` + AppSubURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
|
|
|
|
`,
|
|
|
|
// special syntax
|
|
|
|
`[[Name|Link]]`,
|
|
|
|
// rendered
|
|
|
|
`<p><a href="` + AppSubURL + `wiki/Link" rel="nofollow">Name</a></p>
|
|
|
|
`,
|
|
|
|
// empty
|
|
|
|
``,
|
|
|
|
// rendered
|
|
|
|
``,
|
|
|
|
}
|
|
|
|
|
|
|
|
testCasesDocument := []string{
|
2017-02-14 01:13:59 +00:00
|
|
|
// wine-staging wiki home extract: special wiki syntax, images
|
|
|
|
`## What is Wine Staging?
|
|
|
|
**Wine Staging** on website [wine-staging.com](http://wine-staging.com).
|
|
|
|
|
|
|
|
## Quick Links
|
|
|
|
Here are some links to the most important topics. You can find the full list of pages at the sidebar.
|
|
|
|
|
|
|
|
[[Configuration]]
|
|
|
|
[[images/icon-bug.png]]
|
|
|
|
`,
|
|
|
|
// rendered
|
2019-12-23 22:38:50 +00:00
|
|
|
`<h2 id="user-content-what-is-wine-staging">What is Wine Staging?</h2>
|
2017-02-14 01:13:59 +00:00
|
|
|
<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
|
2019-12-23 22:38:50 +00:00
|
|
|
<h2 id="user-content-quick-links">Quick Links</h2>
|
2017-02-14 01:13:59 +00:00
|
|
|
<p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
|
2020-05-24 08:14:26 +00:00
|
|
|
<p><a href="` + AppSubURL + `wiki/Configuration" rel="nofollow">Configuration</a>
|
2018-02-27 07:09:18 +00:00
|
|
|
<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>
|
2017-02-14 01:13:59 +00:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2023-03-24 06:12:23 +00:00
|
|
|
for i := 0; i < len(testCasesCommon); i += 2 {
|
|
|
|
text := testCasesCommon[i]
|
|
|
|
response := testCasesCommon[i+1]
|
|
|
|
testRenderMarkdown(t, "gfm", text, response, http.StatusOK)
|
|
|
|
testRenderMarkup(t, "gfm", "", text, response, http.StatusOK)
|
|
|
|
testRenderMarkdown(t, "comment", text, response, http.StatusOK)
|
|
|
|
testRenderMarkup(t, "comment", "", text, response, http.StatusOK)
|
|
|
|
testRenderMarkup(t, "file", "path/test.md", text, response, http.StatusOK)
|
2017-02-14 01:13:59 +00:00
|
|
|
}
|
2023-03-24 06:12:23 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(testCasesDocument); i += 2 {
|
|
|
|
text := testCasesDocument[i]
|
|
|
|
response := testCasesDocument[i+1]
|
|
|
|
testRenderMarkdown(t, "gfm", text, response, http.StatusOK)
|
|
|
|
testRenderMarkup(t, "gfm", "", text, response, http.StatusOK)
|
|
|
|
testRenderMarkup(t, "file", "path/test.md", text, response, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
testRenderMarkup(t, "file", "path/test.unknown", "## Test", "Unsupported render extension: .unknown\n", http.StatusUnprocessableEntity)
|
|
|
|
testRenderMarkup(t, "unknown", "", "## Test", "Unknown mode: unknown\n", http.StatusUnprocessableEntity)
|
2017-02-14 01:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var simpleCases = []string{
|
|
|
|
// Guard wiki sidebar: special syntax
|
|
|
|
`[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
|
|
|
|
// rendered
|
|
|
|
`<p>[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]</p>
|
|
|
|
`,
|
|
|
|
// special syntax
|
|
|
|
`[[Name|Link]]`,
|
|
|
|
// rendered
|
|
|
|
`<p>[[Name|Link]]</p>
|
|
|
|
`,
|
|
|
|
// empty
|
|
|
|
``,
|
|
|
|
// rendered
|
|
|
|
``,
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_RenderSimple(t *testing.T) {
|
|
|
|
setting.AppURL = AppURL
|
|
|
|
|
|
|
|
options := api.MarkdownOption{
|
|
|
|
Mode: "markdown",
|
|
|
|
Text: "",
|
|
|
|
Context: Repo,
|
|
|
|
}
|
2018-02-20 12:50:42 +00:00
|
|
|
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
|
2017-02-14 01:13:59 +00:00
|
|
|
req := &http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: requrl,
|
|
|
|
}
|
2023-05-21 01:50:53 +00:00
|
|
|
ctx, resp := createAPIContext(req)
|
2017-02-14 01:13:59 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(simpleCases); i += 2 {
|
|
|
|
options.Text = simpleCases[i]
|
2021-01-26 15:36:53 +00:00
|
|
|
web.SetForm(ctx, &options)
|
|
|
|
Markdown(ctx)
|
2017-02-14 01:13:59 +00:00
|
|
|
assert.Equal(t, simpleCases[i+1], resp.Body.String())
|
|
|
|
resp.Body.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_RenderRaw(t *testing.T) {
|
|
|
|
setting.AppURL = AppURL
|
|
|
|
|
2018-02-20 12:50:42 +00:00
|
|
|
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
|
2017-02-14 01:13:59 +00:00
|
|
|
req := &http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: requrl,
|
|
|
|
}
|
2023-05-21 01:50:53 +00:00
|
|
|
ctx, resp := createAPIContext(req)
|
2017-02-14 01:13:59 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(simpleCases); i += 2 {
|
2021-09-22 05:38:34 +00:00
|
|
|
ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
|
2017-02-14 01:13:59 +00:00
|
|
|
MarkdownRaw(ctx)
|
|
|
|
assert.Equal(t, simpleCases[i+1], resp.Body.String())
|
|
|
|
resp.Body.Reset()
|
|
|
|
}
|
|
|
|
}
|