forked from dukex/mixpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixpanel_test.go
151 lines (118 loc) · 3.41 KB
/
mixpanel_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
package mixpanel
import (
"encoding/base64"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
var (
ts *httptest.Server
client Mixpanel
LastRequest *http.Request
)
func setup() {
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("1\n"))
LastRequest = r
}))
client = New("e3bc4100330c35722740fb8c6f5abddc", ts.URL)
}
func teardown() {
ts.Close()
}
func decodeURL(url string) string {
data := strings.Split(url, "data=")[1]
decoded, _ := base64.StdEncoding.DecodeString(data)
return string(decoded[:])
}
// examples from https://mixpanel.com/help/reference/http
func TestTrack(t *testing.T) {
setup()
defer teardown()
client.Track("13793", "Signed Up", &Event{
Properties: map[string]interface{}{
"Referred By": "Friend",
},
})
want := "{\"event\":\"Signed Up\",\"properties\":{\"Referred By\":\"Friend\",\"distinct_id\":\"13793\",\"token\":\"e3bc4100330c35722740fb8c6f5abddc\"}}"
if !reflect.DeepEqual(decodeURL(LastRequest.URL.String()), want) {
t.Errorf("LastRequest.URL returned %+v, want %+v",
decodeURL(LastRequest.URL.String()), want)
}
want = "/track"
path := LastRequest.URL.Path
if !reflect.DeepEqual(path, want) {
t.Errorf("path returned %+v, want %+v",
path, want)
}
}
func TestPeopleOperations(t *testing.T) {
setup()
defer teardown()
client.Update("13793", &Update{
Operation: "$set",
Properties: map[string]interface{}{
"Address": "1313 Mockingbird Lane",
"Birthday": "1948-01-01",
},
})
want := "{\"$distinct_id\":\"13793\",\"$set\":{\"Address\":\"1313 Mockingbird Lane\",\"Birthday\":\"1948-01-01\"},\"$token\":\"e3bc4100330c35722740fb8c6f5abddc\"}"
if !reflect.DeepEqual(decodeURL(LastRequest.URL.String()), want) {
t.Errorf("LastRequest.URL returned %+v, want %+v",
decodeURL(LastRequest.URL.String()), want)
}
want = "/engage"
path := LastRequest.URL.Path
if !reflect.DeepEqual(path, want) {
t.Errorf("path returned %+v, want %+v",
path, want)
}
}
func TestPeopleTrack(t *testing.T) {
setup()
defer teardown()
client.Track("13793", "Signed Up", &Event{
Properties: map[string]interface{}{
"Referred By": "Friend",
},
})
want := "{\"event\":\"Signed Up\",\"properties\":{\"Referred By\":\"Friend\",\"distinct_id\":\"13793\",\"token\":\"e3bc4100330c35722740fb8c6f5abddc\"}}"
if !reflect.DeepEqual(decodeURL(LastRequest.URL.String()), want) {
t.Errorf("LastRequest.URL returned %+v, want %+v",
decodeURL(LastRequest.URL.String()), want)
}
want = "/track"
path := LastRequest.URL.Path
if !reflect.DeepEqual(path, want) {
t.Errorf("path returned %+v, want %+v",
path, want)
}
}
func TestError(t *testing.T) {
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("0\n"))
LastRequest = r
}))
assertErrTrackFailed := func(err error) {
merr, ok := err.(*MixpanelError)
if !ok {
t.Errorf("Error should be wrapped in a MixpanelError: %v", err)
return
}
terr, ok := merr.Err.(*ErrTrackFailed)
if !ok {
t.Errorf("Error should be a *ErrTrackFailed: %v", err)
return
}
if terr.Body != "0\n" {
t.Errorf("Wrong body carried in the *ErrTrackFailed: %q", terr.Body)
}
}
client = New("e3bc4100330c35722740fb8c6f5abddc", ts.URL)
assertErrTrackFailed(client.Update("1", &Update{}))
assertErrTrackFailed(client.Track("1", "name", &Event{}))
}