mirror of
https://github.com/labstack/echo.git
synced 2024-11-28 08:38:39 +02:00
Replace "io/ioutil"
"io/ioutil" pakcage has been deprecated since Go 1.16.
This commit is contained in:
parent
be23ab67cc
commit
3c4d3b3083
4
echo.go
4
echo.go
@ -43,10 +43,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
stdLog "log"
|
stdLog "log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
@ -700,7 +700,7 @@ func (e *Echo) StartTLS(address string, certFile, keyFile interface{}) (err erro
|
|||||||
func filepathOrContent(fileOrContent interface{}) (content []byte, err error) {
|
func filepathOrContent(fileOrContent interface{}) (content []byte, err error) {
|
||||||
switch v := fileOrContent.(type) {
|
switch v := fileOrContent.(type) {
|
||||||
case string:
|
case string:
|
||||||
return ioutil.ReadFile(v)
|
return os.ReadFile(v)
|
||||||
case []byte:
|
case []byte:
|
||||||
return v, nil
|
return v, nil
|
||||||
default:
|
default:
|
||||||
|
17
echo_test.go
17
echo_test.go
@ -7,7 +7,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
@ -244,7 +243,7 @@ func TestEchoStaticRedirectIndex(t *testing.T) {
|
|||||||
}(resp.Body)
|
}(resp.Body)
|
||||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
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>"))
|
assert.Equal(t, true, strings.HasPrefix(string(body), "<!doctype html>"))
|
||||||
} else {
|
} else {
|
||||||
assert.Fail(t, err.Error())
|
assert.Fail(t, err.Error())
|
||||||
@ -1032,9 +1031,9 @@ func TestEchoStartTLSAndStart(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEchoStartTLSByteString(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)
|
require.NoError(t, err)
|
||||||
key, err := ioutil.ReadFile("_fixture/certs/key.pem")
|
key, err := os.ReadFile("_fixture/certs/key.pem")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
@ -1413,7 +1412,7 @@ func TestEchoListenerNetwork(t *testing.T) {
|
|||||||
}(resp.Body)
|
}(resp.Body)
|
||||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
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))
|
assert.Equal(t, "OK", string(body))
|
||||||
} else {
|
} else {
|
||||||
assert.Fail(t, err.Error())
|
assert.Fail(t, err.Error())
|
||||||
@ -1495,9 +1494,9 @@ func TestEcho_ListenerAddr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEcho_TLSListenerAddr(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)
|
require.NoError(t, err)
|
||||||
key, err := ioutil.ReadFile("_fixture/certs/key.pem")
|
key, err := os.ReadFile("_fixture/certs/key.pem")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
e := New()
|
e := New()
|
||||||
@ -1515,9 +1514,9 @@ func TestEcho_TLSListenerAddr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEcho_StartServer(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)
|
require.NoError(t, err)
|
||||||
key, err := ioutil.ReadFile("_fixture/certs/key.pem")
|
key, err := os.ReadFile("_fixture/certs/key.pem")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
certs, err := tls.X509KeyPair(cert, key)
|
certs, err := tls.X509KeyPair(cert, key)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package echo
|
package echo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -32,7 +32,7 @@ func TestGroupFile(t *testing.T) {
|
|||||||
e := New()
|
e := New()
|
||||||
g := e.Group("/group")
|
g := e.Group("/group")
|
||||||
g.File("/walle", "_fixture/images/walle.png")
|
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)
|
assert.Nil(t, err)
|
||||||
req := httptest.NewRequest(http.MethodGet, "/group/walle", nil)
|
req := httptest.NewRequest(http.MethodGet, "/group/walle", nil)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@ -68,9 +67,9 @@ func BodyDumpWithConfig(config BodyDumpConfig) echo.MiddlewareFunc {
|
|||||||
// Request
|
// Request
|
||||||
reqBody := []byte{}
|
reqBody := []byte{}
|
||||||
if c.Request().Body != nil { // Read
|
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
|
// Response
|
||||||
resBody := new(bytes.Buffer)
|
resBody := new(bytes.Buffer)
|
||||||
|
@ -2,7 +2,7 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
@ -19,7 +19,7 @@ func TestBodyDump(t *testing.T) {
|
|||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
c := e.NewContext(req, rec)
|
c := e.NewContext(req, rec)
|
||||||
h := func(c echo.Context) error {
|
h := func(c echo.Context) error {
|
||||||
body, err := ioutil.ReadAll(c.Request().Body)
|
body, err := io.ReadAll(c.Request().Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
@ -18,7 +18,7 @@ func TestBodyLimit(t *testing.T) {
|
|||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
c := e.NewContext(req, rec)
|
c := e.NewContext(req, rec)
|
||||||
h := func(c echo.Context) error {
|
h := func(c echo.Context) error {
|
||||||
body, err := ioutil.ReadAll(c.Request().Body)
|
body, err := io.ReadAll(c.Request().Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -67,18 +67,18 @@ func TestBodyLimitReader(t *testing.T) {
|
|||||||
}
|
}
|
||||||
reader := &limitedReader{
|
reader := &limitedReader{
|
||||||
BodyLimitConfig: config,
|
BodyLimitConfig: config,
|
||||||
reader: ioutil.NopCloser(bytes.NewReader(hw)),
|
reader: io.NopCloser(bytes.NewReader(hw)),
|
||||||
context: e.NewContext(req, rec),
|
context: e.NewContext(req, rec),
|
||||||
}
|
}
|
||||||
|
|
||||||
// read all should return ErrStatusRequestEntityTooLarge
|
// read all should return ErrStatusRequestEntityTooLarge
|
||||||
_, err := ioutil.ReadAll(reader)
|
_, err := io.ReadAll(reader)
|
||||||
he := err.(*echo.HTTPError)
|
he := err.(*echo.HTTPError)
|
||||||
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code)
|
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code)
|
||||||
|
|
||||||
// reset reader and read two bytes must succeed
|
// reset reader and read two bytes must succeed
|
||||||
bt := make([]byte, 2)
|
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)
|
n, err := reader.Read(bt)
|
||||||
assert.Equal(t, 2, n)
|
assert.Equal(t, 2, n)
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -89,7 +88,7 @@ func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
|
|||||||
// nothing is written to body or error is returned.
|
// nothing is written to body or error is returned.
|
||||||
// See issue #424, #407.
|
// See issue #424, #407.
|
||||||
res.Writer = rw
|
res.Writer = rw
|
||||||
w.Reset(ioutil.Discard)
|
w.Reset(io.Discard)
|
||||||
}
|
}
|
||||||
w.Close()
|
w.Close()
|
||||||
pool.Put(w)
|
pool.Put(w)
|
||||||
@ -135,7 +134,7 @@ func (w *gzipResponseWriter) Push(target string, opts *http.PushOptions) error {
|
|||||||
func gzipCompressPool(config GzipConfig) sync.Pool {
|
func gzipCompressPool(config GzipConfig) sync.Pool {
|
||||||
return sync.Pool{
|
return sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
w, err := gzip.NewWriterLevel(ioutil.Discard, config.Level)
|
w, err := gzip.NewWriterLevel(io.Discard, config.Level)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,9 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
@ -173,7 +173,7 @@ func TestGzipWithStatic(t *testing.T) {
|
|||||||
r, err := gzip.NewReader(rec.Body)
|
r, err := gzip.NewReader(rec.Body)
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
want, err := ioutil.ReadFile("../_fixture/images/walle.png")
|
want, err := os.ReadFile("../_fixture/images/walle.png")
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(r)
|
buf.ReadFrom(r)
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
@ -39,7 +39,7 @@ func TestDecompress(t *testing.T) {
|
|||||||
c = e.NewContext(req, rec)
|
c = e.NewContext(req, rec)
|
||||||
h(c)
|
h(c)
|
||||||
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
|
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.NoError(t, err)
|
||||||
assert.Equal(t, body, string(b))
|
assert.Equal(t, body, string(b))
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ func TestDecompressDefaultConfig(t *testing.T) {
|
|||||||
c = e.NewContext(req, rec)
|
c = e.NewContext(req, rec)
|
||||||
h(c)
|
h(c)
|
||||||
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
|
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.NoError(t, err)
|
||||||
assert.Equal(t, body, string(b))
|
assert.Equal(t, body, string(b))
|
||||||
}
|
}
|
||||||
@ -82,7 +82,7 @@ func TestCompressRequestWithoutDecompressMiddleware(t *testing.T) {
|
|||||||
e.NewContext(req, rec)
|
e.NewContext(req, rec)
|
||||||
e.ServeHTTP(rec, req)
|
e.ServeHTTP(rec, req)
|
||||||
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
|
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.NoError(t, err)
|
||||||
assert.NotEqual(t, b, body)
|
assert.NotEqual(t, b, body)
|
||||||
assert.Equal(t, b, gz)
|
assert.Equal(t, b, gz)
|
||||||
@ -132,7 +132,7 @@ func TestDecompressSkipper(t *testing.T) {
|
|||||||
c := e.NewContext(req, rec)
|
c := e.NewContext(req, rec)
|
||||||
e.ServeHTTP(rec, req)
|
e.ServeHTTP(rec, req)
|
||||||
assert.Equal(t, rec.Header().Get(echo.HeaderContentType), echo.MIMEApplicationJSONCharsetUTF8)
|
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.NoError(t, err)
|
||||||
assert.Equal(t, body, string(reqBody))
|
assert.Equal(t, body, string(reqBody))
|
||||||
}
|
}
|
||||||
@ -161,7 +161,7 @@ func TestDecompressPoolError(t *testing.T) {
|
|||||||
c := e.NewContext(req, rec)
|
c := e.NewContext(req, rec)
|
||||||
e.ServeHTTP(rec, req)
|
e.ServeHTTP(rec, req)
|
||||||
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
|
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.NoError(t, err)
|
||||||
assert.Equal(t, body, string(reqBody))
|
assert.Equal(t, body, string(reqBody))
|
||||||
assert.Equal(t, rec.Code, http.StatusInternalServerError)
|
assert.Equal(t, rec.Code, http.StatusInternalServerError)
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
@ -93,7 +93,7 @@ func TestProxy(t *testing.T) {
|
|||||||
e.Use(ProxyWithConfig(ProxyConfig{
|
e.Use(ProxyWithConfig(ProxyConfig{
|
||||||
Balancer: rrb,
|
Balancer: rrb,
|
||||||
ModifyResponse: func(res *http.Response) error {
|
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")
|
res.Header.Set("X-Modified", "1")
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -142,7 +142,7 @@ func TestRewriteWithConfigPreMiddleware_Issue1143(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
assert.Equal(t, http.StatusOK, rec.Code)
|
||||||
|
|
||||||
defer rec.Result().Body.Close()
|
defer rec.Result().Body.Close()
|
||||||
bodyBytes, _ := ioutil.ReadAll(rec.Result().Body)
|
bodyBytes, _ := io.ReadAll(rec.Result().Body)
|
||||||
assert.Equal(t, "hosts", string(bodyBytes))
|
assert.Equal(t, "hosts", string(bodyBytes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -410,7 +410,7 @@ func TestTimeoutWithFullEchoStack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, tc.expectStatusCode, res.StatusCode)
|
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))
|
assert.Equal(t, tc.expectResponse, string(body))
|
||||||
} else {
|
} else {
|
||||||
assert.Fail(t, err.Error())
|
assert.Fail(t, err.Error())
|
||||||
|
Loading…
Reference in New Issue
Block a user