add function Test_Params_Modify for ghttp_unit_param_test.go

This commit is contained in:
张以诺
2020-08-14 20:02:52 +08:00
parent f6dbaba1f8
commit f2e276eabd

View File

@ -7,10 +7,14 @@
package ghttp_test
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
"time"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/test/gtest"
@ -517,3 +521,39 @@ func Test_Params_GetRequestMap(t *testing.T) {
)
})
}
func Test_Params_Modify(t *testing.T) {
p, _ := ports.PopRand()
s := g.Server(p)
s.BindHandler("/param/modify", func(r *ghttp.Request) {
param := r.GetMap()
param["id"] = 2
paramBytes, err := gjson.Encode(param)
if err != nil {
r.Response.Write(err)
return
}
r.Request.Body = ioutil.NopCloser(bytes.NewReader(paramBytes))
r.ReloadParam()
r.Response.Write(r.GetMap())
})
s.SetPort(p)
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
prefix := fmt.Sprintf("http://127.0.0.1:%d", p)
client := ghttp.NewClient()
client.SetPrefix(prefix)
t.Assert(
client.PostContent(
"/param/modify",
`{"id":1}`,
),
`{"id":2}`,
)
})
}