forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nats_adaptor_test.go
159 lines (138 loc) · 4.67 KB
/
nats_adaptor_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
package nats
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/nats-io/nats"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Adaptor = (*Adaptor)(nil)
func connStub(options ...nats.Option) func() (*nats.Conn, error) {
return func() (*nats.Conn, error) {
opts := nats.DefaultOptions
for _, opt := range options {
if err := opt(&opts); err != nil {
return nil, err
}
}
c := &nats.Conn{Opts: opts}
return c, nil
}
}
func initTestNatsAdaptor() *Adaptor {
a := NewAdaptor("localhost:4222", 9999)
a.connect = func() (*nats.Conn, error) {
c := &nats.Conn{}
return c, nil
}
return a
}
func initTestNatsAdaptorWithAuth() *Adaptor {
a := NewAdaptorWithAuth("localhost:4222", 9999, "user", "pass")
a.connect = func() (*nats.Conn, error) {
c := &nats.Conn{}
return c, nil
}
return a
}
func initTestNatsAdaptorTLS(options ...nats.Option) *Adaptor {
a := NewAdaptor("tls://localhost:4242", 49999, options...)
a.connect = connStub(options...)
return a
}
func TestNatsAdaptorName(t *testing.T) {
a := initTestNatsAdaptor()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "NATS"), true)
a.SetName("NewName")
gobottest.Assert(t, a.Name(), "NewName")
}
func TestNatsAdaptorReturnsHost(t *testing.T) {
a := initTestNatsAdaptor()
gobottest.Assert(t, a.Host, "nats://localhost:4222")
}
func TestNatsAdaptorWithAuth(t *testing.T) {
a := initTestNatsAdaptorWithAuth()
gobottest.Assert(t, a.username, "user")
gobottest.Assert(t, a.password, "pass")
}
func TestNatsAdapterSetsRootCAs(t *testing.T) {
a := initTestNatsAdaptorTLS(nats.RootCAs("test_certs/catest.pem"))
gobottest.Assert(t, a.Host, "tls://localhost:4242")
a.Connect()
o := a.client.Opts
gobottest.Assert(t, len(o.TLSConfig.RootCAs.Subjects()), 1)
gobottest.Assert(t, o.Secure, true)
}
func TestNatsAdapterSetsClientCerts(t *testing.T) {
a := initTestNatsAdaptorTLS(nats.ClientCert("test_certs/client-cert.pem", "test_certs/client-key.pem"))
gobottest.Assert(t, a.Host, "tls://localhost:4242")
a.Connect()
certs := a.client.Opts.TLSConfig.Certificates
gobottest.Assert(t, len(certs), 1)
gobottest.Assert(t, a.client.Opts.Secure, true)
}
func TestNatsAdapterSetsClientCertsWithUserInfo(t *testing.T) {
a := initTestNatsAdaptorTLS(nats.ClientCert("test_certs/client-cert.pem", "test_certs/client-key.pem"), nats.UserInfo("test", "testwd"))
gobottest.Assert(t, a.Host, "tls://localhost:4242")
a.Connect()
certs := a.client.Opts.TLSConfig.Certificates
gobottest.Assert(t, len(certs), 1)
gobottest.Assert(t, a.client.Opts.Secure, true)
gobottest.Assert(t, a.client.Opts.User, "test")
gobottest.Assert(t, a.client.Opts.Password, "testwd")
}
// TODO: implement this test without requiring actual server connection
func TestNatsAdaptorPublishWhenConnected(t *testing.T) {
t.Skip("TODO: implement this test without requiring actual server connection")
a := initTestNatsAdaptor()
a.Connect()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), true)
}
// TODO: implement this test without requiring actual server connection
func TestNatsAdaptorOnWhenConnected(t *testing.T) {
t.Skip("TODO: implement this test without requiring actual server connection")
a := initTestNatsAdaptor()
a.Connect()
gobottest.Assert(t, a.On("hola", func(msg Message) {
fmt.Println("hola")
}), true)
}
// TODO: implement this test without requiring actual server connection
func TestNatsAdaptorPublishWhenConnectedWithAuth(t *testing.T) {
t.Skip("TODO: implement this test without requiring actual server connection")
a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd")
a.Connect()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), true)
}
// TODO: implement this test without requiring actual server connection
func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) {
t.Skip("TODO: implement this test without requiring actual server connection")
a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd")
a.Connect()
gobottest.Assert(t, a.On("hola", func(msg Message) {
fmt.Println("hola")
}), true)
}
func TestNatsAdaptorFailedConnect(t *testing.T) {
a := NewAdaptor("localhost:9999", 9999)
gobottest.Assert(t, a.Connect(), errors.New("nats: no servers available for connection"))
}
func TestNatsAdaptorFinalize(t *testing.T) {
a := NewAdaptor("localhost:9999", 9999)
gobottest.Assert(t, a.Finalize(), nil)
}
func TestNatsAdaptorCannotPublishUnlessConnected(t *testing.T) {
a := NewAdaptor("localhost:9999", 9999)
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), false)
}
func TestNatsAdaptorCannotOnUnlessConnected(t *testing.T) {
a := NewAdaptor("localhost:9999", 9999)
gobottest.Assert(t, a.On("hola", func(msg Message) {
fmt.Println("hola")
}), false)
}