Skip to content

Commit

Permalink
Replace usages of deprecated ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Aug 11, 2021
1 parent 77662a2 commit 50551f9
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 40 deletions.
8 changes: 4 additions & 4 deletions core/ivr/vonage/vonage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"math/rand"
"net/http"
"net/url"
Expand Down Expand Up @@ -105,11 +105,11 @@ func readBody(r *http.Request) ([]byte, error) {
if r.Body == http.NoBody {
return nil, nil
}
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, nil
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
r.Body = io.NopCloser(bytes.NewBuffer(body))
return body, nil
}

Expand Down Expand Up @@ -333,7 +333,7 @@ func (c *client) PreprocessResume(ctx context.Context, db *sqlx.DB, rp *redis.Po
}

// get our recording url out
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, errors.Wrapf(err, "error reading body from request")
}
Expand Down
6 changes: 3 additions & 3 deletions core/models/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models_test
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"sort"
"strings"
"testing"
Expand Down Expand Up @@ -41,7 +41,7 @@ func TestContactImports(t *testing.T) {
// give our org a country by setting country on a channel
db.MustExec(`UPDATE channels_channel SET country = 'US' WHERE id = $1`, testdata.TwilioChannel.ID)

testJSON, err := ioutil.ReadFile("testdata/imports.json")
testJSON, err := os.ReadFile("testdata/imports.json")
require.NoError(t, err)

tcs := []struct {
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestContactImports(t *testing.T) {
testJSON, err = jsonx.MarshalPretty(tcs)
require.NoError(t, err)

err = ioutil.WriteFile("testdata/imports.json", testJSON, 0600)
err = os.WriteFile("testdata/imports.json", testJSON, 0600)
require.NoError(t, err)
}
}
Expand Down
3 changes: 1 addition & 2 deletions core/models/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"path/filepath"
Expand Down Expand Up @@ -186,7 +185,7 @@ func (o *Org) StoreAttachment(ctx context.Context, s storage.Storage, filename s
prefix := config.Mailroom.S3MediaPrefix

// read the content
contentBytes, err := ioutil.ReadAll(content)
contentBytes, err := io.ReadAll(content)
if err != nil {
return "", errors.Wrapf(err, "unable to read attachment content")
}
Expand Down
4 changes: 2 additions & 2 deletions core/msgio/android_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package msgio_test

import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -29,7 +29,7 @@ type MockFCMEndpoint struct {
func (m *MockFCMEndpoint) Handle(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

requestBody, _ := ioutil.ReadAll(r.Body)
requestBody, _ := io.ReadAll(r.Body)

message := &fcm.Message{}
jsonx.Unmarshal(requestBody, message)
Expand Down
3 changes: 1 addition & 2 deletions services/tickets/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"mime"
"net/http"
"path/filepath"
Expand Down Expand Up @@ -138,7 +137,7 @@ func FetchFile(url string, headers map[string]string) (*File, error) {

contentType, _, _ := mime.ParseMediaType(trace.Response.Header.Get("Content-Type"))

return &File{URL: url, ContentType: contentType, Body: ioutil.NopCloser(bytes.NewReader(trace.ResponseBody))}, nil
return &File{URL: url, ContentType: contentType, Body: io.NopCloser(bytes.NewReader(trace.ResponseBody))}, nil
}

// CloseTicket closes the given ticket, and creates and queues a closed event
Expand Down
4 changes: 2 additions & 2 deletions testsuite/elastic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package testsuite

import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
)
Expand Down Expand Up @@ -42,7 +42,7 @@ func NewMockElasticServer() *MockElasticServer {
}

// otherwise read our next body and return our next response
body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)
mock.LastBody = string(body)

w.WriteHeader(200)
Expand Down
3 changes: 1 addition & 2 deletions web/contact/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
"testing"
Expand Down Expand Up @@ -213,7 +212,7 @@ func TestSearch(t *testing.T) {

assert.Equal(t, tc.ExpectedStatus, resp.StatusCode, "%d: unexpected status", i)

content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
assert.NoError(t, err, "%d: error reading body", i)

// on 200 responses parse them
Expand Down
16 changes: 6 additions & 10 deletions web/ivr/ivr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ivr
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestTwilioIVR(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, tc.expectedStatus, resp.StatusCode, "status code mismatch in %s", tc.label)

body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)

if tc.expectedResponse != "" {
assert.Equal(t, `<?xml version="1.0" encoding="UTF-8"?>`+"\n"+tc.expectedResponse, string(body), "response mismatch in %s", tc.label)
Expand Down Expand Up @@ -313,7 +313,7 @@ func TestVonageIVR(t *testing.T) {
} `json:"to"`
Action string `json:"action,omitempty"`
}
body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)
form := &CallForm{}
json.Unmarshal(body, form)
logrus.WithField("method", r.Method).WithField("url", r.URL.String()).WithField("body", string(body)).WithField("form", form).Info("test server called")
Expand Down Expand Up @@ -377,10 +377,6 @@ func TestVonageIVR(t *testing.T) {
tcs := []struct {
label string
url string
action string
channel *testdata.Channel
connectionID models.ConnectionID
form url.Values
body string
expectedStatus int
expectedResponse string
Expand Down Expand Up @@ -424,14 +420,14 @@ func TestVonageIVR(t *testing.T) {
contains: []string{"Great! You said One."},
},
{
label: "dtmf too large",
label: "handle resume with digits out of range in flow",
url: fmt.Sprintf("/ivr/c/%s/handle?action=resume&connection=1&wait_type=gather", testdata.VonageChannel.UUID),
body: `{"dtmf":"101","timed_out":false,"uuid":null,"conversation_uuid":"CON-f90649c3-cbf3-42d6-9ab1-01503befac1c","timestamp":"2019-04-01T21:08:54.680Z"}`,
expectedStatus: 200,
contains: []string{"too big"},
},
{
label: "dtmf 56",
label: "handle resume with digits within range in flow",
url: fmt.Sprintf("/ivr/c/%s/handle?action=resume&connection=1&wait_type=gather", testdata.VonageChannel.UUID),
body: `{"dtmf":"56","timed_out":false,"uuid":null,"conversation_uuid":"CON-f90649c3-cbf3-42d6-9ab1-01503befac1c","timestamp":"2019-04-01T21:08:54.680Z"}`,
expectedStatus: 200,
Expand Down Expand Up @@ -558,7 +554,7 @@ func TestVonageIVR(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, tc.expectedStatus, resp.StatusCode, "status code mismatch in %s", tc.label)

body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)

if tc.expectedResponse != "" {
test.AssertEqualJSON(t, []byte(tc.expectedResponse), body, "response mismatch in %s", tc.label)
Expand Down
4 changes: 2 additions & 2 deletions web/org/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org_test

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"sync"
"testing"
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestMetrics(t *testing.T) {
resp, err := http.DefaultClient.Do(req)
assert.NoError(t, err, "%s: received error", tc.Label)

body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)

if tc.Response != "" {
assert.Equal(t, tc.Response, string(body), "%s: response mismatch", tc.Label)
Expand Down
3 changes: 1 addition & 2 deletions web/simulation/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
"sync"
Expand Down Expand Up @@ -266,7 +265,7 @@ func TestServer(t *testing.T) {

assert.Equal(t, tc.ExpectedStatus, resp.StatusCode, "%d: unexpected status", i)

content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
assert.NoError(t, err, "%d: error reading body", i)

// if this was a success, save our session
Expand Down
7 changes: 4 additions & 3 deletions web/surveyor/surveyor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package surveyor
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
"sync"
"testing"
Expand Down Expand Up @@ -112,7 +113,7 @@ func TestSurveyor(t *testing.T) {
for i, tc := range tcs {
testID := fmt.Sprintf("%s[token=%s]", tc.File, tc.Token)
path := filepath.Join("testdata", tc.File)
submission, err := ioutil.ReadFile(path)
submission, err := os.ReadFile(path)
assert.NoError(t, err)

url := "http://localhost:8090/mr/surveyor/submit"
Expand All @@ -128,7 +129,7 @@ func TestSurveyor(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, tc.StatusCode, resp.StatusCode, "unexpected status code for %s", testID)

body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
assert.Containsf(t, string(body), tc.Contains, "%s does not contain expected body", testID)

id, _ := jsonparser.GetInt(body, "contact", "id")
Expand Down
12 changes: 6 additions & 6 deletions web/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"os"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -66,7 +66,7 @@ func RunWebTests(t *testing.T, truthFile string, substitutions map[string]string
actualResponse []byte
}
tcs := make([]*TestCase, 0, 20)
tcJSON, err := ioutil.ReadFile(truthFile)
tcJSON, err := os.ReadFile(truthFile)
require.NoError(t, err)

for key, value := range substitutions {
Expand Down Expand Up @@ -126,7 +126,7 @@ func RunWebTests(t *testing.T, truthFile string, substitutions map[string]string
actual.HTTPMocks = clonedMocks

tc.HTTPMocks = clonedMocks
tc.actualResponse, err = ioutil.ReadAll(resp.Body)
tc.actualResponse, err = io.ReadAll(resp.Body)
assert.NoError(t, err, "%s: error reading body", tc.Label)

if !test.UpdateSnapshots {
Expand All @@ -136,7 +136,7 @@ func RunWebTests(t *testing.T, truthFile string, substitutions map[string]string
expectedIsJSON := false

if tc.ResponseFile != "" {
expectedResponse, err = ioutil.ReadFile(tc.ResponseFile)
expectedResponse, err = os.ReadFile(tc.ResponseFile)
require.NoError(t, err)

expectedIsJSON = strings.HasSuffix(tc.ResponseFile, ".json")
Expand Down Expand Up @@ -164,7 +164,7 @@ func RunWebTests(t *testing.T, truthFile string, substitutions map[string]string
if test.UpdateSnapshots {
for _, tc := range tcs {
if tc.ResponseFile != "" {
err = ioutil.WriteFile(tc.ResponseFile, tc.actualResponse, 0644)
err = os.WriteFile(tc.ResponseFile, tc.actualResponse, 0644)
require.NoError(t, err, "failed to update response file")
} else {
tc.Response = tc.actualResponse
Expand All @@ -174,7 +174,7 @@ func RunWebTests(t *testing.T, truthFile string, substitutions map[string]string
truth, err := jsonx.MarshalPretty(tcs)
require.NoError(t, err)

err = ioutil.WriteFile(truthFile, truth, 0644)
err = os.WriteFile(truthFile, truth, 0644)
require.NoError(t, err, "failed to update truth file")
}
}
Expand Down

0 comments on commit 50551f9

Please sign in to comment.