2020-04-04 23:20:50 -07:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-02-13 18:13:59 -07:00
|
|
|
package misc
|
|
|
|
|
|
|
|
import (
|
2021-09-21 22:38:34 -07:00
|
|
|
"io"
|
2017-02-13 18:13:59 -07:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2017-09-16 10:17:57 -07:00
|
|
|
"testing"
|
2017-02-13 18:13:59 -07:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 03:21:34 -07:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-01-26 08:36:53 -07:00
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2018-02-20 05:50:42 -07:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 08:36:53 -07:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2017-09-16 10:17:57 -07:00
|
|
|
|
2017-02-13 18:13:59 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-01-20 10:46:10 -07:00
|
|
|
const (
|
|
|
|
AppURL = "http://localhost:3000/"
|
|
|
|
Repo = "gogits/gogs"
|
|
|
|
AppSubURL = AppURL + Repo + "/"
|
|
|
|
)
|
2017-02-13 18:13:59 -07:00
|
|
|
|
2021-01-26 08:36:53 -07:00
|
|
|
func createContext(req *http.Request) (*context.Context, *httptest.ResponseRecorder) {
|
2022-08-28 02:43:25 -07:00
|
|
|
_, rnd := templates.HTMLRenderer(req.Context())
|
2017-02-13 18:13:59 -07:00
|
|
|
resp := httptest.NewRecorder()
|
2021-01-26 08:36:53 -07:00
|
|
|
c := &context.Context{
|
|
|
|
Req: req,
|
|
|
|
Resp: context.NewResponse(resp),
|
|
|
|
Render: rnd,
|
|
|
|
Data: make(map[string]interface{}),
|
2017-02-13 18:13:59 -07:00
|
|
|
}
|
2022-05-05 07:13:23 -07:00
|
|
|
defer c.Close()
|
|
|
|
|
2017-02-13 18:13:59 -07:00
|
|
|
return c, resp
|
|
|
|
}
|
|
|
|
|
2021-01-26 08:36:53 -07:00
|
|
|
func wrap(ctx *context.Context) *context.APIContext {
|
2017-02-13 18:13:59 -07:00
|
|
|
return &context.APIContext{
|
2021-01-26 08:36:53 -07:00
|
|
|
Context: ctx,
|
2017-02-13 18:13:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_RenderGFM(t *testing.T) {
|
|
|
|
setting.AppURL = AppURL
|
|
|
|
|
|
|
|
options := api.MarkdownOption{
|
|
|
|
Mode: "gfm",
|
|
|
|
Text: "",
|
|
|
|
Context: Repo,
|
2017-02-24 07:59:56 -07:00
|
|
|
Wiki: true,
|
2017-02-13 18:13:59 -07:00
|
|
|
}
|
2018-02-20 05:50:42 -07:00
|
|
|
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
|
2017-02-13 18:13:59 -07:00
|
|
|
req := &http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: requrl,
|
|
|
|
}
|
|
|
|
m, resp := createContext(req)
|
|
|
|
ctx := wrap(m)
|
|
|
|
|
|
|
|
testCases := []string{
|
|
|
|
// 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 00:09:18 -07: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-13 18:13:59 -07:00
|
|
|
</ul>
|
|
|
|
`,
|
|
|
|
// 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 15:38:50 -07:00
|
|
|
`<h2 id="user-content-what-is-wine-staging">What is Wine Staging?</h2>
|
2017-02-13 18:13:59 -07:00
|
|
|
<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
|
2019-12-23 15:38:50 -07:00
|
|
|
<h2 id="user-content-quick-links">Quick Links</h2>
|
2017-02-13 18:13:59 -07: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 01:14:26 -07:00
|
|
|
<p><a href="` + AppSubURL + `wiki/Configuration" rel="nofollow">Configuration</a>
|
2018-02-27 00:09:18 -07: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-13 18:13:59 -07: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
|
|
|
|
``,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(testCases); i += 2 {
|
|
|
|
options.Text = testCases[i]
|
2021-01-26 08:36:53 -07:00
|
|
|
web.SetForm(ctx, &options)
|
|
|
|
Markdown(ctx)
|
2017-02-13 18:13:59 -07:00
|
|
|
assert.Equal(t, testCases[i+1], resp.Body.String())
|
|
|
|
resp.Body.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 05:50:42 -07:00
|
|
|
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
|
2017-02-13 18:13:59 -07:00
|
|
|
req := &http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: requrl,
|
|
|
|
}
|
|
|
|
m, resp := createContext(req)
|
|
|
|
ctx := wrap(m)
|
|
|
|
|
|
|
|
for i := 0; i < len(simpleCases); i += 2 {
|
|
|
|
options.Text = simpleCases[i]
|
2021-01-26 08:36:53 -07:00
|
|
|
web.SetForm(ctx, &options)
|
|
|
|
Markdown(ctx)
|
2017-02-13 18:13:59 -07: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 05:50:42 -07:00
|
|
|
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
|
2017-02-13 18:13:59 -07:00
|
|
|
req := &http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: requrl,
|
|
|
|
}
|
|
|
|
m, resp := createContext(req)
|
|
|
|
ctx := wrap(m)
|
|
|
|
|
|
|
|
for i := 0; i < len(simpleCases); i += 2 {
|
2021-09-21 22:38:34 -07:00
|
|
|
ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
|
2017-02-13 18:13:59 -07:00
|
|
|
MarkdownRaw(ctx)
|
|
|
|
assert.Equal(t, simpleCases[i+1], resp.Body.String())
|
|
|
|
resp.Body.Reset()
|
|
|
|
}
|
|
|
|
}
|