diff --git a/internal/cmd/route.go b/internal/cmd/route.go index 1fc0e0f..7e550dc 100644 --- a/internal/cmd/route.go +++ b/internal/cmd/route.go @@ -17,7 +17,7 @@ package cmd import ( - "io/ioutil" + "io" "os" "github.com/BBVA/kapow/internal/client" @@ -62,9 +62,9 @@ func init() { var buf []byte var err error if commandFile == "-" { - buf, err = ioutil.ReadAll(os.Stdin) + buf, err = io.ReadAll(os.Stdin) } else { - buf, err = ioutil.ReadFile(commandFile) + buf, err = os.ReadFile(commandFile) } if err != nil { logger.L.Fatal(err) diff --git a/internal/http/reason.go b/internal/http/reason.go index 1f0fa07..c1fcab9 100644 --- a/internal/http/reason.go +++ b/internal/http/reason.go @@ -19,7 +19,7 @@ package http import ( "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "github.com/BBVA/kapow/internal/server/httperror" @@ -28,7 +28,7 @@ import ( // Reason returns the reason phrase embedded within the JSON error // body, or an error if no reason can be extracted func Reason(r *http.Response) (string, error) { - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) if err != nil { return "", errors.New("error reading response's body") } diff --git a/internal/http/reason_test.go b/internal/http/reason_test.go index 5f0bc5a..130f8ab 100644 --- a/internal/http/reason_test.go +++ b/internal/http/reason_test.go @@ -17,7 +17,7 @@ package http import ( - "io/ioutil" + "io" nethttp "net/http" "strings" "testing" @@ -26,7 +26,7 @@ import ( func TestReasonExtractsReasonFromJSON(t *testing.T) { r := &nethttp.Response{ Status: "200 OK", - Body: ioutil.NopCloser( + Body: io.NopCloser( strings.NewReader( `{"reason": "Because reasons", "foo": "bar"}`, ), @@ -43,7 +43,7 @@ func TestReasonExtractsReasonFromJSON(t *testing.T) { func TestReasonErrorsOnJSONWithNoReason(t *testing.T) { r := &nethttp.Response{ Status: "200 OK", - Body: ioutil.NopCloser( + Body: io.NopCloser( strings.NewReader( `{"madness": "Because madness", "foo": "bar"}`, ), @@ -59,7 +59,7 @@ func TestReasonErrorsOnJSONWithNoReason(t *testing.T) { func TestReasonErrorsOnJSONWithEmptyReason(t *testing.T) { r := &nethttp.Response{ - Body: ioutil.NopCloser( + Body: io.NopCloser( strings.NewReader( `{"reason": "", "foo": "bar"}`, ), @@ -75,7 +75,7 @@ func TestReasonErrorsOnJSONWithEmptyReason(t *testing.T) { func TestReasonErrorsOnNoJSON(t *testing.T) { r := &nethttp.Response{ - Body: ioutil.NopCloser( + Body: io.NopCloser( strings.NewReader(""), ), } @@ -89,7 +89,7 @@ func TestReasonErrorsOnNoJSON(t *testing.T) { func TestReasonErrorsOnInvalidJSON(t *testing.T) { r := &nethttp.Response{ - Body: ioutil.NopCloser( + Body: io.NopCloser( strings.NewReader( `{"reason": "Because reasons", "cliffhanger...`, ), diff --git a/internal/http/request.go b/internal/http/request.go index f38010e..c02ac9d 100644 --- a/internal/http/request.go +++ b/internal/http/request.go @@ -21,7 +21,6 @@ import ( "crypto/x509" "errors" "io" - "io/ioutil" "net/http" "os" @@ -54,7 +53,7 @@ func Delete(url string, r io.Reader, w io.Writer, clientGenerator func() *http.C return Request("DELETE", url, r, w, clientGenerator, reqTuner...) } -var devnull = ioutil.Discard +var devnull = io.Discard // Request will perform the request to the given url and method sending the // content of the given reader as the body and writing all the contents diff --git a/internal/server/control/control.go b/internal/server/control/control.go index 377fd66..0c8c60c 100644 --- a/internal/server/control/control.go +++ b/internal/server/control/control.go @@ -18,7 +18,7 @@ package control import ( "encoding/json" - "io/ioutil" + "io" "net/http" "github.com/google/uuid" @@ -103,7 +103,7 @@ var pathValidator func(string) error = func(path string) error { func addRoute(res http.ResponseWriter, req *http.Request) { var route model.Route - payload, _ := ioutil.ReadAll(req.Body) + payload, _ := io.ReadAll(req.Body) err := json.Unmarshal(payload, &route) if err != nil { httperror.ErrorJSON(res, "Malformed JSON", http.StatusBadRequest) diff --git a/internal/server/control/control_test.go b/internal/server/control/control_test.go index d93500c..45b9f8a 100644 --- a/internal/server/control/control_test.go +++ b/internal/server/control/control_test.go @@ -20,7 +20,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "reflect" @@ -47,7 +47,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)) @@ -434,7 +434,7 @@ func TestGetRouteReturnsTheRequestedRoute(t *testing.T) { t.Errorf("HTTP status mismatch. Expected: %d, got: %d", http.StatusOK, resp.StatusCode) } - bBytes, _ := ioutil.ReadAll(resp.Body) + bBytes, _ := io.ReadAll(resp.Body) if err := json.Unmarshal(bBytes, &respJson); err != nil { t.Errorf("Invalid JSON response. %s", string(bBytes)) } diff --git a/internal/server/data/resource.go b/internal/server/data/resource.go index 744b2a8..07a0c9c 100644 --- a/internal/server/data/resource.go +++ b/internal/server/data/resource.go @@ -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 diff --git a/internal/server/data/resource_test.go b/internal/server/data/resource_test.go index 93d00ad..91f163f 100644 --- a/internal/server/data/resource_test.go +++ b/internal/server/data/resource_test.go @@ -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)) } } diff --git a/internal/server/data/server_test.go b/internal/server/data/server_test.go index 3e99ede..e284eba 100644 --- a/internal/server/data/server_test.go +++ b/internal/server/data/server_test.go @@ -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)) diff --git a/internal/server/httperror/error_test.go b/internal/server/httperror/error_test.go index 71dc78b..b48d61e 100644 --- a/internal/server/httperror/error_test.go +++ b/internal/server/httperror/error_test.go @@ -18,7 +18,7 @@ package httperror_test import ( "encoding/json" - "io/ioutil" + "io" "net/http" "net/http/httptest" "testing" @@ -53,7 +53,7 @@ func TestErrorJSONSetsBodyCorrectly(t *testing.T) { httperror.ErrorJSON(w, expectedReason, http.StatusNotFound) errMsg := httperror.ServerErrMessage{} - if bodyBytes, err := ioutil.ReadAll(w.Result().Body); err != nil { + if bodyBytes, err := io.ReadAll(w.Result().Body); err != nil { t.Errorf("Unexpected error reading response body: %v", err) } else if err := json.Unmarshal(bodyBytes, &errMsg); err != nil { t.Errorf("Response body contains invalid JSON entity: %v", err) diff --git a/internal/server/user/mux/gorillize_test.go b/internal/server/user/mux/gorillize_test.go index 570a823..1ef724c 100644 --- a/internal/server/user/mux/gorillize_test.go +++ b/internal/server/user/mux/gorillize_test.go @@ -18,7 +18,6 @@ package mux import ( "io" - "io/ioutil" "net/http" "net/http/httptest" "reflect" @@ -152,7 +151,7 @@ func TestGorillizeReturnsAMuxThatRespectsRouteOrderAB(t *testing.T) { res := w.Result() - if body, _ := ioutil.ReadAll(res.Body); string(body) != "routeA" { + if body, _ := io.ReadAll(res.Body); string(body) != "routeA" { t.Errorf("Mux did not respect route order %q", body) } } @@ -180,7 +179,7 @@ func TestGorillizeReturnsAMuxThatRespectsRouteOrderBA(t *testing.T) { res := w.Result() - if body, _ := ioutil.ReadAll(res.Body); string(body) != "routeB" { + if body, _ := io.ReadAll(res.Body); string(body) != "routeB" { t.Errorf("Mux did not respect route order %q", body) } } diff --git a/internal/server/user/server.go b/internal/server/user/server.go index c8173bd..6457a6b 100644 --- a/internal/server/user/server.go +++ b/internal/server/user/server.go @@ -20,9 +20,9 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" "net" "net/http" + "os" "sync" "github.com/BBVA/kapow/internal/logger" @@ -90,7 +90,7 @@ func Run(bindAddr string, wg *sync.WaitGroup, certFile, keyFile, cliCaFile strin func loadCertificatesFromFile(certFile string) (pool *x509.CertPool, err error) { if certFile != "" { var caCerts []byte - caCerts, err = ioutil.ReadFile(certFile) + caCerts, err = os.ReadFile(certFile) if err == nil { pool = x509.NewCertPool() if !pool.AppendCertsFromPEM(caCerts) {