forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics_test.go
186 lines (156 loc) · 5.17 KB
/
analytics_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"testing"
"github.com/TykTechnologies/tyk/config"
)
func TestGeoIPLookup(t *testing.T) {
testCases := [...]struct {
in string
wantErr bool
}{
{"", false},
{"foobar", true},
{"1.2.3.4", false},
}
for _, tc := range testCases {
_, err := geoIPLookup(tc.in)
switch {
case tc.wantErr && err == nil:
t.Errorf("geoIPLookup(%q) did not error", tc.in)
case !tc.wantErr && err != nil:
t.Errorf("geoIPLookup(%q) errored", tc.in)
}
}
}
func TestURLReplacer(t *testing.T) {
defer resetTestConfig()
globalConf := config.Global()
globalConf.AnalyticsConfig.NormaliseUrls.Enabled = true
globalConf.AnalyticsConfig.NormaliseUrls.NormaliseUUIDs = true
globalConf.AnalyticsConfig.NormaliseUrls.NormaliseNumbers = true
globalConf.AnalyticsConfig.NormaliseUrls.Custom = []string{"ihatethisstring"}
config.SetGlobal(globalConf)
recordUUID1 := AnalyticsRecord{Path: "/15873a748894492162c402d67e92283b/search"}
recordUUID2 := AnalyticsRecord{Path: "/CA761232-ED42-11CE-BACD-00AA0057B223/search"}
recordUUID3 := AnalyticsRecord{Path: "/ca761232-ed42-11ce-BAcd-00aa0057b223/search"}
recordUUID4 := AnalyticsRecord{Path: "/ca761232-ed42-11ce-BAcd-00aa0057b223/search"}
recordID1 := AnalyticsRecord{Path: "/widgets/123456/getParams"}
recordCust := AnalyticsRecord{Path: "/widgets/123456/getParams/ihatethisstring"}
globalConf.AnalyticsConfig.NormaliseUrls.CompiledPatternSet = initNormalisationPatterns()
config.SetGlobal(globalConf)
recordUUID1.NormalisePath(&globalConf)
recordUUID2.NormalisePath(&globalConf)
recordUUID3.NormalisePath(&globalConf)
recordUUID4.NormalisePath(&globalConf)
recordID1.NormalisePath(&globalConf)
recordCust.NormalisePath(&globalConf)
if recordUUID1.Path != "/{uuid}/search" {
t.Error("Path not altered, is:")
t.Error(recordUUID1.Path)
t.Error(config.Global().AnalyticsConfig.NormaliseUrls)
}
if recordUUID2.Path != "/{uuid}/search" {
t.Error("Path not altered, is:")
t.Error(recordUUID2.Path)
}
if recordUUID3.Path != "/{uuid}/search" {
t.Error("Path not altered, is:")
t.Error(recordUUID3.Path)
}
if recordUUID4.Path != "/{uuid}/search" {
t.Error("Path not altered, is:")
t.Error(recordUUID4.Path)
}
if recordID1.Path != "/widgets/{id}/getParams" {
t.Error("Path not altered, is:")
t.Error(recordID1.Path)
}
if recordCust.Path != "/widgets/{id}/getParams/{var}" {
t.Error("Path not altered, is:")
t.Error(recordCust.Path)
}
}
func BenchmarkURLReplacer(b *testing.B) {
b.ReportAllocs()
defer resetTestConfig()
globalConf := config.Global()
globalConf.AnalyticsConfig.NormaliseUrls.Enabled = true
globalConf.AnalyticsConfig.NormaliseUrls.NormaliseUUIDs = true
globalConf.AnalyticsConfig.NormaliseUrls.NormaliseNumbers = true
globalConf.AnalyticsConfig.NormaliseUrls.Custom = []string{"ihatethisstring"}
globalConf.AnalyticsConfig.NormaliseUrls.CompiledPatternSet = initNormalisationPatterns()
config.SetGlobal(globalConf)
for i := 0; i < b.N; i++ {
recordUUID1 := AnalyticsRecord{Path: "/15873a748894492162c402d67e92283b/search"}
recordUUID2 := AnalyticsRecord{Path: "/CA761232-ED42-11CE-BACD-00AA0057B223/search"}
recordUUID3 := AnalyticsRecord{Path: "/ca761232-ed42-11ce-BAcd-00aa0057b223/search"}
recordUUID4 := AnalyticsRecord{Path: "/ca761232-ed42-11ce-BAcd-00aa0057b223/search"}
recordID1 := AnalyticsRecord{Path: "/widgets/123456/getParams"}
recordCust := AnalyticsRecord{Path: "/widgets/123456/getParams/ihatethisstring"}
recordUUID1.NormalisePath(&globalConf)
recordUUID2.NormalisePath(&globalConf)
recordUUID3.NormalisePath(&globalConf)
recordUUID4.NormalisePath(&globalConf)
recordID1.NormalisePath(&globalConf)
recordCust.NormalisePath(&globalConf)
}
}
func TestTagHeaders(t *testing.T) {
req := testReq(t, "GET", "/tagmeplease", nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Tag-Me", "1")
req.Header.Set("X-Tag-Me2", "2")
req.Header.Set("X-Tag-Me3", "3")
req.Header.Set("X-Ignore-Me", "4")
existingTags := []string{"first", "second"}
existingTags = tagHeaders(req, []string{
"x-tag-me",
"x-tag-me2",
"x-tag-me3"},
existingTags)
if len(existingTags) == 2 {
t.Fatal("Existing tags have not been expanded")
}
if len(existingTags) != 5 {
t.Fatalf("Wrong number of tags, got %v, wanted %v", len(existingTags), 5)
}
check := map[string]bool{
"x-tag-me-1": true,
"x-tag-me2-2": true,
"x-tag-me3-3": true,
}
for _, t := range existingTags {
_, ok := check[t]
if ok {
delete(check, t)
}
}
if len(check) != 0 {
t.Fatalf("Header values not proerly set, got: %v, remnainder: %v", existingTags, check)
}
}
func BenchmarkTagHeaders(b *testing.B) {
b.ReportAllocs()
req := testReq(b, "GET", "/tagmeplease", nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Tag-Me", "1")
req.Header.Set("X-Tag-Me2", "2")
req.Header.Set("X-Tag-Me3", "3")
req.Header.Set("X-Ignore-Me", "4")
existingTags := []string{"first", "second"}
var newExistingTags []string
for i := 0; i < b.N; i++ {
newExistingTags = tagHeaders(
req,
[]string{
"x-tag-me",
"x-tag-me2",
"x-tag-me3",
},
existingTags,
)
if len(newExistingTags) == 2 {
b.Fatal("Existing tags have not been expanded")
}
}
}