2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-10-17 20:31:03 +08:00
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
|
|
// static service testing.
|
|
|
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/encoding/ghtml"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gview"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2022-02-22 14:12:09 +08:00
|
|
|
"github.com/gogf/gf/v2/util/guid"
|
2019-10-17 20:31:03 +08:00
|
|
|
)
|
|
|
|
|
|
2020-03-21 13:45:33 +08:00
|
|
|
func Test_Template_Basic(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-03-17 16:58:04 +08:00
|
|
|
v := gview.New(gtest.DataPath("template", "basic"))
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2020-03-21 13:45:33 +08:00
|
|
|
s.SetView(v)
|
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
|
err := r.Response.WriteTpl("index.html", g.Map{
|
|
|
|
|
"name": "john",
|
|
|
|
|
})
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-21 13:45:33 +08:00
|
|
|
})
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
s.Start()
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2020-03-21 13:45:33 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Name:john")
|
|
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Name:john")
|
2020-03-21 13:45:33 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 19:31:58 +08:00
|
|
|
func Test_Template_Encode(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-03-17 16:58:04 +08:00
|
|
|
v := gview.New(gtest.DataPath("template", "basic"))
|
2020-03-21 19:31:58 +08:00
|
|
|
v.SetAutoEncode(true)
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2020-03-21 19:31:58 +08:00
|
|
|
s.SetView(v)
|
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
|
err := r.Response.WriteTpl("index.html", g.Map{
|
|
|
|
|
"name": "john",
|
|
|
|
|
})
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-21 19:31:58 +08:00
|
|
|
})
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
s.Start()
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2020-03-21 19:31:58 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Name:john")
|
|
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Name:john")
|
2020-03-21 19:31:58 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-17 20:31:03 +08:00
|
|
|
func Test_Template_Layout1(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-03-17 16:58:04 +08:00
|
|
|
v := gview.New(gtest.DataPath("template", "layout1"))
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2019-10-17 20:31:03 +08:00
|
|
|
s.SetView(v)
|
|
|
|
|
s.BindHandler("/layout", func(r *ghttp.Request) {
|
|
|
|
|
err := r.Response.WriteTpl("layout.html", g.Map{
|
|
|
|
|
"mainTpl": "main/main1.html",
|
|
|
|
|
})
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-17 20:31:03 +08:00
|
|
|
})
|
|
|
|
|
s.BindHandler("/nil", func(r *ghttp.Request) {
|
|
|
|
|
err := r.Response.WriteTpl("layout.html", nil)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-17 20:31:03 +08:00
|
|
|
})
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-10-17 20:31:03 +08:00
|
|
|
s.Start()
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2019-10-17 20:31:03 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Not Found")
|
|
|
|
|
t.Assert(client.GetContent(ctx, "/layout"), "123")
|
|
|
|
|
t.Assert(client.GetContent(ctx, "/nil"), "123")
|
2019-10-17 20:31:03 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Test_Template_Layout2(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-03-17 16:58:04 +08:00
|
|
|
v := gview.New(gtest.DataPath("template", "layout2"))
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2019-10-17 20:31:03 +08:00
|
|
|
s.SetView(v)
|
|
|
|
|
s.BindHandler("/main1", func(r *ghttp.Request) {
|
|
|
|
|
err := r.Response.WriteTpl("layout.html", g.Map{
|
|
|
|
|
"mainTpl": "main/main1.html",
|
|
|
|
|
})
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-17 20:31:03 +08:00
|
|
|
})
|
|
|
|
|
s.BindHandler("/main2", func(r *ghttp.Request) {
|
|
|
|
|
err := r.Response.WriteTpl("layout.html", g.Map{
|
|
|
|
|
"mainTpl": "main/main2.html",
|
|
|
|
|
})
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-17 20:31:03 +08:00
|
|
|
})
|
|
|
|
|
s.BindHandler("/nil", func(r *ghttp.Request) {
|
|
|
|
|
err := r.Response.WriteTpl("layout.html", nil)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-17 20:31:03 +08:00
|
|
|
})
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-10-17 20:31:03 +08:00
|
|
|
s.Start()
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2019-10-17 20:31:03 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Not Found")
|
|
|
|
|
t.Assert(client.GetContent(ctx, "/main1"), "a1b")
|
|
|
|
|
t.Assert(client.GetContent(ctx, "/main2"), "a2b")
|
|
|
|
|
t.Assert(client.GetContent(ctx, "/nil"), "ab")
|
2019-10-17 20:31:03 +08:00
|
|
|
})
|
|
|
|
|
}
|
2020-02-06 15:17:10 +08:00
|
|
|
|
2021-01-02 01:53:36 +08:00
|
|
|
func Test_Template_BuildInVarRequest(t *testing.T) {
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2021-01-02 01:53:36 +08:00
|
|
|
s.BindHandler("/:table/test", func(r *ghttp.Request) {
|
|
|
|
|
err := r.Response.WriteTplContent("{{.Request.table}}")
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2021-01-02 01:53:36 +08:00
|
|
|
})
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
s.Start()
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2021-01-02 01:53:36 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/user/test"), "user")
|
|
|
|
|
t.Assert(client.GetContent(ctx, "/order/test"), "order")
|
2021-01-02 01:53:36 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 15:17:10 +08:00
|
|
|
func Test_Template_XSS(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-06 15:17:10 +08:00
|
|
|
v := gview.New()
|
|
|
|
|
v.SetAutoEncode(true)
|
|
|
|
|
c := "<br>"
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2020-02-06 15:17:10 +08:00
|
|
|
s.SetView(v)
|
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
|
err := r.Response.WriteTplContent("{{if eq 1 1}}{{.v}}{{end}}", g.Map{
|
|
|
|
|
"v": c,
|
|
|
|
|
})
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-02-06 15:17:10 +08:00
|
|
|
})
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
s.Start()
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2020-02-06 15:17:10 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), ghtml.Entities(c))
|
2020-02-06 15:17:10 +08:00
|
|
|
})
|
|
|
|
|
}
|