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