2017-12-15 22:11:02 +01:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-12-15 22:11:02 +01:00
|
|
|
|
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2024-12-22 16:33:19 +01:00
|
|
|
"fmt"
|
2017-12-15 22:11:02 +01:00
|
|
|
"net/http"
|
2023-06-16 08:32:43 +02:00
|
|
|
"net/http/httptest"
|
2024-12-22 16:33:19 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2023-05-29 17:00:21 +02:00
|
|
|
"strings"
|
2023-06-16 08:32:43 +02:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
2024-12-22 16:33:19 +01:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2017-12-15 22:11:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// RedirectURL returns the redirect URL of a http response.
|
2023-06-16 08:32:43 +02:00
|
|
|
// It also works for JSONRedirect: `{"redirect": "..."}`
|
2017-12-15 22:11:02 +01:00
|
|
|
func RedirectURL(resp http.ResponseWriter) string {
|
2023-06-16 08:32:43 +02:00
|
|
|
loc := resp.Header().Get("Location")
|
|
|
|
if loc != "" {
|
|
|
|
return loc
|
|
|
|
}
|
|
|
|
if r, ok := resp.(*httptest.ResponseRecorder); ok {
|
|
|
|
m := map[string]any{}
|
|
|
|
err := json.Unmarshal(r.Body.Bytes(), &m)
|
|
|
|
if err == nil {
|
|
|
|
if loc, ok := m["redirect"].(string); ok {
|
|
|
|
return loc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
2017-12-15 22:11:02 +01:00
|
|
|
}
|
2023-05-29 17:00:21 +02:00
|
|
|
|
|
|
|
func IsNormalPageCompleted(s string) bool {
|
|
|
|
return strings.Contains(s, `<footer class="page-footer"`) && strings.Contains(s, `</html>`)
|
|
|
|
}
|
2023-08-05 17:36:45 +02:00
|
|
|
|
2024-06-09 10:29:29 +02:00
|
|
|
func MockVariableValue[T any](p *T, v ...T) (reset func()) {
|
2023-08-05 17:36:45 +02:00
|
|
|
old := *p
|
2024-06-09 10:29:29 +02:00
|
|
|
if len(v) > 0 {
|
|
|
|
*p = v[0]
|
|
|
|
}
|
2023-08-05 17:36:45 +02:00
|
|
|
return func() { *p = old }
|
|
|
|
}
|
2024-12-22 16:33:19 +01:00
|
|
|
|
|
|
|
// SetupGiteaRoot Sets GITEA_ROOT if it is not already set and returns the value
|
|
|
|
func SetupGiteaRoot() string {
|
|
|
|
giteaRoot := os.Getenv("GITEA_ROOT")
|
|
|
|
if giteaRoot != "" {
|
|
|
|
return giteaRoot
|
|
|
|
}
|
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
|
|
giteaRoot = filepath.Dir(filepath.Dir(filepath.Dir(filename)))
|
|
|
|
fixturesDir := filepath.Join(giteaRoot, "models", "fixtures")
|
|
|
|
if exist, _ := util.IsDir(fixturesDir); !exist {
|
|
|
|
panic(fmt.Sprintf("fixtures directory not found: %s", fixturesDir))
|
|
|
|
}
|
|
|
|
_ = os.Setenv("GITEA_ROOT", giteaRoot)
|
|
|
|
return giteaRoot
|
|
|
|
}
|