aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api/v1/misc
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-06-18 15:59:09 +0800
committerGitHub <noreply@github.com>2023-06-18 09:59:09 +0200
commit4e2f1ee58d1aa49f85c3a28a4f96e915f12bdb21 (patch)
treee2751e1c4673f6a051ef04ca156f428a819fdb66 /routers/api/v1/misc
parentfc2115b494e9ba7e4cf7a1440404dce53738b514 (diff)
downloadgitea-4e2f1ee58d1aa49f85c3a28a4f96e915f12bdb21.tar.gz
gitea-4e2f1ee58d1aa49f85c3a28a4f96e915f12bdb21.zip
Refactor web package and context package (#25298)
1. The "web" package shouldn't depends on "modules/context" package, instead, let each "web context" register themselves to the "web" package. 2. The old Init/Free doesn't make sense, so simplify it * The ctx in "Init(ctx)" is never used, and shouldn't be used that way * The "Free" is never called and shouldn't be called because the SSPI instance is shared --------- Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'routers/api/v1/misc')
-rw-r--r--routers/api/v1/misc/markup_test.go58
1 files changed, 7 insertions, 51 deletions
diff --git a/routers/api/v1/misc/markup_test.go b/routers/api/v1/misc/markup_test.go
index fdf540fd65..bab06b3e66 100644
--- a/routers/api/v1/misc/markup_test.go
+++ b/routers/api/v1/misc/markup_test.go
@@ -7,18 +7,14 @@ import (
go_context "context"
"io"
"net/http"
- "net/http/httptest"
- "net/url"
"strings"
"testing"
- "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
- "code.gitea.io/gitea/modules/util"
+ "code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/web"
- "code.gitea.io/gitea/modules/web/middleware"
"github.com/stretchr/testify/assert"
)
@@ -29,34 +25,16 @@ const (
AppSubURL = AppURL + Repo + "/"
)
-func createAPIContext(req *http.Request) (*context.APIContext, *httptest.ResponseRecorder) {
- resp := httptest.NewRecorder()
- base, baseCleanUp := context.NewBaseContext(resp, req)
- base.Data = middleware.ContextData{}
- c := &context.APIContext{Base: base}
- _ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
-
- return c, resp
-}
-
func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, responseCode int) {
setting.AppURL = AppURL
-
options := api.MarkupOption{
Mode: mode,
- Text: "",
+ Text: text,
Context: Repo,
Wiki: true,
FilePath: filePath,
}
- requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markup"))
- req := &http.Request{
- Method: "POST",
- URL: requrl,
- }
- ctx, resp := createAPIContext(req)
-
- options.Text = text
+ ctx, resp := test.MockAPIContext(t, "POST /api/v1/markup")
web.SetForm(ctx, &options)
Markup(ctx)
assert.Equal(t, responseBody, resp.Body.String())
@@ -66,21 +44,13 @@ func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, r
func testRenderMarkdown(t *testing.T, mode, text, responseBody string, responseCode int) {
setting.AppURL = AppURL
-
options := api.MarkdownOption{
Mode: mode,
- Text: "",
+ Text: text,
Context: Repo,
Wiki: true,
}
- requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
- req := &http.Request{
- Method: "POST",
- URL: requrl,
- }
- ctx, resp := createAPIContext(req)
-
- options.Text = text
+ ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
web.SetForm(ctx, &options)
Markdown(ctx)
assert.Equal(t, responseBody, resp.Body.String())
@@ -187,19 +157,12 @@ var simpleCases = []string{
func TestAPI_RenderSimple(t *testing.T) {
setting.AppURL = AppURL
-
options := api.MarkdownOption{
Mode: "markdown",
Text: "",
Context: Repo,
}
- requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
- req := &http.Request{
- Method: "POST",
- URL: requrl,
- }
- ctx, resp := createAPIContext(req)
-
+ ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
for i := 0; i < len(simpleCases); i += 2 {
options.Text = simpleCases[i]
web.SetForm(ctx, &options)
@@ -211,14 +174,7 @@ func TestAPI_RenderSimple(t *testing.T) {
func TestAPI_RenderRaw(t *testing.T) {
setting.AppURL = AppURL
-
- requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
- req := &http.Request{
- Method: "POST",
- URL: requrl,
- }
- ctx, resp := createAPIContext(req)
-
+ ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
for i := 0; i < len(simpleCases); i += 2 {
ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
MarkdownRaw(ctx)