1 package jwtx
2
3 import (
4 "encoding/json"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 )
11
12 func TestParseMapStringInterfaceClaims(t *testing.T) {
13
14 assert.EqualValues(t, &Claims{
15 JTI: "jti",
16 Subject: "sub",
17 Issuer: "iss",
18 Audience: []string{"aud"},
19 ExpiresAt: time.Unix(1234, 0),
20 IssuedAt: time.Unix(1234, 0),
21 NotBefore: time.Unix(1234, 0),
22 }, ParseMapStringInterfaceClaims(map[string]interface{}{
23 "jti": "jti",
24 "aud": "aud",
25 "iss": "iss",
26 "sub": "sub",
27 "exp": 1234,
28 "iat": 1234,
29 "nbf": 1234,
30 }))
31
32 assert.EqualValues(t, &Claims{
33 Audience: []string{"aud", "dua"},
34 ExpiresAt: time.Unix(1234, 0),
35 IssuedAt: time.Unix(1234, 0),
36 NotBefore: time.Unix(1234, 0),
37 }, ParseMapStringInterfaceClaims(map[string]interface{}{
38 "aud": []string{"aud", "dua"},
39 "exp": 1234,
40 "iat": 1234,
41 "nbf": 1234,
42 }))
43
44 out, err := json.Marshal(map[string]interface{}{
45 "aud": []string{"aud", "dua"},
46 "exp": 1234,
47 "iat": 1234,
48 "nbf": 1234,
49 })
50 require.NoError(t, err)
51
52 var in map[string]interface{}
53 require.NoError(t, json.Unmarshal(out, &in))
54
55 assert.EqualValues(t, &Claims{
56 Audience: []string{"aud", "dua"},
57 ExpiresAt: time.Unix(1234, 0),
58 IssuedAt: time.Unix(1234, 0),
59 NotBefore: time.Unix(1234, 0),
60 }, ParseMapStringInterfaceClaims(in))
61 }
62
View as plain text