fix issue missing build-in variable Request for template parsing of ghttp.Response

This commit is contained in:
John Guo
2021-01-02 01:53:36 +08:00
parent 2a9c20bfa2
commit 6c08d5fd81
2 changed files with 22 additions and 0 deletions

View File

@ -84,6 +84,7 @@ func (r *Response) buildInVars(params ...map[string]interface{}) map[string]inte
gutil.MapMerge(m, map[string]interface{}{
"Form": r.Request.GetFormMap(),
"Query": r.Request.GetQueryMap(),
"Request": r.Request.GetMap(),
"Cookie": r.Request.Cookie.Map(),
"Session": r.Request.Session.Map(),
})

View File

@ -139,6 +139,27 @@ func Test_Template_Layout2(t *testing.T) {
})
}
func Test_Template_BuildInVarRequest(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p, _ := ports.PopRand()
s := g.Server(p)
s.BindHandler("/:table/test", func(r *ghttp.Request) {
err := r.Response.WriteTplContent("{{.Request.table}}")
t.Assert(err, nil)
})
s.SetDumpRouterMap(false)
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
t.Assert(client.GetContent("/user/test"), "user")
t.Assert(client.GetContent("/order/test"), "order")
})
}
func Test_Template_XSS(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
v := gview.New()