forked from autogrow/wray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
63 lines (52 loc) · 1.36 KB
/
response_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 wray
import (
"bytes"
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestDecodeResponse(t *testing.T) {
var connectJSON = []byte(`[
{
"clientId":"fwyr8myfb8b2jwffr66jpsucnztffmq",
"channel":"/meta/connect",
"successful":true,
"advice":{
"reconnect":"retry",
"interval":0,
"timeout":45000
}
},
{
"channel":"/foo",
"data":{
"hello":"from foo"
},
"advice":{}
}
]`)
Convey("the response should be decoded", t, func() {
dec := json.NewDecoder(bytes.NewBuffer(connectJSON))
resp, _, err := decodeResponse(dec)
So(err, ShouldBeNil)
So(resp.OK(), ShouldBeTrue)
So(resp.Channel(), ShouldEqual, "/meta/connect")
})
Convey("the messages should be decoded", t, func() {
dec := json.NewDecoder(bytes.NewBuffer(connectJSON))
_, msgs, err := decodeResponse(dec)
So(err, ShouldBeNil)
So(len(msgs), ShouldEqual, 1)
So(msgs[0].Channel(), ShouldEqual, "/foo")
So(msgs[0].Data(), ShouldEqual, map[string]interface{}{"hello": "from foo"})
})
}
func TestMessageError(t *testing.T) {
Convey("the error should be able to be set on a message", t, func() {
msg := msgWrapper{&message{}}
So(msg.HasError(), ShouldBeFalse)
msg.SetError("test")
So(msg.HasError(), ShouldBeTrue)
So(msg.Error(), ShouldEqual, "test")
})
}