Skip to content

Commit

Permalink
Replace "io/ioutil"
Browse files Browse the repository at this point in the history
"io/ioutil" pakcage has been deprecated since Go 1.16.
  • Loading branch information
zeek authored and aldas committed Nov 21, 2022
1 parent be23ab6 commit 3c4d3b3
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 40 deletions.
4 changes: 2 additions & 2 deletions echo.go
Expand Up @@ -43,10 +43,10 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
stdLog "log"
"net"
"net/http"
"os"
"reflect"
"runtime"
"sync"
Expand Down Expand Up @@ -700,7 +700,7 @@ func (e *Echo) StartTLS(address string, certFile, keyFile interface{}) (err erro
func filepathOrContent(fileOrContent interface{}) (content []byte, err error) {
switch v := fileOrContent.(type) {
case string:
return ioutil.ReadFile(v)
return os.ReadFile(v)
case []byte:
return v, nil
default:
Expand Down
17 changes: 8 additions & 9 deletions echo_test.go
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -244,7 +243,7 @@ func TestEchoStaticRedirectIndex(t *testing.T) {
}(resp.Body)
assert.Equal(t, http.StatusOK, resp.StatusCode)

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

func TestEchoStartTLSByteString(t *testing.T) {
cert, err := ioutil.ReadFile("_fixture/certs/cert.pem")
cert, err := os.ReadFile("_fixture/certs/cert.pem")
require.NoError(t, err)
key, err := ioutil.ReadFile("_fixture/certs/key.pem")
key, err := os.ReadFile("_fixture/certs/key.pem")
require.NoError(t, err)

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

if body, err := ioutil.ReadAll(resp.Body); err == nil {
if body, err := io.ReadAll(resp.Body); err == nil {
assert.Equal(t, "OK", string(body))
} else {
assert.Fail(t, err.Error())
Expand Down Expand Up @@ -1495,9 +1494,9 @@ func TestEcho_ListenerAddr(t *testing.T) {
}

func TestEcho_TLSListenerAddr(t *testing.T) {
cert, err := ioutil.ReadFile("_fixture/certs/cert.pem")
cert, err := os.ReadFile("_fixture/certs/cert.pem")
require.NoError(t, err)
key, err := ioutil.ReadFile("_fixture/certs/key.pem")
key, err := os.ReadFile("_fixture/certs/key.pem")
require.NoError(t, err)

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

func TestEcho_StartServer(t *testing.T) {
cert, err := ioutil.ReadFile("_fixture/certs/cert.pem")
cert, err := os.ReadFile("_fixture/certs/cert.pem")
require.NoError(t, err)
key, err := ioutil.ReadFile("_fixture/certs/key.pem")
key, err := os.ReadFile("_fixture/certs/key.pem")
require.NoError(t, err)
certs, err := tls.X509KeyPair(cert, key)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions group_test.go
@@ -1,9 +1,9 @@
package echo

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -32,7 +32,7 @@ func TestGroupFile(t *testing.T) {
e := New()
g := e.Group("/group")
g.File("/walle", "_fixture/images/walle.png")
expectedData, err := ioutil.ReadFile("_fixture/images/walle.png")
expectedData, err := os.ReadFile("_fixture/images/walle.png")
assert.Nil(t, err)
req := httptest.NewRequest(http.MethodGet, "/group/walle", nil)
rec := httptest.NewRecorder()
Expand Down
5 changes: 2 additions & 3 deletions middleware/body_dump.go
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"io"
"io/ioutil"
"net"
"net/http"

Expand Down Expand Up @@ -68,9 +67,9 @@ func BodyDumpWithConfig(config BodyDumpConfig) echo.MiddlewareFunc {
// Request
reqBody := []byte{}
if c.Request().Body != nil { // Read
reqBody, _ = ioutil.ReadAll(c.Request().Body)
reqBody, _ = io.ReadAll(c.Request().Body)
}
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(reqBody)) // Reset
c.Request().Body = io.NopCloser(bytes.NewBuffer(reqBody)) // Reset

// Response
resBody := new(bytes.Buffer)
Expand Down
4 changes: 2 additions & 2 deletions middleware/body_dump_test.go
Expand Up @@ -2,7 +2,7 @@ package middleware

import (
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -19,7 +19,7 @@ func TestBodyDump(t *testing.T) {
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := func(c echo.Context) error {
body, err := ioutil.ReadAll(c.Request().Body)
body, err := io.ReadAll(c.Request().Body)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions middleware/body_limit_test.go
Expand Up @@ -2,7 +2,7 @@ package middleware

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -18,7 +18,7 @@ func TestBodyLimit(t *testing.T) {
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := func(c echo.Context) error {
body, err := ioutil.ReadAll(c.Request().Body)
body, err := io.ReadAll(c.Request().Body)
if err != nil {
return err
}
Expand Down Expand Up @@ -67,18 +67,18 @@ func TestBodyLimitReader(t *testing.T) {
}
reader := &limitedReader{
BodyLimitConfig: config,
reader: ioutil.NopCloser(bytes.NewReader(hw)),
reader: io.NopCloser(bytes.NewReader(hw)),
context: e.NewContext(req, rec),
}

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

// reset reader and read two bytes must succeed
bt := make([]byte, 2)
reader.Reset(ioutil.NopCloser(bytes.NewReader(hw)), e.NewContext(req, rec))
reader.Reset(io.NopCloser(bytes.NewReader(hw)), e.NewContext(req, rec))
n, err := reader.Read(bt)
assert.Equal(t, 2, n)
assert.Equal(t, nil, err)
Expand Down
5 changes: 2 additions & 3 deletions middleware/compress.go
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"compress/gzip"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
Expand Down Expand Up @@ -89,7 +88,7 @@ func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
// nothing is written to body or error is returned.
// See issue #424, #407.
res.Writer = rw
w.Reset(ioutil.Discard)
w.Reset(io.Discard)
}
w.Close()
pool.Put(w)
Expand Down Expand Up @@ -135,7 +134,7 @@ func (w *gzipResponseWriter) Push(target string, opts *http.PushOptions) error {
func gzipCompressPool(config GzipConfig) sync.Pool {
return sync.Pool{
New: func() interface{} {
w, err := gzip.NewWriterLevel(ioutil.Discard, config.Level)
w, err := gzip.NewWriterLevel(io.Discard, config.Level)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions middleware/compress_test.go
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestGzipWithStatic(t *testing.T) {
r, err := gzip.NewReader(rec.Body)
if assert.NoError(t, err) {
defer r.Close()
want, err := ioutil.ReadFile("../_fixture/images/walle.png")
want, err := os.ReadFile("../_fixture/images/walle.png")
if assert.NoError(t, err) {
buf := new(bytes.Buffer)
buf.ReadFrom(r)
Expand Down
12 changes: 6 additions & 6 deletions middleware/decompress_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"compress/gzip"
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -39,7 +39,7 @@ func TestDecompress(t *testing.T) {
c = e.NewContext(req, rec)
h(c)
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
b, err := ioutil.ReadAll(req.Body)
b, err := io.ReadAll(req.Body)
assert.NoError(t, err)
assert.Equal(t, body, string(b))
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestDecompressDefaultConfig(t *testing.T) {
c = e.NewContext(req, rec)
h(c)
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
b, err := ioutil.ReadAll(req.Body)
b, err := io.ReadAll(req.Body)
assert.NoError(t, err)
assert.Equal(t, body, string(b))
}
Expand All @@ -82,7 +82,7 @@ func TestCompressRequestWithoutDecompressMiddleware(t *testing.T) {
e.NewContext(req, rec)
e.ServeHTTP(rec, req)
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
b, err := ioutil.ReadAll(req.Body)
b, err := io.ReadAll(req.Body)
assert.NoError(t, err)
assert.NotEqual(t, b, body)
assert.Equal(t, b, gz)
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestDecompressSkipper(t *testing.T) {
c := e.NewContext(req, rec)
e.ServeHTTP(rec, req)
assert.Equal(t, rec.Header().Get(echo.HeaderContentType), echo.MIMEApplicationJSONCharsetUTF8)
reqBody, err := ioutil.ReadAll(c.Request().Body)
reqBody, err := io.ReadAll(c.Request().Body)
assert.NoError(t, err)
assert.Equal(t, body, string(reqBody))
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestDecompressPoolError(t *testing.T) {
c := e.NewContext(req, rec)
e.ServeHTTP(rec, req)
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
reqBody, err := ioutil.ReadAll(c.Request().Body)
reqBody, err := io.ReadAll(c.Request().Body)
assert.NoError(t, err)
assert.Equal(t, body, string(reqBody))
assert.Equal(t, rec.Code, http.StatusInternalServerError)
Expand Down
4 changes: 2 additions & 2 deletions middleware/proxy_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestProxy(t *testing.T) {
e.Use(ProxyWithConfig(ProxyConfig{
Balancer: rrb,
ModifyResponse: func(res *http.Response) error {
res.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("modified")))
res.Body = io.NopCloser(bytes.NewBuffer([]byte("modified")))
res.Header.Set("X-Modified", "1")
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions middleware/rewrite_test.go
@@ -1,7 +1,7 @@
package middleware

import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestRewriteWithConfigPreMiddleware_Issue1143(t *testing.T) {
assert.Equal(t, http.StatusOK, rec.Code)

defer rec.Result().Body.Close()
bodyBytes, _ := ioutil.ReadAll(rec.Result().Body)
bodyBytes, _ := io.ReadAll(rec.Result().Body)
assert.Equal(t, "hosts", string(bodyBytes))
}
}
Expand Down
4 changes: 2 additions & 2 deletions middleware/timeout_test.go
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -410,7 +410,7 @@ func TestTimeoutWithFullEchoStack(t *testing.T) {
}

assert.Equal(t, tc.expectStatusCode, res.StatusCode)
if body, err := ioutil.ReadAll(res.Body); err == nil {
if body, err := io.ReadAll(res.Body); err == nil {
assert.Equal(t, tc.expectResponse, string(body))
} else {
assert.Fail(t, err.Error())
Expand Down

0 comments on commit 3c4d3b3

Please sign in to comment.