Skip to content

Commit 3c4d3b3

Browse files
zeekaldas
zeek
authored andcommitted
Replace "io/ioutil"
"io/ioutil" pakcage has been deprecated since Go 1.16.
1 parent be23ab6 commit 3c4d3b3

12 files changed

+37
-40
lines changed

echo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ import (
4343
"errors"
4444
"fmt"
4545
"io"
46-
"io/ioutil"
4746
stdLog "log"
4847
"net"
4948
"net/http"
49+
"os"
5050
"reflect"
5151
"runtime"
5252
"sync"
@@ -700,7 +700,7 @@ func (e *Echo) StartTLS(address string, certFile, keyFile interface{}) (err erro
700700
func filepathOrContent(fileOrContent interface{}) (content []byte, err error) {
701701
switch v := fileOrContent.(type) {
702702
case string:
703-
return ioutil.ReadFile(v)
703+
return os.ReadFile(v)
704704
case []byte:
705705
return v, nil
706706
default:

echo_test.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"io"
10-
"io/ioutil"
1110
"net"
1211
"net/http"
1312
"net/http/httptest"
@@ -244,7 +243,7 @@ func TestEchoStaticRedirectIndex(t *testing.T) {
244243
}(resp.Body)
245244
assert.Equal(t, http.StatusOK, resp.StatusCode)
246245

247-
if body, err := ioutil.ReadAll(resp.Body); err == nil {
246+
if body, err := io.ReadAll(resp.Body); err == nil {
248247
assert.Equal(t, true, strings.HasPrefix(string(body), "<!doctype html>"))
249248
} else {
250249
assert.Fail(t, err.Error())
@@ -1032,9 +1031,9 @@ func TestEchoStartTLSAndStart(t *testing.T) {
10321031
}
10331032

10341033
func TestEchoStartTLSByteString(t *testing.T) {
1035-
cert, err := ioutil.ReadFile("_fixture/certs/cert.pem")
1034+
cert, err := os.ReadFile("_fixture/certs/cert.pem")
10361035
require.NoError(t, err)
1037-
key, err := ioutil.ReadFile("_fixture/certs/key.pem")
1036+
key, err := os.ReadFile("_fixture/certs/key.pem")
10381037
require.NoError(t, err)
10391038

10401039
testCases := []struct {
@@ -1413,7 +1412,7 @@ func TestEchoListenerNetwork(t *testing.T) {
14131412
}(resp.Body)
14141413
assert.Equal(t, http.StatusOK, resp.StatusCode)
14151414

1416-
if body, err := ioutil.ReadAll(resp.Body); err == nil {
1415+
if body, err := io.ReadAll(resp.Body); err == nil {
14171416
assert.Equal(t, "OK", string(body))
14181417
} else {
14191418
assert.Fail(t, err.Error())
@@ -1495,9 +1494,9 @@ func TestEcho_ListenerAddr(t *testing.T) {
14951494
}
14961495

14971496
func TestEcho_TLSListenerAddr(t *testing.T) {
1498-
cert, err := ioutil.ReadFile("_fixture/certs/cert.pem")
1497+
cert, err := os.ReadFile("_fixture/certs/cert.pem")
14991498
require.NoError(t, err)
1500-
key, err := ioutil.ReadFile("_fixture/certs/key.pem")
1499+
key, err := os.ReadFile("_fixture/certs/key.pem")
15011500
require.NoError(t, err)
15021501

15031502
e := New()
@@ -1515,9 +1514,9 @@ func TestEcho_TLSListenerAddr(t *testing.T) {
15151514
}
15161515

15171516
func TestEcho_StartServer(t *testing.T) {
1518-
cert, err := ioutil.ReadFile("_fixture/certs/cert.pem")
1517+
cert, err := os.ReadFile("_fixture/certs/cert.pem")
15191518
require.NoError(t, err)
1520-
key, err := ioutil.ReadFile("_fixture/certs/key.pem")
1519+
key, err := os.ReadFile("_fixture/certs/key.pem")
15211520
require.NoError(t, err)
15221521
certs, err := tls.X509KeyPair(cert, key)
15231522
require.NoError(t, err)

group_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package echo
22

33
import (
4-
"io/ioutil"
54
"net/http"
65
"net/http/httptest"
6+
"os"
77
"testing"
88

99
"github.com/stretchr/testify/assert"
@@ -32,7 +32,7 @@ func TestGroupFile(t *testing.T) {
3232
e := New()
3333
g := e.Group("/group")
3434
g.File("/walle", "_fixture/images/walle.png")
35-
expectedData, err := ioutil.ReadFile("_fixture/images/walle.png")
35+
expectedData, err := os.ReadFile("_fixture/images/walle.png")
3636
assert.Nil(t, err)
3737
req := httptest.NewRequest(http.MethodGet, "/group/walle", nil)
3838
rec := httptest.NewRecorder()

middleware/body_dump.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"bytes"
66
"io"
7-
"io/ioutil"
87
"net"
98
"net/http"
109

@@ -68,9 +67,9 @@ func BodyDumpWithConfig(config BodyDumpConfig) echo.MiddlewareFunc {
6867
// Request
6968
reqBody := []byte{}
7069
if c.Request().Body != nil { // Read
71-
reqBody, _ = ioutil.ReadAll(c.Request().Body)
70+
reqBody, _ = io.ReadAll(c.Request().Body)
7271
}
73-
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(reqBody)) // Reset
72+
c.Request().Body = io.NopCloser(bytes.NewBuffer(reqBody)) // Reset
7473

7574
// Response
7675
resBody := new(bytes.Buffer)

middleware/body_dump_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package middleware
22

33
import (
44
"errors"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/http/httptest"
88
"strings"
@@ -19,7 +19,7 @@ func TestBodyDump(t *testing.T) {
1919
rec := httptest.NewRecorder()
2020
c := e.NewContext(req, rec)
2121
h := func(c echo.Context) error {
22-
body, err := ioutil.ReadAll(c.Request().Body)
22+
body, err := io.ReadAll(c.Request().Body)
2323
if err != nil {
2424
return err
2525
}

middleware/body_limit_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package middleware
22

33
import (
44
"bytes"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/http/httptest"
88
"testing"
@@ -18,7 +18,7 @@ func TestBodyLimit(t *testing.T) {
1818
rec := httptest.NewRecorder()
1919
c := e.NewContext(req, rec)
2020
h := func(c echo.Context) error {
21-
body, err := ioutil.ReadAll(c.Request().Body)
21+
body, err := io.ReadAll(c.Request().Body)
2222
if err != nil {
2323
return err
2424
}
@@ -67,18 +67,18 @@ func TestBodyLimitReader(t *testing.T) {
6767
}
6868
reader := &limitedReader{
6969
BodyLimitConfig: config,
70-
reader: ioutil.NopCloser(bytes.NewReader(hw)),
70+
reader: io.NopCloser(bytes.NewReader(hw)),
7171
context: e.NewContext(req, rec),
7272
}
7373

7474
// read all should return ErrStatusRequestEntityTooLarge
75-
_, err := ioutil.ReadAll(reader)
75+
_, err := io.ReadAll(reader)
7676
he := err.(*echo.HTTPError)
7777
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code)
7878

7979
// reset reader and read two bytes must succeed
8080
bt := make([]byte, 2)
81-
reader.Reset(ioutil.NopCloser(bytes.NewReader(hw)), e.NewContext(req, rec))
81+
reader.Reset(io.NopCloser(bytes.NewReader(hw)), e.NewContext(req, rec))
8282
n, err := reader.Read(bt)
8383
assert.Equal(t, 2, n)
8484
assert.Equal(t, nil, err)

middleware/compress.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"compress/gzip"
66
"io"
7-
"io/ioutil"
87
"net"
98
"net/http"
109
"strings"
@@ -89,7 +88,7 @@ func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
8988
// nothing is written to body or error is returned.
9089
// See issue #424, #407.
9190
res.Writer = rw
92-
w.Reset(ioutil.Discard)
91+
w.Reset(io.Discard)
9392
}
9493
w.Close()
9594
pool.Put(w)
@@ -135,7 +134,7 @@ func (w *gzipResponseWriter) Push(target string, opts *http.PushOptions) error {
135134
func gzipCompressPool(config GzipConfig) sync.Pool {
136135
return sync.Pool{
137136
New: func() interface{} {
138-
w, err := gzip.NewWriterLevel(ioutil.Discard, config.Level)
137+
w, err := gzip.NewWriterLevel(io.Discard, config.Level)
139138
if err != nil {
140139
return err
141140
}

middleware/compress_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"bytes"
55
"compress/gzip"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"net/http/httptest"
9+
"os"
1010
"testing"
1111

1212
"github.com/labstack/echo/v4"
@@ -173,7 +173,7 @@ func TestGzipWithStatic(t *testing.T) {
173173
r, err := gzip.NewReader(rec.Body)
174174
if assert.NoError(t, err) {
175175
defer r.Close()
176-
want, err := ioutil.ReadFile("../_fixture/images/walle.png")
176+
want, err := os.ReadFile("../_fixture/images/walle.png")
177177
if assert.NoError(t, err) {
178178
buf := new(bytes.Buffer)
179179
buf.ReadFrom(r)

middleware/decompress_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"compress/gzip"
66
"errors"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99
"net/http/httptest"
1010
"strings"
@@ -39,7 +39,7 @@ func TestDecompress(t *testing.T) {
3939
c = e.NewContext(req, rec)
4040
h(c)
4141
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
42-
b, err := ioutil.ReadAll(req.Body)
42+
b, err := io.ReadAll(req.Body)
4343
assert.NoError(t, err)
4444
assert.Equal(t, body, string(b))
4545
}
@@ -67,7 +67,7 @@ func TestDecompressDefaultConfig(t *testing.T) {
6767
c = e.NewContext(req, rec)
6868
h(c)
6969
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
70-
b, err := ioutil.ReadAll(req.Body)
70+
b, err := io.ReadAll(req.Body)
7171
assert.NoError(t, err)
7272
assert.Equal(t, body, string(b))
7373
}
@@ -82,7 +82,7 @@ func TestCompressRequestWithoutDecompressMiddleware(t *testing.T) {
8282
e.NewContext(req, rec)
8383
e.ServeHTTP(rec, req)
8484
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
85-
b, err := ioutil.ReadAll(req.Body)
85+
b, err := io.ReadAll(req.Body)
8686
assert.NoError(t, err)
8787
assert.NotEqual(t, b, body)
8888
assert.Equal(t, b, gz)
@@ -132,7 +132,7 @@ func TestDecompressSkipper(t *testing.T) {
132132
c := e.NewContext(req, rec)
133133
e.ServeHTTP(rec, req)
134134
assert.Equal(t, rec.Header().Get(echo.HeaderContentType), echo.MIMEApplicationJSONCharsetUTF8)
135-
reqBody, err := ioutil.ReadAll(c.Request().Body)
135+
reqBody, err := io.ReadAll(c.Request().Body)
136136
assert.NoError(t, err)
137137
assert.Equal(t, body, string(reqBody))
138138
}
@@ -161,7 +161,7 @@ func TestDecompressPoolError(t *testing.T) {
161161
c := e.NewContext(req, rec)
162162
e.ServeHTTP(rec, req)
163163
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
164-
reqBody, err := ioutil.ReadAll(c.Request().Body)
164+
reqBody, err := io.ReadAll(c.Request().Body)
165165
assert.NoError(t, err)
166166
assert.Equal(t, body, string(reqBody))
167167
assert.Equal(t, rec.Code, http.StatusInternalServerError)

middleware/proxy_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"net"
99
"net/http"
1010
"net/http/httptest"
@@ -93,7 +93,7 @@ func TestProxy(t *testing.T) {
9393
e.Use(ProxyWithConfig(ProxyConfig{
9494
Balancer: rrb,
9595
ModifyResponse: func(res *http.Response) error {
96-
res.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("modified")))
96+
res.Body = io.NopCloser(bytes.NewBuffer([]byte("modified")))
9797
res.Header.Set("X-Modified", "1")
9898
return nil
9999
},

middleware/rewrite_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package middleware
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"net/http"
66
"net/http/httptest"
77
"net/url"
@@ -142,7 +142,7 @@ func TestRewriteWithConfigPreMiddleware_Issue1143(t *testing.T) {
142142
assert.Equal(t, http.StatusOK, rec.Code)
143143

144144
defer rec.Result().Body.Close()
145-
bodyBytes, _ := ioutil.ReadAll(rec.Result().Body)
145+
bodyBytes, _ := io.ReadAll(rec.Result().Body)
146146
assert.Equal(t, "hosts", string(bodyBytes))
147147
}
148148
}

middleware/timeout_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"log"
1010
"net"
1111
"net/http"
@@ -410,7 +410,7 @@ func TestTimeoutWithFullEchoStack(t *testing.T) {
410410
}
411411

412412
assert.Equal(t, tc.expectStatusCode, res.StatusCode)
413-
if body, err := ioutil.ReadAll(res.Body); err == nil {
413+
if body, err := io.ReadAll(res.Body); err == nil {
414414
assert.Equal(t, tc.expectResponse, string(body))
415415
} else {
416416
assert.Fail(t, err.Error())

0 commit comments

Comments
 (0)