Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

utils_test.go 1.9KB

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