gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package context
  4. import (
  5. "net/http"
  6. "net/http/httptest"
  7. "net/url"
  8. "testing"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/test"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestRemoveSessionCookieHeader(t *testing.T) {
  14. w := httptest.NewRecorder()
  15. w.Header().Add("Set-Cookie", (&http.Cookie{Name: setting.SessionConfig.CookieName, Value: "foo"}).String())
  16. w.Header().Add("Set-Cookie", (&http.Cookie{Name: "other", Value: "bar"}).String())
  17. assert.Len(t, w.Header().Values("Set-Cookie"), 2)
  18. removeSessionCookieHeader(w)
  19. assert.Len(t, w.Header().Values("Set-Cookie"), 1)
  20. assert.Contains(t, "other=bar", w.Header().Get("Set-Cookie"))
  21. }
  22. func TestRedirectToCurrentSite(t *testing.T) {
  23. setting.IsInTesting = true
  24. defer test.MockVariableValue(&setting.AppURL, "http://localhost:3000/sub/")()
  25. defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
  26. cases := []struct {
  27. location string
  28. want string
  29. }{
  30. {"/", "/sub/"},
  31. {"http://localhost:3000/sub?k=v", "http://localhost:3000/sub?k=v"},
  32. {"http://other", "/sub/"},
  33. }
  34. for _, c := range cases {
  35. t.Run(c.location, func(t *testing.T) {
  36. req := &http.Request{URL: &url.URL{Path: "/"}}
  37. resp := httptest.NewRecorder()
  38. base := NewBaseContextForTest(resp, req)
  39. ctx := NewWebContext(base, nil, nil)
  40. ctx.RedirectToCurrentSite(c.location)
  41. redirect := test.RedirectURL(resp)
  42. assert.Equal(t, c.want, redirect)
  43. })
  44. }
  45. }