chore: remove refs to deprecated io/ioutil

Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
This commit is contained in:
guoguangwu
2023-10-19 11:05:38 +08:00
parent 958b0671bd
commit 115e48a202
12 changed files with 56 additions and 59 deletions
+5 -6
View File
@@ -18,11 +18,10 @@ package data
import (
"io"
"io/ioutil"
"net/http"
"net/textproto"
"strconv"
"strings"
"strings"
"github.com/BBVA/kapow/internal/logger"
"github.com/BBVA/kapow/internal/server/httperror"
@@ -183,7 +182,7 @@ func getRouteId(w http.ResponseWriter, r *http.Request, h *model.Handler) {
// FIXME: Allow any HTTP status code. Now we are limited by WriteHeader
// capabilities
func setResponseStatus(w http.ResponseWriter, r *http.Request, h *model.Handler) {
sb, err := ioutil.ReadAll(r.Body)
sb, err := io.ReadAll(r.Body)
if err != nil {
httperror.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
@@ -200,7 +199,7 @@ func setResponseStatus(w http.ResponseWriter, r *http.Request, h *model.Handler)
func setResponseHeaders(w http.ResponseWriter, r *http.Request, h *model.Handler) {
name := mux.Vars(r)["name"]
vb, err := ioutil.ReadAll(r.Body)
vb, err := io.ReadAll(r.Body)
if err != nil {
httperror.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
@@ -212,7 +211,7 @@ func setResponseHeaders(w http.ResponseWriter, r *http.Request, h *model.Handler
func setResponseCookies(w http.ResponseWriter, r *http.Request, h *model.Handler) {
name := mux.Vars(r)["name"]
vb, err := ioutil.ReadAll(r.Body)
vb, err := io.ReadAll(r.Body)
if err != nil {
httperror.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
@@ -242,7 +241,7 @@ func setResponseBody(w http.ResponseWriter, r *http.Request, h *model.Handler) {
}
func setServerLog(w http.ResponseWriter, r *http.Request, h *model.Handler) {
msg, err := ioutil.ReadAll(r.Body)
msg, err := io.ReadAll(r.Body)
if err != nil {
httperror.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
+26 -26
View File
@@ -24,11 +24,11 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"os"
"reflect"
"strings"
"testing"
@@ -110,7 +110,7 @@ func TestGetRequestBodyWritesHandlerRequestBodyToResponseWriter(t *testing.T) {
getRequestBody(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAR" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAR" {
t.Error("Body mismatch")
}
}
@@ -189,7 +189,7 @@ func TestGetRequestMethodReturnsTheCorrectMethod(t *testing.T) {
getRequestMethod(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "FOO" {
if body, _ := io.ReadAll(res.Body); string(body) != "FOO" {
t.Error("Body mismatch")
}
}
@@ -237,7 +237,7 @@ func TestGetRequestHostReturnsTheCorrectHostname(t *testing.T) {
getRequestHost(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "www.foo.bar:8080" {
if body, _ := io.ReadAll(res.Body); string(body) != "www.foo.bar:8080" {
t.Error("Body mismatch")
}
}
@@ -288,7 +288,7 @@ func TestGetRequestVersionReturnsTheCorrectHttpVersion(t *testing.T) {
getRequestVersion(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != h.Request.Proto {
if body, _ := io.ReadAll(res.Body); string(body) != h.Request.Proto {
t.Errorf("Version mismatch. Expected %q, got %q", h.Request.Proto, string(body))
}
}
@@ -336,7 +336,7 @@ func TestGetRequestPathReturnsPath(t *testing.T) {
getRequestPath(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "/foo" {
if body, _ := io.ReadAll(res.Body); string(body) != "/foo" {
t.Error("Body mismatch")
}
}
@@ -352,7 +352,7 @@ func TestGetRequestPathDoesntReturnQueryStringParams(t *testing.T) {
getRequestPath(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "/foo" {
if body, _ := io.ReadAll(res.Body); string(body) != "/foo" {
t.Errorf("Body mismatch. Expected: /foo. Got: %v", string(body))
}
}
@@ -401,7 +401,7 @@ func TestGetRequestRemoteReturnsTheCorrectRemote(t *testing.T) {
getRequestRemote(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != h.Request.RemoteAddr {
if body, _ := io.ReadAll(res.Body); string(body) != h.Request.RemoteAddr {
t.Errorf("Version mismatch. Expected %q, got %q", h.Request.RemoteAddr, string(body))
}
}
@@ -456,7 +456,7 @@ func TestGetRequestMatchesReturnsTheCorrectMatchValue(t *testing.T) {
getRequestMatches(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf("Body mismatch. Expected: BAZ. Got: %v", string(body))
}
@@ -521,7 +521,7 @@ func TestGetRequestParamsReturnsTheCorrectMatchValue(t *testing.T) {
getRequestParams(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf("Body mismatch. Expected: BAZ. Got: %v", string(body))
}
}
@@ -553,7 +553,7 @@ func TestGetRequestParamsReturnsTheFirstCorrectMatchValue(t *testing.T) {
getRequestParams(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf("Body mismatch. Expected: BAZ. Got: %v", string(body))
}
}
@@ -604,7 +604,7 @@ func TestGetRequestHeadersReturnsTheCorrectMatchValue(t *testing.T) {
getRequestHeaders(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf("Body mismatch. Expected: BAZ. Got: %v", string(body))
}
}
@@ -638,7 +638,7 @@ func TestGetRequestHeadersReturnsEmptyBodyWhenHeaderIsEmptyString(t *testing.T)
getRequestHeaders(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "" {
if body, _ := io.ReadAll(res.Body); string(body) != "" {
t.Errorf(`Body mismatch. Expected "". Got: %q`, string(body))
}
}
@@ -655,7 +655,7 @@ func TestGetRequestHeadersReturnsTheCorrectInsensitiveMatchValue(t *testing.T) {
getRequestHeaders(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf("Body mismatch. Expected: BAZ. Got: %v", string(body))
}
}
@@ -688,7 +688,7 @@ func TestGetRequestHeadersReturnsTheFirstCorrectMatchValue(t *testing.T) {
getRequestHeaders(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf("Body mismatch. Expected: BAZ. Got: %v", string(body))
}
}
@@ -720,7 +720,7 @@ func TestGetRequestHeadersReturnsTheCorrectValueForHostHeader(t *testing.T) {
getRequestHeaders(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "www.foo.bar:8080" {
if body, _ := io.ReadAll(res.Body); string(body) != "www.foo.bar:8080" {
t.Errorf("Body mismatch. Expected: %q. Got: %q", "www.foo.bar:8080", string(body))
}
}
@@ -771,7 +771,7 @@ func TestGetRequestCookiesReturnsMatchedCookieValue(t *testing.T) {
getRequestCookies(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf("Body mismatch. Expected: BAZ. Got: %v", string(body))
}
}
@@ -804,7 +804,7 @@ func TestGetRequestCookiesReturnsTheFirstCorrectMatchValue(t *testing.T) {
getRequestCookies(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf("Body mismatch. Expected: BAZ. Got: %v", string(body))
}
}
@@ -869,7 +869,7 @@ func TestGetRequestFormReturnsTheCorrectMatchValue(t *testing.T) {
getRequestForm(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf("Body mismatch. Expected: BAZ. Got: %v", string(body))
}
}
@@ -925,7 +925,7 @@ func TestGetRequestFormReturnsEmptyBodyWhenFieldIsEmptyString(t *testing.T) {
getRequestForm(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "" {
if body, _ := io.ReadAll(res.Body); string(body) != "" {
t.Errorf(`Body mismatch. Expected: "". Got: %q`, string(body))
}
}
@@ -1001,7 +1001,7 @@ func TestGetRequestFileNameReturnsTheCorrectFilename(t *testing.T) {
getRequestFileName(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf(`Body mismatch. Expected: "BAZ". Got: %q`, string(body))
}
}
@@ -1080,7 +1080,7 @@ func TestGetRequestFileContentReturnsTheCorrectFileContent(t *testing.T) {
getRequestFileContent(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf(`Body mismatch. Expected: "BAZ". Got: %q`, string(body))
}
}
@@ -1205,7 +1205,7 @@ func TestGetSSLClientDNSetsOctectStreamContentType(t *testing.T) {
}
func mockAuthenticateClient(tls *tls.ConnectionState) error {
fileData, err := ioutil.ReadFile("./testdata/client_chain.crt")
fileData, err := os.ReadFile("./testdata/client_chain.crt")
if err != nil {
return fmt.Errorf("Error loading certificates file: %v", err)
}
@@ -1237,7 +1237,7 @@ func TestGetSSLClientDNReturnsCorrectDN(t *testing.T) {
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != h.Request.TLS.VerifiedChains[0][0].Subject.String() {
if body, _ := io.ReadAll(res.Body); string(body) != h.Request.TLS.VerifiedChains[0][0].Subject.String() {
t.Errorf("Body mismatch. Expected: %q, got: %q", h.Request.TLS.VerifiedChains[0][0].Subject.String(), string(body))
}
}
@@ -1286,7 +1286,7 @@ func TestGetRouteIdReturnsTheCorrectRouteId(t *testing.T) {
getRouteId(w, r, &h)
res := w.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != h.Route.ID {
if body, _ := io.ReadAll(res.Body); string(body) != h.Route.ID {
t.Errorf("Body mismatch. Expected: %q, got: %q", h.Route.ID, string(body))
}
}
@@ -1575,7 +1575,7 @@ func TestSetResponseBodySetsTheResponseBody(t *testing.T) {
setResponseBody(w, r, &h)
res := hw.Result()
if body, _ := ioutil.ReadAll(res.Body); string(body) != "BAZ" {
if body, _ := io.ReadAll(res.Body); string(body) != "BAZ" {
t.Errorf(`Body mismatch. Expected: "BAZ". Got: %q`, string(body))
}
}
+2 -2
View File
@@ -19,7 +19,7 @@ package data
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
@@ -40,7 +40,7 @@ func checkErrorResponse(r *http.Response, expectedErrcode int, expectedReason st
}
errMsg := httperror.ServerErrMessage{}
if bodyBytes, err := ioutil.ReadAll(r.Body); err != nil {
if bodyBytes, err := io.ReadAll(r.Body); err != nil {
errList = append(errList, fmt.Errorf("Unexpected error reading response body: %v", err))
} else if err := json.Unmarshal(bodyBytes, &errMsg); err != nil {
errList = append(errList, fmt.Errorf("Response body contains invalid JSON entity: %v", err))