migrate away from deprecated ioutil APIs (#7175)

Co-authored-by: Callum Waters <cmwaters19@gmail.com>
Co-authored-by: M. J. Fromberger <fromberger@interchain.io>
This commit is contained in:
Sharad Chand
2021-10-28 23:19:07 +05:45
committed by GitHub
parent 6108d0a9b5
commit 8441b3715a
58 changed files with 158 additions and 192 deletions

View File

@@ -3,7 +3,6 @@ package client_test
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"
@@ -24,7 +23,7 @@ func NodeSuite(t *testing.T) (service.Service, *config.Config) {
require.NoError(t, err)
// start a tendermint node in the background to test against
dir, err := ioutil.TempDir("/tmp", fmt.Sprint("rpc-client-test-", t.Name()))
dir, err := os.MkdirTemp("/tmp", fmt.Sprint("rpc-client-test-", t.Name()))
require.NoError(t, err)
app := kvstore.NewPersistentKVStoreApplication(dir)

View File

@@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
@@ -219,7 +219,7 @@ func (c *Client) Call(
defer httpResponse.Body.Close()
responseBytes, err := ioutil.ReadAll(httpResponse.Body)
responseBytes, err := io.ReadAll(httpResponse.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
@@ -267,7 +267,7 @@ func (c *Client) sendBatch(ctx context.Context, requests []*jsonRPCBufferedReque
defer httpResponse.Body.Close()
responseBytes, err := ioutil.ReadAll(httpResponse.Body)
responseBytes, err := io.ReadAll(httpResponse.Body)
if err != nil {
return nil, fmt.Errorf("read response body: %w", err)
}

View File

@@ -1,7 +1,7 @@
package client
import (
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
@@ -21,7 +21,7 @@ func TestHTTPClientMakeHTTPDialer(t *testing.T) {
defer tsTLS.Close()
// This silences a TLS handshake error, caused by the dialer just immediately
// disconnecting, which we can just ignore.
tsTLS.Config.ErrorLog = log.New(ioutil.Discard, "", 0)
tsTLS.Config.ErrorLog = log.New(io.Discard, "", 0)
for _, testURL := range []string{ts.URL, tsTLS.URL} {
u, err := newParsedURL(testURL)

View File

@@ -3,7 +3,7 @@ package client
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
@@ -76,7 +76,7 @@ func (c *URIClient) Call(ctx context.Context, method string,
}
defer resp.Body.Close()
responseBytes, err := ioutil.ReadAll(resp.Body)
responseBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response body: %w", err)
}

View File

@@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"reflect"
"sort"
@@ -21,7 +21,7 @@ import (
// jsonrpc calls grab the given method's function info and runs reflect.Call
func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
res := rpctypes.RPCInvalidRequestError(nil,
fmt.Errorf("error reading request body: %w", err),

View File

@@ -2,7 +2,7 @@ package server
import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
@@ -65,7 +65,7 @@ func TestRPCParams(t *testing.T) {
defer res.Body.Close()
// Always expecting back a JSONRPCResponse
assert.NotZero(t, res.StatusCode, "#%d: should always return code", i)
blob, err := ioutil.ReadAll(res.Body)
blob, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("#%d: err reading body: %v", i, err)
continue
@@ -112,7 +112,7 @@ func TestJSONRPCID(t *testing.T) {
res := rec.Result()
// Always expecting back a JSONRPCResponse
assert.NotZero(t, res.StatusCode, "#%d: should always return code", i)
blob, err := ioutil.ReadAll(res.Body)
blob, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("#%d: err reading body: %v", i, err)
continue
@@ -142,7 +142,7 @@ func TestRPCNotification(t *testing.T) {
// Always expecting back a JSONRPCResponse
require.True(t, statusOK(res.StatusCode), "should always return 2XX")
blob, err := ioutil.ReadAll(res.Body)
blob, err := io.ReadAll(res.Body)
res.Body.Close()
require.Nil(t, err, "reading from the body should not give back an error")
require.Equal(t, len(blob), 0, "a notification SHOULD NOT be responded to by the server")
@@ -178,7 +178,7 @@ func TestRPCNotificationInBatch(t *testing.T) {
res := rec.Result()
// Always expecting back a JSONRPCResponse
assert.True(t, statusOK(res.StatusCode), "#%d: should always return 2XX", i)
blob, err := ioutil.ReadAll(res.Body)
blob, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("#%d: err reading body: %v", i, err)
continue
@@ -239,7 +239,7 @@ func TestRPCResponseCache(t *testing.T) {
require.True(t, statusOK(res.StatusCode), "should always return 2XX")
require.Equal(t, "max-age=31536000", res.Header.Get("Cache-control"))
_, err := ioutil.ReadAll(res.Body)
_, err := io.ReadAll(res.Body)
res.Body.Close()
require.Nil(t, err, "reading from the body should not give back an error")
@@ -254,7 +254,7 @@ func TestRPCResponseCache(t *testing.T) {
require.True(t, statusOK(res.StatusCode), "should always return 2XX")
require.Equal(t, "", res.Header.Get("Cache-control"))
_, err = ioutil.ReadAll(res.Body)
_, err = io.ReadAll(res.Body)
res.Body.Close()
require.Nil(t, err, "reading from the body should not give back an error")
}

View File

@@ -4,7 +4,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
@@ -101,7 +101,7 @@ func TestServeTLS(t *testing.T) {
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
assert.Equal(t, []byte("some body"), body)
}
@@ -114,7 +114,7 @@ func TestWriteRPCResponseHTTP(t *testing.T) {
err := WriteRPCResponseHTTP(w, true, rpctypes.NewRPCSuccessResponse(id, &sampleResult{"hello"}))
require.NoError(t, err)
resp := w.Result()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
@@ -136,7 +136,7 @@ func TestWriteRPCResponseHTTP(t *testing.T) {
rpctypes.NewRPCSuccessResponse(id, &sampleResult{"world"}))
require.NoError(t, err)
resp = w.Result()
body, err = ioutil.ReadAll(resp.Body)
body, err = io.ReadAll(resp.Body)
_ = resp.Body.Close()
require.NoError(t, err)
@@ -165,7 +165,7 @@ func TestWriteRPCResponseHTTPError(t *testing.T) {
err := WriteRPCResponseHTTPError(w, rpctypes.RPCInternalError(rpctypes.JSONRPCIntID(-1), errors.New("foo")))
require.NoError(t, err)
resp := w.Result()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
require.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)