github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_template_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // static service testing. 8 9 package ghttp_test 10 11 import ( 12 "fmt" 13 "github.com/gogf/gf/debug/gdebug" 14 "github.com/gogf/gf/encoding/ghtml" 15 "github.com/gogf/gf/os/gview" 16 "testing" 17 "time" 18 19 "github.com/gogf/gf/frame/g" 20 "github.com/gogf/gf/net/ghttp" 21 "github.com/gogf/gf/test/gtest" 22 ) 23 24 func Test_Template_Basic(t *testing.T) { 25 gtest.C(t, func(t *gtest.T) { 26 v := gview.New(gdebug.TestDataPath("template", "basic")) 27 p, _ := ports.PopRand() 28 s := g.Server(p) 29 s.SetView(v) 30 s.BindHandler("/", func(r *ghttp.Request) { 31 err := r.Response.WriteTpl("index.html", g.Map{ 32 "name": "john", 33 }) 34 t.Assert(err, nil) 35 }) 36 s.SetDumpRouterMap(false) 37 s.SetPort(p) 38 s.Start() 39 defer s.Shutdown() 40 time.Sleep(100 * time.Millisecond) 41 client := g.Client() 42 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 43 44 t.Assert(client.GetContent("/"), "Name:john") 45 t.Assert(client.GetContent("/"), "Name:john") 46 }) 47 } 48 49 func Test_Template_Encode(t *testing.T) { 50 gtest.C(t, func(t *gtest.T) { 51 v := gview.New(gdebug.TestDataPath("template", "basic")) 52 v.SetAutoEncode(true) 53 p, _ := ports.PopRand() 54 s := g.Server(p) 55 s.SetView(v) 56 s.BindHandler("/", func(r *ghttp.Request) { 57 err := r.Response.WriteTpl("index.html", g.Map{ 58 "name": "john", 59 }) 60 t.Assert(err, nil) 61 }) 62 s.SetDumpRouterMap(false) 63 s.SetPort(p) 64 s.Start() 65 defer s.Shutdown() 66 time.Sleep(100 * time.Millisecond) 67 client := g.Client() 68 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 69 70 t.Assert(client.GetContent("/"), "Name:john") 71 t.Assert(client.GetContent("/"), "Name:john") 72 }) 73 } 74 75 func Test_Template_Layout1(t *testing.T) { 76 gtest.C(t, func(t *gtest.T) { 77 v := gview.New(gdebug.TestDataPath("template", "layout1")) 78 p, _ := ports.PopRand() 79 s := g.Server(p) 80 s.SetView(v) 81 s.BindHandler("/layout", func(r *ghttp.Request) { 82 err := r.Response.WriteTpl("layout.html", g.Map{ 83 "mainTpl": "main/main1.html", 84 }) 85 t.Assert(err, nil) 86 }) 87 s.BindHandler("/nil", func(r *ghttp.Request) { 88 err := r.Response.WriteTpl("layout.html", nil) 89 t.Assert(err, nil) 90 }) 91 s.SetDumpRouterMap(false) 92 s.SetPort(p) 93 s.Start() 94 defer s.Shutdown() 95 time.Sleep(100 * time.Millisecond) 96 client := g.Client() 97 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 98 99 t.Assert(client.GetContent("/"), "Not Found") 100 t.Assert(client.GetContent("/layout"), "123") 101 t.Assert(client.GetContent("/nil"), "123") 102 }) 103 } 104 105 func Test_Template_Layout2(t *testing.T) { 106 gtest.C(t, func(t *gtest.T) { 107 v := gview.New(gdebug.TestDataPath("template", "layout2")) 108 p, _ := ports.PopRand() 109 s := g.Server(p) 110 s.SetView(v) 111 s.BindHandler("/main1", func(r *ghttp.Request) { 112 err := r.Response.WriteTpl("layout.html", g.Map{ 113 "mainTpl": "main/main1.html", 114 }) 115 t.Assert(err, nil) 116 }) 117 s.BindHandler("/main2", func(r *ghttp.Request) { 118 err := r.Response.WriteTpl("layout.html", g.Map{ 119 "mainTpl": "main/main2.html", 120 }) 121 t.Assert(err, nil) 122 }) 123 s.BindHandler("/nil", func(r *ghttp.Request) { 124 err := r.Response.WriteTpl("layout.html", nil) 125 t.Assert(err, nil) 126 }) 127 s.SetDumpRouterMap(false) 128 s.SetPort(p) 129 s.Start() 130 defer s.Shutdown() 131 time.Sleep(100 * time.Millisecond) 132 client := g.Client() 133 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 134 135 t.Assert(client.GetContent("/"), "Not Found") 136 t.Assert(client.GetContent("/main1"), "a1b") 137 t.Assert(client.GetContent("/main2"), "a2b") 138 t.Assert(client.GetContent("/nil"), "ab") 139 }) 140 } 141 142 func Test_Template_BuildInVarRequest(t *testing.T) { 143 gtest.C(t, func(t *gtest.T) { 144 p, _ := ports.PopRand() 145 s := g.Server(p) 146 s.BindHandler("/:table/test", func(r *ghttp.Request) { 147 err := r.Response.WriteTplContent("{{.Request.table}}") 148 t.Assert(err, nil) 149 }) 150 s.SetDumpRouterMap(false) 151 s.SetPort(p) 152 s.Start() 153 defer s.Shutdown() 154 time.Sleep(100 * time.Millisecond) 155 client := g.Client() 156 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 157 158 t.Assert(client.GetContent("/user/test"), "user") 159 t.Assert(client.GetContent("/order/test"), "order") 160 }) 161 } 162 163 func Test_Template_XSS(t *testing.T) { 164 gtest.C(t, func(t *gtest.T) { 165 v := gview.New() 166 v.SetAutoEncode(true) 167 c := "<br>" 168 p, _ := ports.PopRand() 169 s := g.Server(p) 170 s.SetView(v) 171 s.BindHandler("/", func(r *ghttp.Request) { 172 err := r.Response.WriteTplContent("{{if eq 1 1}}{{.v}}{{end}}", g.Map{ 173 "v": c, 174 }) 175 t.Assert(err, nil) 176 }) 177 s.SetDumpRouterMap(false) 178 s.SetPort(p) 179 s.Start() 180 defer s.Shutdown() 181 time.Sleep(100 * time.Millisecond) 182 client := g.Client() 183 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 184 185 t.Assert(client.GetContent("/"), ghtml.Entities(c)) 186 }) 187 }