-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcached_test.go
More file actions
140 lines (116 loc) · 2.97 KB
/
cached_test.go
File metadata and controls
140 lines (116 loc) · 2.97 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package introspector_test
import (
"errors"
"strconv"
"testing"
"github.com/codeclysm/introspector/v3"
)
func TestCached(t *testing.T) {
t.Run("no result in cache", func(t *testing.T) {
cache := mockCache{}
intro := mockIntro(0)
cached := introspector.Cached{
Cache: &cache,
Introspector: &intro,
}
i, err := cached.Introspect("token")
if err != nil {
t.Fatal("expected err to be nil, got", err.Error())
}
if i.ClientID != "1" {
t.Fatal("expected client id to be 1")
}
// Check that the result is now in cache
data, ok := cache["introspect:token"]
if !ok {
t.Fatal("expected cache to be filled")
}
if string(data) != `{"I":{"active":false,"client_id":"1"},"Err":""}` {
t.Fatal(`expected data to be '{"I":{"active":false,"client_id":"1"},"Err":""}', got`, string(data))
}
})
t.Run("ensure mockintro works", func(t *testing.T) {
intro := mockIntro(0)
for i := 1; i < 10; i++ {
introspection, err := intro.Introspect("token")
if err != nil {
t.Fatal("expected err to be nil, got", err)
}
client, _ := strconv.Atoi(introspection.ClientID)
if client != i {
t.Fatal("expected client to be", i, "got", client)
}
}
})
t.Run("ensure results are cached", func(t *testing.T) {
cache := mockCache{}
intro := mockIntro(0)
cached := introspector.Cached{
Cache: &cache,
Introspector: &intro,
}
for i := 0; i < 10; i++ {
i, err := cached.Introspect("token")
if err != nil {
t.Fatal("expected err to be nil, got", err)
}
if i.ClientID != "1" {
t.Fatal("expected client id to be 1")
}
}
})
t.Run("ensure mockintroerr works", func(t *testing.T) {
intro := mockIntroErr(0)
for i := 1; i < 10; i++ {
_, err := intro.Introspect("token")
if err == nil {
t.Fatal("expected err not to be nil, got", err)
}
erro, _ := strconv.Atoi(err.Error())
if erro != i {
t.Fatal("expected error to be", i, "got", erro)
}
}
})
t.Run("ensure results are cached", func(t *testing.T) {
cache := mockCache{}
intro := mockIntroErr(0)
cached := introspector.Cached{
Cache: &cache,
Introspector: &intro,
}
for i := 0; i < 10; i++ {
_, err := cached.Introspect("token")
if err == nil {
t.Fatal("expected err not to be nil, got", err)
}
if err.Error() != "1" {
t.Fatal("expected error to be 1, got", err)
}
}
})
}
type mockCache map[string][]byte
func (m mockCache) Get(key string) ([]byte, error) {
data, ok := m[key]
if !ok {
return nil, errors.New("Not found")
}
return data, nil
}
func (m mockCache) Set(key string, data []byte) error {
m[key] = data
return nil
}
type mockIntro int
func (m *mockIntro) Introspect(string) (introspector.Introspection, error) {
*m++
return introspector.Introspection{
ClientID: strconv.Itoa(int(*m)),
}, nil
}
type mockIntroErr int
func (m *mockIntroErr) Introspect(string) (introspector.Introspection, error) {
*m++
return introspector.Introspection{}, errors.New(strconv.Itoa(int(*m)))
}