Skip to content

Commit dc506f2

Browse files
committed
fix tests
1 parent e9ea089 commit dc506f2

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

routers/api/v1/misc/markup_test.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@ import (
1919
"github.com/stretchr/testify/assert"
2020
)
2121

22-
const (
23-
AppURL = "http://localhost:3000/"
24-
Repo = "gogits/gogs"
25-
Branch = "main"
26-
FullURL = AppURL + Repo + "/"
27-
)
22+
const AppURL = "http://localhost:3000/"
2823

29-
func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, responseBody string, responseCode int) {
24+
func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expectedBody string, expectedCode int) {
25+
t.Helper()
3026
setting.AppURL = AppURL
31-
context := Repo
27+
context := "/gogits/gogs"
3228
if !wiki {
33-
context += "/src/branch/" + Branch
29+
context += "/src/branch/main"
3430
}
3531
options := api.MarkupOption{
3632
Mode: mode,
@@ -42,16 +38,17 @@ func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, resp
4238
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markup")
4339
web.SetForm(ctx, &options)
4440
Markup(ctx)
45-
assert.Equal(t, responseBody, resp.Body.String())
46-
assert.Equal(t, responseCode, resp.Code)
41+
assert.Equal(t, expectedBody, resp.Body.String())
42+
assert.Equal(t, expectedCode, resp.Code)
4743
resp.Body.Reset()
4844
}
4945

5046
func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody string, responseCode int) {
47+
t.Helper()
5148
setting.AppURL = AppURL
52-
context := Repo
49+
context := "/gogits/gogs"
5350
if !wiki {
54-
context += "/src/branch/" + Branch
51+
context += "/src/branch/main"
5552
}
5653
options := api.MarkdownOption{
5754
Mode: mode,
@@ -83,20 +80,20 @@ func TestAPI_RenderGFM(t *testing.T) {
8380
// rendered
8481
`<p>Wiki! Enjoy :)</p>
8582
<ul>
86-
<li><a href="` + FullURL + `wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
87-
<li><a href="` + FullURL + `wiki/Tips" rel="nofollow">Tips</a></li>
88-
<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>
83+
<li><a href="http://localhost:3000/gogits/gogs/wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
84+
<li><a href="http://localhost:3000/gogits/gogs/wiki/Tips" rel="nofollow">Tips</a></li>
85+
<li>Bezier widget (by <a href="http://localhost:3000/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>
8986
</ul>
9087
`,
9188
// Guard wiki sidebar: special syntax
9289
`[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
9390
// rendered
94-
`<p><a href="` + FullURL + `wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
91+
`<p><a href="http://localhost:3000/gogits/gogs/wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
9592
`,
9693
// special syntax
9794
`[[Name|Link]]`,
9895
// rendered
99-
`<p><a href="` + FullURL + `wiki/Link" rel="nofollow">Name</a></p>
96+
`<p><a href="http://localhost:3000/gogits/gogs/wiki/Link" rel="nofollow">Name</a></p>
10097
`,
10198
// empty
10299
``,
@@ -120,8 +117,8 @@ Here are some links to the most important topics. You can find the full list of
120117
<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
121118
<h2 id="user-content-quick-links">Quick Links</h2>
122119
<p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
123-
<p><a href="` + FullURL + `wiki/Configuration" rel="nofollow">Configuration</a>
124-
<a href="` + FullURL + `wiki/raw/images/icon-bug.png" rel="nofollow"><img src="` + FullURL + `wiki/raw/images/icon-bug.png" title="icon-bug.png" alt="images/icon-bug.png"/></a></p>
120+
<p><a href="http://localhost:3000/gogits/gogs/wiki/Configuration" rel="nofollow">Configuration</a>
121+
<a href="http://localhost:3000/gogits/gogs/wiki/raw/images/icon-bug.png" rel="nofollow"><img src="http://localhost:3000/gogits/gogs/wiki/raw/images/icon-bug.png" title="icon-bug.png" alt="images/icon-bug.png"/></a></p>
125122
`,
126123
}
127124

@@ -132,8 +129,8 @@ Here are some links to the most important topics. You can find the full list of
132129
![Image](image.png)`,
133130
// rendered
134131
`<h1 id="user-content-title">Title</h1>
135-
<p><a href="` + FullURL + `src/branch/` + Branch + `/test.md" rel="nofollow">Link</a>
136-
<a href="` + FullURL + `media/branch/` + Branch + `/image.png" target="_blank" rel="nofollow noopener"><img src="` + FullURL + `media/branch/` + Branch + `/image.png" alt="Image"/></a></p>
132+
<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/test.md" rel="nofollow">Link</a>
133+
<a href="http://localhost:3000/gogits/gogs/media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs/media/branch/main/image.png" alt="Image"/></a></p>
137134
`,
138135
}
139136

@@ -189,7 +186,7 @@ func TestAPI_RenderSimple(t *testing.T) {
189186
options := api.MarkdownOption{
190187
Mode: "markdown",
191188
Text: "",
192-
Context: Repo,
189+
Context: "/gogits/gogs",
193190
}
194191
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
195192
for i := 0; i < len(simpleCases); i += 2 {

routers/common/markup.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package common
66

77
import (
8+
"code.gitea.io/gitea/modules/setting"
89
"fmt"
910
"net/http"
1011
"strings"
@@ -13,7 +14,6 @@ import (
1314
"code.gitea.io/gitea/modules/httplib"
1415
"code.gitea.io/gitea/modules/markup"
1516
"code.gitea.io/gitea/modules/markup/markdown"
16-
"code.gitea.io/gitea/modules/setting"
1717
"code.gitea.io/gitea/services/context"
1818
)
1919

@@ -28,9 +28,9 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, pathC
2828

2929
markupType := ""
3030
relativePath := ""
31-
links := markup.Links{
32-
AbsolutePrefix: true,
33-
Base: pathContext, // TODO: this is the legacy logic, but it doesn't seem right, "Base" should also use an absolute URL
31+
links := markup.Links{AbsolutePrefix: true}
32+
if pathContext != "" {
33+
links.Base = fmt.Sprintf("%s%s", httplib.GuessCurrentHostURL(ctx), pathContext)
3434
}
3535

3636
switch mode {
@@ -52,19 +52,20 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, pathC
5252
case "file":
5353
markupType = "" // render the repo file content by its extension
5454
relativePath = filePath
55-
fields := strings.SplitN(strings.TrimPrefix(pathContext, setting.AppSubURL+"/"), "/", 5)
56-
if len(fields) == 5 && fields[2] == "src" && fields[3] == "branch" {
57-
links = markup.Links{
58-
AbsolutePrefix: true,
59-
Base: fmt.Sprintf("%s%s/%s", httplib.GuessCurrentAppURL(ctx), fields[0], fields[1]), // provides "https://host/subpath/{user}/{repo}"
60-
BranchPath: strings.Join(fields[4:], "/"),
61-
}
62-
}
6355
default:
6456
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", mode))
6557
return
6658
}
6759

60+
fields := strings.SplitN(strings.TrimPrefix(pathContext, setting.AppSubURL+"/"), "/", 5)
61+
if len(fields) == 5 && fields[2] == "src" && fields[3] == "branch" {
62+
links = markup.Links{
63+
AbsolutePrefix: true,
64+
Base: fmt.Sprintf("%s%s/%s", httplib.GuessCurrentAppURL(ctx), fields[0], fields[1]), // provides "https://host/subpath/{user}/{repo}"
65+
BranchPath: strings.Join(fields[3:], "/"),
66+
}
67+
}
68+
6869
meta := map[string]string{}
6970
var repoCtx *repo_model.Repository
7071
if repo != nil && repo.Repository != nil {

0 commit comments

Comments
 (0)