gobot.io/x/gobot@v1.16.0/platforms/nats/nats_adaptor_test.go (about) 1 package nats 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 "testing" 8 9 "github.com/nats-io/nats.go" 10 "gobot.io/x/gobot" 11 "gobot.io/x/gobot/gobottest" 12 ) 13 14 var _ gobot.Adaptor = (*Adaptor)(nil) 15 16 func connStub(options ...nats.Option) func() (*nats.Conn, error) { 17 return func() (*nats.Conn, error) { 18 opts := nats.DefaultOptions 19 for _, opt := range options { 20 if err := opt(&opts); err != nil { 21 return nil, err 22 } 23 } 24 c := &nats.Conn{Opts: opts} 25 return c, nil 26 } 27 } 28 29 func initTestNatsAdaptor() *Adaptor { 30 a := NewAdaptor("localhost:4222", 9999) 31 a.connect = func() (*nats.Conn, error) { 32 c := &nats.Conn{} 33 return c, nil 34 } 35 return a 36 } 37 38 func initTestNatsAdaptorWithAuth() *Adaptor { 39 a := NewAdaptorWithAuth("localhost:4222", 9999, "user", "pass") 40 a.connect = func() (*nats.Conn, error) { 41 c := &nats.Conn{} 42 return c, nil 43 } 44 return a 45 } 46 47 func initTestNatsAdaptorTLS(options ...nats.Option) *Adaptor { 48 a := NewAdaptor("tls://localhost:4242", 49999, options...) 49 a.connect = connStub(options...) 50 return a 51 } 52 53 func TestNatsAdaptorName(t *testing.T) { 54 a := initTestNatsAdaptor() 55 gobottest.Assert(t, strings.HasPrefix(a.Name(), "NATS"), true) 56 a.SetName("NewName") 57 gobottest.Assert(t, a.Name(), "NewName") 58 } 59 60 func TestNatsAdaptorReturnsHost(t *testing.T) { 61 a := initTestNatsAdaptor() 62 gobottest.Assert(t, a.Host, "nats://localhost:4222") 63 } 64 65 func TestNatsAdaptorWithAuth(t *testing.T) { 66 a := initTestNatsAdaptorWithAuth() 67 gobottest.Assert(t, a.username, "user") 68 gobottest.Assert(t, a.password, "pass") 69 } 70 71 func TestNatsAdapterSetsRootCAs(t *testing.T) { 72 a := initTestNatsAdaptorTLS(nats.RootCAs("test_certs/catest.pem")) 73 gobottest.Assert(t, a.Host, "tls://localhost:4242") 74 a.Connect() 75 o := a.client.Opts 76 gobottest.Assert(t, len(o.TLSConfig.RootCAs.Subjects()), 1) 77 gobottest.Assert(t, o.Secure, true) 78 } 79 80 func TestNatsAdapterSetsClientCerts(t *testing.T) { 81 a := initTestNatsAdaptorTLS(nats.ClientCert("test_certs/client-cert.pem", "test_certs/client-key.pem")) 82 gobottest.Assert(t, a.Host, "tls://localhost:4242") 83 a.Connect() 84 certs := a.client.Opts.TLSConfig.Certificates 85 gobottest.Assert(t, len(certs), 1) 86 gobottest.Assert(t, a.client.Opts.Secure, true) 87 } 88 89 func TestNatsAdapterSetsClientCertsWithUserInfo(t *testing.T) { 90 a := initTestNatsAdaptorTLS(nats.ClientCert("test_certs/client-cert.pem", "test_certs/client-key.pem"), nats.UserInfo("test", "testwd")) 91 gobottest.Assert(t, a.Host, "tls://localhost:4242") 92 a.Connect() 93 certs := a.client.Opts.TLSConfig.Certificates 94 gobottest.Assert(t, len(certs), 1) 95 gobottest.Assert(t, a.client.Opts.Secure, true) 96 gobottest.Assert(t, a.client.Opts.User, "test") 97 gobottest.Assert(t, a.client.Opts.Password, "testwd") 98 } 99 100 // TODO: implement this test without requiring actual server connection 101 func TestNatsAdaptorPublishWhenConnected(t *testing.T) { 102 t.Skip("TODO: implement this test without requiring actual server connection") 103 a := initTestNatsAdaptor() 104 a.Connect() 105 data := []byte("o") 106 gobottest.Assert(t, a.Publish("test", data), true) 107 } 108 109 // TODO: implement this test without requiring actual server connection 110 func TestNatsAdaptorOnWhenConnected(t *testing.T) { 111 t.Skip("TODO: implement this test without requiring actual server connection") 112 a := initTestNatsAdaptor() 113 a.Connect() 114 gobottest.Assert(t, a.On("hola", func(msg Message) { 115 fmt.Println("hola") 116 }), true) 117 } 118 119 // TODO: implement this test without requiring actual server connection 120 func TestNatsAdaptorPublishWhenConnectedWithAuth(t *testing.T) { 121 t.Skip("TODO: implement this test without requiring actual server connection") 122 a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd") 123 a.Connect() 124 data := []byte("o") 125 gobottest.Assert(t, a.Publish("test", data), true) 126 } 127 128 // TODO: implement this test without requiring actual server connection 129 func TestNatsAdaptorOnWhenConnectedWithAuth(t *testing.T) { 130 t.Skip("TODO: implement this test without requiring actual server connection") 131 a := NewAdaptorWithAuth("localhost:4222", 9999, "test", "testwd") 132 a.Connect() 133 gobottest.Assert(t, a.On("hola", func(msg Message) { 134 fmt.Println("hola") 135 }), true) 136 } 137 138 func TestNatsAdaptorFailedConnect(t *testing.T) { 139 a := NewAdaptor("localhost:9999", 9999) 140 gobottest.Assert(t, a.Connect(), errors.New("nats: no servers available for connection")) 141 } 142 143 func TestNatsAdaptorFinalize(t *testing.T) { 144 a := NewAdaptor("localhost:9999", 9999) 145 gobottest.Assert(t, a.Finalize(), nil) 146 } 147 148 func TestNatsAdaptorCannotPublishUnlessConnected(t *testing.T) { 149 a := NewAdaptor("localhost:9999", 9999) 150 data := []byte("o") 151 gobottest.Assert(t, a.Publish("test", data), false) 152 } 153 154 func TestNatsAdaptorCannotOnUnlessConnected(t *testing.T) { 155 a := NewAdaptor("localhost:9999", 9999) 156 gobottest.Assert(t, a.On("hola", func(msg Message) { 157 fmt.Println("hola") 158 }), false) 159 }