code.gitea.io/gitea@v1.22.3/routers/api/v1/misc/markup_test.go (about) 1 // Copyright 2020 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package misc 5 6 import ( 7 go_context "context" 8 "io" 9 "net/http" 10 "path" 11 "strings" 12 "testing" 13 14 "code.gitea.io/gitea/modules/markup" 15 "code.gitea.io/gitea/modules/setting" 16 api "code.gitea.io/gitea/modules/structs" 17 "code.gitea.io/gitea/modules/web" 18 "code.gitea.io/gitea/services/contexttest" 19 20 "github.com/stretchr/testify/assert" 21 ) 22 23 const AppURL = "http://localhost:3000/" 24 25 func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expectedBody string, expectedCode int) { 26 setting.AppURL = AppURL 27 context := "/gogits/gogs" 28 if !wiki { 29 context += path.Join("/src/branch/main", path.Dir(filePath)) 30 } 31 options := api.MarkupOption{ 32 Mode: mode, 33 Text: text, 34 Context: context, 35 Wiki: wiki, 36 FilePath: filePath, 37 } 38 ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markup") 39 web.SetForm(ctx, &options) 40 Markup(ctx) 41 assert.Equal(t, expectedBody, resp.Body.String()) 42 assert.Equal(t, expectedCode, resp.Code) 43 resp.Body.Reset() 44 } 45 46 func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody string, responseCode int) { 47 setting.AppURL = AppURL 48 context := "/gogits/gogs" 49 if !wiki { 50 context += "/src/branch/main" 51 } 52 options := api.MarkdownOption{ 53 Mode: mode, 54 Text: text, 55 Context: context, 56 Wiki: wiki, 57 } 58 ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown") 59 web.SetForm(ctx, &options) 60 Markdown(ctx) 61 assert.Equal(t, responseBody, resp.Body.String()) 62 assert.Equal(t, responseCode, resp.Code) 63 resp.Body.Reset() 64 } 65 66 func TestAPI_RenderGFM(t *testing.T) { 67 markup.Init(&markup.ProcessorHelper{ 68 IsUsernameMentionable: func(ctx go_context.Context, username string) bool { 69 return username == "r-lyeh" 70 }, 71 }) 72 73 testCasesWiki := []string{ 74 // dear imgui wiki markdown extract: special wiki syntax 75 `Wiki! Enjoy :) 76 - [[Links, Language bindings, Engine bindings|Links]] 77 - [[Tips]] 78 - Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`, 79 // rendered 80 `<p>Wiki! Enjoy :)</p> 81 <ul> 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> 85 </ul> 86 `, 87 // Guard wiki sidebar: special syntax 88 `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`, 89 // rendered 90 `<p><a href="http://localhost:3000/gogits/gogs/wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p> 91 `, 92 // special syntax 93 `[[Name|Link]]`, 94 // rendered 95 `<p><a href="http://localhost:3000/gogits/gogs/wiki/Link" rel="nofollow">Name</a></p> 96 `, 97 // empty 98 ``, 99 // rendered 100 ``, 101 } 102 103 testCasesWikiDocument := []string{ 104 // wine-staging wiki home extract: special wiki syntax, images 105 `## What is Wine Staging? 106 **Wine Staging** on website [wine-staging.com](http://wine-staging.com). 107 108 ## Quick Links 109 Here are some links to the most important topics. You can find the full list of pages at the sidebar. 110 111 [[Configuration]] 112 [[images/icon-bug.png]] 113 `, 114 // rendered 115 `<h2 id="user-content-what-is-wine-staging">What is Wine Staging?</h2> 116 <p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p> 117 <h2 id="user-content-quick-links">Quick Links</h2> 118 <p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</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> 121 `, 122 } 123 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) 132 } 133 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) 140 } 141 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) 161 } 162 163 var simpleCases = []string{ 164 // Guard wiki sidebar: special syntax 165 `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`, 166 // rendered 167 `<p>[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]</p> 168 `, 169 // special syntax 170 `[[Name|Link]]`, 171 // rendered 172 `<p>[[Name|Link]]</p> 173 `, 174 // empty 175 ``, 176 // rendered 177 ``, 178 } 179 180 func TestAPI_RenderSimple(t *testing.T) { 181 setting.AppURL = AppURL 182 options := api.MarkdownOption{ 183 Mode: "markdown", 184 Text: "", 185 Context: "/gogits/gogs", 186 } 187 ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown") 188 for i := 0; i < len(simpleCases); i += 2 { 189 options.Text = simpleCases[i] 190 web.SetForm(ctx, &options) 191 Markdown(ctx) 192 assert.Equal(t, simpleCases[i+1], resp.Body.String()) 193 resp.Body.Reset() 194 } 195 } 196 197 func TestAPI_RenderRaw(t *testing.T) { 198 setting.AppURL = AppURL 199 ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown") 200 for i := 0; i < len(simpleCases); i += 2 { 201 ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i])) 202 MarkdownRaw(ctx) 203 assert.Equal(t, simpleCases[i+1], resp.Body.String()) 204 resp.Body.Reset() 205 } 206 }