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.

utils.go 910B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package test
  4. import (
  5. "net/http"
  6. "net/http/httptest"
  7. "strings"
  8. "code.gitea.io/gitea/modules/json"
  9. )
  10. // RedirectURL returns the redirect URL of a http response.
  11. // It also works for JSONRedirect: `{"redirect": "..."}`
  12. func RedirectURL(resp http.ResponseWriter) string {
  13. loc := resp.Header().Get("Location")
  14. if loc != "" {
  15. return loc
  16. }
  17. if r, ok := resp.(*httptest.ResponseRecorder); ok {
  18. m := map[string]any{}
  19. err := json.Unmarshal(r.Body.Bytes(), &m)
  20. if err == nil {
  21. if loc, ok := m["redirect"].(string); ok {
  22. return loc
  23. }
  24. }
  25. }
  26. return ""
  27. }
  28. func IsNormalPageCompleted(s string) bool {
  29. return strings.Contains(s, `<footer class="page-footer"`) && strings.Contains(s, `</html>`)
  30. }
  31. func MockVariableValue[T any](p *T, v T) (reset func()) {
  32. old := *p
  33. *p = v
  34. return func() { *p = old }
  35. }