You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils_test.go 1.9KB

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