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