Skip to content

Commit

Permalink
refactor: fix deprecated for ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
komisan19 committed Dec 28, 2024
1 parent 10fbeed commit 6ac1b9d
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 32 deletions.
3 changes: 1 addition & 2 deletions io/ringbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io
import (
"bytes"
"io"
"io/ioutil"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -449,7 +448,7 @@ func TestRingBufferWriteRead(t *testing.T) {
t.Errorf("expect %v, got %v", e, a)
}

actual, err := ioutil.ReadAll(ringBuffer)
actual, err := io.ReadAll(ringBuffer)
if err != nil {
t.Errorf("unexpected error, %v", err)
return
Expand Down
4 changes: 2 additions & 2 deletions middleware/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package middleware_test

import (
"context"
"io/ioutil"
"io"
"testing"

"github.com/aws/smithy-go/logging"
Expand All @@ -26,7 +26,7 @@ func TestGetLogger(t *testing.T) {
t.Fatal("expect GetLogger to fallback to Nop")
}

standardLogger := logging.NewStandardLogger(ioutil.Discard)
standardLogger := logging.NewStandardLogger(io.Discard)
ctx := middleware.SetLogger(context.Background(), standardLogger)

if logger := middleware.GetLogger(ctx); logger == nil {
Expand Down
14 changes: 7 additions & 7 deletions private/protocol/middleware_capture_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package protocol

import (
"context"
"github.com/aws/smithy-go/middleware"
smithytesting "github.com/aws/smithy-go/testing"
smithyhttp "github.com/aws/smithy-go/transport/http"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"

"github.com/aws/smithy-go/middleware"
smithytesting "github.com/aws/smithy-go/testing"
smithyhttp "github.com/aws/smithy-go/transport/http"
)

// TestAddCaptureRequestMiddleware tests AddCaptureRequestMiddleware
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestAddCaptureRequestMiddleware(t *testing.T) {
Path: "test/path",
RawPath: "test/path",
},
Body: ioutil.NopCloser(strings.NewReader("hello world.")),
Body: io.NopCloser(strings.NewReader("hello world.")),
},
ExpectQuery: []smithytesting.QueryItem{
{
Expand Down Expand Up @@ -95,11 +95,11 @@ func TestAddCaptureRequestMiddleware(t *testing.T) {
t.Errorf("expect %v path, got %v", e, a)
}
if c.ExpectRequest.Body != nil {
expect, err := ioutil.ReadAll(c.ExpectRequest.Body)
expect, err := io.ReadAll(c.ExpectRequest.Body)
if capturedRequest.Body == nil {
t.Errorf("Expect request stream %v captured, get nil", string(expect))
}
actual, err := ioutil.ReadAll(capturedRequest.Body)
actual, err := io.ReadAll(capturedRequest.Body)
if err != nil {
t.Errorf("unable to read captured request body, %v", err)
}
Expand Down
11 changes: 5 additions & 6 deletions testing/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
)

// Enumeration values for supported compress Algorithms.
Expand All @@ -25,7 +24,7 @@ func CompareReaderEmpty(r io.Reader) error {
if r == nil {
return nil
}
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil && err != io.EOF {
return fmt.Errorf("unable to read from reader, %v", err)
}
Expand All @@ -41,7 +40,7 @@ func CompareReaderBytes(r io.Reader, expect []byte) error {
if r == nil {
return fmt.Errorf("missing body")
}
actual, err := ioutil.ReadAll(r)
actual, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("unable to read, %v", err)
}
Expand All @@ -60,7 +59,7 @@ func CompareJSONReaderBytes(r io.Reader, expect []byte) error {
if r == nil {
return fmt.Errorf("missing body")
}
actual, err := ioutil.ReadAll(r)
actual, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("unable to read, %v", err)
}
Expand All @@ -77,7 +76,7 @@ func CompareXMLReaderBytes(r io.Reader, expect []byte) error {
return fmt.Errorf("missing body")
}

actual, err := ioutil.ReadAll(r)
actual, err := io.ReadAll(r)
if err != nil {
return err
}
Expand All @@ -95,7 +94,7 @@ func CompareURLFormReaderBytes(r io.Reader, expect []byte) error {
if r == nil {
return fmt.Errorf("missing body")
}
actual, err := ioutil.ReadAll(r)
actual, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("unable to read, %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions testing/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testing
import (
"bytes"
"io"
"io/ioutil"
"math"
"strings"
"testing"
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestCompareValues(t *testing.T) {
Foo io.Reader
Bar int
}{
Foo: ioutil.NopCloser(strings.NewReader("abc123")),
Foo: io.NopCloser(strings.NewReader("abc123")),
Bar: 123,
},
},
Expand All @@ -99,7 +98,7 @@ func TestCompareValues(t *testing.T) {
Foo io.Reader
Bar int
}{
Foo: ioutil.NopCloser(strings.NewReader("123abc")),
Foo: io.NopCloser(strings.NewReader("123abc")),
Bar: 123,
},
ExpectErr: "<root>.Foo: bytes do not match",
Expand Down
8 changes: 4 additions & 4 deletions transport/http/middleware_close_response_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package http

import (
"context"
"io"

"github.com/aws/smithy-go/logging"
"github.com/aws/smithy-go/middleware"
"io"
"io/ioutil"
)

// AddErrorCloseResponseBodyMiddleware adds the middleware to automatically
Expand All @@ -30,7 +30,7 @@ func (m *errorCloseResponseBodyMiddleware) HandleDeserialize(
if err != nil {
if resp, ok := out.RawResponse.(*Response); ok && resp != nil && resp.Body != nil {
// Consume the full body to prevent TCP connection resets on some platforms
_, _ = io.Copy(ioutil.Discard, resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
// Do not validate that the response closes successfully.
resp.Body.Close()
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func (m *closeResponseBody) HandleDeserialize(

if resp, ok := out.RawResponse.(*Response); ok {
// Consume the full body to prevent TCP connection resets on some platforms
_, copyErr := io.Copy(ioutil.Discard, resp.Body)
_, copyErr := io.Copy(io.Discard, resp.Body)
if copyErr != nil {
middleware.GetLogger(ctx).Logf(logging.Warn, "failed to discard remaining HTTP response body, this may affect connection reuse")
}
Expand Down
9 changes: 4 additions & 5 deletions transport/http/middleware_http_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -49,7 +48,7 @@ func TestRequestResponseLogger(t *testing.T) {
},
},
},
InputBody: ioutil.NopCloser(bytes.NewReader([]byte(`this is the body`))),
InputBody: io.NopCloser(bytes.NewReader([]byte(`this is the body`))),
ExpectedLog: "Request\n" +
"GET /foo HTTP/1.1\r\n" +
"Host: example.amazonaws.com\r\n" +
Expand All @@ -75,7 +74,7 @@ func TestRequestResponseLogger(t *testing.T) {
ContentLength: 16,
},
},
InputBody: ioutil.NopCloser(bytes.NewReader([]byte(`this is the body`))),
InputBody: io.NopCloser(bytes.NewReader([]byte(`this is the body`))),
ExpectedLog: "Request\n" +
"GET /foo HTTP/1.1\r\n" +
"Host: example.amazonaws.com\r\n" +
Expand All @@ -98,7 +97,7 @@ func TestRequestResponseLogger(t *testing.T) {
Header: map[string][]string{
"Foo": {"Bar"},
},
Body: ioutil.NopCloser(bytes.NewReader([]byte(`this is the body`))),
Body: io.NopCloser(bytes.NewReader([]byte(`this is the body`))),
},
},
ExpectedLog: "Response\n" +
Expand All @@ -119,7 +118,7 @@ func TestRequestResponseLogger(t *testing.T) {
Header: map[string][]string{
"Foo": {"Bar"},
},
Body: ioutil.NopCloser(bytes.NewReader([]byte(`this is the body`))),
Body: io.NopCloser(bytes.NewReader([]byte(`this is the body`))),
},
},
ExpectedLog: "Response\n" +
Expand Down
5 changes: 2 additions & 3 deletions transport/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -167,15 +166,15 @@ func (r *Request) Build(ctx context.Context) *http.Request {

switch stream := r.stream.(type) {
case *io.PipeReader:
req.Body = ioutil.NopCloser(stream)
req.Body = io.NopCloser(stream)
req.ContentLength = -1
default:
// HTTP Client Request must only have a non-nil body if the
// ContentLength is explicitly unknown (-1) or non-zero. The HTTP
// Client will interpret a non-nil body and ContentLength 0 as
// "unknown". This is unwanted behavior.
if req.ContentLength != 0 && r.stream != nil {
req.Body = iointernal.NewSafeReadCloser(ioutil.NopCloser(stream))
req.Body = iointernal.NewSafeReadCloser(io.NopCloser(stream))
}
}

Expand Down

0 comments on commit 6ac1b9d

Please sign in to comment.