// Copyright 2017 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 markup_test import ( "strings" "testing" . "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) var localMetas = map[string]string{ "user": "gogits", "repo": "gogs", } func TestRender_Commits(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL test := func(input, expected string) { buffer := RenderString(".md", input, setting.AppSubURL, localMetas) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579" var commit = util.URLJoin(AppSubURL, "commit", sha) var subtree = util.URLJoin(commit, "src") var tree = strings.Replace(subtree, "/commit/", "/tree/", -1) test(sha, `
`) test(sha[:7], ``) test(sha[:39], ``) test(commit, ``) test(tree, ``) test("commit "+sha, `commit b6dd6210ea
/home/gitea/"+sha+"
") } func TestRender_CrossReferences(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL test := func(input, expected string) { buffer := RenderString("a.md", input, setting.AppSubURL, localMetas) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } test( "gogits/gogs#12345", ``) test( "go-gitea/gitea#12345", ``) test( "/home/gitea/go-gitea/gitea#12345", `/home/gitea/go-gitea/gitea#12345
`) } func TestMisc_IsSameDomain(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579" var commit = util.URLJoin(AppSubURL, "commit", sha) assert.True(t, IsSameDomain(commit)) assert.False(t, IsSameDomain("http://google.com/ncr")) assert.False(t, IsSameDomain("favicon.ico")) } func TestRender_links(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL test := func(input, expected string) { buffer := RenderString("a.md", input, setting.AppSubURL, nil) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } // Text that should be turned into URL test( "https://www.example.com", ``) test( "http://www.example.com", ``) test( "https://example.com", ``) test( "http://example.com", ``) test( "http://foo.com/blah_blah", ``) test( "http://foo.com/blah_blah/", ``) test( "http://www.example.com/wpstyle/?p=364", `http://www.example.com/wpstyle/?p=364
`) test( "https://www.example.com/foo/?bar=baz&inga=42&quux", `https://www.example.com/foo/?bar=baz&inga=42&quux
`) test( "http://142.42.1.1/", ``) test( "https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd", `https://github.com/go-gitea/gitea/?p=aaa/bbb.html#ccc-ddd
`) test( "https://en.wikipedia.org/wiki/URL_(disambiguation)", `https://en.wikipedia.org/wiki/URL_(disambiguation)
`) test( "https://foo_bar.example.com/", ``) test( "https://stackoverflow.com/questions/2896191/what-is-go-used-fore", `https://stackoverflow.com/questions/2896191/what-is-go-used-fore
`) test( "https://username:password@gitea.com", `https://username:password@gitea.com
`) // Test that should *not* be turned into URL test( "www.example.com", `www.example.com
`) test( "example.com", `example.com
`) test( "test.example.com", `test.example.com
`) test( "http://", `http://
`) test( "https://", `https://
`) test( "://", `://
`) test( "www", `www
`) } func TestRender_email(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL test := func(input, expected string) { buffer := RenderString("a.md", input, setting.AppSubURL, nil) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } // Text that should be turned into email link test( "info@gitea.com", ``) test( "(info@gitea.com)", ``) test( "[info@gitea.com]", ``) test( "info@gitea.com.", ``) test( "firstname+lastname@gitea.com", ``) test( "send email to info@gitea.co.uk.", `send email to info@gitea.co.uk.
`) // Test that should *not* be turned into email links test( "\"info@gitea.com\"", `“info@gitea.com”
`) test( "/home/gitea/mailstore/info@gitea/com", `/home/gitea/mailstore/info@gitea/com
`) test( "git@try.gitea.io:go-gitea/gitea.git", `git@try.gitea.io:go-gitea/gitea.git
`) test( "gitea@3", `gitea@3
`) test( "gitea@gmail.c", `gitea@gmail.c
`) test( "email@domain@domain.com", `email@domain@domain.com
`) test( "email@domain..com", `email@domain..com
`) } func TestRender_ShortLinks(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL tree := util.URLJoin(AppSubURL, "src", "master") test := func(input, expected, expectedWiki string) { buffer := markdown.RenderString(input, tree, nil) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, localMetas) assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer)) } rawtree := util.URLJoin(AppSubURL, "raw", "master") url := util.URLJoin(tree, "Link") otherURL := util.URLJoin(tree, "Other-Link") encodedURL := util.URLJoin(tree, "Link%3F") imgurl := util.URLJoin(rawtree, "Link.jpg") otherImgurl := util.URLJoin(rawtree, "Link+Other.jpg") encodedImgurl := util.URLJoin(rawtree, "Link+%23.jpg") notencodedImgurl := util.URLJoin(rawtree, "some", "path", "Link+#.jpg") urlWiki := util.URLJoin(AppSubURL, "wiki", "Link") otherURLWiki := util.URLJoin(AppSubURL, "wiki", "Other-Link") encodedURLWiki := util.URLJoin(AppSubURL, "wiki", "Link%3F") imgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg") otherImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+Other.jpg") encodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+%23.jpg") notencodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "some", "path", "Link+#.jpg") favicon := "http://google.com/favicon.ico" test( "[[Link]]", ``, ``) test( "[[Link.jpg]]", ``, ``) test( "[["+favicon+"]]", ``, ``) test( "[[Name|Link]]", ``, ``) test( "[[Name|Link.jpg]]", ``, ``) test( "[[Name|Link.jpg|alt=AltName]]", ``, ``) test( "[[Name|Link.jpg|title=Title]]", ``, ``) test( "[[Name|Link.jpg|alt=AltName|title=Title]]", ``, ``) test( "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]", ``, ``) test( "[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]", ``, ``) test( "[[Link]] [[Other Link]]", ``, ``) test( "[[Link?]]", ``, ``) test( "[[Link]] [[Other Link]] [[Link?]]", ``, ``) test( "[[Link #.jpg]]", ``, ``) test( "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]", ``, ``) test( "[[some/path/Link #.jpg]]", ``, ``) test( "", ``, ``) }