mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
test(middleware/auth/jwt): add test TestNewContextAndFromContext (#2447)
This commit is contained in:
parent
b5482d1794
commit
54c6fb3631
@ -254,7 +254,7 @@ func TestServer(t *testing.T) {
|
||||
}
|
||||
if test.exceptErr == nil {
|
||||
if testToken == nil {
|
||||
t.Errorf("except testToken not nil, but got nil")
|
||||
t.Fatal("except testToken not nil, but got nil")
|
||||
}
|
||||
_, ok := testToken.(jwt.MapClaims)
|
||||
if !ok {
|
||||
@ -493,3 +493,26 @@ func TestClientMissKey(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewContextAndFromContext(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
claims jwt.MapClaims
|
||||
}{
|
||||
{"val not nil", jwt.MapClaims{"name": "kratos"}},
|
||||
{"val nil", nil},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ctx := NewContext(context.Background(), test.claims)
|
||||
|
||||
claims, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
t.Fatal("ctx not found authKey{}")
|
||||
}
|
||||
if !reflect.DeepEqual(test.claims, claims) {
|
||||
t.Errorf(`want: %s, got: %v`, test.claims, claims)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user