2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-09-13 20:49:16 +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.
|
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
2020-04-30 20:37:09 +08:00
|
|
|
"fmt"
|
2019-09-13 20:49:16 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
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/gfile"
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2019-09-13 20:49:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_ConfigFromMap(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-13 20:49:16 +08:00
|
|
|
m := g.Map{
|
2019-11-07 11:32:25 +08:00
|
|
|
"address": ":8199",
|
2019-09-13 20:49:16 +08:00
|
|
|
"readTimeout": "60s",
|
|
|
|
"indexFiles": g.Slice{"index.php", "main.php"},
|
|
|
|
"errorLogEnabled": true,
|
|
|
|
"cookieMaxAge": "1y",
|
|
|
|
}
|
2019-11-07 11:32:25 +08:00
|
|
|
config, err := ghttp.ConfigFromMap(m)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-09-13 20:49:16 +08:00
|
|
|
d1, _ := time.ParseDuration(gconv.String(m["readTimeout"]))
|
|
|
|
d2, _ := time.ParseDuration(gconv.String(m["cookieMaxAge"]))
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(config.Address, m["address"])
|
|
|
|
t.Assert(config.ReadTimeout, d1)
|
|
|
|
t.Assert(config.CookieMaxAge, d2)
|
|
|
|
t.Assert(config.IndexFiles, m["indexFiles"])
|
|
|
|
t.Assert(config.ErrorLogEnabled, m["errorLogEnabled"])
|
2019-09-13 20:49:16 +08:00
|
|
|
})
|
|
|
|
}
|
2019-11-07 11:32:25 +08:00
|
|
|
|
|
|
|
func Test_SetConfigWithMap(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-11-07 11:32:25 +08:00
|
|
|
m := g.Map{
|
2019-12-02 19:55:45 +08:00
|
|
|
"Address": ":8199",
|
2021-11-13 23:23:55 +08:00
|
|
|
// "ServerRoot": "/var/www/MyServerRoot",
|
2019-12-02 19:55:45 +08:00
|
|
|
"IndexFiles": g.Slice{"index.php", "main.php"},
|
|
|
|
"AccessLogEnabled": true,
|
|
|
|
"ErrorLogEnabled": true,
|
|
|
|
"PProfEnabled": true,
|
2021-01-06 01:00:49 +08:00
|
|
|
"LogPath": "/tmp/log/MyServerLog",
|
2019-12-02 19:55:45 +08:00
|
|
|
"SessionIdName": "MySessionId",
|
|
|
|
"SessionPath": "/tmp/MySessionStoragePath",
|
|
|
|
"SessionMaxAge": 24 * time.Hour,
|
2019-11-07 11:32:25 +08:00
|
|
|
}
|
|
|
|
s := g.Server()
|
|
|
|
err := s.SetConfigWithMap(m)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-11-07 11:32:25 +08:00
|
|
|
})
|
|
|
|
}
|
2020-04-30 20:37:09 +08:00
|
|
|
|
|
|
|
func Test_ClientMaxBodySize(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.POST("/", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.GetBodyString())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
m := g.Map{
|
|
|
|
"Address": p,
|
|
|
|
"ClientMaxBodySize": "1k",
|
|
|
|
}
|
|
|
|
gtest.Assert(s.SetConfigWithMap(m), nil)
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
data := make([]byte, 1056)
|
|
|
|
for i := 0; i < 1056; i++ {
|
|
|
|
data[i] = 'a'
|
|
|
|
}
|
|
|
|
t.Assert(
|
2021-09-30 14:06:46 +08:00
|
|
|
gstr.Trim(c.PostContent(ctx, "/", data)),
|
2020-04-30 20:37:09 +08:00
|
|
|
data[:1024],
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ClientMaxBodySize_File(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.POST("/", func(r *ghttp.Request) {
|
|
|
|
r.GetUploadFile("file")
|
|
|
|
r.Response.Write("ok")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
m := g.Map{
|
|
|
|
"Address": p,
|
|
|
|
"ErrorLogEnabled": false,
|
|
|
|
"ClientMaxBodySize": "1k",
|
|
|
|
}
|
|
|
|
gtest.Assert(s.SetConfigWithMap(m), nil)
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// ok
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
path := gfile.TempDir(gtime.TimestampNanoStr())
|
|
|
|
data := make([]byte, 512)
|
|
|
|
for i := 0; i < 512; i++ {
|
|
|
|
data[i] = 'a'
|
|
|
|
}
|
|
|
|
t.Assert(gfile.PutBytes(path, data), nil)
|
|
|
|
defer gfile.Remove(path)
|
|
|
|
t.Assert(
|
2021-09-30 14:06:46 +08:00
|
|
|
gstr.Trim(c.PostContent(ctx, "/", "name=john&file=@file:"+path)),
|
2020-04-30 20:37:09 +08:00
|
|
|
"ok",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// too large
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
c := g.Client()
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
path := gfile.TempDir(gtime.TimestampNanoStr())
|
|
|
|
data := make([]byte, 1056)
|
|
|
|
for i := 0; i < 1056; i++ {
|
|
|
|
data[i] = 'a'
|
|
|
|
}
|
|
|
|
t.Assert(gfile.PutBytes(path, data), nil)
|
|
|
|
defer gfile.Remove(path)
|
|
|
|
t.Assert(
|
2021-09-30 14:06:46 +08:00
|
|
|
gstr.Trim(c.PostContent(ctx, "/", "name=john&file=@file:"+path)),
|
2021-12-23 00:34:02 +08:00
|
|
|
"r.ParseMultipartForm failed: http: request body too large",
|
2020-04-30 20:37:09 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|