44package httplib
55
66import (
7+ "context"
8+ "net/http"
79 "testing"
810
911 "code.gitea.io/gitea/modules/setting"
@@ -37,9 +39,44 @@ func TestIsRelativeURL(t *testing.T) {
3739 }
3840}
3941
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+
4076func TestIsCurrentGiteaSiteURL (t * testing.T ) {
4177 defer test .MockVariableValue (& setting .AppURL , "http://localhost:3000/sub/" )()
4278 defer test .MockVariableValue (& setting .AppSubURL , "/sub" )()
79+ ctx := context .Background ()
4380 good := []string {
4481 "?key=val" ,
4582 "/sub" ,
@@ -50,7 +87,7 @@ func TestIsCurrentGiteaSiteURL(t *testing.T) {
5087 "http://localhost:3000/sub/" ,
5188 }
5289 for _ , s := range good {
53- assert .True (t , IsCurrentGiteaSiteURL (s ), "good = %q" , s )
90+ assert .True (t , IsCurrentGiteaSiteURL (ctx , s ), "good = %q" , s )
5491 }
5592 bad := []string {
5693 "." ,
@@ -64,13 +101,23 @@ func TestIsCurrentGiteaSiteURL(t *testing.T) {
64101 "http://other/" ,
65102 }
66103 for _ , s := range bad {
67- assert .False (t , IsCurrentGiteaSiteURL (s ), "bad = %q" , s )
104+ assert .False (t , IsCurrentGiteaSiteURL (ctx , s ), "bad = %q" , s )
68105 }
69106
70107 setting .AppURL = "http://localhost:3000/"
71108 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" ))
76123}
0 commit comments