github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_request_file_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/debug/gdebug" 12 "github.com/gogf/gf/os/gfile" 13 "github.com/gogf/gf/os/gtime" 14 "github.com/gogf/gf/text/gstr" 15 "testing" 16 "time" 17 18 "github.com/gogf/gf/frame/g" 19 "github.com/gogf/gf/net/ghttp" 20 "github.com/gogf/gf/test/gtest" 21 ) 22 23 func Test_Params_File_Single(t *testing.T) { 24 dstDirPath := gfile.TempDir(gtime.TimestampNanoStr()) 25 p, _ := ports.PopRand() 26 s := g.Server(p) 27 s.BindHandler("/upload/single", func(r *ghttp.Request) { 28 file := r.GetUploadFile("file") 29 if file == nil { 30 r.Response.WriteExit("upload file cannot be empty") 31 } 32 33 if name, err := file.Save(dstDirPath, r.GetBool("randomlyRename")); err == nil { 34 r.Response.WriteExit(name) 35 } 36 r.Response.WriteExit("upload failed") 37 }) 38 s.SetPort(p) 39 s.SetDumpRouterMap(false) 40 s.Start() 41 defer s.Shutdown() 42 time.Sleep(100 * time.Millisecond) 43 // normal name 44 gtest.C(t, func(t *gtest.T) { 45 client := g.Client() 46 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 47 48 srcPath := gdebug.TestDataPath("upload", "file1.txt") 49 dstPath := gfile.Join(dstDirPath, "file1.txt") 50 content := client.PostContent("/upload/single", g.Map{ 51 "file": "@file:" + srcPath, 52 }) 53 t.AssertNE(content, "") 54 t.AssertNE(content, "upload file cannot be empty") 55 t.AssertNE(content, "upload failed") 56 t.Assert(content, "file1.txt") 57 t.Assert(gfile.GetContents(dstPath), gfile.GetContents(srcPath)) 58 }) 59 // randomly rename. 60 gtest.C(t, func(t *gtest.T) { 61 client := g.Client() 62 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 63 64 srcPath := gdebug.TestDataPath("upload", "file2.txt") 65 content := client.PostContent("/upload/single", g.Map{ 66 "file": "@file:" + srcPath, 67 "randomlyRename": true, 68 }) 69 dstPath := gfile.Join(dstDirPath, content) 70 t.AssertNE(content, "") 71 t.AssertNE(content, "upload file cannot be empty") 72 t.AssertNE(content, "upload failed") 73 t.Assert(gfile.GetContents(dstPath), gfile.GetContents(srcPath)) 74 }) 75 } 76 77 func Test_Params_File_CustomName(t *testing.T) { 78 dstDirPath := gfile.TempDir(gtime.TimestampNanoStr()) 79 p, _ := ports.PopRand() 80 s := g.Server(p) 81 s.BindHandler("/upload/single", func(r *ghttp.Request) { 82 file := r.GetUploadFile("file") 83 if file == nil { 84 r.Response.WriteExit("upload file cannot be empty") 85 } 86 file.Filename = "my.txt" 87 if name, err := file.Save(dstDirPath, r.GetBool("randomlyRename")); err == nil { 88 r.Response.WriteExit(name) 89 } 90 r.Response.WriteExit("upload failed") 91 }) 92 s.SetPort(p) 93 s.SetDumpRouterMap(false) 94 s.Start() 95 defer s.Shutdown() 96 time.Sleep(100 * time.Millisecond) 97 gtest.C(t, func(t *gtest.T) { 98 client := g.Client() 99 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 100 101 srcPath := gdebug.TestDataPath("upload", "file1.txt") 102 dstPath := gfile.Join(dstDirPath, "my.txt") 103 content := client.PostContent("/upload/single", g.Map{ 104 "file": "@file:" + srcPath, 105 }) 106 t.AssertNE(content, "") 107 t.AssertNE(content, "upload file cannot be empty") 108 t.AssertNE(content, "upload failed") 109 t.Assert(content, "my.txt") 110 t.Assert(gfile.GetContents(dstPath), gfile.GetContents(srcPath)) 111 }) 112 } 113 114 func Test_Params_File_Batch(t *testing.T) { 115 dstDirPath := gfile.TempDir(gtime.TimestampNanoStr()) 116 p, _ := ports.PopRand() 117 s := g.Server(p) 118 s.BindHandler("/upload/batch", func(r *ghttp.Request) { 119 files := r.GetUploadFiles("file") 120 if files == nil { 121 r.Response.WriteExit("upload file cannot be empty") 122 } 123 if names, err := files.Save(dstDirPath, r.GetBool("randomlyRename")); err == nil { 124 r.Response.WriteExit(gstr.Join(names, ",")) 125 } 126 r.Response.WriteExit("upload failed") 127 }) 128 s.SetPort(p) 129 s.SetDumpRouterMap(false) 130 s.Start() 131 defer s.Shutdown() 132 time.Sleep(100 * time.Millisecond) 133 // normal name 134 gtest.C(t, func(t *gtest.T) { 135 client := g.Client() 136 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 137 138 srcPath1 := gdebug.TestDataPath("upload", "file1.txt") 139 srcPath2 := gdebug.TestDataPath("upload", "file2.txt") 140 dstPath1 := gfile.Join(dstDirPath, "file1.txt") 141 dstPath2 := gfile.Join(dstDirPath, "file2.txt") 142 content := client.PostContent("/upload/batch", g.Map{ 143 "file[0]": "@file:" + srcPath1, 144 "file[1]": "@file:" + srcPath2, 145 }) 146 t.AssertNE(content, "") 147 t.AssertNE(content, "upload file cannot be empty") 148 t.AssertNE(content, "upload failed") 149 t.Assert(content, "file1.txt,file2.txt") 150 t.Assert(gfile.GetContents(dstPath1), gfile.GetContents(srcPath1)) 151 t.Assert(gfile.GetContents(dstPath2), gfile.GetContents(srcPath2)) 152 }) 153 // randomly rename. 154 gtest.C(t, func(t *gtest.T) { 155 client := g.Client() 156 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 157 158 srcPath1 := gdebug.TestDataPath("upload", "file1.txt") 159 srcPath2 := gdebug.TestDataPath("upload", "file2.txt") 160 content := client.PostContent("/upload/batch", g.Map{ 161 "file[0]": "@file:" + srcPath1, 162 "file[1]": "@file:" + srcPath2, 163 "randomlyRename": true, 164 }) 165 t.AssertNE(content, "") 166 t.AssertNE(content, "upload file cannot be empty") 167 t.AssertNE(content, "upload failed") 168 169 array := gstr.SplitAndTrim(content, ",") 170 t.Assert(len(array), 2) 171 dstPath1 := gfile.Join(dstDirPath, array[0]) 172 dstPath2 := gfile.Join(dstDirPath, array[1]) 173 t.Assert(gfile.GetContents(dstPath1), gfile.GetContents(srcPath1)) 174 t.Assert(gfile.GetContents(dstPath2), gfile.GetContents(srcPath2)) 175 }) 176 }