code.gitea.io/gitea@v1.21.7/routers/web/devtest/devtest.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package devtest
     5  
     6  import (
     7  	"net/http"
     8  	"path"
     9  	"strings"
    10  	"time"
    11  
    12  	"code.gitea.io/gitea/modules/base"
    13  	"code.gitea.io/gitea/modules/context"
    14  	"code.gitea.io/gitea/modules/templates"
    15  )
    16  
    17  // List all devtest templates, they will be used for e2e tests for the UI components
    18  func List(ctx *context.Context) {
    19  	templateNames, err := templates.AssetFS().ListFiles("devtest", true)
    20  	if err != nil {
    21  		ctx.ServerError("AssetFS().ListFiles", err)
    22  		return
    23  	}
    24  	var subNames []string
    25  	for _, tmplName := range templateNames {
    26  		subName := strings.TrimSuffix(tmplName, ".tmpl")
    27  		if subName != "list" {
    28  			subNames = append(subNames, subName)
    29  		}
    30  	}
    31  	ctx.Data["SubNames"] = subNames
    32  	ctx.HTML(http.StatusOK, "devtest/list")
    33  }
    34  
    35  func FetchActionTest(ctx *context.Context) {
    36  	_ = ctx.Req.ParseForm()
    37  	ctx.Flash.Info("fetch-action: " + ctx.Req.Method + " " + ctx.Req.RequestURI + "<br>" +
    38  		"Form: " + ctx.Req.Form.Encode() + "<br>" +
    39  		"PostForm: " + ctx.Req.PostForm.Encode(),
    40  	)
    41  	time.Sleep(2 * time.Second)
    42  	ctx.JSONRedirect("")
    43  }
    44  
    45  func Tmpl(ctx *context.Context) {
    46  	now := time.Now()
    47  	ctx.Data["TimeNow"] = now
    48  	ctx.Data["TimePast5s"] = now.Add(-5 * time.Second)
    49  	ctx.Data["TimeFuture5s"] = now.Add(5 * time.Second)
    50  	ctx.Data["TimePast2m"] = now.Add(-2 * time.Minute)
    51  	ctx.Data["TimeFuture2m"] = now.Add(2 * time.Minute)
    52  	ctx.Data["TimePast1y"] = now.Add(-1 * 366 * 86400 * time.Second)
    53  	ctx.Data["TimeFuture1y"] = now.Add(1 * 366 * 86400 * time.Second)
    54  
    55  	if ctx.Req.Method == "POST" {
    56  		_ = ctx.Req.ParseForm()
    57  		ctx.Flash.Info("form: "+ctx.Req.Method+" "+ctx.Req.RequestURI+"<br>"+
    58  			"Form: "+ctx.Req.Form.Encode()+"<br>"+
    59  			"PostForm: "+ctx.Req.PostForm.Encode(),
    60  			true,
    61  		)
    62  		time.Sleep(2 * time.Second)
    63  	}
    64  
    65  	ctx.HTML(http.StatusOK, base.TplName("devtest"+path.Clean("/"+ctx.Params("sub"))))
    66  }