7
7
go_context "context"
8
8
"io"
9
9
"net/http"
10
+ "path"
10
11
"strings"
11
12
"testing"
12
13
@@ -19,36 +20,40 @@ import (
19
20
"github.com/stretchr/testify/assert"
20
21
)
21
22
22
- const (
23
- AppURL = "http://localhost:3000/"
24
- Repo = "gogits/gogs"
25
- FullURL = AppURL + Repo + "/"
26
- )
23
+ const AppURL = "http://localhost:3000/"
27
24
28
- func testRenderMarkup (t * testing.T , mode , filePath , text , responseBody string , responseCode int ) {
25
+ func testRenderMarkup (t * testing.T , mode string , wiki bool , filePath , text , expectedBody string , expectedCode int ) {
29
26
setting .AppURL = AppURL
27
+ context := "/gogits/gogs"
28
+ if ! wiki {
29
+ context += path .Join ("/src/branch/main" , path .Dir (filePath ))
30
+ }
30
31
options := api.MarkupOption {
31
32
Mode : mode ,
32
33
Text : text ,
33
- Context : Repo ,
34
- Wiki : true ,
34
+ Context : context ,
35
+ Wiki : wiki ,
35
36
FilePath : filePath ,
36
37
}
37
38
ctx , resp := contexttest .MockAPIContext (t , "POST /api/v1/markup" )
38
39
web .SetForm (ctx , & options )
39
40
Markup (ctx )
40
- assert .Equal (t , responseBody , resp .Body .String ())
41
- assert .Equal (t , responseCode , resp .Code )
41
+ assert .Equal (t , expectedBody , resp .Body .String ())
42
+ assert .Equal (t , expectedCode , resp .Code )
42
43
resp .Body .Reset ()
43
44
}
44
45
45
- func testRenderMarkdown (t * testing.T , mode , text , responseBody string , responseCode int ) {
46
+ func testRenderMarkdown (t * testing.T , mode string , wiki bool , text , responseBody string , responseCode int ) {
46
47
setting .AppURL = AppURL
48
+ context := "/gogits/gogs"
49
+ if ! wiki {
50
+ context += "/src/branch/main"
51
+ }
47
52
options := api.MarkdownOption {
48
53
Mode : mode ,
49
54
Text : text ,
50
- Context : Repo ,
51
- Wiki : true ,
55
+ Context : context ,
56
+ Wiki : wiki ,
52
57
}
53
58
ctx , resp := contexttest .MockAPIContext (t , "POST /api/v1/markdown" )
54
59
web .SetForm (ctx , & options )
@@ -65,7 +70,7 @@ func TestAPI_RenderGFM(t *testing.T) {
65
70
},
66
71
})
67
72
68
- testCasesCommon := []string {
73
+ testCasesWiki := []string {
69
74
// dear imgui wiki markdown extract: special wiki syntax
70
75
`Wiki! Enjoy :)
71
76
- [[Links, Language bindings, Engine bindings|Links]]
@@ -74,28 +79,28 @@ func TestAPI_RenderGFM(t *testing.T) {
74
79
// rendered
75
80
`<p>Wiki! Enjoy :)</p>
76
81
<ul>
77
- <li><a href="` + FullURL + ` wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
78
- <li><a href="` + FullURL + ` wiki/Tips" rel="nofollow">Tips</a></li>
79
- <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>
82
+ <li><a href="http://localhost:3000/gogits/gogs/ wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
83
+ <li><a href="http://localhost:3000/gogits/gogs/ wiki/Tips" rel="nofollow">Tips</a></li>
84
+ <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>
80
85
</ul>
81
86
` ,
82
87
// Guard wiki sidebar: special syntax
83
88
`[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]` ,
84
89
// rendered
85
- `<p><a href="` + FullURL + ` wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
90
+ `<p><a href="http://localhost:3000/gogits/gogs/ wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
86
91
` ,
87
92
// special syntax
88
93
`[[Name|Link]]` ,
89
94
// rendered
90
- `<p><a href="` + FullURL + ` wiki/Link" rel="nofollow">Name</a></p>
95
+ `<p><a href="http://localhost:3000/gogits/gogs/ wiki/Link" rel="nofollow">Name</a></p>
91
96
` ,
92
97
// empty
93
98
`` ,
94
99
// rendered
95
100
`` ,
96
101
}
97
102
98
- testCasesDocument := []string {
103
+ testCasesWikiDocument := []string {
99
104
// wine-staging wiki home extract: special wiki syntax, images
100
105
`## What is Wine Staging?
101
106
**Wine Staging** on website [wine-staging.com](http://wine-staging.com).
@@ -111,31 +116,48 @@ Here are some links to the most important topics. You can find the full list of
111
116
<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
112
117
<h2 id="user-content-quick-links">Quick Links</h2>
113
118
<p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
114
- <p><a href="` + FullURL + ` wiki/Configuration" rel="nofollow">Configuration</a>
115
- <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>
119
+ <p><a href="http://localhost:3000/gogits/gogs/ wiki/Configuration" rel="nofollow">Configuration</a>
120
+ <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>
116
121
` ,
117
122
}
118
123
119
- for i := 0 ; i < len (testCasesCommon ); i += 2 {
120
- text := testCasesCommon [i ]
121
- response := testCasesCommon [i + 1 ]
122
- testRenderMarkdown (t , "gfm" , text , response , http .StatusOK )
123
- testRenderMarkup (t , "gfm" , "" , text , response , http .StatusOK )
124
- testRenderMarkdown (t , "comment" , text , response , http .StatusOK )
125
- testRenderMarkup (t , "comment" , "" , text , response , http .StatusOK )
126
- testRenderMarkup (t , "file" , "path/test.md" , text , response , http .StatusOK )
124
+ for i := 0 ; i < len (testCasesWiki ); i += 2 {
125
+ text := testCasesWiki [i ]
126
+ response := testCasesWiki [i + 1 ]
127
+ testRenderMarkdown (t , "gfm" , true , text , response , http .StatusOK )
128
+ testRenderMarkup (t , "gfm" , true , "" , text , response , http .StatusOK )
129
+ testRenderMarkdown (t , "comment" , true , text , response , http .StatusOK )
130
+ testRenderMarkup (t , "comment" , true , "" , text , response , http .StatusOK )
131
+ testRenderMarkup (t , "file" , true , "path/test.md" , text , response , http .StatusOK )
127
132
}
128
133
129
- for i := 0 ; i < len (testCasesDocument ); i += 2 {
130
- text := testCasesDocument [i ]
131
- response := testCasesDocument [i + 1 ]
132
- testRenderMarkdown (t , "gfm" , text , response , http .StatusOK )
133
- testRenderMarkup (t , "gfm" , "" , text , response , http .StatusOK )
134
- testRenderMarkup (t , "file" , "path/test.md" , text , response , http .StatusOK )
134
+ for i := 0 ; i < len (testCasesWikiDocument ); i += 2 {
135
+ text := testCasesWikiDocument [i ]
136
+ response := testCasesWikiDocument [i + 1 ]
137
+ testRenderMarkdown (t , "gfm" , true , text , response , http .StatusOK )
138
+ testRenderMarkup (t , "gfm" , true , "" , text , response , http .StatusOK )
139
+ testRenderMarkup (t , "file" , true , "path/test.md" , text , response , http .StatusOK )
135
140
}
136
141
137
- testRenderMarkup (t , "file" , "path/test.unknown" , "## Test" , "Unsupported render extension: .unknown\n " , http .StatusUnprocessableEntity )
138
- testRenderMarkup (t , "unknown" , "" , "## Test" , "Unknown mode: unknown\n " , http .StatusUnprocessableEntity )
142
+ input := "[Link](test.md)\n "
143
+ testRenderMarkdown (t , "gfm" , false , input , `<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/test.md" rel="nofollow">Link</a>
144
+ <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>
145
+ ` , http .StatusOK )
146
+
147
+ testRenderMarkdown (t , "gfm" , false , input , `<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/test.md" rel="nofollow">Link</a>
148
+ <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>
149
+ ` , http .StatusOK )
150
+
151
+ testRenderMarkup (t , "gfm" , false , "" , input , `<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/test.md" rel="nofollow">Link</a>
152
+ <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>
153
+ ` , http .StatusOK )
154
+
155
+ testRenderMarkup (t , "file" , false , "path/new-file.md" , input , `<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/path/test.md" rel="nofollow">Link</a>
156
+ <a href="http://localhost:3000/gogits/gogs/media/branch/main/path/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs/media/branch/main/path/image.png" alt="Image"/></a></p>
157
+ ` , http .StatusOK )
158
+
159
+ testRenderMarkup (t , "file" , true , "path/test.unknown" , "## Test" , "Unsupported render extension: .unknown\n " , http .StatusUnprocessableEntity )
160
+ testRenderMarkup (t , "unknown" , true , "" , "## Test" , "Unknown mode: unknown\n " , http .StatusUnprocessableEntity )
139
161
}
140
162
141
163
var simpleCases = []string {
@@ -160,7 +182,7 @@ func TestAPI_RenderSimple(t *testing.T) {
160
182
options := api.MarkdownOption {
161
183
Mode : "markdown" ,
162
184
Text : "" ,
163
- Context : Repo ,
185
+ Context : "/gogits/gogs" ,
164
186
}
165
187
ctx , resp := contexttest .MockAPIContext (t , "POST /api/v1/markdown" )
166
188
for i := 0 ; i < len (simpleCases ); i += 2 {
0 commit comments