-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_test.go
111 lines (105 loc) · 2.75 KB
/
key_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package sked
import (
"bytes"
"reflect"
"strings"
"testing"
"time"
)
func TestKey_NewKeyString(t *testing.T) {
now := time.Now().UTC()
ks := NewKeyString(now, 4, "sometag", 5)
if !strings.HasSuffix(ks, ";4;sometag;5") {
t.Errorf("want suffix ';4;sometag;5' got %s", ks)
}
}
func TestKey_String(t *testing.T) {
now := time.Now().UTC()
ks := NewKeyString(now, 4, "sometag", 5)
k := &Key{Time: now, ID: 4, Tag: "sometag", ExpiresAfter: 5}
if k.String() != ks {
t.Errorf("want %s, got %s", ks, k.String())
}
}
func TestKey_IsExpired(t *testing.T) {
now := time.Now().UTC()
key := &Key{}
tests := []struct {
name string
inTime time.Time
inExpiresAfter int
want bool
}{
{"00", now, 1, false},
{"01", now.Add(-2 * time.Second), 1, true},
{"02", now.Add(1 * time.Second), 1, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key.Time = tt.inTime
key.ExpiresAfter = tt.inExpiresAfter
if got := key.IsExpired(now); got != tt.want {
t.Errorf("Key.IsExpired() = %v, want %v", got, tt.want)
}
})
}
}
func TestKey_Parse(t *testing.T) {
now := time.Now().UTC()
tests := []struct {
name string
in string
want *Key
wantErr error
}{
{"00 invalid key", "", &Key{ExpiresAfter: 2, Tag: "abc", Time: now}, ErrInvalidKey},
{"01", now.Format(time.RFC3339) + ";3;abc;2", &Key{ID: 3, ExpiresAfter: 2, Tag: "abc", Time: now}, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseKey(tt.in)
if err != tt.wantErr {
t.Fatalf("got %v, want %v", err, tt.wantErr)
}
if tt.wantErr != nil {
return
}
if !reflect.DeepEqual(got.ExpiresAfter, tt.want.ExpiresAfter) {
t.Errorf("Key.Parse() = %v, want %v", got.ExpiresAfter, tt.want.ExpiresAfter)
}
if !reflect.DeepEqual(got.Tag, tt.want.Tag) {
t.Errorf("Key.Parse() = %v, want %v", got.Tag, tt.want.Tag)
}
if !reflect.DeepEqual(got.Time.Format(time.RFC3339), tt.want.Time.Format(time.RFC3339)) {
t.Errorf("Key.Parse() = %v, want %v", got.Time, tt.want.Time)
}
})
}
}
func TestKey_MatchKey(t *testing.T) {
tests := []struct {
name string
in *Key
want []byte
wantErr error
}{
{"01 id and tag", &Key{ID: 3, Tag: "abc"}, []byte(";3;abc;"), nil},
{"01 id only", &Key{ID: 3, Tag: ""}, []byte(";3;"), nil},
{"02 tag only", &Key{ID: 0, Tag: "def"}, []byte(";def;"), nil},
{"03 no id or tag", &Key{ID: 0, Tag: ""}, nil, ErrNoMatch},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.in.MatchKey()
if err != tt.wantErr {
t.Fatalf("got %v, want %v", err, tt.wantErr)
}
if tt.wantErr != nil {
return
}
if !bytes.Equal(got, tt.want) {
t.Errorf("want %s got %s", tt.want, got)
}
})
}
}