-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_event_test.go
319 lines (273 loc) · 8.81 KB
/
send_event_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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package ga4m
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
"time"
)
// MockHTTPClient is a mock implementation of the HTTP client.
type MockHTTPClient struct {
DoFunc func(req *http.Request) (*http.Response, error)
}
func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) {
return m.DoFunc(req)
}
func TestSendEvent_Success(t *testing.T) {
session := Session{
ClientID: "123456.7654321",
SessionID: "session_123",
ClientVersion: "1",
SessionCount: 1,
IsEngaged: true,
}
eventName := "test_event"
params := map[string]string{
"param1": "value1",
"param2": "2",
}
measurementID := "G-XXXXXXXXXX"
apiSecret := "test_secret"
// Create a mock HTTP client
mockClient := &MockHTTPClient{}
client := NewClient(measurementID, apiSecret)
client.SetHTTPClient(mockClient)
mockClient.DoFunc = func(req *http.Request) (*http.Response, error) {
// Verify the request URL
expectedURL := "https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret=test_secret"
if req.URL.String() != expectedURL {
t.Errorf("Expected URL %s, got %s", expectedURL, req.URL.String())
}
// Verify the request body
bodyBytes, _ := io.ReadAll(req.Body)
var payload AnalyticsEvent
if err := json.Unmarshal(bodyBytes, &payload); err != nil {
t.Errorf("Failed to unmarshal request body: %v", err)
}
if payload.ClientID != session.ClientID {
t.Errorf("Expected ClientID %s, got %s", session.ClientID, payload.ClientID)
}
if len(payload.Events) != 1 {
t.Errorf("Expected 1 event, got %d", len(payload.Events))
}
if payload.Events[0].Name != eventName {
t.Errorf("Expected event name %s, got %s", eventName, payload.Events[0].Name)
}
// Verify that original params are included plus the engagement_time_msec and session_id
expectedParamCount := len(params) + 2 // +2 for engagement_time_msec and session_id
if len(payload.Events[0].Params) != expectedParamCount {
t.Errorf("Expected %d params, got %d", expectedParamCount, len(payload.Events[0].Params))
}
// Verify session_id is included in params
if payload.Events[0].Params["session_id"] != session.SessionID {
t.Errorf("Expected session_id %s, got %s", session.SessionID, payload.Events[0].Params["session_id"])
}
// Optionally, verify engagement_time_msec is included
if _, ok := payload.Events[0].Params["engagement_time_msec"]; !ok {
t.Errorf("Expected engagement_time_msec to be included in params")
}
// Return a successful response
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("")),
}, nil
}
err := client.SendEvent(session, eventName, params)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestSendEvent_InvalidEventName(t *testing.T) {
session := Session{
ClientID: "123456.7654321",
}
eventName := "123_invalid_name" // Starts with a number, which is invalid
params := map[string]string{}
client := NewClient("G-XXXXXXXXXX", "test_secret")
err := client.SendEvent(session, eventName, params)
if err == nil {
t.Errorf("Expected error for invalid event name, got nil")
}
}
func TestSendEvent_InvalidParams(t *testing.T) {
session := Session{
ClientID: "123456.7654321",
}
eventName := "validEvent"
params := map[string]string{
"1_invalid_param": "value", // Invalid parameter name
}
client := NewClient("G-XXXXXXXXXX", "test_secret")
err := client.SendEvent(session, eventName, params)
if err == nil {
t.Errorf("Expected error for invalid parameter name, got nil")
}
}
func TestSendEvents_Success(t *testing.T) {
session := Session{
ClientID: "123456.7654321",
SessionID: "session_123",
ClientVersion: "1",
SessionCount: 1,
IsEngaged: true,
}
events := []EventParams{
{
Name: "event_one",
Params: map[string]string{
"param1": "value1",
},
},
{
Name: "event_two",
Params: map[string]string{
"param2": "value2",
},
},
}
measurementID := "G-XXXXXXXXXX"
apiSecret := "test_secret"
mockClient := &MockHTTPClient{}
client := NewClient(measurementID, apiSecret)
client.SetHTTPClient(mockClient)
mockClient.DoFunc = func(req *http.Request) (*http.Response, error) {
bodyBytes, _ := io.ReadAll(req.Body)
var payload AnalyticsEvent
if err := json.Unmarshal(bodyBytes, &payload); err != nil {
t.Errorf("Failed to unmarshal request body: %v", err)
}
if payload.ClientID != session.ClientID {
t.Errorf("Expected ClientID %s, got %s", session.ClientID, payload.ClientID)
}
if len(payload.Events) != len(events) {
t.Errorf("Expected %d events, got %d", len(events), len(payload.Events))
}
// Verify session_id is included in all events
for _, event := range payload.Events {
if event.Params["session_id"] != session.SessionID {
t.Errorf("Expected session_id %s, got %s", session.SessionID, event.Params["session_id"])
}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("")),
}, nil
}
err := client.SendEvents(session, events)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestSendEvents_TooManyEvents(t *testing.T) {
session := Session{
ClientID: "123456.7654321",
SessionID: "session_123",
ClientVersion: "1",
}
events := make([]EventParams, 26) // 26 events, exceeding the limit
// Initialize events with valid event names and parameters
for i := 0; i < 26; i++ {
events[i] = EventParams{
Name: fmt.Sprintf("event_%d", i),
Params: map[string]string{
"param": fmt.Sprintf("value_%d", i),
},
}
}
client := NewClient("G-XXXXXXXXXX", "test_secret")
err := client.SendEvents(session, events)
if err == nil {
t.Errorf("Expected error for too many events, got nil")
}
}
func TestSendPayload_HTTPError(t *testing.T) {
session := Session{
ClientID: "123456.7654321",
SessionID: "session_123",
ClientVersion: "1",
}
eventName := "test_event"
params := map[string]string{
"param1": "value1",
}
mockClient := &MockHTTPClient{}
client := NewClient("G-XXXXXXXXXX", "test_secret")
client.SetHTTPClient(mockClient)
mockClient.DoFunc = func(req *http.Request) (*http.Response, error) {
// Simulate a 400 Bad Request response
return &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(strings.NewReader("Bad Request")),
}, nil
}
err := client.SendEvent(session, eventName, params)
if err == nil {
t.Errorf("Expected error due to bad response, got nil")
} else if !strings.Contains(err.Error(), "received non-OK status") {
t.Errorf("Expected 'received non-OK status' error, got %v", err)
}
}
func TestSendEvent_WithOptions(t *testing.T) {
session := Session{
ClientID: "123456.7654321",
SessionID: "default_session",
ClientVersion: "1",
}
eventName := "test_event"
params := map[string]string{}
userID := "user_123"
sessionID := "session_456" // This should override the session.SessionID
timestamp := time.Unix(1609459200, 0) // Fixed timestamp: 2021-01-01 00:00:00 UTC
mockClient := &MockHTTPClient{}
client := NewClient("G-XXXXXXXXXX", "test_secret")
client.SetHTTPClient(mockClient)
mockClient.DoFunc = func(req *http.Request) (*http.Response, error) {
bodyBytes, _ := io.ReadAll(req.Body)
var payload AnalyticsEvent
if err := json.Unmarshal(bodyBytes, &payload); err != nil {
t.Errorf("Failed to unmarshal request body: %v", err)
}
// Verify client ID from session is used
if payload.ClientID != session.ClientID {
t.Errorf("Expected ClientID '%s', got '%s'", session.ClientID, payload.ClientID)
}
// Verify option parameters are properly set
if payload.UserID != userID {
t.Errorf("Expected UserID '%s', got '%s'", userID, payload.UserID)
}
if payload.TimestampMicros != timestamp.UnixMicro() {
t.Errorf("Expected TimestampMicros '%d', got '%d'", timestamp.UnixMicro(), payload.TimestampMicros)
}
// Verify session ID from options overrides session.SessionID
if payload.Events[0].Params["session_id"] != sessionID {
t.Errorf("Expected session_id '%s', got '%v'", sessionID, payload.Events[0].Params["session_id"])
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("")),
}, nil
}
err := client.SendEvent(session, eventName, params,
WithUserID(userID),
WithSessionID(sessionID),
WithTimestamp(timestamp),
)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestSendEvent_EmptySession(t *testing.T) {
session := Session{} // Empty session
eventName := "test_event"
params := map[string]string{}
client := NewClient("G-XXXXXXXXXX", "test_secret")
err := client.SendEvent(session, eventName, params)
if err == nil {
t.Error("Expected error for empty session, got nil")
}
if !strings.Contains(err.Error(), "must have a valid client ID") {
t.Errorf("Expected client ID validation error, got: %v", err)
}
}