Skip to content

Commit

Permalink
Merge pull request #270 from testwill/ioutil
Browse files Browse the repository at this point in the history
chore: remove refs to deprecated io/ioutil
  • Loading branch information
CesarGallego authored Jul 7, 2024
2 parents e13f146 + 115e48a commit b8abeb4
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 59 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package cmd

import (
"io/ioutil"
"io"
"os"

"github.com/BBVA/kapow/internal/client"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/http/reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package http
import (
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"

"github.com/BBVA/kapow/internal/server/httperror"
Expand All @@ -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")
}
Expand Down
12 changes: 6 additions & 6 deletions internal/http/reason_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package http

import (
"io/ioutil"
"io"
nethttp "net/http"
"strings"
"testing"
Expand All @@ -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"}`,
),
Expand All @@ -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"}`,
),
Expand All @@ -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"}`,
),
Expand All @@ -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(""),
),
}
Expand All @@ -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...`,
),
Expand Down
3 changes: 1 addition & 2 deletions internal/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/x509"
"errors"
"io"
"io/ioutil"
"net/http"
"os"

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/server/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package control

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"

"github.com/google/uuid"
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/server/control/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"reflect"
Expand All @@ -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))
Expand Down Expand Up @@ -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))
}
Expand Down
11 changes: 5 additions & 6 deletions internal/server/data/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit b8abeb4

Please sign in to comment.