From ccea46c7f72477919aa70c92db3e645a47cc3276 Mon Sep 17 00:00:00 2001 From: Alex S Date: Wed, 22 Feb 2023 00:09:40 +0800 Subject: [PATCH] Remove deprecated ioutil package According to - https://github.com/golang/go/issues/40025 - https://github.com/golang/go/issues/42026 `ioutil` is deprecated, `io` and `os` packages should be used instead. --- README.md | 24 ++++++++-------- _examples/basic/basic_test.go | 9 +++--- _examples/body_file/file_test.go | 9 +++--- _examples/body_match/body_test.go | 9 +++--- _examples/compressed_body/compression_test.go | 9 +++--- _examples/custom_client/client_test.go | 9 +++--- _examples/match_headers/headers_test.go | 8 +++--- _examples/match_query/query_test.go | 9 +++--- _examples/match_url/url_test.go | 9 +++--- _examples/multiple/multiple_test.go | 9 +++--- _examples/networking/networking.go | 4 +-- _examples/networking_filters/filters.go | 4 +-- .../networking.go | 5 ++-- _examples/persistent/persistent_test.go | 9 +++--- _examples/persistent/times_test.go | 9 +++--- _examples/regexp_matching/regexp_test.go | 9 +++--- gock_test.go | 28 +++++++++---------- matchers.go | 3 +- request.go | 12 ++++---- responder.go | 5 ++-- responder_test.go | 8 +++--- response.go | 8 +++--- 22 files changed, 109 insertions(+), 99 deletions(-) diff --git a/README.md b/README.md index 37a835f..c5f6cae 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ See [examples](https://github.com/h2non/gock/tree/master/_examples) directory fo package test import ( - "io/ioutil" + "io" "net/http" "testing" @@ -145,7 +145,7 @@ func TestSimple(t *testing.T) { st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) // Verify that we don't have pending mocks @@ -159,7 +159,7 @@ func TestSimple(t *testing.T) { package test import ( - "io/ioutil" + "io" "net/http" "testing" @@ -185,7 +185,7 @@ func TestMatchHeaders(t *testing.T) { res, err := (&http.Client{}).Do(req) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo foo") // Verify that we don't have pending mocks @@ -199,7 +199,7 @@ func TestMatchHeaders(t *testing.T) { package test import ( - "io/ioutil" + "io" "net/http" "testing" @@ -221,7 +221,7 @@ func TestMatchParams(t *testing.T) { res, err := (&http.Client{}).Do(req) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo foo") // Verify that we don't have pending mocks @@ -236,7 +236,7 @@ package test import ( "bytes" - "io/ioutil" + "io" "net/http" "testing" @@ -259,7 +259,7 @@ func TestMockSimple(t *testing.T) { st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - resBody, _ := ioutil.ReadAll(res.Body) + resBody, _ := io.ReadAll(res.Body) st.Expect(t, string(resBody)[:13], `{"bar":"foo"}`) // Verify that we don't have pending mocks @@ -273,7 +273,7 @@ func TestMockSimple(t *testing.T) { package test import ( - "io/ioutil" + "io" "net/http" "testing" @@ -295,7 +295,7 @@ func TestClient(t *testing.T) { res, err := client.Do(req) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo foo") // Verify that we don't have pending mocks @@ -310,7 +310,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "net/http" "github.com/h2non/gock" @@ -336,7 +336,7 @@ func main() { // The server header comes from mock as well fmt.Printf("Server header: %s\n", res.Header.Get("Server")) // Response body is the original - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) fmt.Printf("Body: %s", string(body)) } ``` diff --git a/_examples/basic/basic_test.go b/_examples/basic/basic_test.go index 5afb8c9..5405e84 100644 --- a/_examples/basic/basic_test.go +++ b/_examples/basic/basic_test.go @@ -1,11 +1,12 @@ package test import ( - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestSimple(t *testing.T) { @@ -20,7 +21,7 @@ func TestSimple(t *testing.T) { st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) // Verify that we don't have pending mocks diff --git a/_examples/body_file/file_test.go b/_examples/body_file/file_test.go index 18d5cf3..2f1769e 100644 --- a/_examples/body_file/file_test.go +++ b/_examples/body_file/file_test.go @@ -2,11 +2,12 @@ package test import ( "bytes" - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestMockBodyFile(t *testing.T) { @@ -24,6 +25,6 @@ func TestMockBodyFile(t *testing.T) { st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - resBody, _ := ioutil.ReadAll(res.Body) + resBody, _ := io.ReadAll(res.Body) st.Expect(t, string(resBody)[:13], `{"bar":"foo"}`) } diff --git a/_examples/body_match/body_test.go b/_examples/body_match/body_test.go index b8965ca..efee8e3 100644 --- a/_examples/body_match/body_test.go +++ b/_examples/body_match/body_test.go @@ -2,11 +2,12 @@ package test import ( "bytes" - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestMockSimple(t *testing.T) { @@ -24,6 +25,6 @@ func TestMockSimple(t *testing.T) { st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - resBody, _ := ioutil.ReadAll(res.Body) + resBody, _ := io.ReadAll(res.Body) st.Expect(t, string(resBody)[:13], `{"bar":"foo"}`) } diff --git a/_examples/compressed_body/compression_test.go b/_examples/compressed_body/compression_test.go index a1aec74..2510d2f 100644 --- a/_examples/compressed_body/compression_test.go +++ b/_examples/compressed_body/compression_test.go @@ -3,11 +3,12 @@ package test import ( "bytes" "compress/gzip" - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestMockSimple(t *testing.T) { @@ -33,6 +34,6 @@ func TestMockSimple(t *testing.T) { st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - resBody, _ := ioutil.ReadAll(res.Body) + resBody, _ := io.ReadAll(res.Body) st.Expect(t, string(resBody)[:13], `{"bar":"foo"}`) } diff --git a/_examples/custom_client/client_test.go b/_examples/custom_client/client_test.go index 60eeeb3..9dbe407 100644 --- a/_examples/custom_client/client_test.go +++ b/_examples/custom_client/client_test.go @@ -1,11 +1,12 @@ package test import ( - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestClient(t *testing.T) { @@ -22,6 +23,6 @@ func TestClient(t *testing.T) { res, err := client.Do(req) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo foo") } diff --git a/_examples/match_headers/headers_test.go b/_examples/match_headers/headers_test.go index fecf728..e83f5b6 100644 --- a/_examples/match_headers/headers_test.go +++ b/_examples/match_headers/headers_test.go @@ -1,12 +1,12 @@ package test import ( - "io/ioutil" + "io" "net/http" "testing" - "github.com/nbio/st" - "github.com/h2non/gock" + "github.com/h2honngockock" + "github.com/ibiosst ) func TestMatchHeaders(t *testing.T) { @@ -27,6 +27,6 @@ func TestMatchHeaders(t *testing.T) { res, err := (&http.Client{}).Do(req) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo foo") } diff --git a/_examples/match_query/query_test.go b/_examples/match_query/query_test.go index 0e85c69..17484d9 100644 --- a/_examples/match_query/query_test.go +++ b/_examples/match_query/query_test.go @@ -1,11 +1,12 @@ package test import ( - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestMatchQueryParams(t *testing.T) { @@ -22,6 +23,6 @@ func TestMatchQueryParams(t *testing.T) { res, err := (&http.Client{}).Do(req) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo foo") } diff --git a/_examples/match_url/url_test.go b/_examples/match_url/url_test.go index aed0e39..1362f58 100644 --- a/_examples/match_url/url_test.go +++ b/_examples/match_url/url_test.go @@ -1,11 +1,12 @@ package test import ( - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestMatchURL(t *testing.T) { @@ -18,6 +19,6 @@ func TestMatchURL(t *testing.T) { res, err := http.Get("http://foo.com") st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo foo") } diff --git a/_examples/multiple/multiple_test.go b/_examples/multiple/multiple_test.go index e9d60a0..c1eafb5 100644 --- a/_examples/multiple/multiple_test.go +++ b/_examples/multiple/multiple_test.go @@ -1,11 +1,12 @@ package test import ( - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestMultipleMocks(t *testing.T) { @@ -38,7 +39,7 @@ func TestMultipleMocks(t *testing.T) { res, err := http.Get("http://server.com" + test.path) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:15], `{"value":"`+test.path[1:]+`"}`) } diff --git a/_examples/networking/networking.go b/_examples/networking/networking.go index d1924d7..2749460 100644 --- a/_examples/networking/networking.go +++ b/_examples/networking/networking.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "net/http" "github.com/h2non/gock" @@ -28,6 +28,6 @@ func main() { // The server header comes from mock as well fmt.Printf("Server header: %s\n", res.Header.Get("Server")) // Response body is the original - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) fmt.Printf("Body: %s", string(body)) } diff --git a/_examples/networking_filters/filters.go b/_examples/networking_filters/filters.go index 6deea3b..411f07e 100644 --- a/_examples/networking_filters/filters.go +++ b/_examples/networking_filters/filters.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "net/http" "github.com/h2non/gock" @@ -35,6 +35,6 @@ func main() { // The server header comes from mock as well fmt.Printf("Server header: %s\n", res.Header.Get("Server")) // Response body is the original - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) fmt.Printf("Body: %s", string(body)) } diff --git a/_examples/networking_partially_enabled/networking.go b/_examples/networking_partially_enabled/networking.go index addb891..edacb42 100644 --- a/_examples/networking_partially_enabled/networking.go +++ b/_examples/networking_partially_enabled/networking.go @@ -5,7 +5,6 @@ package main import ( "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" @@ -24,7 +23,7 @@ func startHTTPServer() *httptest.Server { return } - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) // MUST NOT get original body since the networking // wasn't enabled for this request fmt.Printf("Body From httpbin: %s\n", string(body)) @@ -61,6 +60,6 @@ func main() { // The server header comes from mock as well fmt.Printf("Server header: %s\n", res.Header.Get("Server")) // MUST get original response since the networking was enabled for this request - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) fmt.Printf("Body From Local Server: %s", string(body)) } diff --git a/_examples/persistent/persistent_test.go b/_examples/persistent/persistent_test.go index 6d02a0f..a52c284 100644 --- a/_examples/persistent/persistent_test.go +++ b/_examples/persistent/persistent_test.go @@ -1,11 +1,12 @@ package test import ( - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestPersistent(t *testing.T) { @@ -20,7 +21,7 @@ func TestPersistent(t *testing.T) { res, err := http.Get("http://foo.com/bar") st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) } diff --git a/_examples/persistent/times_test.go b/_examples/persistent/times_test.go index 5f0d21a..83d1138 100644 --- a/_examples/persistent/times_test.go +++ b/_examples/persistent/times_test.go @@ -1,11 +1,12 @@ package test import ( - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestTimes(t *testing.T) { @@ -25,7 +26,7 @@ func TestTimes(t *testing.T) { st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) } } diff --git a/_examples/regexp_matching/regexp_test.go b/_examples/regexp_matching/regexp_test.go index 905e80a..e9c3321 100644 --- a/_examples/regexp_matching/regexp_test.go +++ b/_examples/regexp_matching/regexp_test.go @@ -2,11 +2,12 @@ package test import ( "bytes" - "github.com/nbio/st" - "github.com/h2non/gock" - "io/ioutil" + "io" "net/http" "testing" + + "github.com/h2non/gock" + "github.com/nbio/st" ) func TestRegExpMatching(t *testing.T) { @@ -26,6 +27,6 @@ func TestRegExpMatching(t *testing.T) { st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) st.Expect(t, res.Header.Get("Server"), "gock") - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) } diff --git a/gock_test.go b/gock_test.go index 7df68fe..34cac74 100644 --- a/gock_test.go +++ b/gock_test.go @@ -4,7 +4,7 @@ import ( "bytes" "compress/gzip" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "strings" @@ -19,7 +19,7 @@ func TestMockSimple(t *testing.T) { res, err := http.Get("http://foo.com") st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) } @@ -36,7 +36,7 @@ func TestMockBodyStringResponse(t *testing.T) { res, err := http.Get("http://foo.com") st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo bar") } @@ -46,7 +46,7 @@ func TestMockBodyMatch(t *testing.T) { res, err := http.Post("http://foo.com", "text/plain", bytes.NewBuffer([]byte("foo bar"))) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo foo") } @@ -72,7 +72,7 @@ func TestMockBodyMatchCompressed(t *testing.T) { res, err := http.DefaultClient.Do(req) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo foo") } @@ -94,7 +94,7 @@ func TestMockBodyMatchJSON(t *testing.T) { res, err := http.Post("http://foo.com/bar", "application/json", bytes.NewBuffer([]byte(`{"foo":"bar"}`))) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"bar":"foo"}`) } @@ -130,7 +130,7 @@ func TestMockBodyMatchCompressedJSON(t *testing.T) { res, err := http.DefaultClient.Do(req) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"bar":"foo"}`) } @@ -164,7 +164,7 @@ func TestMockMatchHeaders(t *testing.T) { res, err := http.Post("http://foo.com", "text/plain", bytes.NewBuffer([]byte("foo bar"))) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo foo") } @@ -181,7 +181,7 @@ func TestMockMap(t *testing.T) { res, err := http.Get("http://foo.com") st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) } @@ -197,7 +197,7 @@ func TestMockFilter(t *testing.T) { res, err := http.Get("http://foo.com") st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 201) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) } @@ -273,7 +273,7 @@ func TestMockPersistent(t *testing.T) { res, err := http.Get("http://foo.com/bar") st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) } } @@ -295,7 +295,7 @@ func TestMockPersistTimes(t *testing.T) { st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) } } @@ -348,7 +348,7 @@ func TestMultipleMocks(t *testing.T) { res, err := http.Get("http://server.com" + test.path) st.Expect(t, err, nil) st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:15], `{"value":"`+test.path[1:]+`"}`) } @@ -408,7 +408,7 @@ func TestMockRegExpMatching(t *testing.T) { st.Expect(t, res.StatusCode, 200) st.Expect(t, res.Header.Get("Server"), "gock") - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body)[:13], `{"foo":"bar"}`) } diff --git a/matchers.go b/matchers.go index 658c9a6..8853733 100644 --- a/matchers.go +++ b/matchers.go @@ -4,7 +4,6 @@ import ( "compress/gzip" "encoding/json" "io" - "io/ioutil" "net/http" "reflect" "regexp" @@ -171,7 +170,7 @@ func MatchBody(req *http.Request, ereq *Request) (bool, error) { } // Read the whole request body - body, err := ioutil.ReadAll(bodyReader) + body, err := io.ReadAll(bodyReader) if err != nil { return false, err } diff --git a/request.go b/request.go index 5702417..38df5fd 100644 --- a/request.go +++ b/request.go @@ -3,9 +3,9 @@ package gock import ( "encoding/base64" "io" - "io/ioutil" "net/http" "net/url" + "os" "strings" ) @@ -133,7 +133,7 @@ func (r *Request) method(method, path string) *Request { // Body defines the body data to match based on a io.Reader interface. func (r *Request) Body(body io.Reader) *Request { - r.BodyBuffer, r.Error = ioutil.ReadAll(body) + r.BodyBuffer, r.Error = io.ReadAll(body) return r } @@ -145,7 +145,7 @@ func (r *Request) BodyString(body string) *Request { // File defines the body to match based on the given file path string. func (r *Request) File(path string) *Request { - r.BodyBuffer, r.Error = ioutil.ReadFile(path) + r.BodyBuffer, r.Error = os.ReadFile(path) return r } @@ -239,8 +239,10 @@ func (r *Request) ParamPresent(key string) *Request { // PathParam matches if a given path parameter key is present in the URL. // // The value is representative of the restful resource the key defines, e.g. -// // /users/123/name -// r.PathParam("users", "123") +// +// // /users/123/name +// r.PathParam("users", "123") +// // would match. func (r *Request) PathParam(key, val string) *Request { r.PathParams[key] = val diff --git a/responder.go b/responder.go index b71c742..8d5f7a1 100644 --- a/responder.go +++ b/responder.go @@ -3,7 +3,6 @@ package gock import ( "bytes" "io" - "io/ioutil" "net/http" "strconv" "time" @@ -68,7 +67,7 @@ func Responder(req *http.Request, mock *Response, res *http.Response) (*http.Res // has the added benefit of working even when there is no delay (very small timeouts, already-done contexts, etc.) if err = req.Context().Err(); err != nil { // cleanly close the response and return the context error - io.Copy(ioutil.Discard, res.Body) + io.Copy(io.Discard, res.Body) res.Body.Close() return nil, err } @@ -101,5 +100,5 @@ func mergeHeaders(res *http.Response, mres *Response) http.Header { // createReadCloser creates an io.ReadCloser from a byte slice that is suitable for use as an // http response body. func createReadCloser(body []byte) io.ReadCloser { - return ioutil.NopCloser(bytes.NewReader(body)) + return io.NopCloser(bytes.NewReader(body)) } diff --git a/responder_test.go b/responder_test.go index d5739d3..e9f7963 100644 --- a/responder_test.go +++ b/responder_test.go @@ -3,7 +3,7 @@ package gock import ( "context" "errors" - "io/ioutil" + "io" "net/http" "testing" "time" @@ -21,7 +21,7 @@ func TestResponder(t *testing.T) { st.Expect(t, res.Status, "200 OK") st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo") } @@ -35,10 +35,10 @@ func TestResponder_ReadTwice(t *testing.T) { st.Expect(t, res.Status, "200 OK") st.Expect(t, res.StatusCode, 200) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) st.Expect(t, string(body), "foo") - body, err = ioutil.ReadAll(res.Body) + body, err = io.ReadAll(res.Body) st.Expect(t, err, nil) st.Expect(t, body, []byte{}) } diff --git a/response.go b/response.go index 07b7158..b97fc78 100644 --- a/response.go +++ b/response.go @@ -5,8 +5,8 @@ import ( "encoding/json" "encoding/xml" "io" - "io/ioutil" "net/http" + "os" "time" ) @@ -95,7 +95,7 @@ func (r *Response) SetHeaders(headers map[string]string) *Response { // Body sets the HTTP response body to be used. func (r *Response) Body(body io.Reader) *Response { - r.BodyBuffer, r.Error = ioutil.ReadAll(body) + r.BodyBuffer, r.Error = io.ReadAll(body) return r } @@ -108,7 +108,7 @@ func (r *Response) BodyString(body string) *Response { // File defines the response body reading the data // from disk based on the file path string. func (r *Response) File(path string) *Response { - r.BodyBuffer, r.Error = ioutil.ReadFile(path) + r.BodyBuffer, r.Error = os.ReadFile(path) return r } @@ -182,5 +182,5 @@ func readAndDecode(data interface{}, kind string) ([]byte, error) { } } - return ioutil.ReadAll(buf) + return io.ReadAll(buf) }