From e06da449efe9bf6728dcdc5edf8568ddbbc28789 Mon Sep 17 00:00:00 2001 From: komisan19 <18901496+komisan19@users.noreply.github.com> Date: Sun, 29 Dec 2024 02:45:38 +0900 Subject: [PATCH 1/3] refactor: fix deprecated for ioutil --- io/ringbuffer_test.go | 3 +-- middleware/logging_test.go | 4 ++-- .../protocol/middleware_capture_request_test.go | 14 +++++++------- testing/bytes.go | 11 +++++------ testing/struct_test.go | 5 ++--- transport/http/middleware_close_response_body.go | 8 ++++---- transport/http/middleware_http_logging_test.go | 9 ++++----- transport/http/request.go | 5 ++--- transport/http/request_test.go | 3 +-- 9 files changed, 28 insertions(+), 34 deletions(-) diff --git a/io/ringbuffer_test.go b/io/ringbuffer_test.go index 438bf22e2..80540f070 100644 --- a/io/ringbuffer_test.go +++ b/io/ringbuffer_test.go @@ -3,7 +3,6 @@ package io import ( "bytes" "io" - "io/ioutil" "strconv" "strings" "testing" @@ -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 diff --git a/middleware/logging_test.go b/middleware/logging_test.go index 86c8116ed..8e0f4c2a8 100644 --- a/middleware/logging_test.go +++ b/middleware/logging_test.go @@ -2,7 +2,7 @@ package middleware_test import ( "context" - "io/ioutil" + "io" "testing" "github.com/aws/smithy-go/logging" @@ -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 { diff --git a/private/protocol/middleware_capture_request_test.go b/private/protocol/middleware_capture_request_test.go index 0579260a3..2777f720a 100644 --- a/private/protocol/middleware_capture_request_test.go +++ b/private/protocol/middleware_capture_request_test.go @@ -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 @@ -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{ { @@ -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) } diff --git a/testing/bytes.go b/testing/bytes.go index 955612552..331a8ae3d 100644 --- a/testing/bytes.go +++ b/testing/bytes.go @@ -5,7 +5,6 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" ) // Enumeration values for supported compress Algorithms. @@ -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) } @@ -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) } @@ -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) } @@ -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 } @@ -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) } diff --git a/testing/struct_test.go b/testing/struct_test.go index c1ff11b20..89631a023 100644 --- a/testing/struct_test.go +++ b/testing/struct_test.go @@ -3,7 +3,6 @@ package testing import ( "bytes" "io" - "io/ioutil" "math" "strings" "testing" @@ -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, }, }, @@ -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: ".Foo: bytes do not match", diff --git a/transport/http/middleware_close_response_body.go b/transport/http/middleware_close_response_body.go index 1d3b218a1..914338f2e 100644 --- a/transport/http/middleware_close_response_body.go +++ b/transport/http/middleware_close_response_body.go @@ -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 @@ -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() } @@ -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") } diff --git a/transport/http/middleware_http_logging_test.go b/transport/http/middleware_http_logging_test.go index 14bef3ffe..b87358856 100644 --- a/transport/http/middleware_http_logging_test.go +++ b/transport/http/middleware_http_logging_test.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "net/url" "testing" @@ -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" + @@ -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" + @@ -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" + @@ -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" + diff --git a/transport/http/request.go b/transport/http/request.go index 7177d6f95..5cbf6f10a 100644 --- a/transport/http/request.go +++ b/transport/http/request.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "net/url" "strings" @@ -167,7 +166,7 @@ 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 @@ -175,7 +174,7 @@ func (r *Request) Build(ctx context.Context) *http.Request { // 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)) } } diff --git a/transport/http/request_test.go b/transport/http/request_test.go index af88fbc36..fafd50f95 100644 --- a/transport/http/request_test.go +++ b/transport/http/request_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "io" - "io/ioutil" "net/http" "os" "strconv" @@ -136,7 +135,7 @@ func TestRequestSetStream(t *testing.T) { expectNilBody: true, }, "unseekable no len stream": { - reader: ioutil.NopCloser(bytes.NewBuffer([]byte("abc123"))), + reader: io.NopCloser(bytes.NewBuffer([]byte("abc123"))), expectContentLength: -1, expectNilStream: false, expectNilBody: false, From 1fe40e40b46ad83a468441b98ea131cea0042cdf Mon Sep 17 00:00:00 2001 From: komisan19 <18901496+komisan19@users.noreply.github.com> Date: Wed, 15 Jan 2025 00:20:43 +0900 Subject: [PATCH 2/3] add changelog --- .../e16fb60e-d28a-11ef-828f-6a929c47a931.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .changelog/e16fb60e-d28a-11ef-828f-6a929c47a931.json diff --git a/.changelog/e16fb60e-d28a-11ef-828f-6a929c47a931.json b/.changelog/e16fb60e-d28a-11ef-828f-6a929c47a931.json new file mode 100644 index 000000000..89ab31c80 --- /dev/null +++ b/.changelog/e16fb60e-d28a-11ef-828f-6a929c47a931.json @@ -0,0 +1,14 @@ +{ + "id": "e16fb60e-d28a-11ef-828f-6a929c47a931", + "type": "bugfix", + "description": "refactor: fix deprecated for ioutil", + "collapse": true, + "modules": [ + ".", + "io", + "middleware", + "private/protocol", + "testing", + "transport/http" + ] +} From f402177a82d7ba55497b2d0fc562c2ab4a8eebba Mon Sep 17 00:00:00 2001 From: Luc Talatinian <102624213+lucix-aws@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:01:40 -0500 Subject: [PATCH 3/3] fix changelog --- .changelog/e16fb60e-d28a-11ef-828f-6a929c47a931.json | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/.changelog/e16fb60e-d28a-11ef-828f-6a929c47a931.json b/.changelog/e16fb60e-d28a-11ef-828f-6a929c47a931.json index 89ab31c80..bc3ce1ad1 100644 --- a/.changelog/e16fb60e-d28a-11ef-828f-6a929c47a931.json +++ b/.changelog/e16fb60e-d28a-11ef-828f-6a929c47a931.json @@ -1,14 +1,9 @@ { "id": "e16fb60e-d28a-11ef-828f-6a929c47a931", "type": "bugfix", - "description": "refactor: fix deprecated for ioutil", - "collapse": true, + "description": "Replace usages of deprecated ioutil package.", + "collapse": false, "modules": [ - ".", - "io", - "middleware", - "private/protocol", - "testing", - "transport/http" + "." ] }