forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatsig_test.go
137 lines (121 loc) · 3.95 KB
/
statsig_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
package statsig
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestBootstrap(t *testing.T) {
if IsInitialized() {
t.Errorf("expected statsig to not be initialized yet")
}
bytes, _ := os.ReadFile("download_config_specs.json")
InitializeWithOptions("secret-key", &Options{
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
})
if !IsInitialized() {
t.Errorf("expected statsig to be initialized")
}
if CheckGate(User{UserID: "123"}, "always_on_gate") {
t.Errorf("always_on_gate should return false when there is no bootstrap value")
}
ShutdownAndDangerouslyClearInstance()
opt := &Options{
BootstrapValues: string(bytes[:]),
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt)
if !CheckGate(User{UserID: "123"}, "always_on_gate") {
t.Errorf("always_on_gate should return true bootstrap value is provided")
}
ShutdownAndDangerouslyClearInstance()
}
func TestRulesUpdatedCallback(t *testing.T) {
// First, verify that rules updated callback is called and returns the rules string
bytes, _ := os.ReadFile("download_config_specs.json")
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
if strings.Contains(req.URL.Path, "download_config_specs") {
_, _ = res.Write(bytes)
}
}))
callbacked := false
rules := ""
opt := &Options{
API: testServer.URL,
RulesUpdatedCallback: func(rulesString string, time int64) {
rules = rulesString
if time == 1631638014811 {
callbacked = true
}
},
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt)
if !callbacked {
t.Errorf("rules updated callback did not happen")
}
if !CheckGate(User{UserID: "136"}, "fractional_gate") {
t.Errorf("fractional_gate should return true for the given user")
}
ShutdownAndDangerouslyClearInstance()
// Now use rules from the previous update callback to bootstrap, and validate values
opt_bootstrap := &Options{
BootstrapValues: rules,
LocalMode: true,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt_bootstrap)
if !CheckGate(User{UserID: "123"}, "always_on_gate") {
t.Errorf("always_on_gate should return true bootstrap value is provided")
}
ShutdownAndDangerouslyClearInstance()
}
func TestLogImmediate(t *testing.T) {
env := ""
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if strings.Contains(req.URL.Path, "log_event") {
if req.Method != "POST" {
t.Errorf("Expected ‘POST’ request, got '%s'", req.Method)
}
type requestInput struct {
Events []Event `json:"events"`
StatsigMetadata statsigMetadata `json:"statsigMetadata"`
}
input := &requestInput{}
defer req.Body.Close()
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(req.Body)
_ = json.Unmarshal(buf.Bytes(), &input)
env = input.Events[0].User.StatsigEnvironment["tier"]
}
res.WriteHeader(http.StatusOK)
}))
defer testServer.Close()
opt := &Options{
API: testServer.URL,
Environment: Environment{Tier: "test"},
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt)
event := Event{EventName: "test_event", User: User{UserID: "123"}}
response, err := LogImmediate([]Event{event})
if response.StatusCode != http.StatusOK {
t.Errorf("Status should be OK")
}
if err != nil {
t.Errorf("Error should be nil")
}
if env != "test" {
t.Errorf("Environment not set on user")
}
ShutdownAndDangerouslyClearInstance()
}