diff --git a/autorest/adal/README.md b/autorest/adal/README.md index b11eb0788..97434ea7f 100644 --- a/autorest/adal/README.md +++ b/autorest/adal/README.md @@ -160,7 +160,7 @@ if (err == nil) { ```Go certificatePath := "./example-app.pfx" -certData, err := ioutil.ReadFile(certificatePath) +certData, err := os.ReadFile(certificatePath) if err != nil { return nil, fmt.Errorf("failed to read the certificate file (%s): %v", certificatePath, err) } diff --git a/autorest/adal/cmd/adal.go b/autorest/adal/cmd/adal.go index eebf26784..a6fc1e95a 100644 --- a/autorest/adal/cmd/adal.go +++ b/autorest/adal/cmd/adal.go @@ -18,11 +18,11 @@ import ( "flag" "fmt" "log" + "os" "strings" "crypto/rsa" "crypto/x509" - "io/ioutil" "net/http" "os/user" @@ -203,7 +203,7 @@ func acquireTokenClientCertFlow(oauthConfig adal.OAuthConfig, resource string, callbacks ...adal.TokenRefreshCallback) (*adal.ServicePrincipalToken, error) { - certData, err := ioutil.ReadFile(certificatePath) + certData, err := os.ReadFile(certificatePath) if err != nil { return nil, fmt.Errorf("failed to read the certificate file (%s): %v", certificatePath, err) } diff --git a/autorest/adal/devicetoken.go b/autorest/adal/devicetoken.go index 9daa4b58b..f040e2ac6 100644 --- a/autorest/adal/devicetoken.go +++ b/autorest/adal/devicetoken.go @@ -27,7 +27,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -116,7 +116,7 @@ func InitiateDeviceAuthWithContext(ctx context.Context, sender Sender, oauthConf } s := v.Encode() - body := ioutil.NopCloser(strings.NewReader(s)) + body := io.NopCloser(strings.NewReader(s)) req, err := http.NewRequest(http.MethodPost, oauthConfig.DeviceCodeEndpoint.String(), body) if err != nil { @@ -131,7 +131,7 @@ func InitiateDeviceAuthWithContext(ctx context.Context, sender Sender, oauthConf } defer resp.Body.Close() - rb, err := ioutil.ReadAll(resp.Body) + rb, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("%s %s: %s", logPrefix, errCodeHandlingFails, err.Error()) } @@ -175,7 +175,7 @@ func CheckForUserCompletionWithContext(ctx context.Context, sender Sender, code } s := v.Encode() - body := ioutil.NopCloser(strings.NewReader(s)) + body := io.NopCloser(strings.NewReader(s)) req, err := http.NewRequest(http.MethodPost, code.OAuthConfig.TokenEndpoint.String(), body) if err != nil { @@ -190,7 +190,7 @@ func CheckForUserCompletionWithContext(ctx context.Context, sender Sender, code } defer resp.Body.Close() - rb, err := ioutil.ReadAll(resp.Body) + rb, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("%s %s: %s", logPrefix, errTokenHandlingFails, err.Error()) } diff --git a/autorest/adal/persist.go b/autorest/adal/persist.go index 2a974a39b..fb54a4323 100644 --- a/autorest/adal/persist.go +++ b/autorest/adal/persist.go @@ -20,7 +20,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "os" "path/filepath" @@ -62,7 +61,7 @@ func SaveToken(path string, mode os.FileMode, token Token) error { return fmt.Errorf("failed to create directory (%s) to store token in: %v", dir, err) } - newFile, err := ioutil.TempFile(dir, "token") + newFile, err := os.CreateTemp(dir, "token") if err != nil { return fmt.Errorf("failed to create the temp file to write the token: %v", err) } diff --git a/autorest/adal/persist_test.go b/autorest/adal/persist_test.go index 2c98c127e..7ac7f5e0f 100644 --- a/autorest/adal/persist_test.go +++ b/autorest/adal/persist_test.go @@ -16,7 +16,6 @@ package adal import ( "encoding/json" - "io/ioutil" "os" "path" "reflect" @@ -46,7 +45,7 @@ var TestToken = Token{ } func writeTestTokenFile(t *testing.T, suffix string, contents string) *os.File { - f, err := ioutil.TempFile(os.TempDir(), suffix) + f, err := os.CreateTemp(os.TempDir(), suffix) if err != nil { t.Fatalf("azure: unexpected error when creating temp file: %v", err) } @@ -108,7 +107,7 @@ func token() *Token { } func TestSaveToken(t *testing.T) { - f, err := ioutil.TempFile("", "testloadtoken") + f, err := os.CreateTemp("", "testloadtoken") if err != nil { t.Fatalf("azure: unexpected error when creating temp file: %v", err) } @@ -135,7 +134,7 @@ func TestSaveToken(t *testing.T) { json.Unmarshal([]byte(MockTokenJSON), &expectedToken) - contents, err := ioutil.ReadFile(f.Name()) + contents, err := os.ReadFile(f.Name()) if err != nil { t.Fatal("!!") } diff --git a/autorest/adal/token.go b/autorest/adal/token.go index 2a24ab80c..67baecd83 100644 --- a/autorest/adal/token.go +++ b/autorest/adal/token.go @@ -25,7 +25,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "math" "net/http" "net/url" @@ -1061,7 +1060,7 @@ func (spt *ServicePrincipalToken) refreshInternal(ctx context.Context, resource } else if msiSecret.clientResourceID != "" { data.Set("msi_res_id", msiSecret.clientResourceID) } - req.Body = ioutil.NopCloser(strings.NewReader(data.Encode())) + req.Body = io.NopCloser(strings.NewReader(data.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") break case msiTypeIMDS: @@ -1096,7 +1095,7 @@ func (spt *ServicePrincipalToken) refreshInternal(ctx context.Context, resource } s := v.Encode() - body := ioutil.NopCloser(strings.NewReader(s)) + body := io.NopCloser(strings.NewReader(s)) req.ContentLength = int64(len(s)) req.Header.Set(contentType, mimeTypeFormPost) req.Body = body @@ -1113,7 +1112,7 @@ func (spt *ServicePrincipalToken) refreshInternal(ctx context.Context, resource logger.Instance.WriteResponse(resp, logger.Filter{Body: authBodyFilter}) defer resp.Body.Close() - rb, err := ioutil.ReadAll(resp.Body) + rb, err := io.ReadAll(resp.Body) if resp.StatusCode != http.StatusOK { if err != nil { @@ -1235,7 +1234,7 @@ func retryForIMDS(sender Sender, req *http.Request, maxAttempts int) (resp *http for attempt < maxAttempts { if resp != nil && resp.Body != nil { - io.Copy(ioutil.Discard, resp.Body) + io.Copy(io.Discard, resp.Body) resp.Body.Close() } resp, err = sender.Do(req) diff --git a/autorest/adal/token_test.go b/autorest/adal/token_test.go index 728a0f153..1d556b684 100644 --- a/autorest/adal/token_test.go +++ b/autorest/adal/token_test.go @@ -22,7 +22,7 @@ import ( "crypto/x509/pkix" "encoding/json" "fmt" - "io/ioutil" + "io" "math/big" "net/http" "net/http/httptest" @@ -632,7 +632,7 @@ func testServicePrincipalTokenRefreshSetsBody(t *testing.T, spt *ServicePrincipa (func() SendDecorator { return func(s Sender) Sender { return SenderFunc(func(r *http.Request) (*http.Response, error) { - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("adal: Failed to read body of Service Principal token request (%v)", err) } diff --git a/autorest/azure/async.go b/autorest/azure/async.go index 45575eedb..f119b11d4 100644 --- a/autorest/azure/async.go +++ b/autorest/azure/async.go @@ -19,7 +19,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -318,11 +318,11 @@ func (f Future) GetResult(sender autorest.Sender) (*http.Response, error) { if err == nil && resp.Body != nil { // copy the body and close it so callers don't have to defer resp.Body.Close() - b, err := ioutil.ReadAll(resp.Body) + b, err := io.ReadAll(resp.Body) if err != nil { return resp, err } - resp.Body = ioutil.NopCloser(bytes.NewReader(b)) + resp.Body = io.NopCloser(bytes.NewReader(b)) } return resp, err } @@ -459,12 +459,12 @@ func (pt *pollingTrackerBase) updateRawBody() error { pt.rawBody = map[string]interface{}{} if pt.resp.ContentLength != 0 { defer pt.resp.Body.Close() - b, err := ioutil.ReadAll(pt.resp.Body) + b, err := io.ReadAll(pt.resp.Body) if err != nil { return autorest.NewErrorWithError(err, "pollingTrackerBase", "updateRawBody", nil, "failed to read response body") } // put the body back so it's available to other callers - pt.resp.Body = ioutil.NopCloser(bytes.NewReader(b)) + pt.resp.Body = io.NopCloser(bytes.NewReader(b)) // observed in 204 responses over HTTP/2.0; the content length is -1 but body is empty if len(b) == 0 { return nil @@ -516,11 +516,11 @@ func (pt *pollingTrackerBase) updateErrorFromResponse() { re := respErr{} defer pt.resp.Body.Close() var b []byte - if b, err = ioutil.ReadAll(pt.resp.Body); err != nil { + if b, err = io.ReadAll(pt.resp.Body); err != nil { goto Default } // put the body back so it's available to other callers - pt.resp.Body = ioutil.NopCloser(bytes.NewReader(b)) + pt.resp.Body = io.NopCloser(bytes.NewReader(b)) if len(b) == 0 { goto Default } diff --git a/autorest/azure/auth/auth.go b/autorest/azure/auth/auth.go index e97589dcd..25697b3c8 100644 --- a/autorest/azure/auth/auth.go +++ b/autorest/azure/auth/auth.go @@ -21,7 +21,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "log" "os" "strings" @@ -325,7 +325,7 @@ func GetSettingsFromFile() (FileSettings, error) { return s, errors.New("environment variable AZURE_AUTH_LOCATION is not set") } - contents, err := ioutil.ReadFile(fileLocation) + contents, err := os.ReadFile(fileLocation) if err != nil { return s, err } @@ -488,7 +488,7 @@ func decode(b []byte) ([]byte, error) { } return []byte(string(utf16.Decode(u16))), nil } - return ioutil.ReadAll(reader) + return io.ReadAll(reader) } func (settings FileSettings) getResourceForToken(baseURI string) (string, error) { @@ -636,7 +636,7 @@ func (ccc ClientCertificateConfig) ServicePrincipalToken() (*adal.ServicePrincip if err != nil { return nil, err } - certData, err := ioutil.ReadFile(ccc.CertificatePath) + certData, err := os.ReadFile(ccc.CertificatePath) if err != nil { return nil, fmt.Errorf("failed to read the certificate file (%s): %v", ccc.CertificatePath, err) } @@ -653,7 +653,7 @@ func (ccc ClientCertificateConfig) MultiTenantServicePrincipalToken() (*adal.Mul if err != nil { return nil, err } - certData, err := ioutil.ReadFile(ccc.CertificatePath) + certData, err := os.ReadFile(ccc.CertificatePath) if err != nil { return nil, fmt.Errorf("failed to read the certificate file (%s): %v", ccc.CertificatePath, err) } diff --git a/autorest/azure/azure.go b/autorest/azure/azure.go index 868345db6..09c870809 100644 --- a/autorest/azure/azure.go +++ b/autorest/azure/azure.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "regexp" "strconv" @@ -333,7 +333,7 @@ func WithErrorUnlessStatusCode(codes ...int) autorest.RespondDecorator { // Copy and replace the Body in case it does not contain an error object. // This will leave the Body available to the caller. b, decodeErr := autorest.CopyAndDecode(encodedAs, resp.Body, &e) - resp.Body = ioutil.NopCloser(&b) + resp.Body = io.NopCloser(&b) if decodeErr != nil { return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b, decodeErr) } diff --git a/autorest/azure/azure_test.go b/autorest/azure/azure_test.go index 6400127ac..e5bdf6753 100644 --- a/autorest/azure/azure_test.go +++ b/autorest/azure/azure_test.go @@ -17,7 +17,7 @@ package azure import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "reflect" "strconv" @@ -175,7 +175,7 @@ func TestWithErrorUnlessStatusCode_NotAnAzureError(t *testing.T) { // the error body should still be there defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatal(err) } @@ -226,7 +226,7 @@ func TestWithErrorUnlessStatusCode_FoundAzureErrorWithoutDetails(t *testing.T) { // the error body should still be there defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatal(err) } @@ -305,7 +305,7 @@ func TestWithErrorUnlessStatusCode_FoundAzureFullError(t *testing.T) { // the error body should still be there defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatal(err) } @@ -394,7 +394,7 @@ func TestWithErrorUnlessStatusCode_NoAzureError(t *testing.T) { // the error body should still be there defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatal(err) } @@ -488,7 +488,7 @@ func TestWithErrorUnlessStatusCode_UnwrappedError(t *testing.T) { // the error body should still be there defer r.Body.Close() - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Error(err) } diff --git a/autorest/azure/cli/profile.go b/autorest/azure/cli/profile.go index f45c3a516..16f3be38c 100644 --- a/autorest/azure/cli/profile.go +++ b/autorest/azure/cli/profile.go @@ -18,7 +18,6 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" @@ -66,7 +65,7 @@ func ProfilePath() (string, error) { // LoadProfile restores a Profile object from a file located at 'path'. func LoadProfile(path string) (result Profile, err error) { var contents []byte - contents, err = ioutil.ReadFile(path) + contents, err = os.ReadFile(path) if err != nil { err = fmt.Errorf("failed to open file (%s) while loading token: %v", path, err) return diff --git a/autorest/azure/environments.go b/autorest/azure/environments.go index b0a53769f..4684291a8 100644 --- a/autorest/azure/environments.go +++ b/autorest/azure/environments.go @@ -17,7 +17,6 @@ package azure import ( "encoding/json" "fmt" - "io/ioutil" "os" "strings" ) @@ -315,7 +314,7 @@ func EnvironmentFromName(name string) (Environment, error) { // This function is particularly useful in the Hybrid Cloud model, where one must define their own // endpoints. func EnvironmentFromFile(location string) (unmarshaled Environment, err error) { - fileContents, err := ioutil.ReadFile(location) + fileContents, err := os.ReadFile(location) if err != nil { return } diff --git a/autorest/azure/environments_test.go b/autorest/azure/environments_test.go index 5bf9c66a3..07c40fb24 100644 --- a/autorest/azure/environments_test.go +++ b/autorest/azure/environments_test.go @@ -17,7 +17,6 @@ package azure import ( "encoding/json" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -77,7 +76,7 @@ var testEnvironment1 = Environment{ } func TestEnvironment_EnvironmentFromURL_NoOverride_Success(t *testing.T) { - fileContents, _ := ioutil.ReadFile(filepath.Join("testdata", "test_metadata_environment_1.json")) + fileContents, _ := os.ReadFile(filepath.Join("testdata", "test_metadata_environment_1.json")) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(fileContents)) @@ -95,7 +94,7 @@ func TestEnvironment_EnvironmentFromURL_NoOverride_Success(t *testing.T) { } func TestEnvironment_EnvironmentFromURL_OverrideStorageSuffix_Success(t *testing.T) { - fileContents, _ := ioutil.ReadFile(filepath.Join("testdata", "test_metadata_environment_1.json")) + fileContents, _ := os.ReadFile(filepath.Join("testdata", "test_metadata_environment_1.json")) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(fileContents)) diff --git a/autorest/azure/example/main.go b/autorest/azure/example/main.go index d1de683a6..dd7bfa3ba 100644 --- a/autorest/azure/example/main.go +++ b/autorest/azure/example/main.go @@ -20,9 +20,9 @@ import ( "encoding/json" "flag" "fmt" - "io/ioutil" "log" "net/http" + "os" "strings" "github.com/Azure/go-autorest/autorest" @@ -119,7 +119,7 @@ func decodePkcs12(pkcs []byte, password string) (*x509.Certificate, *rsa.Private } func getSptFromCertificate(oauthConfig adal.OAuthConfig, clientID, resource, certicatePath string, callbacks ...adal.TokenRefreshCallback) (*adal.ServicePrincipalToken, error) { - certData, err := ioutil.ReadFile(certificatePath) + certData, err := os.ReadFile(certificatePath) if err != nil { return nil, fmt.Errorf("failed to read the certificate file (%s): %v", certificatePath, err) } diff --git a/autorest/azure/metadata_environment.go b/autorest/azure/metadata_environment.go index 507f9e95c..f436a4512 100644 --- a/autorest/azure/metadata_environment.go +++ b/autorest/azure/metadata_environment.go @@ -3,7 +3,7 @@ package azure import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strings" @@ -236,7 +236,7 @@ func retrieveMetadataEnvironment(endpoint string) (environment environmentMetada return environment, err } defer response.Body.Close() - jsonResponse, err := ioutil.ReadAll(response.Body) + jsonResponse, err := io.ReadAll(response.Body) if err != nil { return environment, err } diff --git a/autorest/client.go b/autorest/client.go index bb5f9396e..b2f2357e7 100644 --- a/autorest/client.go +++ b/autorest/client.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "net/http" "strings" @@ -106,14 +105,14 @@ func (li LoggingInspector) WithInspection() PrepareDecorator { defer r.Body.Close() - r.Body = ioutil.NopCloser(io.TeeReader(r.Body, &body)) + r.Body = io.NopCloser(io.TeeReader(r.Body, &body)) if err := r.Write(&b); err != nil { return nil, fmt.Errorf("Failed to write response: %v", err) } li.Logger.Printf(requestFormat, b.String()) - r.Body = ioutil.NopCloser(&body) + r.Body = io.NopCloser(&body) return p.Prepare(r) }) } @@ -129,14 +128,14 @@ func (li LoggingInspector) ByInspecting() RespondDecorator { return ResponderFunc(func(resp *http.Response) error { var body, b bytes.Buffer defer resp.Body.Close() - resp.Body = ioutil.NopCloser(io.TeeReader(resp.Body, &body)) + resp.Body = io.NopCloser(io.TeeReader(resp.Body, &body)) if err := resp.Write(&b); err != nil { return fmt.Errorf("Failed to write response: %v", err) } li.Logger.Printf(responseFormat, b.String()) - resp.Body = ioutil.NopCloser(&body) + resp.Body = io.NopCloser(&body) return r.Respond(resp) }) } diff --git a/autorest/client_test.go b/autorest/client_test.go index bdd65a594..d8d717f7d 100644 --- a/autorest/client_test.go +++ b/autorest/client_test.go @@ -19,7 +19,7 @@ import ( "context" "crypto/tls" "fmt" - "io/ioutil" + "io" "log" "math/rand" "net/http" @@ -72,7 +72,7 @@ func TestLoggingInspectorWithInspectionRestoresBody(t *testing.T) { Prepare(r, c.WithInspection()) - s, _ := ioutil.ReadAll(r.Body) + s, _ := io.ReadAll(r.Body) if len(s) <= 0 { t.Fatal("autorest: LoggingInspector#WithInspection did not restore the Request body") } @@ -119,7 +119,7 @@ func TestLoggingInspectorByInspectingRestoresBody(t *testing.T) { Respond(r, c.ByInspecting()) - s, _ := ioutil.ReadAll(r.Body) + s, _ := io.ReadAll(r.Body) if len(s) <= 0 { t.Fatal("autorest: LoggingInspector#ByInspecting did not restore the Response body") } @@ -386,9 +386,9 @@ func TestCookies(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.SetCookie(w, &expected) - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { - t.Fatalf("autorest: ioutil.ReadAll failed reading request body: %s", err) + t.Fatalf("autorest: io.ReadAll failed reading request body: %s", err) } if string(b) == second { cookie, err := r.Cookie(expected.Name) diff --git a/autorest/preparer.go b/autorest/preparer.go index 121a66fa8..f6de8c5e9 100644 --- a/autorest/preparer.go +++ b/autorest/preparer.go @@ -21,7 +21,6 @@ import ( "encoding/xml" "fmt" "io" - "io/ioutil" "mime/multipart" "net/http" "net/url" @@ -268,7 +267,7 @@ func WithBytes(input *[]byte) PrepareDecorator { } r.ContentLength = int64(len(*input)) - r.Body = ioutil.NopCloser(bytes.NewReader(*input)) + r.Body = io.NopCloser(bytes.NewReader(*input)) } return r, err }) @@ -296,7 +295,7 @@ func WithFormData(v url.Values) PrepareDecorator { setHeader(r, http.CanonicalHeaderKey(headerContentType), mimeTypeFormPost) r.ContentLength = int64(len(s)) - r.Body = ioutil.NopCloser(strings.NewReader(s)) + r.Body = io.NopCloser(strings.NewReader(s)) } return r, err }) @@ -331,7 +330,7 @@ func WithMultiPartFormData(formDataParameters map[string]interface{}) PrepareDec return r, err } setHeader(r, http.CanonicalHeaderKey(headerContentType), writer.FormDataContentType()) - r.Body = ioutil.NopCloser(bytes.NewReader(body.Bytes())) + r.Body = io.NopCloser(bytes.NewReader(body.Bytes())) r.ContentLength = int64(body.Len()) return r, err } @@ -346,11 +345,11 @@ func WithFile(f io.ReadCloser) PrepareDecorator { return PreparerFunc(func(r *http.Request) (*http.Request, error) { r, err := p.Prepare(r) if err == nil { - b, err := ioutil.ReadAll(f) + b, err := io.ReadAll(f) if err != nil { return r, err } - r.Body = ioutil.NopCloser(bytes.NewReader(b)) + r.Body = io.NopCloser(bytes.NewReader(b)) r.ContentLength = int64(len(b)) } return r, err @@ -396,7 +395,7 @@ func WithString(v string) PrepareDecorator { r, err := p.Prepare(r) if err == nil { r.ContentLength = int64(len(v)) - r.Body = ioutil.NopCloser(strings.NewReader(v)) + r.Body = io.NopCloser(strings.NewReader(v)) } return r, err }) @@ -413,7 +412,7 @@ func WithJSON(v interface{}) PrepareDecorator { b, err := json.Marshal(v) if err == nil { r.ContentLength = int64(len(b)) - r.Body = ioutil.NopCloser(bytes.NewReader(b)) + r.Body = io.NopCloser(bytes.NewReader(b)) } } return r, err @@ -436,7 +435,7 @@ func WithXML(v interface{}) PrepareDecorator { r.ContentLength = int64(len(bytesWithHeader)) setHeader(r, headerContentLength, fmt.Sprintf("%d", len(bytesWithHeader))) - r.Body = ioutil.NopCloser(bytes.NewReader(bytesWithHeader)) + r.Body = io.NopCloser(bytes.NewReader(bytesWithHeader)) } } return r, err diff --git a/autorest/preparer_test.go b/autorest/preparer_test.go index cf88b982f..0c74793da 100644 --- a/autorest/preparer_test.go +++ b/autorest/preparer_test.go @@ -17,7 +17,7 @@ package autorest import ( "context" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "reflect" @@ -175,7 +175,7 @@ func TestWithBytes(t *testing.T) { t.Fatalf("ERROR: %v\n", err) } - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("ERROR: %v\n", err) } @@ -237,7 +237,7 @@ func ExampleWithFormData() { fmt.Printf("ERROR: %v\n", err) } - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { fmt.Printf("ERROR: %v\n", err) } else { @@ -256,7 +256,7 @@ func ExampleWithJSON() { fmt.Printf("ERROR: %v\n", err) } - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { fmt.Printf("ERROR: %v\n", err) } else { @@ -275,7 +275,7 @@ func ExampleWithXML() { fmt.Printf("ERROR: %v\n", err) } - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { fmt.Printf("ERROR: %v\n", err) } else { @@ -560,7 +560,7 @@ func TestWithFormData(t *testing.T) { t.Fatalf("autorest: WithFormData failed with error (%v)", err) } - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithFormData failed with error (%v)", err) } @@ -581,7 +581,7 @@ func TestWithFormData(t *testing.T) { func TestWithMultiPartFormDataSetsContentLength(t *testing.T) { v := map[string]interface{}{ - "file": ioutil.NopCloser(strings.NewReader("Hello Gopher")), + "file": io.NopCloser(strings.NewReader("Hello Gopher")), "age": "42", } @@ -591,7 +591,7 @@ func TestWithMultiPartFormDataSetsContentLength(t *testing.T) { t.Fatalf("autorest: WithMultiPartFormData failed with error (%v)", err) } - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithMultiPartFormData failed with error (%v)", err) } @@ -613,7 +613,7 @@ func TestWithMultiPartFormDataWithNoFile(t *testing.T) { t.Fatalf("autorest: WithMultiPartFormData failed with error (%v)", err) } - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithMultiPartFormData failed with error (%v)", err) } @@ -625,12 +625,12 @@ func TestWithMultiPartFormDataWithNoFile(t *testing.T) { func TestWithFile(t *testing.T) { r, err := Prepare(&http.Request{}, - WithFile(ioutil.NopCloser(strings.NewReader("Hello Gopher")))) + WithFile(io.NopCloser(strings.NewReader("Hello Gopher")))) if err != nil { t.Fatalf("autorest: WithFile failed with error (%v)", err) } - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithFile failed with error (%v)", err) } @@ -646,7 +646,7 @@ func TestWithBool_SetsTheBody(t *testing.T) { t.Fatalf("autorest: WithBool failed with error (%v)", err) } - s, err := ioutil.ReadAll(r.Body) + s, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithBool failed with error (%v)", err) } @@ -668,7 +668,7 @@ func TestWithFloat32_SetsTheBody(t *testing.T) { t.Fatalf("autorest: WithFloat32 failed with error (%v)", err) } - s, err := ioutil.ReadAll(r.Body) + s, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithFloat32 failed with error (%v)", err) } @@ -690,7 +690,7 @@ func TestWithFloat64_SetsTheBody(t *testing.T) { t.Fatalf("autorest: WithFloat64 failed with error (%v)", err) } - s, err := ioutil.ReadAll(r.Body) + s, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithFloat64 failed with error (%v)", err) } @@ -712,7 +712,7 @@ func TestWithInt32_SetsTheBody(t *testing.T) { t.Fatalf("autorest: WithInt32 failed with error (%v)", err) } - s, err := ioutil.ReadAll(r.Body) + s, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithInt32 failed with error (%v)", err) } @@ -734,7 +734,7 @@ func TestWithInt64_SetsTheBody(t *testing.T) { t.Fatalf("autorest: WithInt64 failed with error (%v)", err) } - s, err := ioutil.ReadAll(r.Body) + s, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithInt64 failed with error (%v)", err) } @@ -756,7 +756,7 @@ func TestWithString_SetsTheBody(t *testing.T) { t.Fatalf("autorest: WithString failed with error (%v)", err) } - s, err := ioutil.ReadAll(r.Body) + s, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithString failed with error (%v)", err) } @@ -777,7 +777,7 @@ func TestWithJSONSetsContentLength(t *testing.T) { t.Fatalf("autorest: WithJSON failed with error (%v)", err) } - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: WithJSON failed with error (%v)", err) } diff --git a/autorest/responder.go b/autorest/responder.go index 349e1963a..69d4b2b67 100644 --- a/autorest/responder.go +++ b/autorest/responder.go @@ -20,7 +20,6 @@ import ( "encoding/xml" "fmt" "io" - "io/ioutil" "net/http" "strings" ) @@ -111,7 +110,7 @@ func ByDiscardingBody() RespondDecorator { return ResponderFunc(func(resp *http.Response) error { err := r.Respond(resp) if err == nil && resp != nil && resp.Body != nil { - if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil { + if _, err := io.Copy(io.Discard, resp.Body); err != nil { return fmt.Errorf("Error discarding the response body: %v", err) } } @@ -160,7 +159,7 @@ func ByUnmarshallingBytes(v *[]byte) RespondDecorator { return ResponderFunc(func(resp *http.Response) error { err := r.Respond(resp) if err == nil { - bytes, errInner := ioutil.ReadAll(resp.Body) + bytes, errInner := io.ReadAll(resp.Body) if errInner != nil { err = fmt.Errorf("Error occurred reading http.Response#Body - Error = '%v'", errInner) } else { @@ -179,7 +178,7 @@ func ByUnmarshallingJSON(v interface{}) RespondDecorator { return ResponderFunc(func(resp *http.Response) error { err := r.Respond(resp) if err == nil { - b, errInner := ioutil.ReadAll(resp.Body) + b, errInner := io.ReadAll(resp.Body) // Some responses might include a BOM, remove for successful unmarshalling b = bytes.TrimPrefix(b, []byte("\xef\xbb\xbf")) if errInner != nil { @@ -203,7 +202,7 @@ func ByUnmarshallingXML(v interface{}) RespondDecorator { return ResponderFunc(func(resp *http.Response) error { err := r.Respond(resp) if err == nil { - b, errInner := ioutil.ReadAll(resp.Body) + b, errInner := io.ReadAll(resp.Body) if errInner != nil { err = fmt.Errorf("Error occurred reading http.Response#Body - Error = '%v'", errInner) } else { @@ -232,9 +231,9 @@ func WithErrorUnlessStatusCode(codes ...int) RespondDecorator { resp.Status) if resp.Body != nil { defer resp.Body.Close() - b, _ := ioutil.ReadAll(resp.Body) + b, _ := io.ReadAll(resp.Body) derr.ServiceError = b - resp.Body = ioutil.NopCloser(bytes.NewReader(b)) + resp.Body = io.NopCloser(bytes.NewReader(b)) } err = derr } diff --git a/autorest/responder_test.go b/autorest/responder_test.go index 1d823ebbc..3e20a47d4 100644 --- a/autorest/responder_test.go +++ b/autorest/responder_test.go @@ -18,7 +18,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "reflect" "strings" @@ -371,7 +371,7 @@ func TestByDiscardingBody(t *testing.T) { if err != nil { t.Fatalf("autorest: ByDiscardingBody failed (%v)", err) } - buf, err := ioutil.ReadAll(r.Body) + buf, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("autorest: Reading result of ByDiscardingBody failed (%v)", err) } diff --git a/autorest/retriablerequest.go b/autorest/retriablerequest.go index fa11dbed7..7634b0f57 100644 --- a/autorest/retriablerequest.go +++ b/autorest/retriablerequest.go @@ -17,7 +17,6 @@ package autorest import ( "bytes" "io" - "io/ioutil" "net/http" ) @@ -41,12 +40,12 @@ func (rr *RetriableRequest) prepareFromByteReader() (err error) { return err } } else { - b, err = ioutil.ReadAll(rr.req.Body) + b, err = io.ReadAll(rr.req.Body) if err != nil { return err } } rr.br = bytes.NewReader(b) - rr.req.Body = ioutil.NopCloser(rr.br) + rr.req.Body = io.NopCloser(rr.br) return err } diff --git a/autorest/retriablerequest_1.7.go b/autorest/retriablerequest_1.7.go index 4c87030e8..3c2eb10ee 100644 --- a/autorest/retriablerequest_1.7.go +++ b/autorest/retriablerequest_1.7.go @@ -19,7 +19,7 @@ package autorest import ( "bytes" - "io/ioutil" + "io" "net/http" ) @@ -36,7 +36,7 @@ func (rr *RetriableRequest) Prepare() (err error) { if rr.req.Body != nil { if rr.br != nil { _, err = rr.br.Seek(0, 0 /*io.SeekStart*/) - rr.req.Body = ioutil.NopCloser(rr.br) + rr.req.Body = io.NopCloser(rr.br) } if err != nil { return err diff --git a/autorest/retriablerequest_1.8.go b/autorest/retriablerequest_1.8.go index 05847c08b..0bd3c8ba4 100644 --- a/autorest/retriablerequest_1.8.go +++ b/autorest/retriablerequest_1.8.go @@ -20,7 +20,6 @@ package autorest import ( "bytes" "io" - "io/ioutil" "net/http" ) @@ -40,7 +39,7 @@ func (rr *RetriableRequest) Prepare() (err error) { rr.req.Body = rr.rc } else if rr.br != nil { _, err = rr.br.Seek(0, io.SeekStart) - rr.req.Body = ioutil.NopCloser(rr.br) + rr.req.Body = io.NopCloser(rr.br) } if err != nil { return err diff --git a/autorest/utility.go b/autorest/utility.go index d35b3850a..8c5eb5dbe 100644 --- a/autorest/utility.go +++ b/autorest/utility.go @@ -20,7 +20,6 @@ import ( "encoding/xml" "fmt" "io" - "io/ioutil" "net" "net/http" "net/url" @@ -217,7 +216,7 @@ func IsTemporaryNetworkError(err error) bool { // DrainResponseBody reads the response body then closes it. func DrainResponseBody(resp *http.Response) error { if resp != nil && resp.Body != nil { - _, err := io.Copy(ioutil.Discard, resp.Body) + _, err := io.Copy(io.Discard, resp.Body) resp.Body.Close() return err } diff --git a/logger/logger.go b/logger/logger.go index e51575f57..e70dc3dc2 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -18,7 +18,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -275,7 +274,7 @@ func (fl fileLogger) WriteRequest(req *http.Request, filter Filter) { } if fl.shouldLogBody(req.Header, req.Body) { // dump body - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) if err == nil { fmt.Fprintln(b, string(filter.processBody(body))) if nc, ok := req.Body.(io.Seeker); ok { @@ -283,7 +282,7 @@ func (fl fileLogger) WriteRequest(req *http.Request, filter Filter) { nc.Seek(0, io.SeekStart) } else { // recreate the body - req.Body = ioutil.NopCloser(bytes.NewReader(body)) + req.Body = io.NopCloser(bytes.NewReader(body)) } } else { fmt.Fprintf(b, "failed to read body: %v\n", err) @@ -310,10 +309,10 @@ func (fl fileLogger) WriteResponse(resp *http.Response, filter Filter) { if fl.shouldLogBody(resp.Header, resp.Body) { // dump body defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err == nil { fmt.Fprintln(b, string(filter.processBody(body))) - resp.Body = ioutil.NopCloser(bytes.NewReader(body)) + resp.Body = io.NopCloser(bytes.NewReader(body)) } else { fmt.Fprintf(b, "failed to read body: %v\n", err) } diff --git a/logger/logger_test.go b/logger/logger_test.go index 5838b2524..4e9645394 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -16,7 +16,7 @@ package logger import ( "fmt" - "io/ioutil" + "io" "net/http" "os" "path" @@ -79,7 +79,7 @@ func TestLogReqRespNoBody(t *testing.T) { t.Fatal("expected Instance to be fileLogger") } // parse log file to ensure contents match - b, err := ioutil.ReadFile(lf) + b, err := os.ReadFile(lf) if err != nil { t.Fatalf("failed to read log file: %v", err) } @@ -134,7 +134,7 @@ func TestLogReqRespWithBody(t *testing.T) { ProtoMinor: 0, Request: req, Header: http.Header{}, - Body: ioutil.NopCloser(strings.NewReader(respBody)), + Body: io.NopCloser(strings.NewReader(respBody)), } resp.Header.Add(respHeaderKey, respHeaderVal) Instance.WriteResponse(resp, Filter{}) @@ -144,7 +144,7 @@ func TestLogReqRespWithBody(t *testing.T) { t.Fatal("expected Instance to be fileLogger") } // parse log file to ensure contents match - b, err := ioutil.ReadFile(lf) + b, err := os.ReadFile(lf) if err != nil { t.Fatalf("failed to read log file: %v", err) }