You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context_test.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. defer test.MockVariableValue(&setting.AppURL, "http://localhost:3000/sub/")()
  24. defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
  25. cases := []struct {
  26. location string
  27. want string
  28. }{
  29. {"/", "/sub/"},
  30. {"http://localhost:3000/sub?k=v", "http://localhost:3000/sub?k=v"},
  31. {"http://other", "/sub/"},
  32. }
  33. for _, c := range cases {
  34. t.Run(c.location, func(t *testing.T) {
  35. req := &http.Request{URL: &url.URL{Path: "/"}}
  36. resp := httptest.NewRecorder()
  37. base, baseCleanUp := NewBaseContext(resp, req)
  38. defer baseCleanUp()
  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. }