|
| 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 | + |
| 5 | +package util |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/modules/setting" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestURLJoin(t *testing.T) { |
| 16 | + type test struct { |
| 17 | + Expected string |
| 18 | + Base string |
| 19 | + Elements []string |
| 20 | + } |
| 21 | + newTest := func(expected, base string, elements ...string) test { |
| 22 | + return test{Expected: expected, Base: base, Elements: elements} |
| 23 | + } |
| 24 | + for _, test := range []test{ |
| 25 | + newTest("https://try.gitea.io/a/b/c", |
| 26 | + "https://try.gitea.io", "a/b", "c"), |
| 27 | + newTest("https://try.gitea.io/a/b/c", |
| 28 | + "https://try.gitea.io/", "/a/b/", "/c/"), |
| 29 | + newTest("https://try.gitea.io/a/c", |
| 30 | + "https://try.gitea.io/", "/a/./b/", "../c/"), |
| 31 | + newTest("a/b/c", |
| 32 | + "a", "b/c/"), |
| 33 | + newTest("a/b/d", |
| 34 | + "a/", "b/c/", "/../d/"), |
| 35 | + newTest("https://try.gitea.io/a/b/c#d", |
| 36 | + "https://try.gitea.io", "a/b", "c#d"), |
| 37 | + newTest("/a/b/d", |
| 38 | + "/a/", "b/c/", "/../d/"), |
| 39 | + newTest("/a/b/c", |
| 40 | + "/a", "b/c/"), |
| 41 | + newTest("/a/b/c#hash", |
| 42 | + "/a", "b/c#hash"), |
| 43 | + } { |
| 44 | + assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...)) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestIsExternalURL(t *testing.T) { |
| 49 | + setting.Domain = "try.gitea.io" |
| 50 | + type test struct { |
| 51 | + Expected bool |
| 52 | + RawURL string |
| 53 | + } |
| 54 | + newTest := func(expected bool, rawURL string) test { |
| 55 | + return test{Expected: expected, RawURL: rawURL} |
| 56 | + } |
| 57 | + for _, test := range []test{ |
| 58 | + newTest(false, |
| 59 | + "https://try.gitea.io"), |
| 60 | + newTest(true, |
| 61 | + "https://example.com/"), |
| 62 | + newTest(true, |
| 63 | + "//example.com"), |
| 64 | + newTest(true, |
| 65 | + "http://example.com"), |
| 66 | + newTest(false, |
| 67 | + "a/"), |
| 68 | + newTest(false, |
| 69 | + "https://try.gitea.io/test?param=false"), |
| 70 | + newTest(false, |
| 71 | + "test?param=false"), |
| 72 | + newTest(false, |
| 73 | + "//try.gitea.io/test?param=false"), |
| 74 | + newTest(false, |
| 75 | + "/hey/hey/hey#3244"), |
| 76 | + } { |
| 77 | + assert.Equal(t, test.Expected, IsExternalURL(test.RawURL)) |
| 78 | + } |
| 79 | +} |
0 commit comments