1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Replace "io/ioutil"

"io/ioutil" pakcage has been deprecated since Go 1.16.
This commit is contained in:
zeek 2022-11-21 21:29:43 +09:00 committed by Martti T
parent be23ab67cc
commit 3c4d3b3083
12 changed files with 37 additions and 40 deletions

View File

@ -43,10 +43,10 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
stdLog "log"
"net"
"net/http"
"os"
"reflect"
"runtime"
"sync"
@ -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:

View File

@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
@ -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())
@ -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 {
@ -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())
@ -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()
@ -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)

View File

@ -1,9 +1,9 @@
package echo
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
@ -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()

View File

@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"io"
"io/ioutil"
"net"
"net/http"
@ -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)

View File

@ -2,7 +2,7 @@ package middleware
import (
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
@ -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
}

View File

@ -2,7 +2,7 @@ package middleware
import (
"bytes"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
@ -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
}
@ -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)

View File

@ -4,7 +4,6 @@ import (
"bufio"
"compress/gzip"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
@ -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)
@ -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
}

View File

@ -4,9 +4,9 @@ import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/labstack/echo/v4"
@ -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)

View File

@ -4,7 +4,7 @@ import (
"bytes"
"compress/gzip"
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
@ -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))
}
@ -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))
}
@ -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)
@ -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))
}
@ -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)

View File

@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
@ -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
},

View File

@ -1,7 +1,7 @@
package middleware
import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
@ -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))
}
}

View File

@ -5,7 +5,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"net"
"net/http"
@ -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())