-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
101 lines (83 loc) · 2.35 KB
/
client_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
package control
import (
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var token string
var url string = API_URL
var apps []string
func TestMain(m *testing.M) {
token = os.Getenv("ABLY_ACCOUNT_TOKEN")
rand.Seed(time.Now().UnixNano())
if token == "" {
panic("ABLY_ACCOUNT_TOKEN not set")
}
if os.Getenv("ABLY_CONTROL_URL") != "" {
url = os.Getenv("ABLY_CONTROL_URL")
}
// Attempt to clean up apps if anything went wrong
client, _, err := NewClientWithURL(token, url)
if err == nil {
for _, v := range apps {
_ = client.DeleteApp(v)
}
}
code := m.Run()
os.Exit(code)
}
func newTestApp(t *testing.T, client *Client) App {
n := rand.Uint64()
name := "test-" + fmt.Sprint(n)
t.Logf("creating app with name: %s", name)
app := NewApp{
Name: name,
Status: "enabled",
//TLSOnly: false,
FcmKey: "",
FcmServiceAccount: "",
FcmProjectId: "",
ApnsCertificate: "",
ApnsPrivateKey: "",
ApnsUseSandboxEndpoint: false,
}
app_ret, err := client.CreateApp(&app)
assert.NoError(t, err)
apps = append(apps, app.ID)
return app_ret
}
func newTestClient(t *testing.T) (Client, Me) {
client, me, err := NewClientWithURL(token, url)
assert.NoError(t, err)
return client, me
}
// TestAblyAgent tests that client requests set the Ably-Agent HTTP header.
func TestAblyAgent(t *testing.T) {
// start a test HTTP server which tracks the value of the Ably-Agent
// HTTP header and returns an empty JSON object.
var ablyAgent string
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ablyAgent = req.Header.Get("Ably-Agent")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", "2")
w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
})
srv := httptest.NewServer(handler)
// initialise a client, which will make a request to /me
client, _, err := NewClientWithURL("s3cr3t", srv.URL)
assert.NoError(t, err)
// check the Ably-Agent HTTP header was set
assert.Equal(t, "ably-control-go/"+VERSION, ablyAgent)
// add an extra Ably-Agent entry
client.AppendAblyAgent("test", "1.2.3")
// check requests now set the updated Ably-Agent HTTP header
_, err = client.Me()
assert.NoError(t, err)
assert.Equal(t, "ably-control-go/"+VERSION+" test/1.2.3", ablyAgent)
}