code.gitea.io/gitea@v1.22.3/modules/test/utils.go (about)

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