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.

79 lines
1.9 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. "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.Domain = "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. }