forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_system_test.go
166 lines (150 loc) · 3.84 KB
/
event_system_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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bytes"
"strings"
"testing"
"time"
"github.com/Sirupsen/logrus"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
)
var (
handlerTypes = []apidef.TykEventHandlerName{
EH_LogHandler,
EH_WebHook,
}
)
func prepareSpecWithEvents(logger *logrus.Logger) (spec *APISpec) {
if logger == nil {
logger = log
}
def := &apidef.APIDefinition{
EventHandlers: apidef.EventHandlerMetaConfig{
Events: map[apidef.TykEvent][]apidef.EventHandlerTriggerConfig{
EventAuthFailure: {
{
Handler: EH_LogHandler,
HandlerMeta: map[string]interface{}{
"prefix": "testprefix",
"logger": logger,
},
},
},
},
},
}
spec = &APISpec{APIDefinition: def}
// From api_definition.go:
spec.EventPaths = make(map[apidef.TykEvent][]config.TykEventHandler)
for eventName, eventHandlerConfs := range def.EventHandlers.Events {
for _, handlerConf := range eventHandlerConfs {
eventHandlerInstance, err := EventHandlerByName(handlerConf, spec)
if err != nil {
log.Error("Failed to init event handler: ", err)
} else {
spec.EventPaths[eventName] = append(spec.EventPaths[eventName], eventHandlerInstance)
}
}
}
return spec
}
func prepareEventsConf() (conf *config.Config) {
return &config.Config{
EventHandlers: apidef.EventHandlerMetaConfig{
Events: map[apidef.TykEvent][]apidef.EventHandlerTriggerConfig{
EventQuotaExceeded: {
{
Handler: EH_LogHandler,
HandlerMeta: map[string]interface{}{
"prefix": "testprefix1",
"logger": log,
},
},
},
EventAuthFailure: {
{
Handler: EH_LogHandler,
HandlerMeta: map[string]interface{}{
"prefix": "testprefix2",
"logger": log,
},
},
},
},
},
}
}
func prepareEventHandlerConfig(handler apidef.TykEventHandlerName) (config apidef.EventHandlerTriggerConfig) {
config.Handler = handler
switch handler {
case EH_LogHandler:
config.HandlerMeta = map[string]interface{}{
"prefix": "testprefix",
"logger": log,
}
case EH_WebHook:
config.HandlerMeta = map[string]interface{}{}
}
return config
}
func TestEventHandlerByName(t *testing.T) {
spec := prepareSpecWithEvents(nil)
for _, handlerType := range handlerTypes {
handlerConfig := prepareEventHandlerConfig(handlerType)
_, err := EventHandlerByName(handlerConfig, spec)
// CP is disabled on standard builds:
if handlerType == EH_CoProcessHandler {
continue
}
if err != nil {
t.Fatalf("Couldn't get handler for %s\n", handlerType)
}
}
}
func TestLogMessageEventHandler(t *testing.T) {
buf := &bytes.Buffer{}
testLogger := logrus.New()
testLogger.Out = buf
spec := prepareSpecWithEvents(testLogger)
handler := spec.EventPaths[EventAuthFailure][0]
em := config.EventMessage{
Type: EventAuthFailure,
Meta: EventKeyFailureMeta{
EventMetaDefault: EventMetaDefault{Message: "Auth Failure"},
Path: "/",
Origin: "127.0.0.1",
Key: "abc",
},
TimeStamp: time.Now().Local().String(),
}
lookup := "testprefix:AuthFailure"
handler.HandleEvent(em)
if !strings.Contains(buf.String(), lookup) {
t.Fatal("Couldn't find log message")
}
}
func TestInitGenericEventHandlers(t *testing.T) {
conf := prepareEventsConf()
initGenericEventHandlers(conf)
triggers := conf.EventTriggers
if len(triggers) != 2 {
t.Fatal("EventTriggers length doesn't match")
}
for _, e := range []apidef.TykEvent{EventQuotaExceeded, EventAuthFailure} {
if triggers[e] == nil {
t.Fatalf("EventTriggers doesn't contain %s handlers", e)
}
}
for _, handlers := range triggers {
if len(handlers) != 1 {
t.Fatal("EventTriggers handlers length doesn't match")
}
}
}
func BenchmarkInitGenericEventHandlers(b *testing.B) {
b.ReportAllocs()
conf := prepareEventsConf()
for i := 0; i < b.N; i++ {
initGenericEventHandlers(conf)
}
}