@ -7,6 +7,7 @@ import (
go_context "context"
"io"
"net/http"
"path"
"strings"
"testing"
@ -19,36 +20,40 @@ import (
"github.com/stretchr/testify/assert"
)
const (
AppURL = "http://localhost:3000/"
Repo = "gogits/gogs"
FullURL = AppURL + Repo + "/"
)
const AppURL = "http://localhost:3000/"
func testRenderMarkup ( t * testing . T , mode , filePath , text , responseBody string , response Code int ) {
func testRenderMarkup ( t * testing . T , mode string , wiki bool , filePath , text , expectedBody string , expectedCode int ) {
setting . AppURL = AppURL
context := "/gogits/gogs"
if ! wiki {
context += path . Join ( "/src/branch/main" , path . Dir ( filePath ) )
}
options := api . MarkupOption {
Mode : mode ,
Text : text ,
Context : Repo ,
Wiki : true ,
Context : context ,
Wiki : wiki ,
FilePath : filePath ,
}
ctx , resp := contexttest . MockAPIContext ( t , "POST /api/v1/markup" )
web . SetForm ( ctx , & options )
Markup ( ctx )
assert . Equal ( t , response Body, resp . Body . String ( ) )
assert . Equal ( t , response Code, resp . Code )
assert . Equal ( t , expected Body, resp . Body . String ( ) )
assert . Equal ( t , expected Code, resp . Code )
resp . Body . Reset ( )
}
func testRenderMarkdown ( t * testing . T , mode , text , responseBody string , responseCode int ) {
func testRenderMarkdown ( t * testing . T , mode string , wiki bool , text , responseBody string , responseCode int ) {
setting . AppURL = AppURL
context := "/gogits/gogs"
if ! wiki {
context += "/src/branch/main"
}
options := api . MarkdownOption {
Mode : mode ,
Text : text ,
Context : Repo ,
Wiki : true ,
Context : context ,
Wiki : wiki ,
}
ctx , resp := contexttest . MockAPIContext ( t , "POST /api/v1/markdown" )
web . SetForm ( ctx , & options )
@ -65,7 +70,7 @@ func TestAPI_RenderGFM(t *testing.T) {
} ,
} )
testCasesCommon := [ ] string {
testCasesWiki := [ ] string {
// dear imgui wiki markdown extract: special wiki syntax
` Wiki ! Enjoy : )
- [ [ Links , Language bindings , Engine bindings | Links ] ]
@ -74,20 +79,20 @@ func TestAPI_RenderGFM(t *testing.T) {
// rendered
` < p > Wiki ! Enjoy : ) < / p >
< ul >
< li > < a href = "` + FullURL + ` wiki/Links" rel = "nofollow" > Links , Language bindings , Engine bindings < / a > < / li >
< li > < a href = "` + FullURL + ` wiki/Tips" rel = "nofollow" > Tips < / a > < / li >
< 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>
< li > < a href = "http://localhost:3000/gogits/gogs/ wiki/Links" rel = "nofollow" > Links , Language bindings , Engine bindings < / a > < / li >
< li > < a href = "http://localhost:3000/gogits/gogs/ wiki/Tips" rel = "nofollow" > Tips < / a > < / li >
< 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>
< / ul >
` ,
// Guard wiki sidebar: special syntax
` [[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]] ` ,
// rendered
` <p><a href=" ` + FullURL + ` wiki / Guardfile - DSL -- - Configuring - Guard " rel=" nofollow " > Guardfile - DSL / Configuring - Guard < / a > < / p >
` < p > < a href = "http://localhost:3000/gogits/gogs/wiki/Guardfile-DSL---Configuring-Guard" rel = "nofollow "> Guardfile - DSL / Configuring - Guard < / a > < / p >
` ,
// special syntax
` [[Name|Link]] ` ,
// rendered
` <p><a href=" ` + FullURL + ` wiki / Link " rel=" nofollow " > Name < / a > < / p >
` < p > < a href = "http://localhost:3000/gogits/gogs/wiki/Link" rel = "nofollow "> Name < / a > < / p >
` ,
// empty
` ` ,
@ -95,7 +100,7 @@ func TestAPI_RenderGFM(t *testing.T) {
` ` ,
}
testCasesDocument := [ ] string {
testCasesWiki Document := [ ] string {
// wine-staging wiki home extract: special wiki syntax, images
` # # What is Wine Staging ?
* * 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
< p > < strong > Wine Staging < / strong > on website < a href = "http://wine-staging.com" rel = "nofollow" > wine - staging . com < / a > . < / p >
< h2 id = "user-content-quick-links" > Quick Links < / h2 >
< p > Here are some links to the most important topics . You can find the full list of pages at the sidebar . < / p >
< p > < a href = "` + FullURL + ` wiki/Configuration" rel = "nofollow" > Configuration < / a >
< 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 >
< p > < a href = "http://localhost:3000/gogits/gogs/ wiki/Configuration" rel = "nofollow" > Configuration < / a >
< 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 >
` ,
}
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 )
for i := 0 ; i < len ( testCasesWiki ) ; i += 2 {
text := testCasesWiki [ i ]
response := testCasesWiki [ i + 1 ]
testRenderMarkdown ( t , "gfm" , true , text , response , http . StatusOK )
testRenderMarkup ( t , "gfm" , true , "" , text , response , http . StatusOK )
testRenderMarkdown ( t , "comment" , true , text , response , http . StatusOK )
testRenderMarkup ( t , "comment" , true , "" , text , response , http . StatusOK )
testRenderMarkup ( t , "file" , true , "path/test.md" , text , response , http . StatusOK )
}
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 )
for i := 0 ; i < len ( testCasesWiki Document ) ; i += 2 {
text := testCasesWiki Document [ i ]
response := testCasesWiki Document [ i + 1 ]
testRenderMarkdown ( t , "gfm" , true , text , response , http . StatusOK )
testRenderMarkup ( t , "gfm" , true , "" , text , response , http . StatusOK )
testRenderMarkup ( t , "file" , true , "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 )
input := "[Link](test.md)\n![Image](image.png)"
testRenderMarkdown ( t , "gfm" , false , input , ` < p > < a href = "http://localhost:3000/gogits/gogs/src/branch/main/test.md" rel = "nofollow" > Link < / a >
< 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 >
` , http . StatusOK )
testRenderMarkdown ( t , "gfm" , false , input , ` < p > < a href = "http://localhost:3000/gogits/gogs/src/branch/main/test.md" rel = "nofollow" > Link < / a >
< 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 >
` , http . StatusOK )
testRenderMarkup ( t , "gfm" , false , "" , input , ` < p > < a href = "http://localhost:3000/gogits/gogs/src/branch/main/test.md" rel = "nofollow" > Link < / a >
< 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 >
` , http . StatusOK )
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 >
< 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 >
` , http . StatusOK )
testRenderMarkup ( t , "file" , true , "path/test.unknown" , "## Test" , "Unsupported render extension: .unknown\n" , http . StatusUnprocessableEntity )
testRenderMarkup ( t , "unknown" , true , "" , "## Test" , "Unknown mode: unknown\n" , http . StatusUnprocessableEntity )
}
var simpleCases = [ ] string {
@ -160,7 +182,7 @@ func TestAPI_RenderSimple(t *testing.T) {
options := api . MarkdownOption {
Mode : "markdown" ,
Text : "" ,
Context : Repo ,
Context : "/gogits/gogs" ,
}
ctx , resp := contexttest . MockAPIContext ( t , "POST /api/v1/markdown" )
for i := 0 ; i < len ( simpleCases ) ; i += 2 {