github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_response_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/wangyougui/gf. 6 7 package ghttp_test 8 9 import ( 10 "fmt" 11 "net/http" 12 "strings" 13 "testing" 14 "time" 15 16 "github.com/wangyougui/gf/v2/encoding/gxml" 17 "github.com/wangyougui/gf/v2/internal/json" 18 "github.com/wangyougui/gf/v2/os/gview" 19 20 "github.com/wangyougui/gf/v2/frame/g" 21 "github.com/wangyougui/gf/v2/net/ghttp" 22 "github.com/wangyougui/gf/v2/test/gtest" 23 "github.com/wangyougui/gf/v2/util/guid" 24 ) 25 26 func Test_Response_ServeFile(t *testing.T) { 27 s := g.Server(guid.S()) 28 s.BindHandler("/ServeFile", func(r *ghttp.Request) { 29 filePath := r.GetQuery("filePath") 30 r.Response.ServeFile(filePath.String()) 31 }) 32 s.SetDumpRouterMap(false) 33 s.Start() 34 defer s.Shutdown() 35 36 time.Sleep(100 * time.Millisecond) 37 gtest.C(t, func(t *gtest.T) { 38 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 39 client := g.Client() 40 client.SetPrefix(prefix) 41 42 srcPath := gtest.DataPath("upload", "file1.txt") 43 t.Assert(client.GetContent(ctx, "/ServeFile", "filePath=file1.txt"), "Not Found") 44 45 t.Assert( 46 client.GetContent(ctx, "/ServeFile", "filePath="+srcPath), 47 "file1.txt: This file is for uploading unit test case.") 48 49 t.Assert( 50 strings.Contains( 51 client.GetContent(ctx, "/ServeFile", "filePath=files/server.key"), 52 "BEGIN RSA PRIVATE KEY"), 53 true) 54 }) 55 } 56 57 func Test_Response_ServeFileDownload(t *testing.T) { 58 s := g.Server(guid.S()) 59 s.BindHandler("/ServeFileDownload", func(r *ghttp.Request) { 60 filePath := r.GetQuery("filePath") 61 r.Response.ServeFileDownload(filePath.String()) 62 }) 63 s.SetDumpRouterMap(false) 64 s.Start() 65 defer s.Shutdown() 66 67 time.Sleep(100 * time.Millisecond) 68 gtest.C(t, func(t *gtest.T) { 69 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 70 client := g.Client() 71 client.SetPrefix(prefix) 72 73 srcPath := gtest.DataPath("upload", "file1.txt") 74 t.Assert(client.GetContent(ctx, "/ServeFileDownload", "filePath=file1.txt"), "Not Found") 75 76 t.Assert( 77 client.GetContent(ctx, "/ServeFileDownload", "filePath="+srcPath), 78 "file1.txt: This file is for uploading unit test case.") 79 80 t.Assert( 81 strings.Contains( 82 client.GetContent(ctx, "/ServeFileDownload", "filePath=files/server.key"), 83 "BEGIN RSA PRIVATE KEY"), 84 true) 85 }) 86 } 87 88 func Test_Response_Redirect(t *testing.T) { 89 s := g.Server(guid.S()) 90 s.BindHandler("/", func(r *ghttp.Request) { 91 r.Response.Write("RedirectResult") 92 }) 93 s.BindHandler("/RedirectTo", func(r *ghttp.Request) { 94 r.Response.RedirectTo("/") 95 }) 96 s.BindHandler("/RedirectTo301", func(r *ghttp.Request) { 97 r.Response.RedirectTo("/", http.StatusMovedPermanently) 98 }) 99 s.BindHandler("/RedirectBack", func(r *ghttp.Request) { 100 r.Response.RedirectBack() 101 }) 102 s.SetDumpRouterMap(false) 103 s.Start() 104 defer s.Shutdown() 105 106 time.Sleep(100 * time.Millisecond) 107 gtest.C(t, func(t *gtest.T) { 108 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 109 client := g.Client() 110 client.SetPrefix(prefix) 111 112 t.Assert(client.GetContent(ctx, "/RedirectTo"), "RedirectResult") 113 t.Assert(client.GetContent(ctx, "/RedirectTo301"), "RedirectResult") 114 t.Assert(client.SetHeader("Referer", "/").GetContent(ctx, "/RedirectBack"), "RedirectResult") 115 }) 116 } 117 118 func Test_Response_Buffer(t *testing.T) { 119 s := g.Server(guid.S()) 120 s.BindHandler("/Buffer", func(r *ghttp.Request) { 121 name := r.GetQuery("name").Bytes() 122 r.Response.SetBuffer(name) 123 buffer := r.Response.Buffer() 124 r.Response.ClearBuffer() 125 r.Response.Write(buffer) 126 }) 127 s.BindHandler("/BufferString", func(r *ghttp.Request) { 128 name := r.GetQuery("name").Bytes() 129 r.Response.SetBuffer(name) 130 bufferString := r.Response.BufferString() 131 r.Response.ClearBuffer() 132 r.Response.Write(bufferString) 133 }) 134 s.SetDumpRouterMap(false) 135 s.Start() 136 defer s.Shutdown() 137 138 time.Sleep(100 * time.Millisecond) 139 gtest.C(t, func(t *gtest.T) { 140 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 141 client := g.Client() 142 client.SetPrefix(prefix) 143 144 t.Assert(client.GetContent(ctx, "/Buffer", "name=john"), []byte("john")) 145 t.Assert(client.GetContent(ctx, "/BufferString", "name=john"), "john") 146 }) 147 } 148 149 func Test_Response_WriteTpl(t *testing.T) { 150 gtest.C(t, func(t *gtest.T) { 151 v := gview.New(gtest.DataPath("template", "basic")) 152 s := g.Server(guid.S()) 153 s.SetView(v) 154 s.BindHandler("/", func(r *ghttp.Request) { 155 err := r.Response.WriteTpl("noexist.html", g.Map{ 156 "name": "john", 157 }) 158 t.AssertNE(err, nil) 159 }) 160 s.SetDumpRouterMap(false) 161 s.Start() 162 defer s.Shutdown() 163 time.Sleep(100 * time.Millisecond) 164 client := g.Client() 165 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 166 167 t.AssertNE(client.GetContent(ctx, "/"), "Name:john") 168 }) 169 } 170 171 func Test_Response_WriteTplDefault(t *testing.T) { 172 gtest.C(t, func(t *gtest.T) { 173 v := gview.New() 174 v.SetDefaultFile(gtest.DataPath("template", "basic", "index.html")) 175 s := g.Server(guid.S()) 176 s.SetView(v) 177 s.BindHandler("/", func(r *ghttp.Request) { 178 err := r.Response.WriteTplDefault(g.Map{"name": "john"}) 179 t.AssertNil(err) 180 }) 181 s.SetDumpRouterMap(false) 182 s.Start() 183 defer s.Shutdown() 184 time.Sleep(100 * time.Millisecond) 185 client := g.Client() 186 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 187 188 t.Assert(client.GetContent(ctx, "/"), "Name:john") 189 }) 190 gtest.C(t, func(t *gtest.T) { 191 v := gview.New() 192 v.SetDefaultFile(gtest.DataPath("template", "basic", "noexit.html")) 193 s := g.Server(guid.S()) 194 s.SetView(v) 195 s.BindHandler("/", func(r *ghttp.Request) { 196 err := r.Response.WriteTplDefault(g.Map{"name": "john"}) 197 t.AssertNil(err) 198 }) 199 s.SetDumpRouterMap(false) 200 s.Start() 201 defer s.Shutdown() 202 time.Sleep(100 * time.Millisecond) 203 client := g.Client() 204 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 205 206 t.AssertNE(client.GetContent(ctx, "/"), "Name:john") 207 }) 208 } 209 210 func Test_Response_ParseTplDefault(t *testing.T) { 211 gtest.C(t, func(t *gtest.T) { 212 v := gview.New() 213 v.SetDefaultFile(gtest.DataPath("template", "basic", "index.html")) 214 s := g.Server(guid.S()) 215 s.SetView(v) 216 s.BindHandler("/", func(r *ghttp.Request) { 217 res, err := r.Response.ParseTplDefault(g.Map{"name": "john"}) 218 t.AssertNil(err) 219 r.Response.Write(res) 220 }) 221 s.SetDumpRouterMap(false) 222 s.Start() 223 defer s.Shutdown() 224 time.Sleep(100 * time.Millisecond) 225 client := g.Client() 226 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 227 228 t.Assert(client.GetContent(ctx, "/"), "Name:john") 229 }) 230 } 231 232 func Test_Response_Write(t *testing.T) { 233 type User struct { 234 Name string `json:"name"` 235 } 236 s := g.Server(guid.S()) 237 s.BindHandler("/", func(r *ghttp.Request) { 238 r.Response.Write() 239 }) 240 s.BindHandler("/WriteOverExit", func(r *ghttp.Request) { 241 r.Response.Write("WriteOverExit") 242 r.Response.WriteOverExit("") 243 }) 244 s.BindHandler("/WritefExit", func(r *ghttp.Request) { 245 r.Response.WritefExit("%s", "WritefExit") 246 }) 247 s.BindHandler("/Writeln", func(r *ghttp.Request) { 248 name := r.GetQuery("name") 249 r.Response.Writeln(name) 250 }) 251 s.BindHandler("/WritelnNil", func(r *ghttp.Request) { 252 r.Response.Writeln() 253 }) 254 s.BindHandler("/Writefln", func(r *ghttp.Request) { 255 name := r.GetQuery("name") 256 r.Response.Writefln("%s", name) 257 }) 258 s.BindHandler("/WriteJson", func(r *ghttp.Request) { 259 m := map[string]string{"name": "john"} 260 if bytes, err := json.Marshal(m); err == nil { 261 r.Response.WriteJson(bytes) 262 } 263 }) 264 s.BindHandler("/WriteJsonP", func(r *ghttp.Request) { 265 m := map[string]string{"name": "john"} 266 if bytes, err := json.Marshal(m); err == nil { 267 r.Response.WriteJsonP(bytes) 268 } 269 }) 270 s.BindHandler("/WriteJsonPWithStruct", func(r *ghttp.Request) { 271 user := User{"john"} 272 r.Response.WriteJsonP(user) 273 }) 274 s.BindHandler("/WriteXml", func(r *ghttp.Request) { 275 m := map[string]interface{}{"name": "john"} 276 if bytes, err := gxml.Encode(m); err == nil { 277 r.Response.WriteXml(bytes) 278 } 279 }) 280 s.BindHandler("/WriteXmlWithStruct", func(r *ghttp.Request) { 281 user := User{"john"} 282 r.Response.WriteXml(user) 283 }) 284 285 s.SetDumpRouterMap(false) 286 s.Start() 287 defer s.Shutdown() 288 289 time.Sleep(100 * time.Millisecond) 290 291 gtest.C(t, func(t *gtest.T) { 292 client := g.Client() 293 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 294 295 t.Assert(client.GetContent(ctx, "/"), "") 296 t.Assert(client.GetContent(ctx, "/WriteOverExit"), "") 297 t.Assert(client.GetContent(ctx, "/WritefExit"), "WritefExit") 298 t.Assert(client.GetContent(ctx, "/Writeln"), "\n") 299 t.Assert(client.GetContent(ctx, "/WritelnNil"), "\n") 300 t.Assert(client.GetContent(ctx, "/Writeln", "name=john"), "john\n") 301 t.Assert(client.GetContent(ctx, "/Writefln", "name=john"), "john\n") 302 t.Assert(client.GetContent(ctx, "/WriteJson"), "{\"name\":\"john\"}") 303 t.Assert(client.GetContent(ctx, "/WriteJsonP"), "{\"name\":\"john\"}") 304 t.Assert(client.GetContent(ctx, "/WriteJsonPWithStruct"), "{\"name\":\"john\"}") 305 t.Assert(client.GetContent(ctx, "/WriteJsonPWithStruct", "callback=callback"), 306 "callback({\"name\":\"john\"})") 307 t.Assert(client.GetContent(ctx, "/WriteXml"), "<name>john</name>") 308 t.Assert(client.GetContent(ctx, "/WriteXmlWithStruct"), "<name>john</name>") 309 }) 310 }