Skip to content

Commit ce89dab

Browse files
committed
test
1 parent 94eba1b commit ce89dab

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

modules/httplib/url_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package httplib
55

66
import (
77
"context"
8+
"net/http"
89
"testing"
910

1011
"code.gitea.io/gitea/modules/setting"
@@ -38,6 +39,39 @@ func TestIsRelativeURL(t *testing.T) {
3839
}
3940
}
4041

42+
func TestMakeAbsoluteURL(t *testing.T) {
43+
defer test.MockVariableValue(&setting.AppURL, "http://the-host/sub/")()
44+
defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
45+
46+
ctx := context.Background()
47+
assert.Equal(t, "http://the-host/sub/", MakeAbsoluteURL(ctx, ""))
48+
assert.Equal(t, "http://the-host/sub/foo", MakeAbsoluteURL(ctx, "foo"))
49+
assert.Equal(t, "http://the-host/sub/foo", MakeAbsoluteURL(ctx, "/foo"))
50+
assert.Equal(t, "http://other/foo", MakeAbsoluteURL(ctx, "http://other/foo"))
51+
52+
ctx = context.WithValue(ctx, HttpRequestContextKey, &http.Request{
53+
Host: "user-host",
54+
})
55+
assert.Equal(t, "http://user-host/sub/foo", MakeAbsoluteURL(ctx, "/foo"))
56+
57+
ctx = context.WithValue(ctx, HttpRequestContextKey, &http.Request{
58+
Host: "user-host",
59+
Header: map[string][]string{
60+
"X-Forwarded-Host": {"forwarded-host"},
61+
},
62+
})
63+
assert.Equal(t, "http://forwarded-host/sub/foo", MakeAbsoluteURL(ctx, "/foo"))
64+
65+
ctx = context.WithValue(ctx, HttpRequestContextKey, &http.Request{
66+
Host: "user-host",
67+
Header: map[string][]string{
68+
"X-Forwarded-Host": {"forwarded-host"},
69+
"X-Forwarded-Proto": {"https"},
70+
},
71+
})
72+
assert.Equal(t, "https://forwarded-host/sub/foo", MakeAbsoluteURL(ctx, "/foo"))
73+
}
74+
4175
func TestIsCurrentGiteaSiteURL(t *testing.T) {
4276
defer test.MockVariableValue(&setting.AppURL, "http://localhost:3000/sub/")()
4377
defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
@@ -75,4 +109,14 @@ func TestIsCurrentGiteaSiteURL(t *testing.T) {
75109
assert.False(t, IsCurrentGiteaSiteURL(ctx, "\\\\"))
76110
assert.False(t, IsCurrentGiteaSiteURL(ctx, "http://localhost"))
77111
assert.True(t, IsCurrentGiteaSiteURL(ctx, "http://localhost:3000?key=val"))
112+
113+
ctx = context.WithValue(ctx, HttpRequestContextKey, &http.Request{
114+
Host: "user-host",
115+
Header: map[string][]string{
116+
"X-Forwarded-Host": {"forwarded-host"},
117+
"X-Forwarded-Proto": {"https"},
118+
},
119+
})
120+
assert.True(t, IsCurrentGiteaSiteURL(ctx, "http://localhost:3000"))
121+
assert.True(t, IsCurrentGiteaSiteURL(ctx, "https://forwarded-host"))
78122
}

0 commit comments

Comments
 (0)