Skip to content

Commit

Permalink
Remove deprecated ioutil package
Browse files Browse the repository at this point in the history
According to
- golang/go#40025
- golang/go#42026

`ioutil` is deprecated, `io` and `os` packages should be used instead.
  • Loading branch information
hlcfan committed Feb 21, 2023
1 parent 4578a40 commit ccea46c
Show file tree
Hide file tree
Showing 22 changed files with 109 additions and 99 deletions.
24 changes: 12 additions & 12 deletions README.md
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -159,7 +159,7 @@ func TestSimple(t *testing.T) {
package test

import (
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -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
Expand All @@ -199,7 +199,7 @@ func TestMatchHeaders(t *testing.T) {
package test

import (
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -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
Expand All @@ -236,7 +236,7 @@ package test

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -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
Expand All @@ -273,7 +273,7 @@ func TestMockSimple(t *testing.T) {
package test

import (
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -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
Expand All @@ -310,7 +310,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/h2non/gock"
Expand All @@ -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))
}
```
Expand Down
9 changes: 5 additions & 4 deletions _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) {
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions _examples/body_file/file_test.go
Expand Up @@ -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) {
Expand All @@ -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"}`)
}
9 changes: 5 additions & 4 deletions _examples/body_match/body_test.go
Expand Up @@ -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) {
Expand All @@ -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"}`)
}
9 changes: 5 additions & 4 deletions _examples/compressed_body/compression_test.go
Expand Up @@ -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) {
Expand All @@ -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"}`)
}
9 changes: 5 additions & 4 deletions _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) {
Expand All @@ -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")
}
8 changes: 4 additions & 4 deletions _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) {
Expand All @@ -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")
}
9 changes: 5 additions & 4 deletions _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) {
Expand All @@ -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")
}
9 changes: 5 additions & 4 deletions _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) {
Expand All @@ -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")
}
9 changes: 5 additions & 4 deletions _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) {
Expand Down Expand Up @@ -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:]+`"}`)
}

Expand Down
4 changes: 2 additions & 2 deletions _examples/networking/networking.go
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/h2non/gock"
Expand All @@ -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))
}
4 changes: 2 additions & 2 deletions _examples/networking_filters/filters.go
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/h2non/gock"
Expand Down Expand Up @@ -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))
}
5 changes: 2 additions & 3 deletions _examples/networking_partially_enabled/networking.go
Expand Up @@ -5,7 +5,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"

Expand All @@ -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))
Expand Down Expand Up @@ -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))
}

0 comments on commit ccea46c

Please sign in to comment.