2014-11-04 10:30:56 +08:00
|
|
|
package mqtt
|
|
|
|
|
|
|
|
import (
|
2014-11-05 00:33:05 +08:00
|
|
|
"fmt"
|
2017-04-06 16:55:06 +08:00
|
|
|
"strings"
|
2014-11-04 10:56:33 +08:00
|
|
|
"testing"
|
2014-11-07 04:28:04 +08:00
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 21:17:02 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2014-11-04 10:30:56 +08:00
|
|
|
)
|
|
|
|
|
2016-09-26 16:13:37 +08:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-07-14 01:01:36 +08:00
|
|
|
|
2016-09-26 16:13:37 +08:00
|
|
|
func initTestMqttAdaptor() *Adaptor {
|
2017-04-19 16:46:28 +08:00
|
|
|
return NewAdaptor("tcp://localhost:1883", "client")
|
2014-11-04 10:30:56 +08:00
|
|
|
}
|
|
|
|
|
2017-04-06 16:55:06 +08:00
|
|
|
func TestMqttAdaptorName(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, strings.HasPrefix(a.Name(), "MQTT"))
|
2017-04-06 16:55:06 +08:00
|
|
|
a.SetName("NewName")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "NewName", a.Name())
|
2017-04-06 16:55:06 +08:00
|
|
|
}
|
|
|
|
|
2017-04-19 16:30:30 +08:00
|
|
|
func TestMqttAdaptorPort(t *testing.T) {
|
2014-11-04 10:56:33 +08:00
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "tcp://localhost:1883", a.Port())
|
2017-04-19 16:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorAutoReconnect(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.False(t, a.AutoReconnect())
|
2017-04-19 16:30:30 +08:00
|
|
|
a.SetAutoReconnect(true)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, a.AutoReconnect())
|
2017-04-19 16:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-09-24 19:45:47 +08:00
|
|
|
func TestMqttAdaptorCleanSession(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, a.CleanSession())
|
2017-09-24 19:45:47 +08:00
|
|
|
a.SetCleanSession(false)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.False(t, a.CleanSession())
|
2017-09-24 19:45:47 +08:00
|
|
|
}
|
|
|
|
|
2017-04-19 16:30:30 +08:00
|
|
|
func TestMqttAdaptorUseSSL(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.False(t, a.UseSSL())
|
2017-04-19 16:30:30 +08:00
|
|
|
a.SetUseSSL(true)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, a.UseSSL())
|
2017-04-19 16:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorUseServerCert(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "", a.ServerCert())
|
2017-04-19 16:30:30 +08:00
|
|
|
a.SetServerCert("/path/to/server.cert")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "/path/to/server.cert", a.ServerCert())
|
2017-04-19 16:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorUseClientCert(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "", a.ClientCert())
|
2017-04-19 16:30:30 +08:00
|
|
|
a.SetClientCert("/path/to/client.cert")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "/path/to/client.cert", a.ClientCert())
|
2017-04-19 16:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorUseClientKey(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "", a.ClientKey())
|
2017-04-19 16:30:30 +08:00
|
|
|
a.SetClientKey("/path/to/client.key")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "/path/to/client.key", a.ClientKey())
|
2017-04-19 16:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnectError(t *testing.T) {
|
2019-05-21 20:29:06 +08:00
|
|
|
a := NewAdaptor("tcp://localhost:1884", "client")
|
2017-04-19 16:46:28 +08:00
|
|
|
|
2017-05-03 02:34:46 +08:00
|
|
|
err := a.Connect()
|
2023-11-16 03:51:52 +08:00
|
|
|
require.ErrorContains(t, err, "connection refused")
|
2017-04-19 16:46:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnectSSLError(t *testing.T) {
|
2019-05-21 20:29:06 +08:00
|
|
|
a := NewAdaptor("tcp://localhost:1884", "client")
|
2017-04-19 16:46:28 +08:00
|
|
|
a.SetUseSSL(true)
|
2017-05-03 02:34:46 +08:00
|
|
|
err := a.Connect()
|
2023-11-16 03:51:52 +08:00
|
|
|
require.ErrorContains(t, err, "connection refused")
|
2017-04-19 16:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnectWithAuthError(t *testing.T) {
|
2019-05-21 20:29:06 +08:00
|
|
|
a := NewAdaptorWithAuth("xyz://localhost:1883", "client", "user", "pass")
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, a.Connect(), "network Error : unknown protocol")
|
2014-11-04 10:30:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorFinalize(t *testing.T) {
|
2014-11-04 10:56:33 +08:00
|
|
|
a := initTestMqttAdaptor()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, a.Finalize())
|
2014-11-04 10:30:56 +08:00
|
|
|
}
|
2014-11-05 00:33:05 +08:00
|
|
|
|
|
|
|
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
data := []byte("o")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.False(t, a.Publish("test", data))
|
2014-11-05 00:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = a.Connect()
|
2014-11-05 00:33:05 +08:00
|
|
|
data := []byte("o")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, a.Publish("test", data))
|
2014-11-05 00:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.False(t, a.On("hola", func(msg Message) {
|
2014-11-05 00:33:05 +08:00
|
|
|
fmt.Println("hola")
|
2023-10-20 16:27:09 +08:00
|
|
|
}))
|
2014-11-05 00:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = a.Connect()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, a.On("hola", func(msg Message) {
|
2014-11-05 00:33:05 +08:00
|
|
|
fmt.Println("hola")
|
2023-10-20 16:27:09 +08:00
|
|
|
}))
|
2014-11-05 00:33:05 +08:00
|
|
|
}
|
2018-12-18 01:06:34 +08:00
|
|
|
|
|
|
|
func TestMqttAdaptorQoS(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
a.SetQoS(1)
|
2023-11-12 21:17:02 +08:00
|
|
|
assert.Equal(t, 1, a.qos)
|
2018-12-18 01:06:34 +08:00
|
|
|
}
|