forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.go
114 lines (106 loc) · 2.96 KB
/
test_utils.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
package statsig
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
)
type events []map[string]interface{}
type testServerOptions struct {
dcsOnline bool
onLogEvent func(events []map[string]interface{})
onDCS func()
onGetIDLists func()
withSampling bool
isLayerExposure bool
}
func getTestServer(opts testServerOptions) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Add("x-statsig-region", "az-westus-2")
if strings.Contains(req.URL.Path, "download_config_specs") {
if !opts.dcsOnline {
res.WriteHeader(http.StatusInternalServerError)
} else {
dcsFile := "download_config_specs.json"
if opts.withSampling {
dcsFile = "download_config_specs_with_diagnostics_sampling.json"
}
if opts.isLayerExposure {
dcsFile = "layer_exposure_download_config_specs.json"
}
bytes, _ := os.ReadFile(dcsFile)
res.WriteHeader(http.StatusOK)
_, _ = res.Write(bytes)
if opts.onDCS != nil {
opts.onDCS()
}
}
if opts.onDCS != nil {
opts.onDCS()
}
} else if strings.Contains(req.URL.Path, "log_event") {
res.WriteHeader(http.StatusOK)
type requestInput struct {
Events []map[string]interface{} `json:"events"`
StatsigMetadata statsigMetadata `json:"statsigMetadata"`
}
input := &requestInput{}
defer req.Body.Close()
if req.Header.Get("Content-Encoding") == "gzip" {
gz, _ := gzip.NewReader(req.Body)
bodyBytes, _ := io.ReadAll(gz)
_ = json.Unmarshal(bodyBytes, &input)
gz.Close()
} else {
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(req.Body)
_ = json.Unmarshal(buf.Bytes(), &input)
}
if opts.onLogEvent != nil {
opts.onLogEvent(input.Events)
}
} else if strings.Contains(req.URL.Path, "get_id_lists") {
res.WriteHeader(http.StatusOK)
response, _ := json.Marshal(map[string]map[string]interface{}{
"my_id_list": {
"name": "my_id_list",
"size": 1,
"url": fmt.Sprintf("%s/my_id_list", getTestIDListServer().URL),
"creationTime": 1,
"fileID": "a_file_id",
},
})
_, _ = res.Write(response)
if opts.onGetIDLists != nil {
opts.onGetIDLists()
}
}
}))
}
func getTestIDListServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if strings.Contains(req.URL.Path, "my_id_list") {
res.WriteHeader(http.StatusOK)
response, _ := json.Marshal("+asdfcd")
_, _ = res.Write(response)
}
}))
}
func convertToExposureEvent(eventData map[string]interface{}) Event {
eventJSON, err := json.Marshal(eventData)
if err != nil {
fmt.Println("Error marshalling:", err)
return Event{}
}
var event Event
if err := json.Unmarshal(eventJSON, &event); err != nil {
fmt.Println("Error unmarshalling:", err)
return Event{}
}
return event
}