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
import (
"bytes"
"crypto/rsa"
"fmt"
"io"
@ -93,11 +92,10 @@ func Example_getTokenViaHTTP() {
}
// Read the token out of the response body
buf := new(bytes.Buffer)
_, err = io.Copy(buf, res.Body)
buf, err := io.ReadAll(res.Body)
fatal(err)
res.Body.Close()
tokenString := strings.TrimSpace(buf.String())
tokenString := strings.TrimSpace(string(buf))
// Parse the token
token, err := jwt.ParseWithClaims(tokenString, &CustomClaimsExample{}, func(token *jwt.Token) (interface{}, error) {
@ -128,11 +126,10 @@ func Example_useTokenViaHTTP() {
fatal(err)
// Read the response body
buf := new(bytes.Buffer)
_, err = io.Copy(buf, res.Body)
buf, err := io.ReadAll(res.Body)
fatal(err)
res.Body.Close()
fmt.Println(buf.String())
fmt.Printf("%s", buf)
// Output: Welcome, foo
}