@@ -6,9 +6,11 @@ package context
66import (
77 "net/http"
88 "net/http/httptest"
9+ "net/url"
910 "testing"
1011
1112 "code.gitea.io/gitea/modules/setting"
13+ "code.gitea.io/gitea/modules/test"
1214
1315 "github.com/stretchr/testify/assert"
1416)
@@ -22,3 +24,28 @@ func TestRemoveSessionCookieHeader(t *testing.T) {
2224 assert .Len (t , w .Header ().Values ("Set-Cookie" ), 1 )
2325 assert .Contains (t , "other=bar" , w .Header ().Get ("Set-Cookie" ))
2426}
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