// Copyright 2020 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package misc 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/templates" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "github.com/stretchr/testify/assert" ) const ( AppURL = "http://localhost:3000/" Repo = "gogits/gogs" AppSubURL = AppURL + Repo + "/" ) func createContext(req *http.Request) (*context.Context, *httptest.ResponseRecorder) { _, rnd := templates.HTMLRenderer(req.Context()) resp := httptest.NewRecorder() c := &context.Context{ Req: req, Resp: context.NewResponse(resp), Render: rnd, Data: make(map[string]interface{}), } defer c.Close() return c, resp } func wrap(ctx *context.Context) *context.APIContext { return &context.APIContext{ Context: ctx, } } func TestAPI_RenderGFM(t *testing.T) { setting.AppURL = AppURL markup.Init(&markup.ProcessorHelper{ IsUsernameMentionable: func(ctx go_context.Context, username string) bool { return username == "r-lyeh" }, }) options := api.MarkdownOption{ Mode: "gfm", Text: "", Context: Repo, Wiki: true, } requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown")) req := &http.Request{ Method: "POST", URL: requrl, } m, resp := createContext(req) ctx := wrap(m) testCases := []string{ // dear imgui wiki markdown extract: special wiki syntax `Wiki! Enjoy :) - [[Links, Language bindings, Engine bindings|Links]] - [[Tips]] - Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`, // rendered `

Wiki! Enjoy :)

`, // wine-staging wiki home extract: special wiki syntax, images `## What is Wine Staging? **Wine Staging** on website [wine-staging.com](http://wine-staging.com). ## Quick Links Here are some links to the most important topics. You can find the full list of pages at the sidebar. [[Configuration]] [[images/icon-bug.png]] `, // rendered `

What is Wine Staging?

Wine Staging on website wine-staging.com.

Here are some links to the most important topics. You can find the full list of pages at the sidebar.

Configuration images/icon-bug.png

`, // Guard wiki sidebar: special syntax `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`, // rendered `

Guardfile-DSL / Configuring-Guard

`, // special syntax `[[Name|Link]]`, // rendered `

Name

`, // empty ``, // rendered ``, } for i := 0; i < len(testCases); i += 2 { options.Text = testCases[i] web.SetForm(ctx, &options) Markdown(ctx) assert.Equal(t, testCases[i+1], resp.Body.String()) resp.Body.Reset() } } var simpleCases = []string{ // Guard wiki sidebar: special syntax `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`, // rendered `

[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]

`, // special syntax `[[Name|Link]]`, // rendered `

[[Name|Link]]

`, // empty ``, // rendered ``, } 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, } m, resp := createContext(req) ctx := wrap(m) for i := 0; i < len(simpleCases); i += 2 { options.Text = simpleCases[i] web.SetForm(ctx, &options) Markdown(ctx) assert.Equal(t, simpleCases[i+1], resp.Body.String()) resp.Body.Reset() } } 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, } m, resp := createContext(req) ctx := wrap(m) for i := 0; i < len(simpleCases); i += 2 { ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i])) MarkdownRaw(ctx) assert.Equal(t, simpleCases[i+1], resp.Body.String()) resp.Body.Reset() } }