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.

155 lines
3.6 KiB

  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. "strings"
  7. "testing"
  8. "code.gitea.io/gitea/modules/setting"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestURLJoin(t *testing.T) {
  12. type test struct {
  13. Expected string
  14. Base string
  15. Elements []string
  16. }
  17. newTest := func(expected, base string, elements ...string) test {
  18. return test{Expected: expected, Base: base, Elements: elements}
  19. }
  20. for _, test := range []test{
  21. newTest("https://try.gitea.io/a/b/c",
  22. "https://try.gitea.io", "a/b", "c"),
  23. newTest("https://try.gitea.io/a/b/c",
  24. "https://try.gitea.io/", "/a/b/", "/c/"),
  25. newTest("https://try.gitea.io/a/c",
  26. "https://try.gitea.io/", "/a/./b/", "../c/"),
  27. newTest("a/b/c",
  28. "a", "b/c/"),
  29. newTest("a/b/d",
  30. "a/", "b/c/", "/../d/"),
  31. newTest("https://try.gitea.io/a/b/c#d",
  32. "https://try.gitea.io", "a/b", "c#d"),
  33. newTest("/a/b/d",
  34. "/a/", "b/c/", "/../d/"),
  35. newTest("/a/b/c",
  36. "/a", "b/c/"),
  37. newTest("/a/b/c#hash",
  38. "/a", "b/c#hash"),
  39. } {
  40. assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
  41. }
  42. }
  43. func TestIsExternalURL(t *testing.T) {
  44. setting.AppURL = "https://try.gitea.io"
  45. type test struct {
  46. Expected bool
  47. RawURL string
  48. }
  49. newTest := func(expected bool, rawURL string) test {
  50. return test{Expected: expected, RawURL: rawURL}
  51. }
  52. for _, test := range []test{
  53. newTest(false,
  54. "https://try.gitea.io"),
  55. newTest(true,
  56. "https://example.com/"),
  57. newTest(true,
  58. "//example.com"),
  59. newTest(true,
  60. "http://example.com"),
  61. newTest(false,
  62. "a/"),
  63. newTest(false,
  64. "https://try.gitea.io/test?param=false"),
  65. newTest(false,
  66. "test?param=false"),
  67. newTest(false,
  68. "//try.gitea.io/test?param=false"),
  69. newTest(false,
  70. "/hey/hey/hey#3244"),
  71. } {
  72. assert.Equal(t, test.Expected, IsExternalURL(test.RawURL))
  73. }
  74. }
  75. func TestIsEmptyString(t *testing.T) {
  76. cases := []struct {
  77. s string
  78. expected bool
  79. }{
  80. {"", true},
  81. {" ", true},
  82. {" ", true},
  83. {" a", false},
  84. }
  85. for _, v := range cases {
  86. assert.Equal(t, v.expected, IsEmptyString(v.s))
  87. }
  88. }
  89. func Test_NormalizeEOL(t *testing.T) {
  90. data1 := []string{
  91. "",
  92. "This text starts with empty lines",
  93. "another",
  94. "",
  95. "",
  96. "",
  97. "Some other empty lines in the middle",
  98. "more.",
  99. "And more.",
  100. "Ends with empty lines too.",
  101. "",
  102. "",
  103. "",
  104. }
  105. data2 := []string{
  106. "This text does not start with empty lines",
  107. "another",
  108. "",
  109. "",
  110. "",
  111. "Some other empty lines in the middle",
  112. "more.",
  113. "And more.",
  114. "Ends without EOLtoo.",
  115. }
  116. buildEOLData := func(data []string, eol string) []byte {
  117. return []byte(strings.Join(data, eol))
  118. }
  119. dos := buildEOLData(data1, "\r\n")
  120. unix := buildEOLData(data1, "\n")
  121. mac := buildEOLData(data1, "\r")
  122. assert.Equal(t, unix, NormalizeEOL(dos))
  123. assert.Equal(t, unix, NormalizeEOL(mac))
  124. assert.Equal(t, unix, NormalizeEOL(unix))
  125. dos = buildEOLData(data2, "\r\n")
  126. unix = buildEOLData(data2, "\n")
  127. mac = buildEOLData(data2, "\r")
  128. assert.Equal(t, unix, NormalizeEOL(dos))
  129. assert.Equal(t, unix, NormalizeEOL(mac))
  130. assert.Equal(t, unix, NormalizeEOL(unix))
  131. assert.Equal(t, []byte("one liner"), NormalizeEOL([]byte("one liner")))
  132. assert.Equal(t, []byte("\n"), NormalizeEOL([]byte("\n")))
  133. assert.Equal(t, []byte("\ntwo liner"), NormalizeEOL([]byte("\ntwo liner")))
  134. assert.Equal(t, []byte("two liner\n"), NormalizeEOL([]byte("two liner\n")))
  135. assert.Equal(t, []byte{}, NormalizeEOL([]byte{}))
  136. assert.Equal(t, []byte("mix\nand\nmatch\n."), NormalizeEOL([]byte("mix\r\nand\rmatch\n.")))
  137. }