From 5d33ec8d0c2c6855d4d0c44b8374002064820ba2 Mon Sep 17 00:00:00 2001 From: miajio Date: Mon, 23 Sep 2024 10:56:27 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=95=B4=E7=90=86io/ioutil=E5=8C=85?= =?UTF-8?q?=E4=B8=AD=E5=AF=B9=E9=A1=B9=E7=9B=AE=E7=9A=84=E4=BE=B5=E5=85=A5?= =?UTF-8?q?,=20=E5=88=A0=E9=99=A4ioutil=E5=8C=85=E7=9A=84=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=B9=B6=E5=8F=98=E6=9B=B4=E5=85=B6=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/hz/generator/custom_files.go | 6 +++--- cmd/hz/generator/handler.go | 4 ++-- cmd/hz/generator/layout.go | 6 +++--- cmd/hz/generator/package.go | 4 ++-- cmd/hz/generator/router.go | 6 +++--- cmd/hz/protobuf/plugin.go | 4 ++-- cmd/hz/protobuf/plugin_test.go | 4 ++-- cmd/hz/protobuf/tag_test.go | 4 ++-- cmd/hz/thrift/plugin.go | 4 ++-- cmd/hz/thrift/plugin_test.go | 4 ++-- cmd/hz/thrift/tag_test.go | 4 ++-- cmd/hz/util/env.go | 3 +-- internal/bytesconv/bytesconv_table_gen.go | 4 ++-- pkg/app/client/client_test.go | 7 +++---- pkg/app/context_test.go | 6 +++--- pkg/app/fs.go | 3 +-- pkg/app/fs_test.go | 7 +++---- pkg/app/middlewares/server/recovery/recovery.go | 4 ++-- pkg/app/server/binding/tagexpr_bind_test.go | 3 +-- pkg/app/server/hertz_test.go | 3 +-- pkg/app/server/render/html_test.go | 3 +-- pkg/common/adaptor/request_test.go | 6 +++--- pkg/common/stackless/writer_test.go | 3 +-- pkg/common/ut/context.go | 3 +-- pkg/protocol/http1/client_test.go | 4 ++-- pkg/protocol/http1/req/request_test.go | 13 ++++++------- pkg/protocol/http1/resp/response_test.go | 5 ++--- pkg/protocol/request_test.go | 5 ++--- pkg/route/engine_test.go | 8 ++++---- pkg/route/routergroup_test.go | 6 +++--- pkg/route/routes_test.go | 3 +-- 31 files changed, 68 insertions(+), 81 deletions(-) diff --git a/cmd/hz/generator/custom_files.go b/cmd/hz/generator/custom_files.go index d3a905ba7..17e2afb79 100644 --- a/cmd/hz/generator/custom_files.go +++ b/cmd/hz/generator/custom_files.go @@ -19,7 +19,7 @@ package generator import ( "bytes" "fmt" - "io/ioutil" + "os" "path/filepath" "strings" "text/template" @@ -315,7 +315,7 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe return err } case Append: // todo: append logic need to be optimized for method - fileContent, err := ioutil.ReadFile(filePath) + fileContent, err := os.ReadFile(filePath) if err != nil { return err } @@ -492,7 +492,7 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f return err } case Append: // todo: append logic need to be optimized for single file - fileContent, err := ioutil.ReadFile(filePath) + fileContent, err := os.ReadFile(filePath) if err != nil { return err } diff --git a/cmd/hz/generator/handler.go b/cmd/hz/generator/handler.go index 6b353c626..e1eb7fb34 100644 --- a/cmd/hz/generator/handler.go +++ b/cmd/hz/generator/handler.go @@ -19,7 +19,7 @@ package generator import ( "bytes" "fmt" - "io/ioutil" + "os" "path/filepath" "strings" @@ -196,7 +196,7 @@ func (pkgGen *HttpPackageGenerator) updateHandler(handler interface{}, handlerTp return nil } - file, err := ioutil.ReadFile(filePath) + file, err := os.ReadFile(filePath) if err != nil { return err } diff --git a/cmd/hz/generator/layout.go b/cmd/hz/generator/layout.go index ea189f5a8..a68477660 100644 --- a/cmd/hz/generator/layout.go +++ b/cmd/hz/generator/layout.go @@ -20,7 +20,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "os" "path/filepath" "reflect" "strings" @@ -63,7 +63,7 @@ func (lg *LayoutGenerator) Init() error { config := layoutConfig // unmarshal from user-defined config file if it exists if lg.ConfigPath != "" { - cdata, err := ioutil.ReadFile(lg.ConfigPath) + cdata, err := os.ReadFile(lg.ConfigPath) if err != nil { return fmt.Errorf("read layout config from %s failed, err: %v", lg.ConfigPath, err.Error()) } @@ -216,7 +216,7 @@ func (lg *LayoutGenerator) GenerateByConfig(configPath string) error { if err := lg.checkInited(); err != nil { return err } - buf, err := ioutil.ReadFile(configPath) + buf, err := os.ReadFile(configPath) if err != nil { return fmt.Errorf("read data file '%s' failed, err: %v", configPath, err.Error()) } diff --git a/cmd/hz/generator/package.go b/cmd/hz/generator/package.go index 6896229b0..45d59e35d 100644 --- a/cmd/hz/generator/package.go +++ b/cmd/hz/generator/package.go @@ -19,7 +19,7 @@ package generator import ( "errors" "fmt" - "io/ioutil" + "os" "path/filepath" "reflect" "text/template" @@ -84,7 +84,7 @@ func (pkgGen *HttpPackageGenerator) Init() error { customConfig := TemplateConfig{} // unmarshal from user-defined config file if it exists if pkgGen.ConfigPath != "" { - cdata, err := ioutil.ReadFile(pkgGen.ConfigPath) + cdata, err := os.ReadFile(pkgGen.ConfigPath) if err != nil { return fmt.Errorf("read layout config from %s failed, err: %v", pkgGen.ConfigPath, err.Error()) } diff --git a/cmd/hz/generator/router.go b/cmd/hz/generator/router.go index c5e5cbba9..e4df0b043 100644 --- a/cmd/hz/generator/router.go +++ b/cmd/hz/generator/router.go @@ -19,8 +19,8 @@ package generator import ( "bytes" "fmt" - "io/ioutil" "math" + "os" "path/filepath" "regexp" "sort" @@ -345,7 +345,7 @@ func (pkgGen *HttpPackageGenerator) updateRegister(pkg, rDir, pkgName string) er return pkgGen.TemplateGenerator.Generate(register, registerTplName, registerPath, false) } - file, err := ioutil.ReadFile(registerPath) + file, err := os.ReadFile(registerPath) if err != nil { return fmt.Errorf("read register '%s' failed, err: %v", registerPath, err.Error()) } @@ -493,7 +493,7 @@ func (pkgGen *HttpPackageGenerator) updateMiddlewareReg(router interface{}, midd return nil }) - file, err := ioutil.ReadFile(filePath) + file, err := os.ReadFile(filePath) if err != nil { return err } diff --git a/cmd/hz/protobuf/plugin.go b/cmd/hz/protobuf/plugin.go index abb7616c4..fa7843ddc 100644 --- a/cmd/hz/protobuf/plugin.go +++ b/cmd/hz/protobuf/plugin.go @@ -49,7 +49,7 @@ package protobuf import ( "fmt" - "io/ioutil" + "io" "os" "path/filepath" "strings" @@ -112,7 +112,7 @@ func (plugin *Plugin) Run() int { } }() // read protoc request - in, err := ioutil.ReadAll(os.Stdin) + in, err := io.ReadAll(os.Stdin) if err != nil { logs.Errorf("read request failed: %s\n", err.Error()) return meta.PluginError diff --git a/cmd/hz/protobuf/plugin_test.go b/cmd/hz/protobuf/plugin_test.go index b34d8b868..7d6470925 100644 --- a/cmd/hz/protobuf/plugin_test.go +++ b/cmd/hz/protobuf/plugin_test.go @@ -17,7 +17,7 @@ package protobuf import ( - "io/ioutil" + "os" "strings" "testing" @@ -27,7 +27,7 @@ import ( ) func TestPlugin_Handle(t *testing.T) { - in, err := ioutil.ReadFile("../testdata/request_protoc.out") + in, err := os.ReadFile("../testdata/request_protoc.out") if err != nil { t.Fatal(err) } diff --git a/cmd/hz/protobuf/tag_test.go b/cmd/hz/protobuf/tag_test.go index e8e9561ec..1491934fb 100644 --- a/cmd/hz/protobuf/tag_test.go +++ b/cmd/hz/protobuf/tag_test.go @@ -17,7 +17,7 @@ package protobuf import ( - "io/ioutil" + "os" "strings" "testing" @@ -104,7 +104,7 @@ func TestTagGenerate(t *testing.T) { }, } - in, err := ioutil.ReadFile("./test_data/protobuf_tag_test.out") + in, err := os.ReadFile("./test_data/protobuf_tag_test.out") if err != nil { t.Fatal(err) } diff --git a/cmd/hz/thrift/plugin.go b/cmd/hz/thrift/plugin.go index 0e773df30..9e703fcc0 100644 --- a/cmd/hz/thrift/plugin.go +++ b/cmd/hz/thrift/plugin.go @@ -19,7 +19,7 @@ package thrift import ( "errors" "fmt" - "io/ioutil" + "io" "os" "path/filepath" "strings" @@ -221,7 +221,7 @@ func (plugin *Plugin) recvVerboseLogger() string { } func (plugin *Plugin) handleRequest() error { - data, err := ioutil.ReadAll(os.Stdin) + data, err := io.ReadAll(os.Stdin) if err != nil { return fmt.Errorf("read request failed: %s", err.Error()) } diff --git a/cmd/hz/thrift/plugin_test.go b/cmd/hz/thrift/plugin_test.go index 1cd4d8b67..510597b37 100644 --- a/cmd/hz/thrift/plugin_test.go +++ b/cmd/hz/thrift/plugin_test.go @@ -17,7 +17,7 @@ package thrift import ( - "io/ioutil" + "os" "testing" "github.com/cloudwego/hertz/cmd/hz/generator" @@ -26,7 +26,7 @@ import ( ) func TestRun(t *testing.T) { - data, err := ioutil.ReadFile("../testdata/request_thrift.out") + data, err := os.ReadFile("../testdata/request_thrift.out") if err != nil { t.Fatal(err) } diff --git a/cmd/hz/thrift/tag_test.go b/cmd/hz/thrift/tag_test.go index 108dea881..143fd8be8 100644 --- a/cmd/hz/thrift/tag_test.go +++ b/cmd/hz/thrift/tag_test.go @@ -17,7 +17,7 @@ package thrift import ( - "io/ioutil" + "os" "strings" "testing" @@ -26,7 +26,7 @@ import ( ) func TestInsertTag(t *testing.T) { - data, err := ioutil.ReadFile("./test_data/thrift_tag_test.out") + data, err := os.ReadFile("./test_data/thrift_tag_test.out") if err != nil { t.Fatal(err) } diff --git a/cmd/hz/util/env.go b/cmd/hz/util/env.go index 65289a620..ab19b4a45 100644 --- a/cmd/hz/util/env.go +++ b/cmd/hz/util/env.go @@ -20,7 +20,6 @@ import ( "bytes" "fmt" "go/build" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -85,7 +84,7 @@ var goModReg = regexp.MustCompile(`^\s*module\s+(\S+)\s*`) func SearchGoMod(cwd string, recurse bool) (moduleName, path string, found bool) { for { path = filepath.Join(cwd, "go.mod") - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err == nil { for _, line := range strings.Split(string(data), "\n") { m := goModReg.FindStringSubmatch(line) diff --git a/internal/bytesconv/bytesconv_table_gen.go b/internal/bytesconv/bytesconv_table_gen.go index eff361b7b..f53266e01 100644 --- a/internal/bytesconv/bytesconv_table_gen.go +++ b/internal/bytesconv/bytesconv_table_gen.go @@ -47,8 +47,8 @@ package main import ( "bytes" "fmt" - "io/ioutil" "log" + "os" ) const ( @@ -251,7 +251,7 @@ func main() { fmt.Fprintf(w, "\tValidHeaderFieldNameTable = %q\n", validHeaderFieldNameTable) fmt.Fprintf(w, ")\n") - if err := ioutil.WriteFile("bytesconv_table.go", w.Bytes(), 0o660); err != nil { + if err := os.WriteFile("bytesconv_table.go", w.Bytes(), 0o660); err != nil { log.Fatal(err) } } diff --git a/pkg/app/client/client_test.go b/pkg/app/client/client_test.go index ed5959bea..0d6c4730c 100644 --- a/pkg/app/client/client_test.go +++ b/pkg/app/client/client_test.go @@ -48,7 +48,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -1625,7 +1624,7 @@ func TestClientReadResponseBodyStream(t *testing.T) { if string(p) != part1 { t.Errorf("read len=%v, read content=%v; want len=%v, want content=%v", r, string(p), len(part1), part1) } - left, _ := ioutil.ReadAll(bodyStream) + left, _ := io.ReadAll(bodyStream) if string(left) != part2 { t.Errorf("left len=%v, left content=%v; want len=%v, want content=%v", len(left), string(left), len(part2), part2) } @@ -2067,13 +2066,13 @@ func TestClientReadResponseBodyStreamWithDoubleRequest(t *testing.T) { if bodyStream1 == nil { t.Errorf("bodystream1 is nil") } - data, _ := ioutil.ReadAll(bodyStream1) + data, _ := io.ReadAll(bodyStream1) if string(data) != part1+part2 { t.Errorf("read len=%v, read content=%v; want len=%v, want content=%v", len(data), data, len(part1+part2), part1+part2) } // read left bodystream - left, _ := ioutil.ReadAll(bodyStream) + left, _ := io.ReadAll(bodyStream) if string(left) != part2 { t.Errorf("left len=%v, left content=%v; want len=%v, want content=%v", len(left), string(left), len(part2), part2) } diff --git a/pkg/app/context_test.go b/pkg/app/context_test.go index b2aef633f..99a0aca06 100644 --- a/pkg/app/context_test.go +++ b/pkg/app/context_test.go @@ -23,7 +23,7 @@ import ( "errors" "fmt" "html/template" - "io/ioutil" + "io" "net" "os" "reflect" @@ -462,7 +462,7 @@ tailfoobar` if err := req.Read(&ctx.Request, mr); err != nil { t.Fatalf("unexpected error: %s", err) } - tail, err := ioutil.ReadAll(mr) + tail, err := io.ReadAll(mr) if err != nil { t.Fatalf("unexpected error: %s", err) } @@ -988,7 +988,7 @@ func TestRequestBodyStream(t *testing.T) { s := "testRequestBodyStream" mr := bytes.NewBufferString(s) c.Request.SetBodyStream(mr, -1) - data, err := ioutil.ReadAll(c.RequestBodyStream()) + data, err := io.ReadAll(c.RequestBodyStream()) assert.Nil(t, err) assert.DeepEqual(t, "testRequestBodyStream", string(data)) } diff --git a/pkg/app/fs.go b/pkg/app/fs.go index 5bad7cc03..11526bca3 100644 --- a/pkg/app/fs.go +++ b/pkg/app/fs.go @@ -48,7 +48,6 @@ import ( "fmt" "html" "io" - "io/ioutil" "mime" "net/http" "os" @@ -1075,7 +1074,7 @@ func readFileHeader(f *os.File, compressed bool) ([]byte, error) { R: r, N: 512, } - data, err := ioutil.ReadAll(lr) + data, err := io.ReadAll(lr) if _, err := f.Seek(0, 0); err != nil { return nil, err } diff --git a/pkg/app/fs_test.go b/pkg/app/fs_test.go index c4ad0e426..2058afaa4 100644 --- a/pkg/app/fs_test.go +++ b/pkg/app/fs_test.go @@ -46,7 +46,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "math/rand" "os" "path" @@ -186,13 +185,13 @@ func TestServeFileSmallNoReadFrom(t *testing.T) { teststr := "hello, world!" - tempdir, err := ioutil.TempDir("", "httpexpect") + tempdir, err := os.TempDir("", "httpexpect") if err != nil { t.Fatal(err) } defer os.RemoveAll(tempdir) - if err := ioutil.WriteFile( + if err := os.WriteFile( path.Join(tempdir, "hello"), []byte(teststr), 0o666); err != nil { t.Fatal(err) } @@ -426,7 +425,7 @@ func getFileContents(path string) ([]byte, error) { return nil, err } defer f.Close() - return ioutil.ReadAll(f) + return io.ReadAll(f) } func TestParseByteRangeSuccess(t *testing.T) { diff --git a/pkg/app/middlewares/server/recovery/recovery.go b/pkg/app/middlewares/server/recovery/recovery.go index f05aad217..02eccba45 100644 --- a/pkg/app/middlewares/server/recovery/recovery.go +++ b/pkg/app/middlewares/server/recovery/recovery.go @@ -20,7 +20,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "os" "runtime" "github.com/cloudwego/hertz/pkg/app" @@ -66,7 +66,7 @@ func stack(skip int) []byte { // Print this much at least. If we can't find the source, it won't show. fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc) if file != lastFile { - data, err := ioutil.ReadFile(file) + data, err := os.ReadFile(file) if err != nil { continue } diff --git a/pkg/app/server/binding/tagexpr_bind_test.go b/pkg/app/server/binding/tagexpr_bind_test.go index 1d331e163..100f393a6 100644 --- a/pkg/app/server/binding/tagexpr_bind_test.go +++ b/pkg/app/server/binding/tagexpr_bind_test.go @@ -38,7 +38,6 @@ import ( "bytes" "encoding/json" "io" - "io/ioutil" "mime/multipart" "net/http" "net/url" @@ -798,7 +797,7 @@ func newRequest(u string, header http.Header, cookies []*http.Cookie, bodyReader method := "GET" var body []byte if bodyReader != nil { - body, _ = ioutil.ReadAll(bodyReader) + body, _ = io.ReadAll(bodyReader) method = "POST" } if u == "" { diff --git a/pkg/app/server/hertz_test.go b/pkg/app/server/hertz_test.go index 09e77dd40..3606925c5 100644 --- a/pkg/app/server/hertz_test.go +++ b/pkg/app/server/hertz_test.go @@ -23,7 +23,6 @@ import ( "fmt" "html/template" "io" - "io/ioutil" "net" "net/http" "strings" @@ -415,7 +414,7 @@ func TestNotEnoughBodySize(t *testing.T) { resp, err := http.Post("http://127.0.0.1:8889/test", "application/x-www-form-urlencoded", body) assert.Nil(t, err) assert.DeepEqual(t, 413, resp.StatusCode) - bodyBytes, _ := ioutil.ReadAll(resp.Body) + bodyBytes, _ := io.ReadAll(resp.Body) assert.DeepEqual(t, "Request Entity Too Large", string(bodyBytes)) } diff --git a/pkg/app/server/render/html_test.go b/pkg/app/server/render/html_test.go index d474f1c63..ddf7b0111 100644 --- a/pkg/app/server/render/html_test.go +++ b/pkg/app/server/render/html_test.go @@ -18,7 +18,6 @@ package render import ( "html/template" - "io/ioutil" "os" "testing" "time" @@ -50,7 +49,7 @@ func TestHTMLDebug_StartChecker_timer(t *testing.T) { } func TestHTMLDebug_StartChecker_fs_watcher(t *testing.T) { - f, _ := ioutil.TempFile("./", "test.tmpl") + f, _ := os.CreateTemp("./", "test.tmpl") defer func() { f.Close() os.Remove(f.Name()) diff --git a/pkg/common/adaptor/request_test.go b/pkg/common/adaptor/request_test.go index ba3ec6ad3..4aff35d82 100644 --- a/pkg/common/adaptor/request_test.go +++ b/pkg/common/adaptor/request_test.go @@ -18,7 +18,7 @@ package adaptor import ( "context" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -87,7 +87,7 @@ func makeACall(t *testing.T, method, url string, header http.Header, body string } } - b, err := ioutil.ReadAll(resp.Body) + b, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("Read body error: %s", err) } @@ -116,7 +116,7 @@ func handlerAndCheck(t *testing.T, writer http.ResponseWriter, request *http.Req } } - body, err := ioutil.ReadAll(request.Body) + body, err := io.ReadAll(request.Body) if err != nil { t.Fatalf("Read body error: %s", err) } diff --git a/pkg/common/stackless/writer_test.go b/pkg/common/stackless/writer_test.go index cae68bf5c..73347cb49 100644 --- a/pkg/common/stackless/writer_test.go +++ b/pkg/common/stackless/writer_test.go @@ -47,7 +47,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "testing" "time" ) @@ -129,7 +128,7 @@ func testWriterReuse(w Writer, r io.Reader, newReader func(io.Reader) io.Reader) w.Close() zr := newReader(r) - data, err := ioutil.ReadAll(zr) + data, err := io.ReadAll(zr) if err != nil { return fmt.Errorf("unexpected error: %s, data=%q", err, data) } diff --git a/pkg/common/ut/context.go b/pkg/common/ut/context.go index 30023dd0d..ca46bbb21 100644 --- a/pkg/common/ut/context.go +++ b/pkg/common/ut/context.go @@ -18,7 +18,6 @@ package ut import ( "io" - "io/ioutil" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/common/config" @@ -42,7 +41,7 @@ func createUtRequestContext(engine *route.Engine, method, url string, body *Body if engine.IsStreamRequestBody() || body.Len == -1 { ctx.Request.SetBodyStream(body.Body, body.Len) } else { - buf, err := ioutil.ReadAll(&io.LimitedReader{R: body.Body, N: int64(body.Len)}) + buf, err := io.ReadAll(&io.LimitedReader{R: body.Body, N: int64(body.Len)}) ctx.Request.SetBody(buf) if err != nil && err != io.EOF { panic(err) diff --git a/pkg/protocol/http1/client_test.go b/pkg/protocol/http1/client_test.go index 8c0869dde..73a7a6228 100644 --- a/pkg/protocol/http1/client_test.go +++ b/pkg/protocol/http1/client_test.go @@ -47,7 +47,7 @@ import ( "crypto/tls" "errors" "fmt" - "io/ioutil" + "io" "net" "strings" "sync" @@ -192,7 +192,7 @@ func testContinueReadResponseBodyStream(t *testing.T, header, body string, maxBo t.Fatalf("should read %d from stream body, but got %d", firstRead, sR) } - leftB, _ := ioutil.ReadAll(r.BodyStream()) + leftB, _ := io.ReadAll(r.BodyStream()) if len(leftB) != leftBytes { t.Fatalf("should left %d bytes from stream body, but left %d", leftBytes, len(leftB)) } diff --git a/pkg/protocol/http1/req/request_test.go b/pkg/protocol/http1/req/request_test.go index 0411187a5..ac503a511 100644 --- a/pkg/protocol/http1/req/request_test.go +++ b/pkg/protocol/http1/req/request_test.go @@ -48,7 +48,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "mime/multipart" "net/url" "strings" @@ -704,7 +703,7 @@ func TestRequestReadPostNoBody(t *testing.T) { t.Fatalf("unexpected content-length: %d. Expecting 0", r.Header.ContentLength()) } - tail, err := ioutil.ReadAll(zr) + tail, err := io.ReadAll(zr) if err != nil { t.Fatalf("unexpected error: %s", err) } @@ -1142,7 +1141,7 @@ tailfoobar` t.Fatalf("unexpected error: %s", err) } - tail, err := ioutil.ReadAll(mr) + tail, err := io.ReadAll(mr) if err != nil { t.Fatalf("unexpected error: %s", err) } @@ -1223,7 +1222,7 @@ func testReadIncompleteStream(t *testing.T, header, body string) { if err := ContinueReadBodyStream(&r, mr, 1, true); err != nil { t.Fatalf("error when reading request body stream: %s", err) } - readBody, err := ioutil.ReadAll(r.BodyStream()) + readBody, err := io.ReadAll(r.BodyStream()) if !bytes.Equal(readBody, []byte(body)) || len(readBody) != len(body) { t.Fatalf("readBody is not equal to the rawBody: %b(len: %d)", readBody, len(readBody)) } @@ -1253,7 +1252,7 @@ func testReadChunked(t *testing.T, header, body string, firstRead, leftBytes int if fr != firstRead { t.Fatalf("should read %d from stream body, but got %d", streamRead, fr) } - leftB, _ := ioutil.ReadAll(r.BodyStream()) + leftB, _ := io.ReadAll(r.BodyStream()) if len(leftB) != leftBytes { t.Fatalf("should left %d bytes from stream body, but left %d", leftBytes, len(leftB)) } @@ -1276,7 +1275,7 @@ func testContinueReadBodyStream(t *testing.T, header, body string, maxBodySize, t.Fatalf("should read %d from stream body, but got %d", firstRead, sR) } - leftB, _ := ioutil.ReadAll(r.BodyStream()) + leftB, _ := io.ReadAll(r.BodyStream()) if len(leftB) != leftBytes { t.Fatalf("should left %d bytes from stream body, but left %d", leftBytes, len(leftB)) } @@ -1348,7 +1347,7 @@ tailfoobar` t.Fatalf("unexpected error: %s", err) } - tail, err := ioutil.ReadAll(mr) + tail, err := io.ReadAll(mr) if err != nil { t.Fatalf("unexpected error: %s", err) } diff --git a/pkg/protocol/http1/resp/response_test.go b/pkg/protocol/http1/resp/response_test.go index cff0a5783..357ecb121 100644 --- a/pkg/protocol/http1/resp/response_test.go +++ b/pkg/protocol/http1/resp/response_test.go @@ -46,7 +46,6 @@ import ( "bytes" "errors" "io" - "io/ioutil" "strings" "testing" @@ -738,7 +737,7 @@ func testResponseReadBodyStreamSuccess(t *testing.T, resp *protocol.Response, re } assert.True(t, resp.IsBodyStream()) - body, err := ioutil.ReadAll(resp.BodyStream()) + body, err := io.ReadAll(resp.BodyStream()) if err != nil && err != io.EOF { t.Fatalf("Unexpected error: %s", err) } @@ -757,7 +756,7 @@ func testResponseReadBodyStreamBadTrailer(t *testing.T, resp *protocol.Response, } assert.True(t, resp.IsBodyStream()) - _, err = ioutil.ReadAll(resp.BodyStream()) + _, err = io.ReadAll(resp.BodyStream()) if err == nil || err == io.EOF { t.Fatalf("expected error when reading response.") } diff --git a/pkg/protocol/request_test.go b/pkg/protocol/request_test.go index fda96cd47..b89e37b09 100644 --- a/pkg/protocol/request_test.go +++ b/pkg/protocol/request_test.go @@ -46,7 +46,6 @@ import ( "encoding/base64" "fmt" "io" - "io/ioutil" "math" "mime/multipart" "strings" @@ -149,7 +148,7 @@ tailfoobar` defer r.RemoveMultipartFormFiles() // verify tail - tail, err := ioutil.ReadAll(mr) + tail, err := io.ReadAll(mr) if err != nil { t.Fatalf("unexpected error: %s", err) } @@ -231,7 +230,7 @@ tailfoobar` defer r.RemoveMultipartFormFiles() // all data must be consumed if the content length is unknown - tail, err := ioutil.ReadAll(mr) + tail, err := io.ReadAll(mr) if err != nil { t.Fatalf("unexpected error: %s", err) } diff --git a/pkg/route/engine_test.go b/pkg/route/engine_test.go index a5da5dc56..0d33811c1 100644 --- a/pkg/route/engine_test.go +++ b/pkg/route/engine_test.go @@ -46,7 +46,7 @@ import ( "errors" "fmt" "html/template" - "io/ioutil" + "io" "net" "net/http" "sync/atomic" @@ -416,7 +416,7 @@ func TestRenderHtml(t *testing.T) { }) }) rr := performRequest(e, "GET", "/templateName") - b, _ := ioutil.ReadAll(rr.Body) + b, _ := io.ReadAll(rr.Body) assert.DeepEqual(t, consts.StatusOK, rr.Code) assert.DeepEqual(t, []byte("

Date: 2017/07/01

"), b) assert.DeepEqual(t, "text/html; charset=utf-8", rr.Header().Get("Content-Type")) @@ -463,7 +463,7 @@ func TestRenderHtmlOfGlobWithAutoRender(t *testing.T) { }) }) rr := performRequest(e, "GET", "/templateName") - b, _ := ioutil.ReadAll(rr.Body) + b, _ := io.ReadAll(rr.Body) assert.DeepEqual(t, consts.StatusOK, rr.Code) assert.DeepEqual(t, []byte("

Date: 2017/07/01

"), b) assert.DeepEqual(t, "text/html; charset=utf-8", rr.Header().Get("Content-Type")) @@ -501,7 +501,7 @@ func TestRenderHtmlOfFilesWithAutoRender(t *testing.T) { }) }) rr := performRequest(e, "GET", "/templateName") - b, _ := ioutil.ReadAll(rr.Body) + b, _ := io.ReadAll(rr.Body) assert.DeepEqual(t, consts.StatusOK, rr.Code) assert.DeepEqual(t, []byte("

Date: 2017/07/01

"), b) assert.DeepEqual(t, "text/html; charset=utf-8", rr.Header().Get("Content-Type")) diff --git a/pkg/route/routergroup_test.go b/pkg/route/routergroup_test.go index 3a6a1c07b..5f72d2122 100644 --- a/pkg/route/routergroup_test.go +++ b/pkg/route/routergroup_test.go @@ -42,7 +42,7 @@ package route import ( "context" - "io/ioutil" + "io" "net/http" "os" "testing" @@ -137,7 +137,7 @@ func TestRouterGroupStatic(t *testing.T) { } assert.DeepEqual(t, http.StatusOK, w.Code) defer fd.Close() - content, err := ioutil.ReadAll(fd) + content, err := io.ReadAll(fd) if err != nil { panic(err) } @@ -154,7 +154,7 @@ func TestRouterGroupStaticFile(t *testing.T) { panic(err) } defer fd.Close() - content, err := ioutil.ReadAll(fd) + content, err := io.ReadAll(fd) if err != nil { panic(err) } diff --git a/pkg/route/routes_test.go b/pkg/route/routes_test.go index 1e76d673e..c94baa0ee 100644 --- a/pkg/route/routes_test.go +++ b/pkg/route/routes_test.go @@ -43,7 +43,6 @@ package route import ( "context" "fmt" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -398,7 +397,7 @@ func TestRouteParamsByNameWithExtraSlash(t *testing.T) { func TestRouteStaticFile(t *testing.T) { // SETUP file testRoot, _ := os.Getwd() - f, err := ioutil.TempFile(testRoot, "") + f, err := os.CreateTemp(testRoot, "") if err != nil { t.Error(err) } From 69ed25346554a1ce11f8eab82524ce077eafe78d Mon Sep 17 00:00:00 2001 From: miajio Date: Mon, 23 Sep 2024 11:04:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9ioutil=E9=81=97=E7=95=99?= =?UTF-8?q?=E6=95=B4=E6=94=B9=E6=9C=AA=E6=94=B9=E5=AE=8C=E5=85=A8=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/app/fs_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/app/fs_test.go b/pkg/app/fs_test.go index 2058afaa4..d0481cffc 100644 --- a/pkg/app/fs_test.go +++ b/pkg/app/fs_test.go @@ -185,7 +185,7 @@ func TestServeFileSmallNoReadFrom(t *testing.T) { teststr := "hello, world!" - tempdir, err := os.TempDir("", "httpexpect") + tempdir, err := os.MkdirTemp("", "httpexpect") if err != nil { t.Fatal(err) }