code.gitea.io/gitea@v1.22.3/services/context/context_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package context 5 6 import ( 7 "net/http" 8 "net/http/httptest" 9 "net/url" 10 "testing" 11 12 "code.gitea.io/gitea/modules/setting" 13 "code.gitea.io/gitea/modules/test" 14 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestRemoveSessionCookieHeader(t *testing.T) { 19 w := httptest.NewRecorder() 20 w.Header().Add("Set-Cookie", (&http.Cookie{Name: setting.SessionConfig.CookieName, Value: "foo"}).String()) 21 w.Header().Add("Set-Cookie", (&http.Cookie{Name: "other", Value: "bar"}).String()) 22 assert.Len(t, w.Header().Values("Set-Cookie"), 2) 23 removeSessionCookieHeader(w) 24 assert.Len(t, w.Header().Values("Set-Cookie"), 1) 25 assert.Contains(t, "other=bar", w.Header().Get("Set-Cookie")) 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 }