@@ -5,6 +5,7 @@ package httplib
5
5
6
6
import (
7
7
"context"
8
+ "net/http"
8
9
"testing"
9
10
10
11
"code.gitea.io/gitea/modules/setting"
@@ -38,6 +39,39 @@ func TestIsRelativeURL(t *testing.T) {
38
39
}
39
40
}
40
41
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
+
41
75
func TestIsCurrentGiteaSiteURL (t * testing.T ) {
42
76
defer test .MockVariableValue (& setting .AppURL , "http://localhost:3000/sub/" )()
43
77
defer test .MockVariableValue (& setting .AppSubURL , "/sub" )()
@@ -75,4 +109,14 @@ func TestIsCurrentGiteaSiteURL(t *testing.T) {
75
109
assert .False (t , IsCurrentGiteaSiteURL (ctx , "\\ \\ " ))
76
110
assert .False (t , IsCurrentGiteaSiteURL (ctx , "http://localhost" ))
77
111
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" ))
78
122
}
0 commit comments