github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_config_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 "strings" 12 "testing" 13 "time" 14 15 "github.com/wangyougui/gf/v2/frame/g" 16 "github.com/wangyougui/gf/v2/net/ghttp" 17 "github.com/wangyougui/gf/v2/os/gfile" 18 "github.com/wangyougui/gf/v2/os/gtime" 19 "github.com/wangyougui/gf/v2/test/gtest" 20 "github.com/wangyougui/gf/v2/text/gstr" 21 "github.com/wangyougui/gf/v2/util/gconv" 22 "github.com/wangyougui/gf/v2/util/guid" 23 ) 24 25 func Test_ConfigFromMap(t *testing.T) { 26 gtest.C(t, func(t *gtest.T) { 27 m := g.Map{ 28 "address": ":12345", 29 "listeners": nil, 30 "readTimeout": "60s", 31 "indexFiles": g.Slice{"index.php", "main.php"}, 32 "errorLogEnabled": true, 33 "cookieMaxAge": "1y", 34 "cookieSameSite": "lax", 35 "cookieSecure": true, 36 "cookieHttpOnly": true, 37 } 38 config, err := ghttp.ConfigFromMap(m) 39 t.AssertNil(err) 40 d1, _ := time.ParseDuration(gconv.String(m["readTimeout"])) 41 d2, _ := time.ParseDuration(gconv.String(m["cookieMaxAge"])) 42 t.Assert(config.Address, m["address"]) 43 t.Assert(config.ReadTimeout, d1) 44 t.Assert(config.CookieMaxAge, d2) 45 t.Assert(config.IndexFiles, m["indexFiles"]) 46 t.Assert(config.ErrorLogEnabled, m["errorLogEnabled"]) 47 t.Assert(config.CookieSameSite, m["cookieSameSite"]) 48 t.Assert(config.CookieSecure, m["cookieSecure"]) 49 t.Assert(config.CookieHttpOnly, m["cookieHttpOnly"]) 50 }) 51 } 52 53 func Test_SetConfigWithMap(t *testing.T) { 54 gtest.C(t, func(t *gtest.T) { 55 m := g.Map{ 56 "Address": ":8199", 57 // "ServerRoot": "/var/www/MyServerRoot", 58 "IndexFiles": g.Slice{"index.php", "main.php"}, 59 "AccessLogEnabled": true, 60 "ErrorLogEnabled": true, 61 "PProfEnabled": true, 62 "LogPath": "/tmp/log/MyServerLog", 63 "SessionIdName": "MySessionId", 64 "SessionPath": "/tmp/MySessionStoragePath", 65 "SessionMaxAge": 24 * time.Hour, 66 "cookieSameSite": "lax", 67 "cookieSecure": true, 68 "cookieHttpOnly": true, 69 } 70 s := g.Server() 71 err := s.SetConfigWithMap(m) 72 t.AssertNil(err) 73 }) 74 } 75 76 func Test_ClientMaxBodySize(t *testing.T) { 77 s := g.Server(guid.S()) 78 s.Group("/", func(group *ghttp.RouterGroup) { 79 group.POST("/", func(r *ghttp.Request) { 80 r.Response.Write(r.GetBodyString()) 81 }) 82 }) 83 m := g.Map{ 84 "ClientMaxBodySize": "1k", 85 } 86 gtest.Assert(s.SetConfigWithMap(m), nil) 87 s.SetDumpRouterMap(false) 88 s.Start() 89 defer s.Shutdown() 90 91 time.Sleep(100 * time.Millisecond) 92 93 gtest.C(t, func(t *gtest.T) { 94 c := g.Client() 95 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 96 97 data := make([]byte, 1056) 98 for i := 0; i < 1056; i++ { 99 data[i] = 'a' 100 } 101 t.Assert( 102 gstr.Trim(c.PostContent(ctx, "/", data)), 103 `Read from request Body failed: http: request body too large`, 104 ) 105 }) 106 } 107 108 func Test_ClientMaxBodySize_File(t *testing.T) { 109 s := g.Server(guid.S()) 110 s.Group("/", func(group *ghttp.RouterGroup) { 111 group.POST("/", func(r *ghttp.Request) { 112 r.GetUploadFile("file") 113 r.Response.Write("ok") 114 }) 115 }) 116 m := g.Map{ 117 "ErrorLogEnabled": false, 118 "ClientMaxBodySize": "1k", 119 } 120 gtest.Assert(s.SetConfigWithMap(m), nil) 121 s.SetDumpRouterMap(false) 122 s.Start() 123 defer s.Shutdown() 124 125 time.Sleep(100 * time.Millisecond) 126 127 // ok 128 gtest.C(t, func(t *gtest.T) { 129 c := g.Client() 130 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 131 132 path := gfile.Temp(gtime.TimestampNanoStr()) 133 data := make([]byte, 512) 134 for i := 0; i < 512; i++ { 135 data[i] = 'a' 136 } 137 t.Assert(gfile.PutBytes(path, data), nil) 138 defer gfile.Remove(path) 139 t.Assert( 140 gstr.Trim(c.PostContent(ctx, "/", "name=john&file=@file:"+path)), 141 "ok", 142 ) 143 }) 144 145 // too large 146 gtest.C(t, func(t *gtest.T) { 147 c := g.Client() 148 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 149 150 path := gfile.Temp(gtime.TimestampNanoStr()) 151 data := make([]byte, 1056) 152 for i := 0; i < 1056; i++ { 153 data[i] = 'a' 154 } 155 t.Assert(gfile.PutBytes(path, data), nil) 156 defer gfile.Remove(path) 157 t.Assert( 158 true, 159 strings.Contains( 160 gstr.Trim(c.PostContent(ctx, "/", "name=john&file=@file:"+path)), 161 "http: request body too large", 162 ), 163 ) 164 }) 165 }