Skip to content

Commit f53600a

Browse files
authored
Refactor example: use io.ReadAll instead of io.Copy (#320)
1 parent b2b6509 commit f53600a

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

http_example_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package jwt_test
44
// This is based on a (now outdated) example at https://gist.github.com/cryptix/45c33ecf0ae54828e63b
55

66
import (
7-
"bytes"
87
"crypto/rsa"
98
"fmt"
109
"io"
@@ -93,11 +92,10 @@ func Example_getTokenViaHTTP() {
9392
}
9493

9594
// Read the token out of the response body
96-
buf := new(bytes.Buffer)
97-
_, err = io.Copy(buf, res.Body)
95+
buf, err := io.ReadAll(res.Body)
9896
fatal(err)
9997
res.Body.Close()
100-
tokenString := strings.TrimSpace(buf.String())
98+
tokenString := strings.TrimSpace(string(buf))
10199

102100
// Parse the token
103101
token, err := jwt.ParseWithClaims(tokenString, &CustomClaimsExample{}, func(token *jwt.Token) (interface{}, error) {
@@ -128,11 +126,10 @@ func Example_useTokenViaHTTP() {
128126
fatal(err)
129127

130128
// Read the response body
131-
buf := new(bytes.Buffer)
132-
_, err = io.Copy(buf, res.Body)
129+
buf, err := io.ReadAll(res.Body)
133130
fatal(err)
134131
res.Body.Close()
135-
fmt.Println(buf.String())
132+
fmt.Printf("%s", buf)
136133

137134
// Output: Welcome, foo
138135
}

0 commit comments

Comments
 (0)