Commit cdfe0621 authored by Henry's avatar Henry

use ioutil.ReadFile for reading test keys

parent fecd9ab2
package jwt package jwt
import ( import (
"bytes"
"fmt" "fmt"
"io" "io/ioutil"
"net/http" "net/http"
"os"
"reflect" "reflect"
"testing" "testing"
"time" "time"
...@@ -56,11 +54,10 @@ var jwtTestData = []struct { ...@@ -56,11 +54,10 @@ var jwtTestData = []struct {
} }
func makeSample(c map[string]interface{}) string { func makeSample(c map[string]interface{}) string {
file, _ := os.Open("test/sample_key") key, e := ioutil.ReadFile("test/sample_key")
buf := new(bytes.Buffer) if e != nil {
io.Copy(buf, file) panic(e.Error())
key := buf.Bytes() }
file.Close()
token := New(GetSigningMethod("RS256")) token := New(GetSigningMethod("RS256"))
token.Claims = c token.Claims = c
...@@ -74,17 +71,15 @@ func makeSample(c map[string]interface{}) string { ...@@ -74,17 +71,15 @@ func makeSample(c map[string]interface{}) string {
} }
func TestJWT(t *testing.T) { func TestJWT(t *testing.T) {
file, _ := os.Open("test/sample_key.pub") key, e := ioutil.ReadFile("test/sample_key.pub")
buf := new(bytes.Buffer) if e != nil {
io.Copy(buf, file) t.Fatal(e)
key := buf.Bytes() }
file.Close()
for _, data := range jwtTestData { for _, data := range jwtTestData {
if data.tokenString == "" { if data.tokenString == "" {
data.tokenString = makeSample(data.claims) data.tokenString = makeSample(data.claims)
} }
token, err := Parse(data.tokenString, func(t *Token) ([]byte, error) { return key, nil }) token, err := Parse(data.tokenString, func(t *Token) ([]byte, error) { return key, nil })
if !reflect.DeepEqual(data.claims, token.Claims) { if !reflect.DeepEqual(data.claims, token.Claims) {
...@@ -112,11 +107,10 @@ func TestJWT(t *testing.T) { ...@@ -112,11 +107,10 @@ func TestJWT(t *testing.T) {
} }
func TestParseRequest(t *testing.T) { func TestParseRequest(t *testing.T) {
file, _ := os.Open("test/sample_key.pub") key, e := ioutil.ReadFile("test/sample_key.pub")
buf := new(bytes.Buffer) if e != nil {
io.Copy(buf, file) t.Fatal(e)
key := buf.Bytes() }
file.Close()
// Bearer token request // Bearer token request
for _, data := range jwtTestData { for _, data := range jwtTestData {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment