2023-06-16 14:32:43 +08:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package httplib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-03-21 20:02:34 +08:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2023-06-16 14:32:43 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-03-21 20:02:34 +08:00
|
|
|
func TestIsRelativeURL(t *testing.T) {
|
|
|
|
defer test.MockVariableValue(&setting.AppURL, "http://localhost:3000/sub/")()
|
|
|
|
defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
|
|
|
|
rel := []string{
|
|
|
|
"",
|
|
|
|
"foo",
|
|
|
|
"/",
|
|
|
|
"/foo?k=%20#abc",
|
|
|
|
}
|
|
|
|
for _, s := range rel {
|
|
|
|
assert.True(t, IsRelativeURL(s), "rel = %q", s)
|
|
|
|
}
|
|
|
|
abs := []string{
|
|
|
|
"//",
|
|
|
|
"\\\\",
|
|
|
|
"/\\",
|
|
|
|
"\\/",
|
|
|
|
"mailto:a@b.com",
|
|
|
|
"https://test.com",
|
|
|
|
}
|
|
|
|
for _, s := range abs {
|
|
|
|
assert.False(t, IsRelativeURL(s), "abs = %q", s)
|
|
|
|
}
|
|
|
|
}
|
2023-06-16 14:32:43 +08:00
|
|
|
|
2024-03-21 20:02:34 +08:00
|
|
|
func TestIsCurrentGiteaSiteURL(t *testing.T) {
|
|
|
|
defer test.MockVariableValue(&setting.AppURL, "http://localhost:3000/sub/")()
|
|
|
|
defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
|
|
|
|
good := []string{
|
|
|
|
"?key=val",
|
|
|
|
"/sub",
|
|
|
|
"/sub/",
|
|
|
|
"/sub/foo",
|
|
|
|
"/sub/foo/",
|
|
|
|
"http://localhost:3000/sub?key=val",
|
|
|
|
"http://localhost:3000/sub/",
|
2023-06-16 14:32:43 +08:00
|
|
|
}
|
2024-03-21 20:02:34 +08:00
|
|
|
for _, s := range good {
|
|
|
|
assert.True(t, IsCurrentGiteaSiteURL(s), "good = %q", s)
|
|
|
|
}
|
|
|
|
bad := []string{
|
2024-03-22 04:32:40 +08:00
|
|
|
".",
|
|
|
|
"foo",
|
2024-03-21 20:02:34 +08:00
|
|
|
"/",
|
|
|
|
"//",
|
|
|
|
"\\\\",
|
|
|
|
"/foo",
|
|
|
|
"http://localhost:3000/sub/..",
|
|
|
|
"http://localhost:3000/other",
|
|
|
|
"http://other/",
|
|
|
|
}
|
|
|
|
for _, s := range bad {
|
|
|
|
assert.False(t, IsCurrentGiteaSiteURL(s), "bad = %q", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
setting.AppURL = "http://localhost:3000/"
|
|
|
|
setting.AppSubURL = ""
|
2024-03-22 04:32:40 +08:00
|
|
|
assert.False(t, IsCurrentGiteaSiteURL("//"))
|
|
|
|
assert.False(t, IsCurrentGiteaSiteURL("\\\\"))
|
|
|
|
assert.False(t, IsCurrentGiteaSiteURL("http://localhost"))
|
2024-03-21 20:02:34 +08:00
|
|
|
assert.True(t, IsCurrentGiteaSiteURL("http://localhost:3000?key=val"))
|
2023-06-16 14:32:43 +08:00
|
|
|
}
|