2023-06-16 00:32:43 -06:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package httplib
|
|
|
|
|
|
|
|
import (
|
2024-05-08 07:34:43 -06:00
|
|
|
"context"
|
|
|
|
"net/http"
|
2023-06-16 00:32:43 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-03-21 06:02:34 -06:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2023-06-16 00:32:43 -06:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-03-21 06:02:34 -06: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 00:32:43 -06:00
|
|
|
|
2024-05-08 07:34:43 -06:00
|
|
|
func TestMakeAbsoluteURL(t *testing.T) {
|
|
|
|
defer test.MockVariableValue(&setting.Protocol, "http")()
|
2024-05-19 09:22:54 -06:00
|
|
|
defer test.MockVariableValue(&setting.AppURL, "http://cfg-host/sub/")()
|
2024-05-08 07:34:43 -06:00
|
|
|
defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2024-05-19 09:22:54 -06:00
|
|
|
assert.Equal(t, "http://cfg-host/sub/", MakeAbsoluteURL(ctx, ""))
|
2024-06-14 21:44:44 -06:00
|
|
|
assert.Equal(t, "http://cfg-host/foo", MakeAbsoluteURL(ctx, "foo"))
|
|
|
|
assert.Equal(t, "http://cfg-host/foo", MakeAbsoluteURL(ctx, "/foo"))
|
2024-05-08 07:34:43 -06:00
|
|
|
assert.Equal(t, "http://other/foo", MakeAbsoluteURL(ctx, "http://other/foo"))
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, RequestContextKey, &http.Request{
|
|
|
|
Host: "user-host",
|
|
|
|
})
|
2024-06-14 21:44:44 -06:00
|
|
|
assert.Equal(t, "http://cfg-host/foo", MakeAbsoluteURL(ctx, "/foo"))
|
2024-05-08 07:34:43 -06:00
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, RequestContextKey, &http.Request{
|
|
|
|
Host: "user-host",
|
|
|
|
Header: map[string][]string{
|
|
|
|
"X-Forwarded-Host": {"forwarded-host"},
|
|
|
|
},
|
|
|
|
})
|
2024-06-14 21:44:44 -06:00
|
|
|
assert.Equal(t, "http://cfg-host/foo", MakeAbsoluteURL(ctx, "/foo"))
|
2024-05-08 07:34:43 -06:00
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, RequestContextKey, &http.Request{
|
|
|
|
Host: "user-host",
|
|
|
|
Header: map[string][]string{
|
|
|
|
"X-Forwarded-Host": {"forwarded-host"},
|
|
|
|
"X-Forwarded-Proto": {"https"},
|
|
|
|
},
|
|
|
|
})
|
2024-06-14 21:44:44 -06:00
|
|
|
assert.Equal(t, "https://forwarded-host/foo", MakeAbsoluteURL(ctx, "/foo"))
|
2024-05-08 07:34:43 -06:00
|
|
|
}
|
|
|
|
|
2024-03-21 06:02:34 -06:00
|
|
|
func TestIsCurrentGiteaSiteURL(t *testing.T) {
|
|
|
|
defer test.MockVariableValue(&setting.AppURL, "http://localhost:3000/sub/")()
|
|
|
|
defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
|
2024-05-08 07:34:43 -06:00
|
|
|
ctx := context.Background()
|
2024-03-21 06:02:34 -06:00
|
|
|
good := []string{
|
|
|
|
"?key=val",
|
|
|
|
"/sub",
|
|
|
|
"/sub/",
|
|
|
|
"/sub/foo",
|
|
|
|
"/sub/foo/",
|
|
|
|
"http://localhost:3000/sub?key=val",
|
|
|
|
"http://localhost:3000/sub/",
|
2023-06-16 00:32:43 -06:00
|
|
|
}
|
2024-03-21 06:02:34 -06:00
|
|
|
for _, s := range good {
|
2024-05-08 07:34:43 -06:00
|
|
|
assert.True(t, IsCurrentGiteaSiteURL(ctx, s), "good = %q", s)
|
2024-03-21 06:02:34 -06:00
|
|
|
}
|
|
|
|
bad := []string{
|
2024-03-21 14:32:40 -06:00
|
|
|
".",
|
|
|
|
"foo",
|
2024-03-21 06:02:34 -06:00
|
|
|
"/",
|
|
|
|
"//",
|
|
|
|
"\\\\",
|
|
|
|
"/foo",
|
|
|
|
"http://localhost:3000/sub/..",
|
|
|
|
"http://localhost:3000/other",
|
|
|
|
"http://other/",
|
|
|
|
}
|
|
|
|
for _, s := range bad {
|
2024-05-08 07:34:43 -06:00
|
|
|
assert.False(t, IsCurrentGiteaSiteURL(ctx, s), "bad = %q", s)
|
2024-03-21 06:02:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
setting.AppURL = "http://localhost:3000/"
|
|
|
|
setting.AppSubURL = ""
|
2024-05-08 07:34:43 -06:00
|
|
|
assert.False(t, IsCurrentGiteaSiteURL(ctx, "//"))
|
|
|
|
assert.False(t, IsCurrentGiteaSiteURL(ctx, "\\\\"))
|
|
|
|
assert.False(t, IsCurrentGiteaSiteURL(ctx, "http://localhost"))
|
|
|
|
assert.True(t, IsCurrentGiteaSiteURL(ctx, "http://localhost:3000?key=val"))
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, RequestContextKey, &http.Request{
|
|
|
|
Host: "user-host",
|
|
|
|
Header: map[string][]string{
|
|
|
|
"X-Forwarded-Host": {"forwarded-host"},
|
|
|
|
"X-Forwarded-Proto": {"https"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.True(t, IsCurrentGiteaSiteURL(ctx, "http://localhost:3000"))
|
|
|
|
assert.True(t, IsCurrentGiteaSiteURL(ctx, "https://forwarded-host"))
|
2023-06-16 00:32:43 -06:00
|
|
|
}
|