Skip to content

Commit

Permalink
Migrate from deprecated io/ioutil (#34)
Browse files Browse the repository at this point in the history
* Migrate from deprecated `io/ioutil`

* fix typo

* fix space formatting for :=io
  • Loading branch information
keystroke3 committed Feb 27, 2023
1 parent 417207d commit 8acbd42
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 25 deletions.
2 changes: 1 addition & 1 deletion _examples/autobahn/fasthttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func echoCopyFull(ctx *fasthttp.RequestCtx) {
}

// echoReadAll echoes messages from the client by reading the entire message
// with ioutil.ReadAll.
// with io.ReadAll.
func echoReadAll(ctx *fasthttp.RequestCtx, writeMessage, writePrepared bool) {
err := upgrader.Upgrade(ctx, func(conn *websocket.Conn) {
defer conn.Close()
Expand Down
2 changes: 1 addition & 1 deletion _examples/autobahn/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func echoCopyFull(w http.ResponseWriter, r *http.Request) {
}

// echoReadAll echoes messages from the client by reading the entire message
// with ioutil.ReadAll.
// with io.ReadAll.
func echoReadAll(w http.ResponseWriter, r *http.Request, writeMessage, writePrepared bool) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions _examples/filewatch/fasthttp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main
import (
"flag"
"html/template"
"io/ioutil"
"log"
"os"
"strconv"
Expand Down Expand Up @@ -49,7 +48,7 @@ func readFileIfModified(lastMod time.Time) ([]byte, time.Time, error) {
if !fi.ModTime().After(lastMod) {
return nil, lastMod, nil
}
p, err := ioutil.ReadFile(filename)
p, err := io.ReadFile(filename)
if err != nil {
return nil, fi.ModTime(), err
}
Expand Down
3 changes: 1 addition & 2 deletions _examples/filewatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main
import (
"flag"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -49,7 +48,7 @@ func readFileIfModified(lastMod time.Time) ([]byte, time.Time, error) {
if !fi.ModTime().After(lastMod) {
return nil, lastMod, nil
}
p, err := ioutil.ReadFile(filename)
p, err := io.ReadFile(filename)
if err != nil {
return nil, fi.ModTime(), err
}
Expand Down
5 changes: 2 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptrace"
Expand Down Expand Up @@ -400,7 +399,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
// debugging.
buf := make([]byte, 1024)
n, _ := io.ReadFull(resp.Body, buf)
resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
resp.Body = io.NopCloser(bytes.NewReader(buf[:n]))
return nil, resp, ErrBadHandshake
}

Expand All @@ -418,7 +417,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
break
}

resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
resp.Body = io.NopCloser(bytes.NewReader([]byte{}))
conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")

netConn.SetDeadline(time.Time{})
Expand Down
3 changes: 1 addition & 2 deletions client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -549,7 +548,7 @@ func TestRespOnBadHandshake(t *testing.T) {
t.Errorf("resp.StatusCode=%d, want %d", resp.StatusCode, expectedStatus)
}

p, err := ioutil.ReadAll(resp.Body)
p, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("ReadFull(resp.Body) returned error %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"testing"
)

Expand Down Expand Up @@ -42,7 +41,7 @@ func textMessages(num int) [][]byte {
}

func BenchmarkWriteNoCompression(b *testing.B) {
w := ioutil.Discard
w := io.Discard
c := newTestConn(nil, w, false)
messages := textMessages(100)
b.ResetTimer()
Expand All @@ -53,7 +52,7 @@ func BenchmarkWriteNoCompression(b *testing.B) {
}

func BenchmarkWriteWithCompression(b *testing.B) {
w := ioutil.Discard
w := io.Discard
c := newTestConn(nil, w, false)
messages := textMessages(100)
c.enableWriteCompression = true
Expand Down
5 changes: 2 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/binary"
"errors"
"io"
"io/ioutil"
"math/rand"
"net"
"strconv"
Expand Down Expand Up @@ -843,7 +842,7 @@ func (c *Conn) advanceFrame() (int, error) {
// 1. Skip remainder of previous frame.

if c.readRemaining > 0 {
if _, err := io.CopyN(ioutil.Discard, c.br, c.readRemaining); err != nil {
if _, err := io.CopyN(io.Discard, c.br, c.readRemaining); err != nil {
return noFrame, err
}
}
Expand Down Expand Up @@ -1154,7 +1153,7 @@ func (c *Conn) ReadMessage() (messageType int, p []byte, err error) {
if err != nil {
return messageType, nil, err
}
p, err = ioutil.ReadAll(r)
p, err = io.ReadAll(r)
return messageType, p, err
}

Expand Down
3 changes: 1 addition & 2 deletions conn_broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package websocket

import (
"io"
"io/ioutil"
"sync/atomic"
"testing"
)
Expand Down Expand Up @@ -45,7 +44,7 @@ func newBroadcastConn(c *Conn) *broadcastConn {

func newBroadcastBench(usePrepared, compression bool) *broadcastBench {
bench := &broadcastBench{
w: ioutil.Discard,
w: io.Discard,
doneCh: make(chan struct{}),
closeCh: make(chan struct{}),
usePrepared: usePrepared,
Expand Down
11 changes: 5 additions & 6 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"reflect"
"sync"
Expand Down Expand Up @@ -125,7 +124,7 @@ func TestFraming(t *testing.T) {
}

t.Logf("frame size: %d", n)
rbuf, err := ioutil.ReadAll(r)
rbuf, err := io.ReadAll(r)
if err != nil {
t.Errorf("%s: ReadFull() returned rbuf, %v", name, err)
continue
Expand Down Expand Up @@ -367,7 +366,7 @@ func TestCloseFrameBeforeFinalMessageFrame(t *testing.T) {
if op != BinaryMessage || err != nil {
t.Fatalf("NextReader() returned %d, %v", op, err)
}
_, err = io.Copy(ioutil.Discard, r)
_, err = io.Copy(io.Discard, r)
if !reflect.DeepEqual(err, expectedErr) {
t.Fatalf("io.Copy() returned %v, want %v", err, expectedErr)
}
Expand Down Expand Up @@ -401,7 +400,7 @@ func TestEOFWithinFrame(t *testing.T) {
if op != BinaryMessage || err != nil {
t.Fatalf("%d: NextReader() returned %d, %v", n, op, err)
}
_, err = io.Copy(ioutil.Discard, r)
_, err = io.Copy(io.Discard, r)
if err != errUnexpectedEOF {
t.Fatalf("%d: io.Copy() returned %v, want %v", n, err, errUnexpectedEOF)
}
Expand All @@ -426,7 +425,7 @@ func TestEOFBeforeFinalFrame(t *testing.T) {
if op != BinaryMessage || err != nil {
t.Fatalf("NextReader() returned %d, %v", op, err)
}
_, err = io.Copy(ioutil.Discard, r)
_, err = io.Copy(io.Discard, r)
if err != errUnexpectedEOF {
t.Fatalf("io.Copy() returned %v, want %v", err, errUnexpectedEOF)
}
Expand Down Expand Up @@ -490,7 +489,7 @@ func TestReadLimit(t *testing.T) {
if op != BinaryMessage || err != nil {
t.Fatalf("2: NextReader() returned %d, %v", op, err)
}
_, err = io.Copy(ioutil.Discard, r)
_, err = io.Copy(io.Discard, r)
if err != ErrReadLimit {
t.Fatalf("io.Copy() returned %v", err)
}
Expand Down

0 comments on commit 8acbd42

Please sign in to comment.