-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_test.go
222 lines (196 loc) · 4.96 KB
/
web_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
package gogadgets_test
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"time"
"github.com/cswank/gogadgets"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var _ = Describe("server", func() {
var (
port int
addr string
cliAddr string
out chan gogadgets.Message
in chan gogadgets.Message
s *gogadgets.Server
)
BeforeEach(func() {
port = 1024 + rand.Intn(65535-1024)
addr = fmt.Sprintf("http://localhost:%d/gadgets", port)
cliAddr = fmt.Sprintf("http://localhost:%d/clients", port)
s = gogadgets.NewServer("", "", port)
in = make(chan gogadgets.Message)
out = make(chan gogadgets.Message)
go s.Start(out, in)
out <- gogadgets.Message{
Type: gogadgets.UPDATE,
Sender: "lab led",
Location: "lab",
Name: "led",
Value: gogadgets.Value{
Value: true,
Output: map[string]bool{"gpio": true},
},
}
out <- gogadgets.Message{
Type: gogadgets.UPDATE,
Sender: "hall led",
Location: "hall",
Name: "led",
Value: gogadgets.Value{
Value: false,
Output: map[string]bool{"gpio": true},
},
}
})
Describe("when all's good", func() {
It("sends the status", func() {
var r *http.Response
Eventually(func() int {
var err error
r, err = http.Get(addr)
if err != nil {
return 0
}
return r.StatusCode
}).Should(Equal(http.StatusOK))
defer r.Body.Close()
Expect(r.StatusCode).To(Equal(http.StatusOK))
msgs := map[string]gogadgets.Message{}
dec := json.NewDecoder(r.Body)
err := dec.Decode(&msgs)
Expect(err).To(BeNil())
Expect(len(msgs)).To(Equal(2))
msg, ok := msgs["lab led"]
Expect(ok).To(BeTrue())
Expect(msg.Value.Value).To(BeTrue())
msg, ok = msgs["hall led"]
Expect(ok).To(BeTrue())
Expect(msg.Value.Value).To(BeFalse())
})
It("accepts a message from the outside world", func() {
msg := gogadgets.Message{
Type: gogadgets.COMMAND,
Sender: "me",
Body: "turn on lab led",
}
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
err := enc.Encode(&msg)
Expect(err).To(BeNil())
Eventually(func() int {
r, err := http.Post(addr, "application/json", buf)
if err != nil {
return 500
}
r.Body.Close()
return r.StatusCode
}).Should(Equal(http.StatusOK))
m := <-in
Expect(m.Body).To(Equal("turn on lab led"))
})
It("registers a new client", func() {
msgs := []gogadgets.Message{}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var msg gogadgets.Message
dec := json.NewDecoder(r.Body)
dec.Decode(&msg)
msgs = append(msgs, msg)
}))
defer ts.Close()
a := map[string]string{
"address": ts.URL,
"token": "xyxx",
}
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.Encode(&a)
Eventually(func() int {
r, err := http.Post(cliAddr, "application/json", buf)
if err != nil {
return 500
}
r.Body.Close()
return r.StatusCode
}).Should(Equal(http.StatusOK))
r, err := http.Get(cliAddr)
Expect(err).To(BeNil())
Expect(r.StatusCode).To(Equal(http.StatusOK))
var c map[string]string
dec := json.NewDecoder(r.Body)
err = dec.Decode(&c)
Expect(err).To(BeNil())
r.Body.Close()
Expect(c[ts.URL]).To(Equal("xyxx"))
msg := gogadgets.Message{
Type: gogadgets.COMMAND,
Sender: "me",
Body: "turn on lab led",
}
out <- msg
Eventually(func() []gogadgets.Message {
return msgs
}).Should(HaveLen(1))
Expect(msgs[0].Body).To(Equal("turn on lab led"))
})
It("unregisters the client when it goes down", func() {
msgs := []gogadgets.Message{}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var msg gogadgets.Message
dec := json.NewDecoder(r.Body)
dec.Decode(&msg)
msgs = append(msgs, msg)
}))
a := map[string]string{
"address": ts.URL,
"token": "xxxy",
}
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.Encode(&a)
Eventually(func() int {
r, err := http.Post(cliAddr, "application/json", buf)
if err != nil {
return 500
}
r.Body.Close()
return r.StatusCode
}).Should(Equal(http.StatusOK))
r, err := http.Get(cliAddr)
Expect(err).To(BeNil())
Expect(r.StatusCode).To(Equal(http.StatusOK))
var c map[string]string
dec := json.NewDecoder(r.Body)
err = dec.Decode(&c)
Expect(err).To(BeNil())
r.Body.Close()
Expect(c[ts.URL]).To(Equal("xxxy"))
ts.Close()
msg := gogadgets.Message{
Type: gogadgets.COMMAND,
Sender: "me",
Body: "turn on lab led",
}
out <- msg
Eventually(func() int {
r, err = http.Get(cliAddr)
Expect(err).To(BeNil())
Expect(r.StatusCode).To(Equal(http.StatusOK))
var c2 map[string]bool
dec = json.NewDecoder(r.Body)
err = dec.Decode(&c2)
r.Body.Close()
return len(c2)
}).Should(Equal(0))
})
})
})