@@ -6,9 +6,11 @@ package context
6
6
import (
7
7
"net/http"
8
8
"net/http/httptest"
9
+ "net/url"
9
10
"testing"
10
11
11
12
"code.gitea.io/gitea/modules/setting"
13
+ "code.gitea.io/gitea/modules/test"
12
14
13
15
"github.com/stretchr/testify/assert"
14
16
)
@@ -22,3 +24,28 @@ func TestRemoveSessionCookieHeader(t *testing.T) {
22
24
assert .Len (t , w .Header ().Values ("Set-Cookie" ), 1 )
23
25
assert .Contains (t , "other=bar" , w .Header ().Get ("Set-Cookie" ))
24
26
}
27
+
28
+ func TestRedirectToCurrentSite (t * testing.T ) {
29
+ defer test .MockVariableValue (& setting .AppURL , "http://localhost:3000/sub/" )()
30
+ defer test .MockVariableValue (& setting .AppSubURL , "/sub" )()
31
+ cases := []struct {
32
+ location string
33
+ want string
34
+ }{
35
+ {"/" , "/sub/" },
36
+ {"http://localhost:3000/sub?k=v" , "http://localhost:3000/sub?k=v" },
37
+ {"http://other" , "/sub/" },
38
+ }
39
+ for _ , c := range cases {
40
+ t .Run (c .location , func (t * testing.T ) {
41
+ req := & http.Request {URL : & url.URL {Path : "/" }}
42
+ resp := httptest .NewRecorder ()
43
+ base , baseCleanUp := NewBaseContext (resp , req )
44
+ defer baseCleanUp ()
45
+ ctx := NewWebContext (base , nil , nil )
46
+ ctx .RedirectToCurrentSite (c .location )
47
+ redirect := test .RedirectURL (resp )
48
+ assert .Equal (t , c .want , redirect )
49
+ })
50
+ }
51
+ }
0 commit comments