From 72da1642eea3c703c782e1c2eb34b78aeb1940d5 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 2 Jul 2020 19:12:01 +0800 Subject: [PATCH] add more unit testing case for ghttp.Client --- net/ghttp/ghttp_unit_client_test.go | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/net/ghttp/ghttp_unit_client_test.go b/net/ghttp/ghttp_unit_client_test.go index aa85c171e..3a87236e3 100644 --- a/net/ghttp/ghttp_unit_client_test.go +++ b/net/ghttp/ghttp_unit_client_test.go @@ -9,6 +9,9 @@ package ghttp_test import ( "context" "fmt" + "github.com/gogf/gf/debug/gdebug" + "github.com/gogf/gf/os/gfile" + "github.com/gogf/gf/util/guid" "testing" "time" @@ -291,3 +294,38 @@ func Test_Client_Param_Containing_Special_Char(t *testing.T) { }), "k1=MTIxMg==&k2=100") }) } + +func Test_Client_File_And_Param(t *testing.T) { + p, _ := ports.PopRand() + s := g.Server(p) + s.BindHandler("/", func(r *ghttp.Request) { + tmpPath := gfile.TempDir(guid.S()) + err := gfile.Mkdir(tmpPath) + gtest.Assert(err, nil) + defer gfile.Remove(tmpPath) + + file := r.GetUploadFile("file") + _, err = file.Save(tmpPath) + gtest.Assert(err, nil) + r.Response.Write( + r.Get("key"), + gfile.GetContents(gfile.Join(tmpPath, gfile.Basename(file.Filename))), + ) + }) + 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)) + filePath := gdebug.TestDataPath("upload", "file1.txt") + t.Assert( + c.PostContent("/", "key=1&file=@file:"+filePath), + "1"+gfile.GetContents(filePath), + ) + }) +}