mirror of https://github.com/golang-jwt/jwt.git
added a couple tests for request parsing that don't rely on giant parser test dataset
This commit is contained in:
parent
e3cd52238a
commit
288cee4336
|
@ -1,42 +1,84 @@
|
||||||
package request
|
package request
|
||||||
|
|
||||||
// import (
|
import (
|
||||||
// "fmt"
|
"fmt"
|
||||||
// "github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
// "net/http"
|
"github.com/dgrijalva/jwt-go/test"
|
||||||
// "reflect"
|
"net/http"
|
||||||
// "testing"
|
"net/url"
|
||||||
// )
|
"reflect"
|
||||||
//
|
"strings"
|
||||||
// func TestParseRequest(t *testing.T) {
|
"testing"
|
||||||
// // Bearer token request
|
)
|
||||||
// for _, data := range jwtTestData {
|
|
||||||
// // FIXME: custom parsers are not supported by this helper. skip tests that require them
|
var requestTestData = []struct {
|
||||||
// if data.parser != nil {
|
name string
|
||||||
// t.Logf("Skipping [%v]. Custom parsers are not supported by ParseRequest", data.name)
|
claims jwt.MapClaims
|
||||||
// continue
|
headers map[string]string
|
||||||
// }
|
query url.Values
|
||||||
//
|
valid bool
|
||||||
// if data.tokenString == "" {
|
}{
|
||||||
// data.tokenString = makeSample(data.claims)
|
{
|
||||||
// }
|
"oauth bearer token - header",
|
||||||
//
|
jwt.MapClaims{"foo": "bar"},
|
||||||
// r, _ := http.NewRequest("GET", "/", nil)
|
map[string]string{"Authorization": "Bearer %v"},
|
||||||
// r.Header.Set("Authorization", fmt.Sprintf("Bearer %v", data.tokenString))
|
url.Values{},
|
||||||
// token, err := ParseFromRequestWithClaims(r, data.keyfunc, &jwt.MapClaims{})
|
true,
|
||||||
//
|
},
|
||||||
// if token == nil {
|
{
|
||||||
// t.Errorf("[%v] Token was not found: %v", data.name, err)
|
"oauth bearer token - url",
|
||||||
// continue
|
jwt.MapClaims{"foo": "bar"},
|
||||||
// }
|
map[string]string{},
|
||||||
// if !reflect.DeepEqual(&data.claims, token.Claims) {
|
url.Values{"access_token": {"%v"}},
|
||||||
// t.Errorf("[%v] Claims mismatch. Expecting: %v Got: %v", data.name, data.claims, token.Claims)
|
true,
|
||||||
// }
|
},
|
||||||
// if data.valid && err != nil {
|
}
|
||||||
// t.Errorf("[%v] Error while verifying token: %v", data.name, err)
|
|
||||||
// }
|
func TestParseRequest(t *testing.T) {
|
||||||
// if !data.valid && err == nil {
|
// load keys from disk
|
||||||
// t.Errorf("[%v] Invalid token passed validation", data.name)
|
privateKey := test.LoadRSAPrivateKeyFromDisk("../test/sample_key")
|
||||||
// }
|
publicKey := test.LoadRSAPublicKeyFromDisk("../test/sample_key.pub")
|
||||||
// }
|
keyfunc := func(*jwt.Token) (interface{}, error) {
|
||||||
// }
|
return publicKey, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bearer token request
|
||||||
|
for _, data := range requestTestData {
|
||||||
|
// Make token from claims
|
||||||
|
tokenString := test.MakeSampleToken(data.claims, privateKey)
|
||||||
|
|
||||||
|
// Make query string
|
||||||
|
for k, vv := range data.query {
|
||||||
|
for i, v := range vv {
|
||||||
|
if strings.Contains(v, "%v") {
|
||||||
|
data.query[k][i] = fmt.Sprintf(v, tokenString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make request from test struct
|
||||||
|
r, _ := http.NewRequest("GET", fmt.Sprintf("/?%v", data.query.Encode()), nil)
|
||||||
|
for k, v := range data.headers {
|
||||||
|
if strings.Contains(v, "%v") {
|
||||||
|
r.Header.Set(k, fmt.Sprintf(v, tokenString))
|
||||||
|
} else {
|
||||||
|
r.Header.Set(k, tokenString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
token, err := ParseFromRequestWithClaims(r, keyfunc, &jwt.MapClaims{})
|
||||||
|
|
||||||
|
if token == nil {
|
||||||
|
t.Errorf("[%v] Token was not found: %v", data.name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(&data.claims, token.Claims) {
|
||||||
|
t.Errorf("[%v] Claims mismatch. Expecting: %v Got: %v", data.name, data.claims, token.Claims)
|
||||||
|
}
|
||||||
|
if data.valid && err != nil {
|
||||||
|
t.Errorf("[%v] Error while verifying token: %v", data.name, err)
|
||||||
|
}
|
||||||
|
if !data.valid && err == nil {
|
||||||
|
t.Errorf("[%v] Invalid token passed validation", data.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue