forked from launchdarkly/ld-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutils_test.go
257 lines (231 loc) · 6.55 KB
/
testutils_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package relay
import (
"bufio"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
ld "gopkg.in/launchdarkly/go-server-sdk.v4"
"gopkg.in/launchdarkly/go-server-sdk.v4/ldlog"
)
var nullLogger = log.New(ioutil.Discard, "", 0)
var emptyStore = ld.NewInMemoryFeatureStore(nullLogger)
var zero = 0
func makeNullLoggers() ldlog.Loggers {
ls := ldlog.Loggers{}
ls.SetMinLevel(ldlog.None)
return ls
}
type testFlag struct {
flag ld.FeatureFlag
expectedValue interface{}
expectedVariation int
expectedReason map[string]interface{}
isExperiment bool
}
var flag1ServerSide = testFlag{
flag: ld.FeatureFlag{Key: "some-flag-key", OffVariation: &zero, Variations: []interface{}{true}, Version: 2},
expectedValue: true,
expectedVariation: 0,
expectedReason: map[string]interface{}{"kind": "OFF"},
}
var flag2ServerSide = testFlag{
flag: ld.FeatureFlag{Key: "another-flag-key", On: true, Fallthrough: ld.VariationOrRollout{Variation: &zero}, Variations: []interface{}{3}, Version: 1},
expectedValue: 3,
expectedVariation: 0,
expectedReason: map[string]interface{}{"kind": "FALLTHROUGH"},
}
var flag3ServerSideNotMobile = testFlag{
flag: ld.FeatureFlag{
Key: "off-variation-key",
Version: 3,
ClientSideAvailability: &ld.ClientSideAvailability{
UsingMobileKey: false,
},
},
expectedValue: nil,
expectedReason: map[string]interface{}{"kind": "OFF"},
}
var flag4ClientSide = testFlag{
flag: ld.FeatureFlag{Key: "client-flag-key", OffVariation: &zero, Variations: []interface{}{5}, Version: 2, ClientSide: true},
expectedValue: 5,
expectedVariation: 0,
expectedReason: map[string]interface{}{"kind": "OFF"},
}
var flag5ClientSide = testFlag{
flag: ld.FeatureFlag{
Key: "fallthrough-experiment-flag-key",
On: true,
Fallthrough: ld.VariationOrRollout{Variation: &zero},
Variations: []interface{}{3},
TrackEventsFallthrough: true,
Version: 1,
ClientSide: true,
},
expectedValue: 3,
expectedReason: map[string]interface{}{"kind": "FALLTHROUGH"},
isExperiment: true,
}
var flag6ClientSideNotMobile = testFlag{
flag: ld.FeatureFlag{
Key: "rule-match-experiment-flag-key",
On: true,
Fallthrough: ld.VariationOrRollout{},
Rules: []ld.Rule{
ld.Rule{
VariationOrRollout: ld.VariationOrRollout{Variation: &zero},
ID: "rule-id",
TrackEvents: true,
Clauses: []ld.Clause{
ld.Clause{
Attribute: "key",
Op: "in",
Values: []interface{}{"not-a-real-user-key "},
Negate: true,
},
},
},
},
Variations: []interface{}{4},
Version: 1,
ClientSideAvailability: &ld.ClientSideAvailability{
UsingEnvironmentID: true,
UsingMobileKey: false,
},
},
expectedValue: 4,
expectedReason: map[string]interface{}{"kind": "RULE_MATCH", "ruleIndex": 0, "ruleId": "rule-id"},
isExperiment: true,
}
var flag7Mobile = testFlag{
flag: ld.FeatureFlag{
Key: "mobile-flag-key",
OffVariation: &zero,
Variations: []interface{}{5},
Version: 2,
ClientSideAvailability: &ld.ClientSideAvailability{
UsingMobileKey: true,
},
},
expectedValue: 5,
expectedVariation: 0,
expectedReason: map[string]interface{}{"kind": "OFF"},
}
var allFlags = []testFlag{flag1ServerSide, flag2ServerSide, flag3ServerSideNotMobile, flag4ClientSide,
flag5ClientSide, flag6ClientSideNotMobile, flag7Mobile}
var clientSideFlags = []testFlag{flag4ClientSide, flag5ClientSide, flag6ClientSideNotMobile}
var mobileFlags = []testFlag{flag1ServerSide, flag2ServerSide, flag4ClientSide, flag5ClientSide, flag7Mobile}
var segment1 = ld.Segment{Key: "segment-key"}
// Returns a key matching the UUID header pattern
func key() string {
return "mob-ffffffff-ffff-4fff-afff-ffffffffffff"
}
func user() string {
return "eyJrZXkiOiJ0ZXN0In0="
}
func makeStoreWithData(initialized bool) ld.FeatureStore {
store := ld.NewInMemoryFeatureStore(nullLogger)
addAllFlags(store, initialized)
return store
}
func addAllFlags(store ld.FeatureStore, initialized bool) {
if initialized {
store.Init(nil)
}
for _, flag := range allFlags {
f := flag
store.Upsert(ld.Features, &f.flag)
}
store.Upsert(ld.Segments, &segment1)
}
func flagsMap(testFlags []testFlag) map[string]interface{} {
ret := make(map[string]interface{})
for _, f := range testFlags {
ret[f.flag.Key] = f.flag
}
return ret
}
func makeTestContextWithData() *clientContextImpl {
return &clientContextImpl{
client: FakeLDClient{initialized: true},
store: makeStoreWithData(true),
loggers: makeNullLoggers(),
}
}
func makeEvalBody(flags []testFlag, fullData bool, reasons bool) string {
obj := make(map[string]interface{})
for _, f := range flags {
value := f.expectedValue
if fullData {
m := map[string]interface{}{"value": value, "version": f.flag.Version}
if value != nil {
m["variation"] = f.expectedVariation
}
if reasons || f.isExperiment {
m["reason"] = f.expectedReason
}
if f.flag.TrackEvents || f.isExperiment {
m["trackEvents"] = true
}
if f.isExperiment {
m["trackReason"] = true
}
value = m
}
obj[f.flag.Key] = value
}
out, _ := json.Marshal(obj)
return string(out)
}
// jsonFind returns the nested entity at a path in a json obj
func jsonFind(obj map[string]interface{}, paths ...string) interface{} {
var value interface{} = obj
for _, p := range paths {
if v, ok := value.(map[string]interface{}); !ok {
return nil
} else {
value = v[p]
}
}
return value
}
type bodyMatcher func(t *testing.T, body []byte)
func expectBody(expectedBody string) bodyMatcher {
return func(t *testing.T, body []byte) {
assert.EqualValues(t, expectedBody, body)
}
}
func expectJSONBody(expectedBody string) bodyMatcher {
return func(t *testing.T, body []byte) {
assert.JSONEq(t, expectedBody, string(body))
}
}
type StreamRecorder struct {
*bufio.Writer
*httptest.ResponseRecorder
closer chan bool
}
func (r StreamRecorder) CloseNotify() <-chan bool {
return r.closer
}
func (r StreamRecorder) Close() {
r.closer <- true
}
func (r StreamRecorder) Write(data []byte) (int, error) {
return r.Writer.Write(data)
}
func (r StreamRecorder) Flush() {
r.Writer.Flush()
}
func NewStreamRecorder() (StreamRecorder, io.Reader) {
reader, writer := io.Pipe()
recorder := httptest.NewRecorder()
return StreamRecorder{
ResponseRecorder: recorder,
Writer: bufio.NewWriter(writer),
closer: make(chan bool),
}, reader
}