-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient_test.go
More file actions
88 lines (71 loc) · 1.64 KB
/
client_test.go
File metadata and controls
88 lines (71 loc) · 1.64 KB
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
package amplitude
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
)
func ExampleEvent() {
keyResp, bodyResp, server := mockServer("event")
defer server.Close()
client := New("s3cr3ts")
client.eventEndpoint = server.URL
client.Event(Event{
UserId: "0000001",
EventType: "joined",
})
key := <-keyResp
body := <-bodyResp
fmt.Printf("Key: %s\n%s", string(key), string(body))
// Output:
// Key: s3cr3ts
// {
// "event_type": "joined",
// "user_id": "0000001"
// }
}
func ExampleIdentify() {
keyResp, bodyResp, server := mockServer("identification")
defer server.Close()
client := New("s3cr3ts")
client.identifyEndpoint = server.URL
client.Identify(Identify{
UserId: "0000001",
UserProperties: map[string]interface{}{
"name": "Art Vandelay",
"email": "art@vandelayindustries.com",
},
})
key := <-keyResp
body := <-bodyResp
fmt.Printf("Key: %s\n%s", string(key), string(body))
// Output:
// Key: s3cr3ts
// {
// "user_id": "0000001",
// "user_properties": {
// "email": "art@vandelayindustries.com",
// "name": "Art Vandelay"
// }
// }
}
func mockServer(msgKey string) (chan []byte, chan []byte, *httptest.Server) {
key, body := make(chan []byte, 1), make(chan []byte, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
k := r.FormValue("api_key")
id := r.FormValue(msgKey)
var v interface{}
err := json.Unmarshal([]byte(id), &v)
if err != nil {
panic(err)
}
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(err)
}
key <- []byte(k)
body <- b
}))
return key, body, server
}