Skip to content

Commit

Permalink
Merge pull request #43 from chriswolfdesign/42-ioutil-replacement
Browse files Browse the repository at this point in the history
Remove usage of ioutil and replace with non-deprecated matching functions
  • Loading branch information
MicahParks authored Aug 11, 2022
2 parents 48a54b0 + f2c4b77 commit a5375b4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
7 changes: 3 additions & 4 deletions checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keyfunc_test

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -17,7 +16,7 @@ import (

// TestChecksum confirms that the JWKS will only perform a refresh if a new JWKS is read from the remote resource.
func TestChecksum(t *testing.T) {
tempDir, err := ioutil.TempDir("", "*")
tempDir, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
Expand All @@ -30,7 +29,7 @@ func TestChecksum(t *testing.T) {

jwksFile := filepath.Join(tempDir, jwksFilePath)

err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600)
err = os.WriteFile(jwksFile, []byte(jwksJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
Expand Down Expand Up @@ -86,7 +85,7 @@ func TestChecksum(t *testing.T) {
if err != nil {
t.Fatalf(logFmt, "Failed to create a test JWKS.", err)
}
err = ioutil.WriteFile(jwksFile, jwksBytes, 0600)
err = os.WriteFile(jwksFile, jwksBytes, 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
Expand Down
4 changes: 2 additions & 2 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package keyfunc
import (
"bytes"
"context"
"io/ioutil"
"io"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -151,7 +151,7 @@ func (j *JWKS) refresh() (err error) {
//goland:noinspection GoUnhandledErrorResult
defer resp.Body.Close()

jwksBytes, err := ioutil.ReadAll(resp.Body)
jwksBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
25 changes: 12 additions & 13 deletions jwks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -63,7 +62,7 @@ func TestInvalidServer(t *testing.T) {

// TestJWKS performs a table test on the JWKS code.
func TestJWKS(t *testing.T) {
tempDir, err := ioutil.TempDir("", "*")
tempDir, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
Expand All @@ -76,7 +75,7 @@ func TestJWKS(t *testing.T) {

jwksFile := filepath.Join(tempDir, jwksFilePath)

err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600)
err = os.WriteFile(jwksFile, []byte(jwksJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
Expand Down Expand Up @@ -208,7 +207,7 @@ func TestJWKS_KIDs(t *testing.T) {

// TestRateLimit performs a test to confirm the rate limiter works as expected.
func TestRateLimit(t *testing.T) {
tempDir, err := ioutil.TempDir("", "*")
tempDir, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
Expand Down Expand Up @@ -330,7 +329,7 @@ func TestRateLimit(t *testing.T) {

// TestRawJWKS confirms a copy of the raw JWKS is returned from the method.
func TestRawJWKS(t *testing.T) {
tempDir, err := ioutil.TempDir("", "*")
tempDir, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
Expand All @@ -343,7 +342,7 @@ func TestRawJWKS(t *testing.T) {

jwksFile := filepath.Join(tempDir, jwksFilePath)

err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600)
err = os.WriteFile(jwksFile, []byte(jwksJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
Expand Down Expand Up @@ -377,7 +376,7 @@ func TestRawJWKS(t *testing.T) {
func TestRequestFactory(t *testing.T) {
var fullJWKSHandler http.Handler
{
tempDir, err := ioutil.TempDir("", "*")
tempDir, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
Expand All @@ -390,7 +389,7 @@ func TestRequestFactory(t *testing.T) {

jwksFile := filepath.Join(tempDir, jwksFilePath)

err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600)
err = os.WriteFile(jwksFile, []byte(jwksJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
Expand All @@ -399,7 +398,7 @@ func TestRequestFactory(t *testing.T) {
}
var emptyJWKSHandler http.Handler
{
tempDir, err := ioutil.TempDir("", "*")
tempDir, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
Expand All @@ -412,7 +411,7 @@ func TestRequestFactory(t *testing.T) {

jwksFile := filepath.Join(tempDir, jwksFilePath)

err = ioutil.WriteFile(jwksFile, []byte(emptyJWKSJSON), 0600)
err = os.WriteFile(jwksFile, []byte(emptyJWKSJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
Expand Down Expand Up @@ -486,7 +485,7 @@ func TestRequestFactory(t *testing.T) {

// TestUnknownKIDRefresh performs a test to confirm that an Unknown kid with refresh the JWKS.
func TestUnknownKIDRefresh(t *testing.T) {
tempDir, err := ioutil.TempDir("", "*")
tempDir, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
Expand All @@ -499,7 +498,7 @@ func TestUnknownKIDRefresh(t *testing.T) {

jwksFile := filepath.Join(tempDir, strings.TrimPrefix(jwksFilePath, "/"))

err = ioutil.WriteFile(jwksFile, []byte(emptyJWKSJSON), 0600)
err = os.WriteFile(jwksFile, []byte(emptyJWKSJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
Expand All @@ -524,7 +523,7 @@ func TestUnknownKIDRefresh(t *testing.T) {
}
defer jwks.EndBackground()

err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600)
err = os.WriteFile(jwksFile, []byte(jwksJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
Expand Down
5 changes: 2 additions & 3 deletions override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -40,7 +39,7 @@ type pseudoJSONKey struct {

// TestNewGiven tests that given keys will be added to a JWKS with a remote resource.
func TestNewGiven(t *testing.T) {
tempDir, err := ioutil.TempDir("", "*")
tempDir, err := os.MkdirTemp("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
Expand All @@ -58,7 +57,7 @@ func TestNewGiven(t *testing.T) {
t.Fatalf(logFmt, "Failed to create cryptographic keys for the test.", err)
}

err = ioutil.WriteFile(jwksFile, jwksBytes, 0600)
err = os.WriteFile(jwksFile, jwksBytes, 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
Expand Down

0 comments on commit a5375b4

Please sign in to comment.