forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_initialize_response_test.go
127 lines (114 loc) · 4.62 KB
/
client_initialize_response_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
package statsig
import (
"bytes"
"encoding/json"
"net/http"
"os"
"strconv"
"testing"
)
func TestInitializeResponseConsistency(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Disabled until optimization are complete")
}
user := User{
UserID: "123",
Email: "test@statsig.com",
Country: "US",
Custom: map[string]interface{}{"test": "123"},
CustomIDs: map[string]string{"stableID": "12345"},
}
for _, api := range testAPIs {
t.Run("Validate consistency for "+api, func(t *testing.T) {
endpoint := api + "/initialize"
input := map[string]interface{}{
"user": user,
"statsigMetadata": map[string]string{
"sdkType": "consistency-test",
"sessionID": "x123",
},
}
body, _ := json.Marshal(input)
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(body))
if err != nil {
t.Errorf("Failed to make a request to %s", endpoint)
}
clientKey := os.Getenv("test_client_key")
req.Header.Add("STATSIG-API-KEY", clientKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("STATSIG-CLIENT-TIME", strconv.FormatInt(getUnixMilli(), 10))
req.Header.Add("STATSIG-SDK-TYPE", getStatsigMetadata().SDKType)
req.Header.Add("STATSIG-SDK-VERSION", getStatsigMetadata().SDKVersion)
client := http.Client{}
response, err := client.Do(req)
if err != nil {
t.Errorf("Failed to get a valid response from %s", endpoint)
}
defer response.Body.Close()
if response.StatusCode < 200 || response.StatusCode >= 300 {
t.Errorf("Request to %s failed with status %d", endpoint, response.StatusCode)
}
actualResponseBody, err := filterHttpResponseAndReadBody(response)
if err != nil {
t.Errorf("Error reading %s response body", endpoint)
}
InitializeWithOptions(secret, &Options{
API: api,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
})
defer ShutdownAndDangerouslyClearInstance()
formattedResponse := GetClientInitializeResponse(user)
filterClientInitializeResponse(&formattedResponse)
formattedResponseJson, _ := json.Marshal(formattedResponse)
formattedResponseWithOptions := GetClientInitializeResponseWithOptions(user, &GCIROptions{})
filterClientInitializeResponse(&formattedResponseWithOptions)
formattedResponseWithOptionsJson, _ := json.Marshal(formattedResponseWithOptions)
if string(actualResponseBody) != string(formattedResponseJson) {
t.Errorf("Inconsistent response from GetClientInitializeResponse vs %s", endpoint)
}
if string(actualResponseBody) != string(formattedResponseWithOptionsJson) {
t.Errorf("Inconsistent response from GetClientInitializeResponseWithOptions vs %s", endpoint)
}
})
}
}
func filterHttpResponseAndReadBody(httpResponse *http.Response) ([]byte, error) {
var interfaceBody ClientInitializeResponse
// Initialize nullable fields so that JSON Unmarshal doesn't convert to null
interfaceBody.FeatureGates = make(map[string]GateInitializeResponse)
interfaceBody.DynamicConfigs = make(map[string]ConfigInitializeResponse)
interfaceBody.LayerConfigs = make(map[string]LayerInitializeResponse)
interfaceBody.SdkParams = make(map[string]string)
interfaceBody.EvaluatedKeys = make(map[string]interface{})
err := json.NewDecoder(httpResponse.Body).Decode(&interfaceBody)
if err != nil {
return make([]byte, 0), err
}
filterClientInitializeResponse(&interfaceBody)
return json.Marshal(interfaceBody)
}
func filterClientInitializeResponse(clientInitializeResponse *ClientInitializeResponse) {
for i := range clientInitializeResponse.FeatureGates {
for j := range clientInitializeResponse.FeatureGates[i].SecondaryExposures {
clientInitializeResponse.FeatureGates[i].SecondaryExposures[j].Gate = "__REMOVED_FOR_TEST__"
}
}
for i := range clientInitializeResponse.DynamicConfigs {
for j := range clientInitializeResponse.DynamicConfigs[i].SecondaryExposures {
clientInitializeResponse.DynamicConfigs[i].SecondaryExposures[j].Gate = "__REMOVED_FOR_TEST__"
}
}
for i := range clientInitializeResponse.LayerConfigs {
for j := range clientInitializeResponse.LayerConfigs[i].SecondaryExposures {
clientInitializeResponse.LayerConfigs[i].SecondaryExposures[j].Gate = "__REMOVED_FOR_TEST__"
}
for j := range clientInitializeResponse.LayerConfigs[i].UndelegatedSecondaryExposures {
clientInitializeResponse.LayerConfigs[i].UndelegatedSecondaryExposures[j].Gate = "__REMOVED_FOR_TEST__"
}
}
clientInitializeResponse.Generator = "__REMOVED_FOR_TEST__"
clientInitializeResponse.Time = 0
clientInitializeResponse.SDKInfo = SDKInfo{}
clientInitializeResponse.User = User{}
}