本站源代码
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

97 lines
2.1KB

  1. // Copyright 2018 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 util
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/modules/setting"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestURLJoin(t *testing.T) {
  11. type test struct {
  12. Expected string
  13. Base string
  14. Elements []string
  15. }
  16. newTest := func(expected, base string, elements ...string) test {
  17. return test{Expected: expected, Base: base, Elements: elements}
  18. }
  19. for _, test := range []test{
  20. newTest("https://try.gitea.io/a/b/c",
  21. "https://try.gitea.io", "a/b", "c"),
  22. newTest("https://try.gitea.io/a/b/c",
  23. "https://try.gitea.io/", "/a/b/", "/c/"),
  24. newTest("https://try.gitea.io/a/c",
  25. "https://try.gitea.io/", "/a/./b/", "../c/"),
  26. newTest("a/b/c",
  27. "a", "b/c/"),
  28. newTest("a/b/d",
  29. "a/", "b/c/", "/../d/"),
  30. newTest("https://try.gitea.io/a/b/c#d",
  31. "https://try.gitea.io", "a/b", "c#d"),
  32. newTest("/a/b/d",
  33. "/a/", "b/c/", "/../d/"),
  34. newTest("/a/b/c",
  35. "/a", "b/c/"),
  36. newTest("/a/b/c#hash",
  37. "/a", "b/c#hash"),
  38. } {
  39. assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
  40. }
  41. }
  42. func TestIsExternalURL(t *testing.T) {
  43. setting.AppURL = "https://try.gitea.io"
  44. type test struct {
  45. Expected bool
  46. RawURL string
  47. }
  48. newTest := func(expected bool, rawURL string) test {
  49. return test{Expected: expected, RawURL: rawURL}
  50. }
  51. for _, test := range []test{
  52. newTest(false,
  53. "https://try.gitea.io"),
  54. newTest(true,
  55. "https://example.com/"),
  56. newTest(true,
  57. "//example.com"),
  58. newTest(true,
  59. "http://example.com"),
  60. newTest(false,
  61. "a/"),
  62. newTest(false,
  63. "https://try.gitea.io/test?param=false"),
  64. newTest(false,
  65. "test?param=false"),
  66. newTest(false,
  67. "//try.gitea.io/test?param=false"),
  68. newTest(false,
  69. "/hey/hey/hey#3244"),
  70. } {
  71. assert.Equal(t, test.Expected, IsExternalURL(test.RawURL))
  72. }
  73. }
  74. func TestIsEmptyString(t *testing.T) {
  75. cases := []struct {
  76. s string
  77. expected bool
  78. }{
  79. {"", true},
  80. {" ", true},
  81. {" ", true},
  82. {" a", false},
  83. }
  84. for _, v := range cases {
  85. assert.Equal(t, v.expected, IsEmptyString(v.s))
  86. }
  87. }
上海开阖软件有限公司 沪ICP备12045867号-1