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