code.gitea.io/gitea@v1.21.7/routers/utils/utils_test.go (about) 1 // Copyright 2017 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package utils 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/modules/setting" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestRemoveUsernameParameterSuffix(t *testing.T) { 15 assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar (Foo Bar)")) 16 assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar")) 17 assert.Equal(t, "", RemoveUsernameParameterSuffix("")) 18 } 19 20 func TestIsExternalURL(t *testing.T) { 21 setting.AppURL = "https://try.gitea.io/" 22 type test struct { 23 Expected bool 24 RawURL string 25 } 26 newTest := func(expected bool, rawURL string) test { 27 return test{Expected: expected, RawURL: rawURL} 28 } 29 for _, test := range []test{ 30 newTest(false, 31 "https://try.gitea.io"), 32 newTest(true, 33 "https://example.com/"), 34 newTest(true, 35 "//example.com"), 36 newTest(true, 37 "http://example.com"), 38 newTest(false, 39 "a/"), 40 newTest(false, 41 "https://try.gitea.io/test?param=false"), 42 newTest(false, 43 "test?param=false"), 44 newTest(false, 45 "//try.gitea.io/test?param=false"), 46 newTest(false, 47 "/hey/hey/hey#3244"), 48 newTest(true, 49 "://missing protocol scheme"), 50 } { 51 assert.Equal(t, test.Expected, IsExternalURL(test.RawURL)) 52 } 53 } 54 55 func TestSanitizeFlashErrorString(t *testing.T) { 56 tests := []struct { 57 name string 58 arg string 59 want string 60 }{ 61 { 62 name: "no error", 63 arg: "", 64 want: "", 65 }, 66 { 67 name: "normal error", 68 arg: "can not open file: \"abc.exe\"", 69 want: "can not open file: "abc.exe"", 70 }, 71 { 72 name: "line break error", 73 arg: "some error:\n\nawesome!", 74 want: "some error:<br><br>awesome!", 75 }, 76 } 77 78 for _, tt := range tests { 79 t.Run(tt.name, func(t *testing.T) { 80 if got := SanitizeFlashErrorString(tt.arg); got != tt.want { 81 t.Errorf("SanitizeFlashErrorString() = '%v', want '%v'", got, tt.want) 82 } 83 }) 84 } 85 }