4
4
package httplib
5
5
6
6
import (
7
+ "context"
8
+ "net/http"
7
9
"testing"
8
10
9
11
"code.gitea.io/gitea/modules/setting"
@@ -37,9 +39,44 @@ func TestIsRelativeURL(t *testing.T) {
37
39
}
38
40
}
39
41
42
+ func TestMakeAbsoluteURL (t * testing.T ) {
43
+ defer test .MockVariableValue (& setting .Protocol , "http" )()
44
+ defer test .MockVariableValue (& setting .AppURL , "http://the-host/sub/" )()
45
+ defer test .MockVariableValue (& setting .AppSubURL , "/sub" )()
46
+
47
+ ctx := context .Background ()
48
+ assert .Equal (t , "http://the-host/sub/" , MakeAbsoluteURL (ctx , "" ))
49
+ assert .Equal (t , "http://the-host/sub/foo" , MakeAbsoluteURL (ctx , "foo" ))
50
+ assert .Equal (t , "http://the-host/sub/foo" , MakeAbsoluteURL (ctx , "/foo" ))
51
+ assert .Equal (t , "http://other/foo" , MakeAbsoluteURL (ctx , "http://other/foo" ))
52
+
53
+ ctx = context .WithValue (ctx , RequestContextKey , & http.Request {
54
+ Host : "user-host" ,
55
+ })
56
+ assert .Equal (t , "http://user-host/sub/foo" , MakeAbsoluteURL (ctx , "/foo" ))
57
+
58
+ ctx = context .WithValue (ctx , RequestContextKey , & http.Request {
59
+ Host : "user-host" ,
60
+ Header : map [string ][]string {
61
+ "X-Forwarded-Host" : {"forwarded-host" },
62
+ },
63
+ })
64
+ assert .Equal (t , "https://forwarded-host/sub/foo" , MakeAbsoluteURL (ctx , "/foo" ))
65
+
66
+ ctx = context .WithValue (ctx , RequestContextKey , & http.Request {
67
+ Host : "user-host" ,
68
+ Header : map [string ][]string {
69
+ "X-Forwarded-Host" : {"forwarded-host" },
70
+ "X-Forwarded-Proto" : {"https" },
71
+ },
72
+ })
73
+ assert .Equal (t , "https://forwarded-host/sub/foo" , MakeAbsoluteURL (ctx , "/foo" ))
74
+ }
75
+
40
76
func TestIsCurrentGiteaSiteURL (t * testing.T ) {
41
77
defer test .MockVariableValue (& setting .AppURL , "http://localhost:3000/sub/" )()
42
78
defer test .MockVariableValue (& setting .AppSubURL , "/sub" )()
79
+ ctx := context .Background ()
43
80
good := []string {
44
81
"?key=val" ,
45
82
"/sub" ,
@@ -50,7 +87,7 @@ func TestIsCurrentGiteaSiteURL(t *testing.T) {
50
87
"http://localhost:3000/sub/" ,
51
88
}
52
89
for _ , s := range good {
53
- assert .True (t , IsCurrentGiteaSiteURL (s ), "good = %q" , s )
90
+ assert .True (t , IsCurrentGiteaSiteURL (ctx , s ), "good = %q" , s )
54
91
}
55
92
bad := []string {
56
93
"." ,
@@ -64,13 +101,23 @@ func TestIsCurrentGiteaSiteURL(t *testing.T) {
64
101
"http://other/" ,
65
102
}
66
103
for _ , s := range bad {
67
- assert .False (t , IsCurrentGiteaSiteURL (s ), "bad = %q" , s )
104
+ assert .False (t , IsCurrentGiteaSiteURL (ctx , s ), "bad = %q" , s )
68
105
}
69
106
70
107
setting .AppURL = "http://localhost:3000/"
71
108
setting .AppSubURL = ""
72
- assert .False (t , IsCurrentGiteaSiteURL ("//" ))
73
- assert .False (t , IsCurrentGiteaSiteURL ("\\ \\ " ))
74
- assert .False (t , IsCurrentGiteaSiteURL ("http://localhost" ))
75
- assert .True (t , IsCurrentGiteaSiteURL ("http://localhost:3000?key=val" ))
109
+ assert .False (t , IsCurrentGiteaSiteURL (ctx , "//" ))
110
+ assert .False (t , IsCurrentGiteaSiteURL (ctx , "\\ \\ " ))
111
+ assert .False (t , IsCurrentGiteaSiteURL (ctx , "http://localhost" ))
112
+ assert .True (t , IsCurrentGiteaSiteURL (ctx , "http://localhost:3000?key=val" ))
113
+
114
+ ctx = context .WithValue (ctx , RequestContextKey , & http.Request {
115
+ Host : "user-host" ,
116
+ Header : map [string ][]string {
117
+ "X-Forwarded-Host" : {"forwarded-host" },
118
+ "X-Forwarded-Proto" : {"https" },
119
+ },
120
+ })
121
+ assert .True (t , IsCurrentGiteaSiteURL (ctx , "http://localhost:3000" ))
122
+ assert .True (t , IsCurrentGiteaSiteURL (ctx , "https://forwarded-host" ))
76
123
}
0 commit comments