-
Notifications
You must be signed in to change notification settings - Fork 15
/
analytics_test.go
63 lines (51 loc) · 1.72 KB
/
analytics_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
package flagsmith
import (
"context"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/go-resty/resty/v2"
)
const BaseURL = "http://localhost:8000/api/v1/"
const EnvironmentAPIKey = "test_key"
func TestAnalytics(t *testing.T) {
// First, we need to create a test server
// to capture the requests made to the API
actualRequestBody := struct {
mu sync.Mutex
body string
}{}
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
actualRequestBodyRaw, err := io.ReadAll(req.Body)
assert.NoError(t, err)
actualRequestBody.mu.Lock()
actualRequestBody.body = string(actualRequestBodyRaw)
actualRequestBody.mu.Unlock()
assert.Equal(t, "/api/v1/analytics/flags/", req.URL.Path)
assert.Equal(t, EnvironmentAPIKey, req.Header.Get("X-Environment-Key"))
}))
defer server.Close()
expectedRequstBody := "{\"feature_1\":1,\"feature_2\":2}"
analyticsTimer := 10
// and, the http client
client := resty.New()
client.SetHeader("X-Environment-Key", EnvironmentAPIKey)
// Now let's create the processor
processor := NewAnalyticsProcessor(context.Background(), client, server.URL+"/api/v1/", &analyticsTimer, createLogger())
// and, track some features
processor.TrackFeature("feature_1")
processor.TrackFeature("feature_2")
processor.TrackFeature("feature_2")
// Next, let's sleep a little to let the processor flush the data
time.Sleep(50 * time.Millisecond)
// Finally, let's make sure correct data was sent to the API
actualRequestBody.mu.Lock()
assert.Equal(t, expectedRequstBody, actualRequestBody.body)
// and, that the data was cleared
processor.store.mu.Lock()
assert.Equal(t, 0, len(processor.store.data))
}