Refactor example: use io.ReadAll instead of io.Copy (#320)

This commit is contained in:
Oleksandr Redko 2023-07-18 09:42:22 +03:00 committed by GitHub
parent b2b650971a
commit f53600aa9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 7 deletions

View File

@ -4,7 +4,6 @@ package jwt_test
// This is based on a (now outdated) example at https://gist.github.com/cryptix/45c33ecf0ae54828e63b // This is based on a (now outdated) example at https://gist.github.com/cryptix/45c33ecf0ae54828e63b
import ( import (
"bytes"
"crypto/rsa" "crypto/rsa"
"fmt" "fmt"
"io" "io"
@ -93,11 +92,10 @@ func Example_getTokenViaHTTP() {
} }
// Read the token out of the response body // Read the token out of the response body
buf := new(bytes.Buffer) buf, err := io.ReadAll(res.Body)
_, err = io.Copy(buf, res.Body)
fatal(err) fatal(err)
res.Body.Close() res.Body.Close()
tokenString := strings.TrimSpace(buf.String()) tokenString := strings.TrimSpace(string(buf))
// Parse the token // Parse the token
token, err := jwt.ParseWithClaims(tokenString, &CustomClaimsExample{}, func(token *jwt.Token) (interface{}, error) { token, err := jwt.ParseWithClaims(tokenString, &CustomClaimsExample{}, func(token *jwt.Token) (interface{}, error) {
@ -128,11 +126,10 @@ func Example_useTokenViaHTTP() {
fatal(err) fatal(err)
// Read the response body // Read the response body
buf := new(bytes.Buffer) buf, err := io.ReadAll(res.Body)
_, err = io.Copy(buf, res.Body)
fatal(err) fatal(err)
res.Body.Close() res.Body.Close()
fmt.Println(buf.String()) fmt.Printf("%s", buf)
// Output: Welcome, foo // Output: Welcome, foo
} }