2016-07-17 11:59:07 +08:00
|
|
|
package nats
|
|
|
|
|
|
|
|
import (
|
2016-11-08 02:55:43 +08:00
|
|
|
"errors"
|
2016-07-17 11:59:07 +08:00
|
|
|
"fmt"
|
2017-04-06 16:56:49 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-10-02 06:25:09 +08:00
|
|
|
"github.com/nats-io/nats.go"
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2016-07-17 11:59:07 +08:00
|
|
|
)
|
|
|
|
|
2016-10-01 23:23:40 +08:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-07-17 11:59:07 +08:00
|
|
|
|
2017-02-16 17:17:12 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 16:56:37 +08:00
|
|
|
func initTestNatsAdaptor() *Adaptor {
|
2016-10-01 23:23:40 +08:00
|
|
|
a := NewAdaptor("localhost:4222", 9999)
|
2016-12-02 16:56:37 +08:00
|
|
|
a.connect = func() (*nats.Conn, error) {
|
|
|
|
c := &nats.Conn{}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-04-29 19:37:18 +08:00
|
|
|
func initTestNatsAdaptorWithAuth() *Adaptor {
|
|
|
|
a := NewAdaptorWithAuth("localhost:4222", 9999, "user", "pass")
|
|
|
|
a.connect = func() (*nats.Conn, error) {
|
|
|
|
c := &nats.Conn{}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:17:12 +08:00
|
|
|
func initTestNatsAdaptorTLS(options ...nats.Option) *Adaptor {
|
|
|
|
a := NewAdaptor("tls://localhost:4242", 49999, options...)
|
|
|
|
a.connect = connStub(options...)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-04-06 16:56:49 +08:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2016-12-02 16:56:37 +08:00
|
|
|
func TestNatsAdaptorReturnsHost(t *testing.T) {
|
|
|
|
a := initTestNatsAdaptor()
|
2017-02-16 17:17:12 +08:00
|
|
|
gobottest.Assert(t, a.Host, "nats://localhost:4222")
|
|
|
|
}
|
|
|
|
|
2017-04-29 19:37:18 +08:00
|
|
|
func TestNatsAdaptorWithAuth(t *testing.T) {
|
|
|
|
a := initTestNatsAdaptorWithAuth()
|
|
|
|
gobottest.Assert(t, a.username, "user")
|
|
|
|
gobottest.Assert(t, a.password, "pass")
|
|
|
|
}
|
|
|
|
|
2017-02-16 17:17:12 +08:00
|
|
|
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")
|
2016-07-18 06:53:12 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 16:56:37 +08:00
|
|
|
// TODO: implement this test without requiring actual server connection
|
2016-07-17 11:59:07 +08:00
|
|
|
func TestNatsAdaptorPublishWhenConnected(t *testing.T) {
|
2016-12-02 16:56:37 +08:00
|
|
|
t.Skip("TODO: implement this test without requiring actual server connection")
|
|
|
|
a := initTestNatsAdaptor()
|
2016-07-17 11:59:07 +08:00
|
|
|
a.Connect()
|
|
|
|
data := []byte("o")
|
|
|
|
gobottest.Assert(t, a.Publish("test", data), true)
|
|
|
|
}
|
|
|
|
|
2016-12-02 16:56:37 +08:00
|
|
|
// TODO: implement this test without requiring actual server connection
|
2016-07-17 11:59:07 +08:00
|
|
|
func TestNatsAdaptorOnWhenConnected(t *testing.T) {
|
2016-12-02 16:56:37 +08:00
|
|
|
t.Skip("TODO: implement this test without requiring actual server connection")
|
|
|
|
a := initTestNatsAdaptor()
|
2016-07-18 05:39:34 +08:00
|
|
|
a.Connect()
|
2017-04-22 02:46:45 +08:00
|
|
|
gobottest.Assert(t, a.On("hola", func(msg Message) {
|
2016-07-18 05:39:34 +08:00
|
|
|
fmt.Println("hola")
|
|
|
|
}), true)
|
|
|
|
}
|
|
|
|
|
2016-12-02 16:56:37 +08:00
|
|
|
// TODO: implement this test without requiring actual server connection
|
2016-07-18 05:39:34 +08:00
|
|
|
func TestNatsAdaptorPublishWhenConnectedWithAuth(t *testing.T) {
|
2016-12-02 16:56:37 +08:00
|
|
|
t.Skip("TODO: implement this test without requiring actual server connection")
|
2016-10-01 23:23:40 +08:00
|
|
|
a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd")
|
2016-07-18 05:39:34 +08:00
|
|
|
a.Connect()
|
|
|
|
data := []byte("o")
|
|
|
|
gobottest.Assert(t, a.Publish("test", data), true)
|
|
|
|
}
|
|
|
|
|
2016-12-02 16:56:37 +08:00
|
|
|
// TODO: implement this test without requiring actual server connection
|
2016-07-18 05:39:34 +08:00
|
|
|
func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) {
|
2016-12-02 16:56:37 +08:00
|
|
|
t.Skip("TODO: implement this test without requiring actual server connection")
|
2016-10-01 23:23:40 +08:00
|
|
|
a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd")
|
2016-07-17 11:59:07 +08:00
|
|
|
a.Connect()
|
2017-04-22 02:46:45 +08:00
|
|
|
gobottest.Assert(t, a.On("hola", func(msg Message) {
|
2016-07-17 11:59:07 +08:00
|
|
|
fmt.Println("hola")
|
|
|
|
}), true)
|
|
|
|
}
|
|
|
|
|
2016-12-02 16:56:37 +08:00
|
|
|
func TestNatsAdaptorFailedConnect(t *testing.T) {
|
2016-10-01 23:23:40 +08:00
|
|
|
a := NewAdaptor("localhost:9999", 9999)
|
2016-12-02 16:56:37 +08:00
|
|
|
gobottest.Assert(t, a.Connect(), errors.New("nats: no servers available for connection"))
|
2016-07-17 11:59:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNatsAdaptorFinalize(t *testing.T) {
|
2016-10-01 23:23:40 +08:00
|
|
|
a := NewAdaptor("localhost:9999", 9999)
|
2016-11-08 02:55:43 +08:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2016-07-17 11:59:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNatsAdaptorCannotPublishUnlessConnected(t *testing.T) {
|
2016-10-01 23:23:40 +08:00
|
|
|
a := NewAdaptor("localhost:9999", 9999)
|
2016-07-17 11:59:07 +08:00
|
|
|
data := []byte("o")
|
|
|
|
gobottest.Assert(t, a.Publish("test", data), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNatsAdaptorCannotOnUnlessConnected(t *testing.T) {
|
2016-10-01 23:23:40 +08:00
|
|
|
a := NewAdaptor("localhost:9999", 9999)
|
2017-04-22 02:46:45 +08:00
|
|
|
gobottest.Assert(t, a.On("hola", func(msg Message) {
|
2016-07-17 11:59:07 +08:00
|
|
|
fmt.Println("hola")
|
|
|
|
}), false)
|
|
|
|
}
|