-
Notifications
You must be signed in to change notification settings - Fork 19
/
required_test.go
52 lines (45 loc) · 1.26 KB
/
required_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package jwt
import (
"errors"
"testing"
)
func TestUnmarshalWithRequired(t *testing.T) {
type Nested struct {
Name string `json:"name,required"`
}
token, err := Sign(testAlg, testSecret, Map{"username": "kataras", "age": 27, "nested": Map{"name": ""}})
if err != nil {
t.Fatal(err)
}
verifiedToken, err := Verify(testAlg, testSecret, token)
if err != nil {
t.Fatal(err)
}
var claims = struct {
Username string `json:"username,required"`
Nested Nested `json:"nested"`
}{}
err = verifiedToken.Claims(&claims)
if err != nil {
t.Fatal(err)
}
if expected, got := "kataras", claims.Username; expected != got {
t.Fatalf("expected claims{username} to be: %s but got: %s", expected, got)
}
var claimsShouldFail = struct {
Username string `json:"username,required"`
Age int `json:"age,required"`
Nested *Nested/* test indirect too */ `json:"nested"`
}{}
err = verifiedToken.Claims(&claimsShouldFail)
// this should pass as we don't set the Unmarshal func yet.
if err != nil {
t.Fatal(err)
}
Unmarshal = UnmarshalWithRequired
// this should fail now because nested.name is missing.
err = verifiedToken.Claims(&claimsShouldFail)
if !errors.Is(err, ErrMissingKey) {
t.Fatalf("expected error: ErrMissingKey but got: %v", err)
}
}