2017-12-16 05:11:02 +08:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-12-16 05:11:02 +08:00
|
|
|
|
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-06-16 14:32:43 +08:00
|
|
|
"net/http/httptest"
|
2023-05-29 23:00:21 +08:00
|
|
|
"strings"
|
2023-06-16 14:32:43 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
2017-12-16 05:11:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// RedirectURL returns the redirect URL of a http response.
|
2023-06-16 14:32:43 +08:00
|
|
|
// It also works for JSONRedirect: `{"redirect": "..."}`
|
2017-12-16 05:11:02 +08:00
|
|
|
func RedirectURL(resp http.ResponseWriter) string {
|
2023-06-16 14:32:43 +08: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-16 05:11:02 +08:00
|
|
|
}
|
2023-05-29 23:00:21 +08:00
|
|
|
|
|
|
|
func IsNormalPageCompleted(s string) bool {
|
|
|
|
return strings.Contains(s, `<footer class="page-footer"`) && strings.Contains(s, `</html>`)
|
|
|
|
}
|
2023-08-05 23:36:45 +08:00
|
|
|
|
2024-06-10 12:04:49 +08:00
|
|
|
func MockVariableValue[T any](p *T, v ...T) (reset func()) {
|
2023-08-05 23:36:45 +08:00
|
|
|
old := *p
|
2024-06-10 12:04:49 +08:00
|
|
|
if len(v) > 0 {
|
|
|
|
*p = v[0]
|
|
|
|
}
|
2023-08-05 23:36:45 +08:00
|
|
|
return func() { *p = old }
|
|
|
|
}
|