77 go_context "context"
88 "io"
99 "net/http"
10+ "path"
1011 "strings"
1112 "testing"
1213
@@ -19,36 +20,40 @@ import (
1920 "github.com/stretchr/testify/assert"
2021)
2122
22- const (
23- AppURL = "http://localhost:3000/"
24- Repo = "gogits/gogs"
25- FullURL = AppURL + Repo + "/"
26- )
23+ const AppURL = "http://localhost:3000/"
2724
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 ) {
2926 setting .AppURL = AppURL
27+ context := "/gogits/gogs"
28+ if ! wiki {
29+ context += path .Join ("/src/branch/main" , path .Dir (filePath ))
30+ }
3031 options := api.MarkupOption {
3132 Mode : mode ,
3233 Text : text ,
33- Context : Repo ,
34- Wiki : true ,
34+ Context : context ,
35+ Wiki : wiki ,
3536 FilePath : filePath ,
3637 }
3738 ctx , resp := contexttest .MockAPIContext (t , "POST /api/v1/markup" )
3839 web .SetForm (ctx , & options )
3940 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 )
4243 resp .Body .Reset ()
4344}
4445
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 ) {
4647 setting .AppURL = AppURL
48+ context := "/gogits/gogs"
49+ if ! wiki {
50+ context += "/src/branch/main"
51+ }
4752 options := api.MarkdownOption {
4853 Mode : mode ,
4954 Text : text ,
50- Context : Repo ,
51- Wiki : true ,
55+ Context : context ,
56+ Wiki : wiki ,
5257 }
5358 ctx , resp := contexttest .MockAPIContext (t , "POST /api/v1/markdown" )
5459 web .SetForm (ctx , & options )
@@ -65,7 +70,7 @@ func TestAPI_RenderGFM(t *testing.T) {
6570 },
6671 })
6772
68- testCasesCommon := []string {
73+ testCasesWiki := []string {
6974 // dear imgui wiki markdown extract: special wiki syntax
7075 `Wiki! Enjoy :)
7176- [[Links, Language bindings, Engine bindings|Links]]
@@ -74,28 +79,28 @@ func TestAPI_RenderGFM(t *testing.T) {
7479 // rendered
7580 `<p>Wiki! Enjoy :)</p>
7681<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>
8085</ul>
8186` ,
8287 // Guard wiki sidebar: special syntax
8388 `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]` ,
8489 // 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>
8691` ,
8792 // special syntax
8893 `[[Name|Link]]` ,
8994 // 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>
9196` ,
9297 // empty
9398 `` ,
9499 // rendered
95100 `` ,
96101 }
97102
98- testCasesDocument := []string {
103+ testCasesWikiDocument := []string {
99104 // wine-staging wiki home extract: special wiki syntax, images
100105 `## What is Wine Staging?
101106**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
111116<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
112117<h2 id="user-content-quick-links">Quick Links</h2>
113118<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>
116121` ,
117122 }
118123
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 )
127132 }
128133
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 )
135140 }
136141
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 )
139161}
140162
141163var simpleCases = []string {
@@ -160,7 +182,7 @@ func TestAPI_RenderSimple(t *testing.T) {
160182 options := api.MarkdownOption {
161183 Mode : "markdown" ,
162184 Text : "" ,
163- Context : Repo ,
185+ Context : "/gogits/gogs" ,
164186 }
165187 ctx , resp := contexttest .MockAPIContext (t , "POST /api/v1/markdown" )
166188 for i := 0 ; i < len (simpleCases ); i += 2 {
0 commit comments