-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbot_test.go
152 lines (123 loc) · 2.87 KB
/
bot_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
package gozulipbot
import (
"io/ioutil"
"net/http"
"testing"
)
func TestBot_Init(t *testing.T) {
bot := Bot{}
bot.Init()
if bot.Client == nil {
t.Error("expected bot to have client")
}
}
func TestBot_GetStreamList(t *testing.T) {
bot := getTestBot()
type Case struct {
URL string
Error error
}
cases := map[string]Case{
"1": Case{URL: "https://api.example.com/streams", Error: nil},
}
for num, c := range cases {
_, err := bot.GetStreamList()
if err != c.Error {
t.Fatalf("got %q, expected nil, case %q", err, num)
}
req := bot.Client.(*testClient).Request
if req.URL.String() != c.URL {
t.Errorf("got %q, expected %q, case %q", req.URL.String(), c.URL, num)
}
body, _ := ioutil.ReadAll(req.Body)
if string(body) != "" {
t.Errorf(`got %q, expected "", case %q`, string(body), num)
}
}
}
func TestBot_GetStreams(t *testing.T) {
t.Skip()
}
func TestBot_Subscribe(t *testing.T) {
t.Skip()
}
func TestBot_Unsubscribe(t *testing.T) {
t.Skip()
}
func TestBot_ListSubscriptions(t *testing.T) {
t.Skip()
}
func TestBot_RegisterEvents(t *testing.T) {
t.Skip()
}
func TestBot_RegisterAll(t *testing.T) {
t.Skip()
}
func TestBot_RegisterAt(t *testing.T) {
t.Skip()
}
func TestBot_RegisterPrivate(t *testing.T) {
t.Skip()
}
func TestBot_RegisterSubscriptions(t *testing.T) {
t.Skip()
}
func TestBot_RawRegisterEvents(t *testing.T) {
t.Skip()
}
// ensure constructRequest adds a JSON header and uses basic auth
func TestBot_constructRequest(t *testing.T) {
bot := getTestBot()
type Case struct {
Method string
Endpoint string
Body string
ReqBody string
Error error
}
JSONHeader := "application/x-www-form-urlencoded"
cases := map[string]Case{
"1": Case{"GET", "endpoint", "", "", nil},
}
for num, c := range cases {
req, err := bot.constructRequest(c.Method, c.Endpoint, c.Body)
if err != nil {
t.Fatalf("got %q, expected nil, case %q", err, num)
}
header := req.Header.Get("Content-Type")
if string(header) != JSONHeader {
t.Errorf("got %q, expected %q, case %q", header, JSONHeader, num)
}
email, key, ok := req.BasicAuth()
if !ok || email != bot.Email || key != bot.APIKey {
t.Errorf("got %t, expected true, case %q", ok, num)
t.Errorf("got %q, expected %q, case %q", email, bot.Email, num)
t.Errorf("got %q, expected %q, case %q", key, bot.APIKey, num)
}
}
}
func getTestBot() *Bot {
return &Bot{
Email: "testbot@example.com",
APIKey: "apikey",
APIURL: "https://api.example.com/",
Streams: []string{"stream a", "test bots"},
Client: &testClient{},
}
}
type testClient struct {
Request *http.Request
Response *http.Response
}
func (t *testClient) Do(r *http.Request) (*http.Response, error) {
t.Request = r
return t.Response, nil
}
func isIn(needle string, haystack []string) bool {
for _, straw := range haystack {
if needle == straw {
return true
}
}
return false
}